Search in sources :

Example 1 with GeneratedResult

use of com.google.api.codegen.common.GeneratedResult in project toolkit by googleapis.

the class PythonGrpcPackageGenerator method generate.

@Override
public Map<String, GeneratedResult<Doc>> generate() throws IOException {
    ImmutableMap.Builder<String, GeneratedResult<Doc>> results = new ImmutableMap.Builder<>();
    ArrayList<PackageMetadataView> metadataViews = new ArrayList<>();
    PythonPackageCopier copier = new PythonPackageCopier();
    PythonPackageCopierResult copierResult = copier.run(options, config);
    results.putAll(copierResult.results());
    PythonGrpcPackageTransformer pythonTransformer = new PythonGrpcPackageTransformer(copierResult);
    ProtoApiModel apiModel = new ProtoApiModel(model);
    metadataViews.addAll(pythonTransformer.transform(apiModel, config));
    for (PackageMetadataView view : metadataViews) {
        CommonSnippetSetRunner runner = new CommonSnippetSetRunner(view, false);
        results.putAll(runner.generate(view));
    }
    return results.build();
}
Also used : CommonSnippetSetRunner(com.google.api.codegen.rendering.CommonSnippetSetRunner) GeneratedResult(com.google.api.codegen.common.GeneratedResult) ArrayList(java.util.ArrayList) ImmutableMap(com.google.common.collect.ImmutableMap) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) PackageMetadataView(com.google.api.codegen.viewmodel.metadata.PackageMetadataView)

Example 2 with GeneratedResult

use of com.google.api.codegen.common.GeneratedResult 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 GeneratedResult

use of com.google.api.codegen.common.GeneratedResult in project toolkit by googleapis.

the class JavaGrpcPackageGenerator method generate.

@Override
public Map<String, GeneratedResult<Doc>> generate() {
    ImmutableMap.Builder<String, GeneratedResult<Doc>> results = new ImmutableMap.Builder<>();
    ProtoApiModel apiModel = new ProtoApiModel(model);
    List<PackageMetadataView> metadataViews = transformer.transform(apiModel, config);
    for (PackageMetadataView view : metadataViews) {
        CommonSnippetSetRunner runner = new CommonSnippetSetRunner(view, false);
        results.putAll(runner.generate(view));
    }
    return results.build();
}
Also used : CommonSnippetSetRunner(com.google.api.codegen.rendering.CommonSnippetSetRunner) GeneratedResult(com.google.api.codegen.common.GeneratedResult) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) PackageMetadataView(com.google.api.codegen.viewmodel.metadata.PackageMetadataView) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with GeneratedResult

use of com.google.api.codegen.common.GeneratedResult in project toolkit by googleapis.

the class PackageGeneratorAppTest method run.

@Override
@Nullable
protected Map<String, Doc> run() throws Exception {
    String outFile = tempDir.getRoot().getPath() + File.separator + baselineFileName();
    String metadataConfigPath = getTestDataLocator().findTestData(packageConfig).getPath();
    ToolOptions options = ToolOptions.create();
    options.set(PackageGeneratorApp.OUTPUT_DIR, outFile);
    options.set(PackageGeneratorApp.INPUT_DIR, getTestDataLocator().findTestData("fakeprotodir").getPath());
    options.set(PackageGeneratorApp.PACKAGE_CONFIG2_FILE, metadataConfigPath);
    options.set(PackageGeneratorApp.LANGUAGE, language);
    options.set(PackageGeneratorApp.ARTIFACT_TYPE, artifactType);
    URL dependenciesYamlUrl = getTestDataLocator().findTestData("com/google/api/codegen/testsrc/common/frozen_dependencies.yaml");
    Preconditions.checkNotNull(dependenciesYamlUrl);
    Map<String, GeneratedResult<Doc>> generatedDocs = new PackageGeneratorApp(options, dependenciesYamlUrl).generate(model);
    if (TargetLanguage.fromString(language) == TargetLanguage.PYTHON) {
        OutputCollector collector = new OutputCollector(Paths.get(outFile));
        Files.walkFileTree(Paths.get(outFile), collector);
        return new ImmutableMap.Builder<String, Doc>().putAll(GeneratedResult.extractBodies(generatedDocs)).putAll(GeneratedResult.extractBodies(collector.getResults())).build();
    } else {
        return new ImmutableMap.Builder<String, Doc>().putAll(GeneratedResult.extractBodies(generatedDocs)).build();
    }
}
Also used : GeneratedResult(com.google.api.codegen.common.GeneratedResult) Doc(com.google.api.tools.framework.snippet.Doc) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) URL(java.net.URL) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Example 5 with GeneratedResult

use of com.google.api.codegen.common.GeneratedResult in project toolkit by googleapis.

the class GapicGenerator method generate.

@Override
public Map<String, GeneratedResult<Doc>> generate() {
    // Establish required stage for generation.
    model.establishStage(Merged.KEY);
    if (model.getDiagReporter().getDiagCollector().getErrorCount() > 0) {
        return null;
    }
    List<ViewModel> surfaceDocs = modelToViewTransformer.transform(new ProtoApiModel(model), productConfig);
    if (model.getDiagReporter().getDiagCollector().getErrorCount() > 0) {
        return null;
    }
    Map<String, GeneratedResult<Doc>> results = new TreeMap<>();
    for (ViewModel surfaceDoc : surfaceDocs) {
        results.putAll(snippetSetRunner.generate(surfaceDoc));
    }
    return results;
}
Also used : GeneratedResult(com.google.api.codegen.common.GeneratedResult) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) ViewModel(com.google.api.codegen.viewmodel.ViewModel) TreeMap(java.util.TreeMap)

Aggregations

GeneratedResult (com.google.api.codegen.common.GeneratedResult)8 ImmutableMap (com.google.common.collect.ImmutableMap)5 ProtoApiModel (com.google.api.codegen.config.ProtoApiModel)3 CommonSnippetSetRunner (com.google.api.codegen.rendering.CommonSnippetSetRunner)3 ViewModel (com.google.api.codegen.viewmodel.ViewModel)2 PackageMetadataView (com.google.api.codegen.viewmodel.metadata.PackageMetadataView)2 Diag (com.google.api.tools.framework.model.Diag)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 ConfigProto (com.google.api.codegen.ConfigProto)1 CodeGenerator (com.google.api.codegen.common.CodeGenerator)1 TargetLanguage (com.google.api.codegen.common.TargetLanguage)1 ApiDefaultsConfig (com.google.api.codegen.config.ApiDefaultsConfig)1 DependenciesConfig (com.google.api.codegen.config.DependenciesConfig)1 DiscoApiModel (com.google.api.codegen.config.DiscoApiModel)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 PackageMetadataConfig (com.google.api.codegen.config.PackageMetadataConfig)1 PackagingConfig (com.google.api.codegen.config.PackagingConfig)1 TransportProtocol (com.google.api.codegen.config.TransportProtocol)1 DiscoConfigTransformer (com.google.api.codegen.configgen.transformer.DiscoConfigTransformer)1