use of com.google.api.codegen.common.CodeGenerator 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());
}
use of com.google.api.codegen.common.CodeGenerator in project toolkit by googleapis.
the class DiscoGapicGeneratorFactory method create.
/**
* Create the DiscoGapicGenerator based on the given id
*/
public static List<CodeGenerator<?>> create(TargetLanguage language, DiscoApiModel model, GapicProductConfig productConfig, PackageMetadataConfig packageConfig, ArtifactFlags artifactFlags) {
ArrayList<CodeGenerator<?>> generators = new ArrayList<>();
// Please keep the following IDs in alphabetical order
if (language.equals(JAVA)) {
if (artifactFlags.surfaceGeneratorEnabled()) {
if (artifactFlags.codeFilesEnabled()) {
GapicCodePathMapper javaPathMapper = CommonGapicCodePathMapper.newBuilder().setPrefix("src/main/java").setShouldAppendPackage(true).build();
List<ModelToViewTransformer<DiscoApiModel>> transformers = Arrays.asList(new JavaDiscoGapicResourceNameToViewTransformer(javaPathMapper), new JavaDiscoGapicSchemaToViewTransformer(javaPathMapper), new JavaDiscoGapicRequestToViewTransformer(javaPathMapper), new JavaDiscoGapicSurfaceTransformer(javaPathMapper));
DiscoGapicGenerator generator = DiscoGapicGenerator.newBuilder().setDiscoApiModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new JavaRenderingUtil())).setModelToViewTransformers(transformers).build();
generators.add(generator);
}
if (artifactFlags.packagingFilesEnabled()) {
CodeGenerator metadataGenerator = DiscoGapicGenerator.newBuilder().setDiscoApiModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new JavaRenderingUtil())).setModelToViewTransformers(Collections.singletonList(new JavaGapicPackageTransformer<>(packageConfig))).build();
generators.add(metadataGenerator);
}
}
if (artifactFlags.testGeneratorEnabled()) {
GapicCodePathMapper javaTestPathMapper = CommonGapicCodePathMapper.newBuilder().setPrefix("src/test/java").setShouldAppendPackage(true).build();
CodeGenerator<?> testGenerator = DiscoGapicGenerator.newBuilder().setDiscoApiModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformers(Arrays.asList(new JavaSurfaceTestTransformer<>(javaTestPathMapper, new JavaDiscoGapicSurfaceTransformer(javaTestPathMapper), "java/http_test.snip"))).build();
generators.add(testGenerator);
}
return generators;
} else {
throw new UnsupportedOperationException("DiscoGapicGeneratorFactory: unsupported language \"" + language + "\"");
}
}
use of com.google.api.codegen.common.CodeGenerator in project toolkit by googleapis.
the class GapicTestBase2 method run.
@Override
public Map<String, ?> run() throws IOException {
model.establishStage(Merged.KEY);
if (model.getDiagReporter().getDiagCollector().getErrorCount() > 0) {
for (Diag diag : model.getDiagReporter().getDiagCollector().getDiags()) {
System.err.println(diag.toString());
}
return null;
}
if (sampleConfig == null) {
sampleConfig = SampleConfigProto.getDefaultInstance();
}
GapicProductConfig productConfig = GapicProductConfig.create(model, gapicConfig, sampleConfig, protoPackage, clientPackage, language, grpcServiceConfig, transportProtocol);
if (productConfig == null) {
for (Diag diag : model.getDiagReporter().getDiagCollector().getDiags()) {
System.err.println(diag.toString());
}
return null;
}
List<String> enabledArtifacts = new ArrayList<>();
if (hasSmokeTestConfig(productConfig)) {
enabledArtifacts.addAll(Arrays.asList("surface", "test", "samples"));
}
ArtifactFlags artifactFlags = new ArtifactFlags(enabledArtifacts, ArtifactType.LEGACY_GAPIC_AND_PACKAGE, true);
PackagingConfig actualPackagingConfig = packagingConfig;
if (actualPackagingConfig == null) {
actualPackagingConfig = PackagingConfig.loadFromProductConfig(productConfig.getInterfaceConfigMap());
}
PackageMetadataConfig packageConfig = PackageMetadataConfig.createFromPackaging(apiDefaultsConfig, dependenciesConfig, actualPackagingConfig);
List<CodeGenerator<?>> generators = GapicGeneratorFactory.create(language, model, productConfig, packageConfig, artifactFlags);
// Don't run any generators we're not testing.
ArrayList<CodeGenerator<?>> testedGenerators = new ArrayList<>();
for (CodeGenerator<?> generator : generators) {
if (!Collections.disjoint(generator.getInputFileNames(), snippetNames)) {
testedGenerators.add(generator);
}
}
Map<String, Object> output = new TreeMap<>();
for (CodeGenerator<?> generator : testedGenerators) {
Map<String, ? extends GeneratedResult<?>> out = generator.generate();
if (!Collections.disjoint(out.keySet(), output.keySet())) {
throw new IllegalStateException("file conflict");
}
for (Map.Entry<String, ? extends GeneratedResult<?>> entry : out.entrySet()) {
Object value = (entry.getValue().getBody() instanceof byte[]) ? "Static or binary file content is not shown." : entry.getValue().getBody();
output.put(entry.getKey(), value);
}
}
return output;
}
use of com.google.api.codegen.common.CodeGenerator 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 langauge String is passed to GapicGeneratorFactory to get the GapicGenerators
* provided by that language, and then the snippet file names are scraped from those generators,
* and a set of arguments is created for each combination of CodeGenerator x snippet that
* GapicGeneratorFactory returns.
*/
public static Object[] createTestConfig(TargetLanguage language, String[] gapicConfigFileNames, String packageConfigFileName, String apiName, String protoPackage, String clientPackage, String grpcServiceConfigFileName, String[] sampleConfigFileNames, String baseline, TransportProtocol transportProtocol, String... baseNames) {
Model model = Model.create(Service.getDefaultInstance());
GapicProductConfig productConfig = GapicProductConfig.createDummyInstance();
PackageMetadataConfig packageConfig = PackageMetadataConfig.createDummyPackageMetadataConfig();
ArtifactFlags artifactFlags = new ArtifactFlags(Arrays.asList("surface", "test", "samples"), ArtifactType.LEGACY_GAPIC_AND_PACKAGE, true);
List<CodeGenerator<?>> generators = GapicGeneratorFactory.create(language, model, productConfig, packageConfig, artifactFlags);
List<String> snippetNames = new ArrayList<>();
for (CodeGenerator<?> generator : generators) {
snippetNames.addAll(generator.getInputFileNames());
}
// autogenerated name will be provided.
if (baseline == null) {
StringBuilder suffix = new StringBuilder();
if (gapicConfigFileNames == null || gapicConfigFileNames.length == 0) {
suffix.append("_no_gapic_config");
}
if (!Strings.isNullOrEmpty(grpcServiceConfigFileName)) {
suffix.append("_with_grpc_service_config");
}
baseline = language.toString().toLowerCase() + "_" + apiName + suffix + ".baseline";
}
baseNames = Lists.asList(apiName, baseNames).toArray(new String[0]);
return new Object[] { language, gapicConfigFileNames, sampleConfigFileNames, packageConfigFileName, snippetNames, baseline, protoPackage, clientPackage, grpcServiceConfigFileName, transportProtocol, baseNames };
}
use of com.google.api.codegen.common.CodeGenerator in project toolkit by googleapis.
the class GapicGeneratorFactory method create.
/**
* Create the GapicGenerators based on the given id.
*
* <p>If {@code devSamples} is set, standalone samples are generated for all languages, including
* those that currently have development stubs. Otherwise, samples are only produced in languages
* where they are production-ready.
*/
public static List<CodeGenerator<?>> create(TargetLanguage language, Model model, GapicProductConfig productConfig, PackageMetadataConfig packageConfig, ArtifactFlags artifactFlags) {
ArrayList<CodeGenerator<?>> generators = new ArrayList<>();
// Please keep the following IDs in alphabetical order
if (language.equals(CSHARP)) {
String packageName = productConfig.getPackageName();
Function<String, GapicCodePathMapper> newCodePathMapper = suffix -> CommonGapicCodePathMapper.newBuilder().setPrefix(packageName + File.separator + packageName + suffix).setPackageFilePathNameFormatter(new CSharpNameFormatter()).build();
Function<ModelToViewTransformer<ProtoApiModel>, CodeGenerator> newCsharpGenerator = transformer -> GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CSharpRenderingUtil())).setModelToViewTransformer(transformer).build();
if (artifactFlags.surfaceGeneratorEnabled()) {
GapicCodePathMapper clientPathMapper = newCodePathMapper.apply("");
if (artifactFlags.codeFilesEnabled()) {
generators.add(newCsharpGenerator.apply(new CSharpGapicClientTransformer(clientPathMapper)));
}
if (artifactFlags.packagingFilesEnabled()) {
generators.add(newCsharpGenerator.apply(new CSharpGapicClientPackageTransformer(clientPathMapper, packageConfig)));
}
GapicCodePathMapper snippetPathMapper = newCodePathMapper.apply(".Snippets");
if (artifactFlags.codeFilesEnabled()) {
generators.add(newCsharpGenerator.apply(new CSharpGapicSnippetsTransformer(snippetPathMapper)));
}
if (artifactFlags.packagingFilesEnabled()) {
generators.add(newCsharpGenerator.apply(CSharpBasicPackageTransformer.forSnippets(snippetPathMapper)));
}
}
if (artifactFlags.testGeneratorEnabled()) {
GapicCodePathMapper smokeTestPathMapper = newCodePathMapper.apply(".SmokeTests");
if (artifactFlags.codeFilesEnabled()) {
generators.add(newCsharpGenerator.apply(new CSharpGapicSmokeTestTransformer(smokeTestPathMapper)));
}
if (artifactFlags.packagingFilesEnabled()) {
generators.add(newCsharpGenerator.apply(CSharpBasicPackageTransformer.forSmokeTests(smokeTestPathMapper)));
}
GapicCodePathMapper unitTestPathMapper = newCodePathMapper.apply(".Tests");
if (artifactFlags.codeFilesEnabled()) {
generators.add(newCsharpGenerator.apply(new CSharpGapicUnitTestTransformer(unitTestPathMapper)));
}
if (artifactFlags.packagingFilesEnabled()) {
generators.add(newCsharpGenerator.apply(CSharpBasicPackageTransformer.forUnitTests(unitTestPathMapper)));
}
}
if (artifactFlags.devSamplesEnabled()) {
GapicCodePathMapper samplePathMapper = newCodePathMapper.apply(".Samples");
CSharpStandaloneSampleTransformer csharpSampleTransformer = new CSharpStandaloneSampleTransformer(samplePathMapper);
generators.add(newCsharpGenerator.apply(csharpSampleTransformer));
if (artifactFlags.packagingFilesEnabled()) {
generators.add(newCsharpGenerator.apply(CSharpBasicPackageTransformer.forSamples(samplePathMapper)));
}
}
} else if (language.equals(GO)) {
if (artifactFlags.surfaceGeneratorEnabled()) {
CodeGenerator generator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new GoGapicSurfaceTransformer(new PackageNameCodePathMapper())).build();
generators.add(generator);
}
if (artifactFlags.testGeneratorEnabled()) {
CodeGenerator testGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new GoGapicSurfaceTestTransformer()).build();
generators.add(testGenerator);
}
} else if (language.equals(JAVA)) {
Function<ModelToViewTransformer<ProtoApiModel>, CodeGenerator> newJavaGenerator = transformer -> GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new JavaRenderingUtil())).setModelToViewTransformer(transformer).build();
if (artifactFlags.surfaceGeneratorEnabled()) {
GapicCodePathMapper javaPathMapper = JavaGapicCodePathMapper.newBuilder().prefix("src/main/java").build();
if (artifactFlags.codeFilesEnabled()) {
generators.add(newJavaGenerator.apply(new JavaGapicSurfaceTransformer(javaPathMapper)));
if (artifactFlags.devSamplesEnabled()) {
generators.add(newJavaGenerator.apply(new JavaGapicSamplesTransformer()));
generators.add(newJavaGenerator.apply(new JavaGapicSamplesPackageTransformer(packageConfig)));
generators.add(newJavaGenerator.apply(JavaGapicSamplesTransformer.createManifestTransformer()));
}
}
if (artifactFlags.packagingFilesEnabled()) {
generators.add(newJavaGenerator.apply(new JavaGapicPackageTransformer<>(packageConfig)));
CodeGenerator staticResourcesGenerator = new StaticResourcesGenerator(ImmutableMap.<String, String>builder().put("java/static/build.gradle", "../build.gradle").put("java/static/settings.gradle", "../settings.gradle").put("java/static/gradlew", "../gradlew").put("java/static/gradlew.bat", "../gradlew.bat").put("java/static/gradle/wrapper/gradle-wrapper.jar", "../gradle/wrapper/gradle-wrapper.jar").put("java/static/gradle/wrapper/gradle-wrapper.properties", "../gradle/wrapper/gradle-wrapper.properties").build(), ImmutableSet.of("../gradlew"));
generators.add(staticResourcesGenerator);
}
}
if (artifactFlags.testGeneratorEnabled()) {
if (artifactFlags.codeFilesEnabled()) {
GapicCodePathMapper javaTestPathMapper = JavaGapicCodePathMapper.newBuilder().prefix("src/test/java").build();
generators.add(newJavaGenerator.apply(new JavaSurfaceTestTransformer<>(javaTestPathMapper, new JavaGapicSurfaceTransformer(javaTestPathMapper), "java/test.snip")));
}
}
return generators;
} else if (language.equals(NODEJS)) {
if (artifactFlags.surfaceGeneratorEnabled()) {
GapicCodePathMapper nodeJSPathMapper = new NodeJSCodePathMapper();
CodeGenerator mainGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new NodeJSGapicSurfaceTransformer(nodeJSPathMapper, packageConfig)).build();
CodeGenerator metadataGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new NodeJSPackageMetadataTransformer(packageConfig)).build();
CodeGenerator clientConfigGenerator = LegacyGapicGenerator.newBuilder().setModel(model).setContext(new ClientConfigGapicContext(model, productConfig)).setSnippetSetRunner(new ClientConfigSnippetSetRunner<>(SnippetSetRunner.SNIPPET_RESOURCE_ROOT)).setSnippetFileNames(Arrays.asList("clientconfig/json.snip")).setCodePathMapper(nodeJSPathMapper).build();
generators.add(mainGenerator);
generators.add(metadataGenerator);
generators.add(clientConfigGenerator);
if (artifactFlags.devSamplesEnabled()) {
GapicCodePathMapper nodejsSamplePathMapper = new CommonSampleCodePathMapper();
NodeJSGapicSamplesTransformer nodejsGapicSamplesTransformer = new NodeJSGapicSamplesTransformer(nodejsSamplePathMapper);
CodeGenerator sampleGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(nodejsGapicSamplesTransformer).build();
CodeGenerator sampleMetadataGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new NodeJSSamplePackageMetadataTransformer(packageConfig)).build();
generators.add(sampleGenerator);
generators.add(sampleMetadataGenerator);
}
CodeGenerator messageGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new NodeJSGapicSurfaceDocTransformer()).build();
generators.add(messageGenerator);
}
if (artifactFlags.testGeneratorEnabled()) {
CodeGenerator testGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new NodeJSGapicSurfaceTestTransformer()).build();
generators.add(testGenerator);
}
} else if (language.equals(PHP)) {
if (artifactFlags.surfaceGeneratorEnabled()) {
if (artifactFlags.codeFilesEnabled()) {
GapicCodePathMapper phpPathMapper = PhpGapicCodePathMapper.newBuilder().setPrefix("src").build();
CodeGenerator generator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new PhpGapicSurfaceTransformer(productConfig, phpPathMapper, model)).build();
generators.add(generator);
GapicCodePathMapper phpClientConfigPathMapper = PhpGapicCodePathMapper.newBuilder().setPrefix("src").setSuffix("resources").build();
CodeGenerator clientConfigGenerator = LegacyGapicGenerator.newBuilder().setModel(model).setContext(new PhpClientConfigGapicContext(model, productConfig)).setSnippetSetRunner(new ClientConfigSnippetSetRunner<>(SnippetSetRunner.SNIPPET_RESOURCE_ROOT)).setSnippetFileNames(Arrays.asList("clientconfig/json.snip")).setCodePathMapper(phpClientConfigPathMapper).build();
generators.add(clientConfigGenerator);
}
if (artifactFlags.packagingFilesEnabled()) {
CodeGenerator metadataGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new PhpPackageMetadataTransformer(packageConfig)).build();
generators.add(metadataGenerator);
}
if (artifactFlags.devSamplesEnabled()) {
GapicCodePathMapper phpSamplePathMapper = PhpGapicCodePathMapper.newBuilder().setPrefix("samples").build();
PhpGapicSamplesTransformer phpGapicSamplesTransformer = new PhpGapicSamplesTransformer(phpSamplePathMapper);
CodeGenerator sampleGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(phpGapicSamplesTransformer).build();
generators.add(sampleGenerator);
}
}
if (artifactFlags.testGeneratorEnabled()) {
CodeGenerator testGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new PhpGapicSurfaceTestTransformer()).build();
generators.add(testGenerator);
}
} else if (language.equals(PYTHON)) {
if (artifactFlags.surfaceGeneratorEnabled()) {
GapicCodePathMapper pythonPathMapper = CommonGapicCodePathMapper.newBuilder().setShouldAppendPackage(true).build();
CodeGenerator mainGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new PythonRenderingUtil())).setModelToViewTransformer(new PythonGapicSurfaceTransformer(pythonPathMapper, packageConfig)).build();
CodeGenerator clientConfigGenerator = LegacyGapicGenerator.newBuilder().setModel(model).setContext(new ClientConfigGapicContext(model, productConfig)).setSnippetSetRunner(new ClientConfigSnippetSetRunner<>(SnippetSetRunner.SNIPPET_RESOURCE_ROOT)).setSnippetFileNames(Arrays.asList("clientconfig/python_clientconfig.snip")).setCodePathMapper(pythonPathMapper).build();
generators.add(mainGenerator);
generators.add(clientConfigGenerator);
if (artifactFlags.devSamplesEnabled()) {
GapicCodePathMapper pythonSamplePathMapper = new CommonSampleCodePathMapper();
PythonGapicSamplesTransformer pythonGapicSampleTransformer = new PythonGapicSamplesTransformer(pythonSamplePathMapper, packageConfig);
CodeGenerator sampleGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new PythonRenderingUtil())).setModelToViewTransformer(pythonGapicSampleTransformer).build();
generators.add(sampleGenerator);
}
CodeGenerator metadataGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new PythonRenderingUtil())).setModelToViewTransformer(new PythonPackageMetadataTransformer(packageConfig)).build();
generators.add(metadataGenerator);
}
if (artifactFlags.testGeneratorEnabled()) {
GapicCodePathMapper pythonTestPathMapper = CommonGapicCodePathMapper.newBuilder().setPrefix("test").setShouldAppendPackage(true).build();
CodeGenerator testGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new PythonGapicSurfaceTestTransformer(pythonTestPathMapper, packageConfig)).build();
generators.add(testGenerator);
}
} else if (language.equals(RUBY)) {
if (artifactFlags.surfaceGeneratorEnabled()) {
GapicCodePathMapper rubyPathMapper = CommonGapicCodePathMapper.newBuilder().setPrefix("lib").setShouldAppendPackage(true).setPackageFilePathNameFormatter(new RubyNameFormatter()).build();
CodeGenerator mainGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new RubyGapicSurfaceTransformer(rubyPathMapper, packageConfig)).build();
CodeGenerator clientConfigGenerator = LegacyGapicGenerator.newBuilder().setModel(model).setContext(new ClientConfigGapicContext(model, productConfig)).setSnippetSetRunner(new ClientConfigSnippetSetRunner<>(SnippetSetRunner.SNIPPET_RESOURCE_ROOT)).setSnippetFileNames(Arrays.asList("clientconfig/json.snip")).setCodePathMapper(rubyPathMapper).build();
CodeGenerator metadataGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new RubyPackageMetadataTransformer(packageConfig)).build();
if (artifactFlags.devSamplesEnabled()) {
GapicCodePathMapper rubySamplePathMapper = new CommonSampleCodePathMapper();
RubyGapicSamplesTransformer rubyGapicSamplesTransformer = new RubyGapicSamplesTransformer(rubySamplePathMapper);
CodeGenerator sampleGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(rubyGapicSamplesTransformer).build();
CodeGenerator sampleMetadataGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new RubySamplePackageMetadataTransformer(packageConfig)).build();
generators.add(sampleGenerator);
generators.add(sampleMetadataGenerator);
}
generators.add(mainGenerator);
generators.add(clientConfigGenerator);
generators.add(metadataGenerator);
CodeGenerator messageGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new RubyGapicSurfaceDocTransformer(rubyPathMapper, packageConfig)).build();
generators.add(messageGenerator);
}
if (artifactFlags.testGeneratorEnabled()) {
CommonGapicCodePathMapper.Builder rubyTestPathMapperBuilder = CommonGapicCodePathMapper.newBuilder().setShouldAppendPackage(true).setPackageFilePathNameFormatter(new RubyNameFormatter());
CodeGenerator testGenerator = GapicGenerator.newBuilder().setModel(model).setProductConfig(productConfig).setSnippetSetRunner(new CommonSnippetSetRunner(new CommonRenderingUtil())).setModelToViewTransformer(new RubyGapicSurfaceTestTransformer(rubyTestPathMapperBuilder.setPrefix("test").build(), rubyTestPathMapperBuilder.setPrefix("acceptance").build(), packageConfig)).build();
generators.add(testGenerator);
}
} else {
throw new UnsupportedOperationException("GapicGeneratorFactory: unsupported language \"" + language + "\"");
}
if (generators.isEmpty()) {
throw new IllegalArgumentException("No artifacts are enabled.");
}
return generators;
}
Aggregations