Search in sources :

Example 1 with GapicProvider

use of com.google.api.codegen.gapic.GapicProvider in project toolkit by googleapis.

the class MainDiscoGapicProviderFactory method defaultCreate.

/**
 * Create the DiscoGapicProvider based on the given id
 */
public static List<GapicProvider> defaultCreate(DiscoApiModel model, GapicProductConfig productConfig, GapicGeneratorConfig generatorConfig, PackageMetadataConfig packageConfig) {
    ArrayList<GapicProvider> providers = new ArrayList<>();
    String id = generatorConfig.id();
    // Please keep the following IDs in alphabetical order
    if (id.equals(JAVA)) {
        if (generatorConfig.enableSurfaceGenerator()) {
            GapicCodePathMapper javaPathMapper = CommonGapicCodePathMapper.newBuilder().setPrefix("src/main/java").setShouldAppendPackage(true).build();
            List<DocumentToViewTransformer> transformers = Arrays.asList(new JavaDiscoGapicResourceNameToViewTransformer(javaPathMapper, packageConfig), new JavaDiscoGapicSchemaToViewTransformer(javaPathMapper, packageConfig), new JavaDiscoGapicRequestToViewTransformer(javaPathMapper, packageConfig), new JavaDiscoGapicSurfaceTransformer(javaPathMapper, packageConfig));
            DiscoGapicProvider provider = DiscoGapicProvider.newBuilder().setDiscoApiModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new JavaRenderingUtil())).setDocumentToViewTransformers(transformers).build();
            providers.add(provider);
        }
        if (generatorConfig.enableTestGenerator()) {
            GapicCodePathMapper javaTestPathMapper = CommonGapicCodePathMapper.newBuilder().setPrefix("src/test/java").setShouldAppendPackage(true).build();
            GapicProvider testProvider = ViewModelDiscoGapicProvider.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new JavaSurfaceTestTransformer(javaTestPathMapper, new JavaDiscoGapicSurfaceTransformer(javaTestPathMapper, packageConfig), "java/http_test.snip")).build();
            providers.add(testProvider);
        }
        return providers;
    } else {
        throw new NotImplementedException("DiscoGapicProviderFactory: invalid id \"" + id + "\"");
    }
}
Also used : JavaDiscoGapicResourceNameToViewTransformer(com.google.api.codegen.discogapic.transformer.java.JavaDiscoGapicResourceNameToViewTransformer) JavaDiscoGapicRequestToViewTransformer(com.google.api.codegen.discogapic.transformer.java.JavaDiscoGapicRequestToViewTransformer) CommonGapicCodePathMapper(com.google.api.codegen.gapic.CommonGapicCodePathMapper) GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) CommonSnippetSetRunner(com.google.api.codegen.rendering.CommonSnippetSetRunner) JavaRenderingUtil(com.google.api.codegen.util.java.JavaRenderingUtil) NotImplementedException(org.apache.commons.lang3.NotImplementedException) GapicProvider(com.google.api.codegen.gapic.GapicProvider) ArrayList(java.util.ArrayList) JavaSurfaceTestTransformer(com.google.api.codegen.transformer.java.JavaSurfaceTestTransformer) JavaDiscoGapicSchemaToViewTransformer(com.google.api.codegen.discogapic.transformer.java.JavaDiscoGapicSchemaToViewTransformer) JavaDiscoGapicSurfaceTransformer(com.google.api.codegen.discogapic.transformer.java.JavaDiscoGapicSurfaceTransformer) CommonRenderingUtil(com.google.api.codegen.util.CommonRenderingUtil) DocumentToViewTransformer(com.google.api.codegen.discogapic.transformer.DocumentToViewTransformer)

Example 2 with GapicProvider

use of com.google.api.codegen.gapic.GapicProvider in project toolkit by googleapis.

the class DiscoGapicTestBase method run.

@Override
protected Map<String, Doc> run() {
    Map<String, Doc> outputDocs = new LinkedHashMap<>();
    for (GapicProvider provider : discoGapicProviders) {
        Map<String, Doc> docs = provider.generate();
        outputDocs.putAll(docs);
    }
    return outputDocs;
}
Also used : GapicProvider(com.google.api.codegen.gapic.GapicProvider) Doc(com.google.api.tools.framework.snippet.Doc) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with GapicProvider

use of com.google.api.codegen.gapic.GapicProvider in project toolkit by googleapis.

the class CodeGeneratorApi method process.

@Override
protected void process() throws Exception {
    // Read the YAML config and convert it to proto.
    List<String> configFileNames = options.get(GENERATOR_CONFIG_FILES);
    if (configFileNames.size() == 0) {
        error(String.format("--%s must be provided", GENERATOR_CONFIG_FILES.name()));
        return;
    }
    ConfigSource configSource = loadConfigFromFiles(configFileNames);
    if (configSource == null) {
        return;
    }
    ConfigProto configProto = (ConfigProto) configSource.getConfig();
    if (configProto == null) {
        return;
    }
    model.establishStage(Merged.KEY);
    List<String> adviceSuppressors = options.get(ADVICE_SUPPRESSORS);
    Adviser adviser = new Adviser(adviceSuppressors);
    adviser.advise(model, configProto);
    if (model.getDiagCollector().getErrorCount() > 0) {
        for (Diag diag : model.getDiagCollector().getDiags()) {
            System.err.println(diag.toString());
        }
        return;
    }
    PackageMetadataConfig packageConfig = null;
    if (!Strings.isNullOrEmpty(options.get(PACKAGE_CONFIG_FILE))) {
        String contents = new String(Files.readAllBytes(Paths.get(options.get(PACKAGE_CONFIG_FILE))), StandardCharsets.UTF_8);
        packageConfig = PackageMetadataConfig.createFromString(contents);
    }
    GeneratorProto generator = configProto.getGenerator();
    GapicProductConfig productConfig = GapicProductConfig.create(model, configProto);
    if (productConfig == null) {
        return;
    }
    if (generator != null) {
        String factory = generator.getFactory();
        String id = generator.getId();
        GapicProviderFactory<GapicProvider> providerFactory = createProviderFactory(model, factory);
        GapicGeneratorConfig generatorConfig = GapicGeneratorConfig.newBuilder().id(id).enabledArtifacts(options.get(ENABLED_ARTIFACTS)).build();
        String outputPath = options.get(OUTPUT_FILE);
        List<GapicProvider> providers = providerFactory.create(model, productConfig, generatorConfig, packageConfig);
        Map<String, Doc> outputFiles = Maps.newHashMap();
        for (GapicProvider provider : providers) {
            outputFiles.putAll(provider.generate());
        }
        writeCodeGenOutput(outputFiles, outputPath);
    }
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) Adviser(com.google.api.codegen.advising.Adviser) GapicProvider(com.google.api.codegen.gapic.GapicProvider) Diag(com.google.api.tools.framework.model.Diag) ConfigSource(com.google.api.tools.framework.model.ConfigSource) GapicGeneratorConfig(com.google.api.codegen.gapic.GapicGeneratorConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) Doc(com.google.api.tools.framework.snippet.Doc)

Example 4 with GapicProvider

use of com.google.api.codegen.gapic.GapicProvider in project toolkit by googleapis.

the class DiscoGapicGeneratorApi method run.

public void run() throws Exception {
    String discoveryDocPath = options.get(DISCOVERY_DOC);
    List<String> configFileNames = options.get(GENERATOR_CONFIG_FILES);
    String packageConfigFile = options.get(PACKAGE_CONFIG_FILE);
    List<String> enabledArtifacts = options.get(ENABLED_ARTIFACTS);
    List<GapicProvider> providers = getProviders(discoveryDocPath, configFileNames, packageConfigFile, enabledArtifacts);
    Map<String, Doc> outputFiles = Maps.newHashMap();
    for (GapicProvider provider : providers) {
        outputFiles.putAll(provider.generate());
    }
    ToolUtil.writeFiles(outputFiles, options.get(OUTPUT_FILE));
}
Also used : GapicProvider(com.google.api.codegen.gapic.GapicProvider) Doc(com.google.api.tools.framework.snippet.Doc)

Example 5 with GapicProvider

use of com.google.api.codegen.gapic.GapicProvider in project toolkit by googleapis.

the class GapicTestBase2 method createTestConfig.

/**
 * Creates the constructor arguments to be passed onto this class (GapicTestBase2) to create test
 * methods. The idForFactory String is passed to GapicProviderFactory to get the GapicProviders
 * provided by that id, and then the snippet file names are scraped from those providers, and a
 * set of arguments is created for each combination of GapicProvider x snippet that
 * GapicProviderFactory returns.
 */
public static Object[] createTestConfig(String idForFactory, String[] gapicConfigFileNames, String packageConfigFileName, String apiName) {
    Model model = Model.create(Service.getDefaultInstance());
    GapicProductConfig productConfig = GapicProductConfig.createDummyInstance();
    PackageMetadataConfig packageConfig = PackageMetadataConfig.createDummyPackageMetadataConfig();
    GapicGeneratorConfig generatorConfig = GapicGeneratorConfig.newBuilder().id(idForFactory).enabledArtifacts(Arrays.asList("surface", "test")).build();
    List<GapicProvider> providers = MainGapicProviderFactory.defaultCreate(model, productConfig, generatorConfig, packageConfig);
    List<String> snippetNames = new ArrayList<>();
    for (GapicProvider provider : providers) {
        snippetNames.addAll(provider.getSnippetFileNames());
    }
    String baseline = idForFactory + "_" + apiName + ".baseline";
    return new Object[] { idForFactory, gapicConfigFileNames, packageConfigFileName, snippetNames, apiName, baseline };
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) GapicGeneratorConfig(com.google.api.codegen.gapic.GapicGeneratorConfig) Model(com.google.api.tools.framework.model.Model) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) GapicProvider(com.google.api.codegen.gapic.GapicProvider) ArrayList(java.util.ArrayList)

Aggregations

GapicProvider (com.google.api.codegen.gapic.GapicProvider)6 Doc (com.google.api.tools.framework.snippet.Doc)4 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)3 GapicGeneratorConfig (com.google.api.codegen.gapic.GapicGeneratorConfig)3 ArrayList (java.util.ArrayList)3 PackageMetadataConfig (com.google.api.codegen.config.PackageMetadataConfig)2 Diag (com.google.api.tools.framework.model.Diag)2 Adviser (com.google.api.codegen.advising.Adviser)1 DocumentToViewTransformer (com.google.api.codegen.discogapic.transformer.DocumentToViewTransformer)1 JavaDiscoGapicRequestToViewTransformer (com.google.api.codegen.discogapic.transformer.java.JavaDiscoGapicRequestToViewTransformer)1 JavaDiscoGapicResourceNameToViewTransformer (com.google.api.codegen.discogapic.transformer.java.JavaDiscoGapicResourceNameToViewTransformer)1 JavaDiscoGapicSchemaToViewTransformer (com.google.api.codegen.discogapic.transformer.java.JavaDiscoGapicSchemaToViewTransformer)1 JavaDiscoGapicSurfaceTransformer (com.google.api.codegen.discogapic.transformer.java.JavaDiscoGapicSurfaceTransformer)1 CommonGapicCodePathMapper (com.google.api.codegen.gapic.CommonGapicCodePathMapper)1 GapicCodePathMapper (com.google.api.codegen.gapic.GapicCodePathMapper)1 CommonSnippetSetRunner (com.google.api.codegen.rendering.CommonSnippetSetRunner)1 JavaSurfaceTestTransformer (com.google.api.codegen.transformer.java.JavaSurfaceTestTransformer)1 CommonRenderingUtil (com.google.api.codegen.util.CommonRenderingUtil)1 JavaRenderingUtil (com.google.api.codegen.util.java.JavaRenderingUtil)1 ConfigSource (com.google.api.tools.framework.model.ConfigSource)1