Search in sources :

Example 1 with DefaultGeneratorResult

use of com.linkedin.pegasus.generator.DefaultGeneratorResult in project rest.li by linkedin.

the class RestRequestBuilderGenerator method run.

public static GeneratorResult run(String resolverPath, String defaultPackage, final boolean generateImported, final boolean generateDataTemplates, RestliVersion version, RestliVersion deprecatedByVersion, String targetDirectoryPath, String[] sources) throws IOException {
    final RestSpecParser parser = new RestSpecParser();
    final JavaRequestBuilderGenerator generator = new JavaRequestBuilderGenerator(resolverPath, defaultPackage, generateDataTemplates, version, deprecatedByVersion);
    final ClassLoader classLoader = JavaCodeUtil.classLoaderFromResolverPath(resolverPath);
    final RestSpecParser.ParseResult parseResult = parser.parseSources(sources);
    final StringBuilder message = new StringBuilder();
    for (CodeUtil.Pair<ResourceSchema, File> pair : parseResult.getSchemaAndFiles()) {
        try {
            final JDefinedClass clazz = generator.generate(pair.first, pair.second);
        } catch (Exception e) {
            message.append(e.getMessage() + "\n");
        }
    }
    if (message.length() > 0) {
        throw new IOException(message.toString());
    }
    final PegasusDataTemplateGenerator.DataTemplatePersistentClassChecker dataTemplateChecker = new PegasusDataTemplateGenerator.DataTemplatePersistentClassChecker(generateImported, generator.getSpecGenerator(), generator.getJavaDataTemplateGenerator(), Collections.<File>emptySet());
    final JavaCodeUtil.PersistentClassChecker checker = new JavaCodeUtil.PersistentClassChecker() {

        @Override
        public boolean isPersistent(JDefinedClass clazz) {
            if (generateDataTemplates || generator.isGeneratedArrayClass(clazz)) {
                try {
                    Class.forName(clazz.fullName(), false, classLoader);
                } catch (ClassNotFoundException e) {
                    return true;
                }
            }
            return dataTemplateChecker.isPersistent(clazz);
        }
    };
    final JCodeModel requestBuilderCodeModel = generator.getCodeModel();
    final JCodeModel dataTemplateCodeModel = generator.getJavaDataTemplateGenerator().getCodeModel();
    final File targetDirectory = new File(targetDirectoryPath);
    final List<File> targetFiles = JavaCodeUtil.targetFiles(targetDirectory, requestBuilderCodeModel, classLoader, checker);
    targetFiles.addAll(JavaCodeUtil.targetFiles(targetDirectory, dataTemplateCodeModel, classLoader, checker));
    final List<File> modifiedFiles;
    if (FileUtil.upToDate(parseResult.getSourceFiles(), targetFiles)) {
        modifiedFiles = Collections.emptyList();
        _log.info("Target files are up-to-date: " + targetFiles);
    } else {
        modifiedFiles = targetFiles;
        _log.info("Generating " + targetFiles.size() + " files: " + targetFiles);
        requestBuilderCodeModel.build(new FileCodeWriter(targetDirectory, true));
        dataTemplateCodeModel.build(new FileCodeWriter(targetDirectory, true));
    }
    return new DefaultGeneratorResult(parseResult.getSourceFiles(), targetFiles, modifiedFiles);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) JDefinedClass(com.sun.codemodel.JDefinedClass) IOException(java.io.IOException) PegasusDataTemplateGenerator(com.linkedin.pegasus.generator.PegasusDataTemplateGenerator) JCodeModel(com.sun.codemodel.JCodeModel) IOException(java.io.IOException) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) JavaCodeUtil(com.linkedin.pegasus.generator.JavaCodeUtil) CodeUtil(com.linkedin.pegasus.generator.CodeUtil) DefaultGeneratorResult(com.linkedin.pegasus.generator.DefaultGeneratorResult) JavaCodeUtil(com.linkedin.pegasus.generator.JavaCodeUtil) File(java.io.File)

Aggregations

CodeUtil (com.linkedin.pegasus.generator.CodeUtil)1 DefaultGeneratorResult (com.linkedin.pegasus.generator.DefaultGeneratorResult)1 JavaCodeUtil (com.linkedin.pegasus.generator.JavaCodeUtil)1 PegasusDataTemplateGenerator (com.linkedin.pegasus.generator.PegasusDataTemplateGenerator)1 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)1 JCodeModel (com.sun.codemodel.JCodeModel)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 FileCodeWriter (com.sun.codemodel.writer.FileCodeWriter)1 File (java.io.File)1 IOException (java.io.IOException)1