Search in sources :

Example 1 with PackageMetadataConfig

use of com.google.api.codegen.config.PackageMetadataConfig in project toolkit by googleapis.

the class GrpcMetadataGenerator method generate.

protected Map<String, Doc> generate(Model model) throws IOException {
    TargetLanguage language = TargetLanguage.fromString(options.get(LANGUAGE));
    String configContent = new String(Files.readAllBytes(Paths.get(options.get(METADATA_CONFIG_FILE))), StandardCharsets.UTF_8);
    PackageMetadataConfig config = PackageMetadataConfig.createFromString(configContent);
    ArtifactType artifactType = options.get(GrpcMetadataGenerator.ARTIFACT_TYPE);
    if (artifactType == null) {
        artifactType = config.artifactType();
    }
    GrpcMetadataProvider provider = GrpcMetadataProviderFactory.create(language, artifactType, options);
    return provider.generate(model, config);
}
Also used : PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) TargetLanguage(com.google.api.codegen.TargetLanguage)

Example 2 with PackageMetadataConfig

use of com.google.api.codegen.config.PackageMetadataConfig 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 3 with PackageMetadataConfig

use of com.google.api.codegen.config.PackageMetadataConfig in project toolkit by googleapis.

the class DiscoGapicGeneratorApi method getProviders.

/**
 * From config file paths, constructs the DiscoGapicProviders to run.
 */
@VisibleForTesting
static List<GapicProvider> getProviders(String discoveryDocPath, List<String> configFileNames, String packageConfigFile, List<String> enabledArtifacts) throws IOException {
    if (!new File(discoveryDocPath).exists()) {
        throw new IOException("File not found: " + discoveryDocPath);
    }
    Reader reader = new InputStreamReader(new FileInputStream(new File(discoveryDocPath)));
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(reader);
    // Read the YAML config and convert it to proto.
    if (configFileNames.size() == 0) {
        throw new IOException(String.format("--%s must be provided", GENERATOR_CONFIG_FILES.name()));
    }
    ConfigProto configProto = loadConfigFromFiles(configFileNames);
    if (configProto == null) {
        throw new IOException("Failed to load config proto.");
    }
    PackageMetadataConfig packageConfig = null;
    if (!Strings.isNullOrEmpty(packageConfigFile)) {
        String contents = new String(Files.readAllBytes(Paths.get(packageConfigFile)), StandardCharsets.UTF_8);
        packageConfig = PackageMetadataConfig.createFromString(contents);
    }
    GeneratorProto generator = configProto.getGenerator();
    String language = configProto.getLanguage();
    String defaultPackageName = configProto.getLanguageSettingsMap().get(language).getPackageName();
    SurfaceNamer surfaceNamer = null;
    if (language.equals(JAVA)) {
        surfaceNamer = new JavaSurfaceNamer(defaultPackageName, defaultPackageName, new JavaNameFormatter());
    }
    if (surfaceNamer == null) {
        throw new UnsupportedOperationException("DiscoGapicGeneratorApi: language \"" + language + "\" not yet supported");
    }
    DiscoApiModel model = new DiscoApiModel(Document.from(new DiscoveryNode(root)), defaultPackageName);
    GapicProductConfig productConfig = GapicProductConfig.create(model, configProto, surfaceNamer);
    String factory = generator.getFactory();
    String id = generator.getId();
    DiscoGapicProviderFactory providerFactory = createProviderFactory(factory);
    GapicGeneratorConfig generatorConfig = GapicGeneratorConfig.newBuilder().id(id).enabledArtifacts(enabledArtifacts).build();
    return providerFactory.create(model, productConfig, generatorConfig, packageConfig);
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) DiscoveryNode(com.google.api.codegen.discovery.DiscoveryNode) InputStreamReader(java.io.InputStreamReader) ConfigYamlReader(com.google.api.codegen.configgen.ConfigYamlReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) FileInputStream(java.io.FileInputStream) JavaNameFormatter(com.google.api.codegen.util.java.JavaNameFormatter) DiscoGapicProviderFactory(com.google.api.codegen.discogapic.DiscoGapicProviderFactory) DiscoApiModel(com.google.api.codegen.config.DiscoApiModel) GapicGeneratorConfig(com.google.api.codegen.gapic.GapicGeneratorConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with PackageMetadataConfig

use of com.google.api.codegen.config.PackageMetadataConfig 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

PackageMetadataConfig (com.google.api.codegen.config.PackageMetadataConfig)4 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)3 GapicGeneratorConfig (com.google.api.codegen.gapic.GapicGeneratorConfig)3 GapicProvider (com.google.api.codegen.gapic.GapicProvider)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TargetLanguage (com.google.api.codegen.TargetLanguage)1 Adviser (com.google.api.codegen.advising.Adviser)1 DiscoApiModel (com.google.api.codegen.config.DiscoApiModel)1 ConfigYamlReader (com.google.api.codegen.configgen.ConfigYamlReader)1 DiscoGapicProviderFactory (com.google.api.codegen.discogapic.DiscoGapicProviderFactory)1 DiscoveryNode (com.google.api.codegen.discovery.DiscoveryNode)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 JavaSurfaceNamer (com.google.api.codegen.transformer.java.JavaSurfaceNamer)1 JavaNameFormatter (com.google.api.codegen.util.java.JavaNameFormatter)1 ConfigSource (com.google.api.tools.framework.model.ConfigSource)1 Diag (com.google.api.tools.framework.model.Diag)1 Model (com.google.api.tools.framework.model.Model)1 Doc (com.google.api.tools.framework.snippet.Doc)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1