Search in sources :

Example 1 with DependenciesConfig

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

the class DiscoGapicGeneratorApp method getGenerators.

/**
 * From config file paths, constructs the DiscoGapicGenerators to run.
 */
@VisibleForTesting
public static List<CodeGenerator<?>> getGenerators(String discoveryDocPath, List<String> configFileNames, String packageConfig2File, String dependencyConfigFile, String languageStr, List<String> enabledArtifacts, ArtifactType artifactType) 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(packageConfig2File)) {
        ApiDefaultsConfig apiDefaultsConfig = ApiDefaultsConfig.load();
        DependenciesConfig dependenciesConfig;
        if (dependencyConfigFile != null) {
            dependenciesConfig = DependenciesConfig.loadFromURL(new File(dependencyConfigFile).toURI().toURL());
        } else {
            dependenciesConfig = DependenciesConfig.load();
        }
        PackagingConfig packagingConfig = PackagingConfig.load(packageConfig2File);
        packageConfig = PackageMetadataConfig.createFromPackaging(apiDefaultsConfig, dependenciesConfig, packagingConfig);
    }
    TargetLanguage language;
    if (!Strings.isNullOrEmpty(languageStr)) {
        language = TargetLanguage.fromString(languageStr.toUpperCase());
    } else {
        throw new IllegalArgumentException("Language not set by --language option.");
    }
    String defaultPackageName = configProto.getLanguageSettingsMap().get(languageStr).getPackageName();
    DiscoApiModel model = new DiscoApiModel(Document.from(new DiscoveryNode(root)), defaultPackageName);
    GapicProductConfig productConfig = GapicProductConfig.create(model, configProto, language);
    ArtifactFlags artifactFlags = new ArtifactFlags(enabledArtifacts, artifactType, false);
    return DiscoGapicGeneratorFactory.create(language, model, productConfig, packageConfig, artifactFlags);
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) DiscoveryNode(com.google.api.codegen.discovery.DiscoveryNode) InputStreamReader(java.io.InputStreamReader) ConfigProto(com.google.api.codegen.ConfigProto) MultiYamlReader(com.google.api.codegen.util.MultiYamlReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiDefaultsConfig(com.google.api.codegen.config.ApiDefaultsConfig) IOException(java.io.IOException) ArtifactFlags(com.google.api.codegen.gapic.ArtifactFlags) FileInputStream(java.io.FileInputStream) DiscoApiModel(com.google.api.codegen.config.DiscoApiModel) PackagingConfig(com.google.api.codegen.config.PackagingConfig) DependenciesConfig(com.google.api.codegen.config.DependenciesConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) File(java.io.File) TargetLanguage(com.google.api.codegen.common.TargetLanguage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with DependenciesConfig

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

the class GapicGeneratorApp method process.

@Override
protected void process() throws Exception {
    String protoPackage = Strings.emptyToNull(options.get(PROTO_PACKAGE));
    // Read the GAPIC config, if it was given, and convert it to proto.
    List<String> configFileNames = options.get(GENERATOR_CONFIG_FILES);
    ConfigProto configProto = null;
    if (configFileNames.size() > 0) {
        // Read the YAML config and convert it to proto.
        ConfigSource configSource = loadConfigFromFiles(configFileNames, ConfigProto.getDescriptor().getFullName(), ConfigProto.getDefaultInstance());
        if (configSource == null) {
            return;
        }
        configProto = (ConfigProto) configSource.getConfig();
        if (configProto == null) {
            return;
        }
    }
    // Consume gRPC Service Config if it is given with gapic_v2.
    String gRPCServiceConfigPath = options.get(GRPC_SERVICE_CONFIG);
    ServiceConfig gRPCServiceConfig = null;
    if (!Strings.isNullOrEmpty(gRPCServiceConfigPath) && configProto.getConfigSchemaVersion().equals("2.0.0")) {
        ServiceConfig.Builder builder = ServiceConfig.newBuilder();
        FileReader file = new FileReader(gRPCServiceConfigPath);
        JsonFormat.parser().merge(file, builder);
        gRPCServiceConfig = builder.build();
    }
    // Read the sample configs, if they are given, and convert them to protos.
    SampleConfigProto sampleConfigProto = null;
    List<String> sampleConfigFileNames = options.get(SAMPLE_CONFIG_FILES);
    if (sampleConfigFileNames.size() > 0) {
        ConfigSource configSource = loadConfigFromFiles(SampleConfigSanitizer.sanitize(sampleConfigFileNames), SampleConfigProto.getDescriptor().getFullName(), SampleConfigProto.getDefaultInstance());
        // TODO(hzyi): Verify this works for repeated fields as well
        // TODO(hzyi): Allow users to put arbitrary top-level directives not
        // used by gapic-generator
        sampleConfigProto = (SampleConfigProto) configSource.getConfig();
    }
    model.establishStage(Merged.KEY);
    if (model.getDiagReporter().getDiagCollector().getErrorCount() > 0) {
        for (Diag diag : model.getDiagReporter().getDiagCollector().getDiags()) {
            System.err.println(diag.toString());
        }
        return;
    }
    ApiDefaultsConfig apiDefaultsConfig = ApiDefaultsConfig.load();
    DependenciesConfig dependenciesConfig = DependenciesConfig.load();
    TargetLanguage language;
    if (!Strings.isNullOrEmpty(options.get(LANGUAGE))) {
        language = TargetLanguage.fromString(options.get(LANGUAGE).toUpperCase());
    } else {
        throw new IllegalArgumentException("Language not set by --language option.");
    }
    String clientPackage = Strings.emptyToNull(options.get(CLIENT_PACKAGE));
    String transport = options.get(TRANSPORT).toLowerCase();
    TransportProtocol tp;
    if (transport.equals("grpc")) {
        tp = TransportProtocol.GRPC;
    } else if (transport.equals("rest")) {
        tp = TransportProtocol.HTTP;
    } else {
        throw new IllegalArgumentException("Unknown transport protocol: " + transport);
    }
    GapicProductConfig productConfig = GapicProductConfig.create(model, configProto, sampleConfigProto, protoPackage, clientPackage, language, gRPCServiceConfig, tp);
    if (productConfig == null) {
        ToolUtil.reportDiags(model.getDiagReporter().getDiagCollector(), true);
        return;
    }
    PackagingConfig packagingConfig;
    if (!Strings.isNullOrEmpty(options.get(PACKAGE_CONFIG2_FILE))) {
        packagingConfig = PackagingConfig.load(options.get(PACKAGE_CONFIG2_FILE));
    } else {
        packagingConfig = PackagingConfig.loadFromProductConfig(productConfig.getInterfaceConfigMap());
    }
    PackageMetadataConfig packageConfig = PackageMetadataConfig.createFromPackaging(apiDefaultsConfig, dependenciesConfig, packagingConfig);
    // TODO(hzyi-google): Once we switch to sample configs, require an
    // additional check to generate samples:
    // `sampleConfigProto != null`
    ArtifactFlags artifactFlags = new ArtifactFlags(options.get(ENABLED_ARTIFACTS), artifactType, options.get(DEV_SAMPLES));
    List<CodeGenerator<?>> generators = GapicGeneratorFactory.create(language, model, productConfig, packageConfig, artifactFlags);
    ImmutableMap.Builder<String, GeneratedResult<?>> generatedResults = ImmutableMap.builder();
    for (CodeGenerator<?> generator : generators) {
        Map<String, ? extends GeneratedResult<?>> generatorResult = generator.generate();
        for (Map.Entry<String, ? extends GeneratedResult<?>> entry : generatorResult.entrySet()) {
            generatedResults.put(entry.getKey(), entry.getValue());
        }
    }
    gapicWriter.writeCodeGenOutput(generatedResults.build(), model.getDiagReporter().getDiagCollector());
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) SampleConfigProto(com.google.api.codegen.samplegen.v1p2.SampleConfigProto) ConfigProto(com.google.api.codegen.ConfigProto) GeneratedResult(com.google.api.codegen.common.GeneratedResult) ApiDefaultsConfig(com.google.api.codegen.config.ApiDefaultsConfig) ServiceConfig(com.google.api.codegen.grpc.ServiceConfig) SampleConfigProto(com.google.api.codegen.samplegen.v1p2.SampleConfigProto) PackagingConfig(com.google.api.codegen.config.PackagingConfig) FileReader(java.io.FileReader) TargetLanguage(com.google.api.codegen.common.TargetLanguage) Diag(com.google.api.tools.framework.model.Diag) CodeGenerator(com.google.api.codegen.common.CodeGenerator) ImmutableMap(com.google.common.collect.ImmutableMap) ConfigSource(com.google.api.tools.framework.model.ConfigSource) DependenciesConfig(com.google.api.codegen.config.DependenciesConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) TransportProtocol(com.google.api.codegen.config.TransportProtocol) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with DependenciesConfig

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

the class PackageGeneratorApp method generate.

protected Map<String, GeneratedResult<Doc>> generate(Model model) throws IOException {
    TargetLanguage language = TargetLanguage.fromString(options.get(LANGUAGE));
    PackageMetadataConfig config = null;
    ApiDefaultsConfig apiDefaultsConfig = ApiDefaultsConfig.load();
    DependenciesConfig dependenciesConfig;
    if (dependenciesYamlUrl != null) {
        dependenciesConfig = DependenciesConfig.loadFromURL(dependenciesYamlUrl);
    } else {
        dependenciesConfig = DependenciesConfig.load();
    }
    PackagingConfig packagingConfig = null;
    if (!Strings.isNullOrEmpty(options.get(PACKAGE_CONFIG2_FILE))) {
        packagingConfig = PackagingConfig.load(options.get(PACKAGE_CONFIG2_FILE));
    } else {
    // TODO(andrealin): Get PackageMetadataConfig from proto annotations.
    }
    config = PackageMetadataConfig.createFromPackaging(apiDefaultsConfig, dependenciesConfig, packagingConfig);
    Preconditions.checkNotNull(config);
    PackagingArtifactType artifactType = options.get(PackageGeneratorApp.ARTIFACT_TYPE);
    if (artifactType == null) {
        artifactType = config.artifactType();
    }
    CodeGenerator<Doc> generator = PackageGeneratorFactory.create(language, artifactType, options, model, config);
    return generator.generate();
}
Also used : PackagingConfig(com.google.api.codegen.config.PackagingConfig) DependenciesConfig(com.google.api.codegen.config.DependenciesConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) Doc(com.google.api.tools.framework.snippet.Doc) ApiDefaultsConfig(com.google.api.codegen.config.ApiDefaultsConfig) TargetLanguage(com.google.api.codegen.common.TargetLanguage)

Aggregations

TargetLanguage (com.google.api.codegen.common.TargetLanguage)3 ApiDefaultsConfig (com.google.api.codegen.config.ApiDefaultsConfig)3 DependenciesConfig (com.google.api.codegen.config.DependenciesConfig)3 PackageMetadataConfig (com.google.api.codegen.config.PackageMetadataConfig)3 PackagingConfig (com.google.api.codegen.config.PackagingConfig)3 ConfigProto (com.google.api.codegen.ConfigProto)2 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CodeGenerator (com.google.api.codegen.common.CodeGenerator)1 GeneratedResult (com.google.api.codegen.common.GeneratedResult)1 DiscoApiModel (com.google.api.codegen.config.DiscoApiModel)1 TransportProtocol (com.google.api.codegen.config.TransportProtocol)1 DiscoveryNode (com.google.api.codegen.discovery.DiscoveryNode)1 ArtifactFlags (com.google.api.codegen.gapic.ArtifactFlags)1 ServiceConfig (com.google.api.codegen.grpc.ServiceConfig)1 SampleConfigProto (com.google.api.codegen.samplegen.v1p2.SampleConfigProto)1 MultiYamlReader (com.google.api.codegen.util.MultiYamlReader)1 ConfigSource (com.google.api.tools.framework.model.ConfigSource)1 Diag (com.google.api.tools.framework.model.Diag)1