Search in sources :

Example 1 with CaseSensitiveFileCodeWriter

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

the class RestRequestBuilderGenerator method run.

/**
 * @param generateLowercasePath true, files are generated with a lower case path; false, files are generated as spec specifies.
 */
public static GeneratorResult run(String resolverPath, String defaultPackage, String rootPath, final boolean generateImported, final boolean generateDataTemplates, RestliVersion version, RestliVersion deprecatedByVersion, String targetDirectoryPath, String[] sources, boolean generateLowercasePath) throws IOException {
    final RestSpecParser parser = new RestSpecParser();
    final JavaRequestBuilderGenerator generator = new JavaRequestBuilderGenerator(resolverPath, defaultPackage, generateDataTemplates, version, deprecatedByVersion, rootPath);
    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()) {
        ResourceSchema resourceSchema = pair.first;
        // Skip unstructured data resources for client generation
        if (resourceSchema == null || ResourceEntityType.UNSTRUCTURED_DATA == resourceSchema.getEntityType()) {
            continue;
        }
        try {
            final JDefinedClass clazz = generator.generate(resourceSchema, pair.second, rootPath);
        } catch (Exception e) {
            _log.error("Failed to generate request builders for schema: " + resourceSchema.getName(), e);
            message.append(e.getMessage()).append("\n");
        }
    }
    if (message.length() > 0) {
        throw new IOException(message.toString());
    }
    final DataTemplatePersistentClassChecker dataTemplateChecker = new 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, generateLowercasePath);
    targetFiles.addAll(JavaCodeUtil.targetFiles(targetDirectory, dataTemplateCodeModel, classLoader, checker, generateLowercasePath));
    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");
        _log.debug("Files: " + targetFiles);
        requestBuilderCodeModel.build(new CaseSensitiveFileCodeWriter(targetDirectory, true, generateLowercasePath));
        dataTemplateCodeModel.build(new CaseSensitiveFileCodeWriter(targetDirectory, true, generateLowercasePath));
    }
    return new DefaultGeneratorResult(parseResult.getSourceFiles(), targetFiles, modifiedFiles);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) CaseSensitiveFileCodeWriter(com.linkedin.pegasus.generator.CaseSensitiveFileCodeWriter) JDefinedClass(com.sun.codemodel.JDefinedClass) IOException(java.io.IOException) JCodeModel(com.sun.codemodel.JCodeModel) IOException(java.io.IOException) DataTemplatePersistentClassChecker(com.linkedin.pegasus.generator.DataTemplatePersistentClassChecker) JavaCodeUtil(com.linkedin.pegasus.generator.JavaCodeUtil) CodeUtil(com.linkedin.pegasus.generator.CodeUtil) DefaultGeneratorResult(com.linkedin.pegasus.generator.DefaultGeneratorResult) DataTemplatePersistentClassChecker(com.linkedin.pegasus.generator.DataTemplatePersistentClassChecker) JavaCodeUtil(com.linkedin.pegasus.generator.JavaCodeUtil) File(java.io.File)

Aggregations

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