Search in sources :

Example 1 with GeneratorResult

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

the class RestLiResourceModelExporter method generateIDLFiles.

private GeneratorResult generateIDLFiles(String apiName, String outdir, Map<String, ResourceModel> rootResourceMap, DocsProvider docsProvider) throws IOException {
    Result result = new Result();
    final File outdirFile = new File(outdir);
    if (!outdirFile.exists()) {
        if (!outdirFile.mkdirs()) {
            throw new RuntimeException("Output directory '" + outdir + "' could not be created!");
        }
    }
    if (!outdirFile.isDirectory()) {
        throw new RuntimeException("Output directory '" + outdir + "' is not a directory");
    }
    if (!outdirFile.canRead() || !outdirFile.canWrite()) {
        throw new RuntimeException("Output directory '" + outdir + "' must be writeable");
    }
    final ResourceModelEncoder encoder = new ResourceModelEncoder(docsProvider);
    final List<ResourceSchema> rootResourceNodes = new ArrayList<ResourceSchema>();
    for (Entry<String, ResourceModel> entry : rootResourceMap.entrySet()) {
        final ResourceSchema rootResourceNode = encoder.buildResourceSchema(entry.getValue());
        rootResourceNodes.add(rootResourceNode);
    }
    for (ResourceSchema rootResourceNode : rootResourceNodes) {
        String fileName = rootResourceNode.getName();
        if (rootResourceNode.getNamespace() != null) {
            final String namespace = rootResourceNode.getNamespace();
            fileName = namespace + "." + fileName;
        }
        if (apiName != null && !apiName.isEmpty()) {
            fileName = apiName + "-" + fileName;
        }
        File writtenFile = writeIDLFile(outdirFile, fileName, rootResourceNode);
        result.addModifiedFile(writtenFile);
        result.addTargetFile(writtenFile);
    }
    return result;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceModelEncoder(com.linkedin.restli.internal.server.model.ResourceModelEncoder) ArrayList(java.util.ArrayList) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) File(java.io.File) GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult)

Example 2 with GeneratorResult

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

the class TestRestLiResourceModelExporter method testSimpleModel.

@Test
public void testSimpleModel() throws Exception {
    RestLiResourceModelExporter exporter = new RestLiResourceModelExporter();
    assertEquals(outdir.list().length, 0);
    GeneratorResult result = exporter.export("twitter", null, new String[] { moduleDir + FS + TEST_DIR }, new String[] { "com.linkedin.restli.tools.twitter" }, null, outdir.getAbsolutePath());
    String[] expectedFiles = { STATUSES_FILE, FOLLOWS_FILE, ACCOUNTS_FILE, STATUSES_PARAMS_FILE, TRENDING_FILE };
    assertEquals(outdir.list().length, expectedFiles.length);
    assertEquals(result.getModifiedFiles().size(), expectedFiles.length);
    assertEquals(result.getTargetFiles().size(), expectedFiles.length);
    for (String file : expectedFiles) {
        String actualFile = outdir + FS + file;
        String expectedFile = moduleDir + FS + IDLS_DIR + FS + file;
        compareFiles(actualFile, expectedFile);
        assertTrue(result.getModifiedFiles().contains(new File(actualFile)));
        assertTrue(result.getTargetFiles().contains(new File(actualFile)));
    }
}
Also used : GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult) File(java.io.File) Test(org.testng.annotations.Test)

Example 3 with GeneratorResult

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

the class TestRestLiSnapshotExporter method testSimpleSnapshot.

@Test
public void testSimpleSnapshot() throws Exception {
    RestLiSnapshotExporter exporter = new RestLiSnapshotExporter();
    exporter.setResolverPath(resolverPath);
    assertEquals(outdir.list().length, 0);
    GeneratorResult result = exporter.export("twitter", null, new String[] { moduleDir + FS + TEST_DIR }, new String[] { "com.linkedin.restli.tools.twitter" }, null, outdir.getAbsolutePath());
    String[] expectedFiles = { STATUSES_FILE, FOLLOWS_FILE, ACCOUNTS_FILE, STATUSES_PARAMS_FILE, TRENDING_FILE };
    assertEquals(outdir.list().length, expectedFiles.length);
    assertEquals(result.getModifiedFiles().size(), expectedFiles.length);
    assertEquals(result.getTargetFiles().size(), expectedFiles.length);
    for (String file : expectedFiles) {
        String actualFile = outdir + FS + file;
        String expectedFile = SNAPSHOTS_DIR + FS + file;
        compareFiles(actualFile, expectedFile);
        assertTrue(result.getModifiedFiles().contains(new File(actualFile)));
        assertTrue(result.getTargetFiles().contains(new File(actualFile)));
    }
}
Also used : GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult) File(java.io.File) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with GeneratorResult

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

the class RestLiResourceModelExporter method export.

/**
   * @param apiName the name of the API
   * @param classpath classpath to to load the resources. this is purely for Javadoc Doclet {@link RestLiDoclet}
   * @param sourcePaths paths to scan for resource Java source files. this is purely for Javadoc Doclet {@link RestLiDoclet}
   *                    if both resourcePackages and resourceClasses is null, all classes defined in the directories will be scanned
   * @param resourcePackages packages to scan for resources
   * @param resourceClasses specific classes as resources
   * @param outdir directory in which to output the IDL files
   * @param additionalDocProviders names of additional classes in the classpath that implement DocsProvider, if empty,
   *                      only javadoc will be supported.
   * @return a result that includes collection of files generated and modified. Note: getSourceFiles() on the result
   * will always return an empty List as the code generation operates on classpaths and the ClassLoader and not files.
   * @throws IOException could be {@link java.io.FileNotFoundException} if unable to write the output file,
   *                     otherwise, {@link IOException} if failure happened when writing the output file
   */
public GeneratorResult export(String apiName, String[] classpath, String[] sourcePaths, String[] resourcePackages, String[] resourceClasses, String outdir, List<DocsProvider> additionalDocProviders) throws IOException {
    final RestLiConfig config = new RestLiConfig();
    if (resourcePackages != null) {
        config.addResourcePackageNames(resourcePackages);
    }
    final Map<String, String> classFileNames = new HashMap<String, String>();
    for (String path : sourcePaths) {
        classFileNames.putAll(FileClassNameScanner.scan(path));
    }
    Collection<String> sourceFileNames = null;
    if (resourceClasses != null || resourcePackages == null) {
        if (resourceClasses != null) {
            config.addResourceClassNames(resourceClasses);
            sourceFileNames = new ArrayList<String>(resourceClasses.length);
            for (String resourceClass : resourceClasses) {
                final String resourceFileName = classFileNames.get(resourceClass);
                if (resourceFileName == null) {
                    log.warn("Unable to find source file for class " + resourceClass + " .  No documentation will be generated for it.");
                } else {
                    sourceFileNames.add(resourceFileName);
                }
            }
        } else {
            config.addResourceClassNames(classFileNames.keySet());
            sourceFileNames = classFileNames.values();
        }
    }
    log.info("Executing Rest.li annotation processor...");
    final RestLiApiBuilder apiBuilder = new RestLiApiBuilder(config);
    final Map<String, ResourceModel> rootResourceMap = apiBuilder.build();
    if (rootResourceMap.isEmpty()) {
        return new Result();
    }
    // We always include the doc provider for javadoc
    DocsProvider javadocProvider = new DocletDocsProvider(apiName, classpath, sourcePaths, resourcePackages);
    DocsProvider docsProvider;
    if (additionalDocProviders == null || additionalDocProviders.isEmpty()) {
        docsProvider = javadocProvider;
    } else {
        // dynamically load doc providers for additional language, if available
        List<DocsProvider> languageSpecificDocsProviders = new ArrayList<DocsProvider>();
        languageSpecificDocsProviders.add(javadocProvider);
        languageSpecificDocsProviders.addAll(MultiLanguageDocsProvider.loadExternalProviders(additionalDocProviders));
        docsProvider = new MultiLanguageDocsProvider(languageSpecificDocsProviders);
    }
    log.info("Registering source files with doc providers...");
    docsProvider.registerSourceFiles(classFileNames.values());
    log.info("Exporting IDL files...");
    final GeneratorResult result = generateIDLFiles(apiName, outdir, rootResourceMap, docsProvider);
    log.info("Done!");
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult) DocsProvider(com.linkedin.restli.internal.server.model.ResourceModelEncoder.DocsProvider) RestLiApiBuilder(com.linkedin.restli.internal.server.model.RestLiApiBuilder) GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Example 5 with GeneratorResult

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

the class RestLiSnapshotExporter method export.

public GeneratorResult export(String apiName, String[] classpath, String[] sourcePaths, String[] resourcePackages, String[] resourceClasses, String outdir, List<DocsProvider> additionalDocProviders) throws IOException {
    final RestLiConfig config = new RestLiConfig();
    if (resourcePackages != null) {
        config.addResourcePackageNames(resourcePackages);
    }
    final Map<String, String> classFileNames = new HashMap<String, String>();
    for (String path : sourcePaths) {
        classFileNames.putAll(FileClassNameScanner.scan(path));
    }
    Collection<String> sourceFileNames = null;
    if (resourceClasses != null || resourcePackages == null) {
        if (resourceClasses != null) {
            config.addResourceClassNames(resourceClasses);
            sourceFileNames = new ArrayList<String>(resourceClasses.length);
            for (String resourceClass : resourceClasses) {
                final String resourceFileName = classFileNames.get(resourceClass);
                if (resourceFileName == null) {
                    log.warn("Unable to find source file for class " + resourceClass + " .  No Javadoc will be generated for it.");
                } else {
                    sourceFileNames.add(resourceFileName);
                }
            }
        } else {
            config.addResourceClassNames(classFileNames.keySet());
        }
    }
    log.info("Executing Rest.li annotation processor...");
    final RestLiApiBuilder apiBuilder = new RestLiApiBuilder(config);
    final Map<String, ResourceModel> rootResourceMap = apiBuilder.build();
    if (rootResourceMap.isEmpty()) {
        return new SnapshotResult();
    }
    // We always include the doc provider for javadoc
    DocsProvider javadocProvider = new DocletDocsProvider(apiName, classpath, sourcePaths, resourcePackages);
    DocsProvider docsProvider;
    if (additionalDocProviders == null || additionalDocProviders.isEmpty()) {
        docsProvider = javadocProvider;
    } else {
        // dynamically load doc providers for additional language, if available
        List<DocsProvider> languageSpecificDocsProviders = new ArrayList<DocsProvider>();
        languageSpecificDocsProviders.add(javadocProvider);
        languageSpecificDocsProviders.addAll(MultiLanguageDocsProvider.loadExternalProviders(additionalDocProviders));
        docsProvider = new MultiLanguageDocsProvider(languageSpecificDocsProviders);
    }
    log.info("Registering source files with doc providers...");
    docsProvider.registerSourceFiles(classFileNames.values());
    log.info("Exporting snapshot files...");
    final GeneratorResult result = generateSnapshotFiles(apiName, outdir, rootResourceMap, docsProvider);
    log.info("Done!");
    return result;
}
Also used : MultiLanguageDocsProvider(com.linkedin.restli.tools.idlgen.MultiLanguageDocsProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DocletDocsProvider(com.linkedin.restli.tools.idlgen.DocletDocsProvider) MultiLanguageDocsProvider(com.linkedin.restli.tools.idlgen.MultiLanguageDocsProvider) DocsProvider(com.linkedin.restli.internal.server.model.ResourceModelEncoder.DocsProvider) RestLiApiBuilder(com.linkedin.restli.internal.server.model.RestLiApiBuilder) DocletDocsProvider(com.linkedin.restli.tools.idlgen.DocletDocsProvider) GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Aggregations

GeneratorResult (com.linkedin.pegasus.generator.GeneratorResult)8 File (java.io.File)6 Test (org.testng.annotations.Test)5 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)3 ArrayList (java.util.ArrayList)3 AfterTest (org.testng.annotations.AfterTest)3 BeforeTest (org.testng.annotations.BeforeTest)3 DocsProvider (com.linkedin.restli.internal.server.model.ResourceModelEncoder.DocsProvider)2 RestLiApiBuilder (com.linkedin.restli.internal.server.model.RestLiApiBuilder)2 RestLiConfig (com.linkedin.restli.server.RestLiConfig)2 HashMap (java.util.HashMap)2 ResourceModelEncoder (com.linkedin.restli.internal.server.model.ResourceModelEncoder)1 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)1 DocletDocsProvider (com.linkedin.restli.tools.idlgen.DocletDocsProvider)1 MultiLanguageDocsProvider (com.linkedin.restli.tools.idlgen.MultiLanguageDocsProvider)1