use of com.google.api.codegen.gapic.GapicGeneratorApp in project toolkit by googleapis.
the class GeneratorMain method gapicGeneratorMain.
public static void gapicGeneratorMain(ArtifactType artifactType, String[] args) throws Exception {
Options options = new Options();
options.addOption("h", "help", false, "show usage");
options.addOption(DESCRIPTOR_SET_OPTION);
options.addOption(SERVICE_YAML_NONREQUIRED_OPTION);
// TODO make required after artman passes this in
options.addOption(LANGUAGE_NONREQUIRED_OPTION);
options.addOption(GAPIC_YAML_NONREQUIRED_OPTION);
options.addOption(PACKAGE_YAML2_OPTION);
options.addOption(TARGET_API_PROTO_PACKAGE);
options.addOption(OUTPUT_OPTION);
options.addOption(SAMPLE_YAML_NONREQUIRED_OPTION);
options.addOption(GRPC_SERVICE_CONFIG_OPTION);
options.addOption(TRANSPORT);
Option enabledArtifactsOption = Option.builder().longOpt("enabled_artifacts").desc("Optional. Artifacts enabled for the generator. " + "Currently supports 'surface' and 'test'.").hasArg().argName("ENABLED_ARTIFACTS").required(false).build();
options.addOption(enabledArtifactsOption);
Option devSamplesOption = Option.builder().longOpt("dev_samples").desc("Whether to generate samples in non-production-ready languages.").argName("DEV_SAMPLES").required(false).build();
options.addOption(devSamplesOption);
CommandLine cl = (new DefaultParser()).parse(options, args);
if (cl.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("GapicGeneratorTool", options);
}
ToolOptions toolOptions = ToolOptions.create();
toolOptions.set(ToolOptions.DESCRIPTOR_SET, cl.getOptionValue(DESCRIPTOR_SET_OPTION.getLongOpt()));
// TODO(andrealin): Write system tests to ensure at least one option given.
checkAtLeastOneOption(cl, SERVICE_YAML_NONREQUIRED_OPTION, TARGET_API_PROTO_PACKAGE);
checkAtLeastOneOption(cl, GAPIC_YAML_NONREQUIRED_OPTION, TARGET_API_PROTO_PACKAGE);
toolOptions.set(GapicGeneratorApp.PROTO_PACKAGE, cl.getOptionValue(TARGET_API_PROTO_PACKAGE.getLongOpt()));
toolOptions.set(GapicGeneratorApp.LANGUAGE, cl.getOptionValue(LANGUAGE_NONREQUIRED_OPTION.getLongOpt()));
toolOptions.set(GapicGeneratorApp.OUTPUT_FILE, cl.getOptionValue(OUTPUT_OPTION.getLongOpt(), ""));
toolOptions.set(GapicGeneratorApp.PACKAGE_CONFIG2_FILE, cl.getOptionValue(PACKAGE_YAML2_OPTION.getLongOpt()));
toolOptions.set(GapicGeneratorApp.GRPC_SERVICE_CONFIG, cl.getOptionValue(GRPC_SERVICE_CONFIG_OPTION.getLongOpt()));
checkFile(toolOptions.get(ToolOptions.DESCRIPTOR_SET));
if (cl.getOptionValue(TRANSPORT.getLongOpt()) != null) {
toolOptions.set(GapicGeneratorApp.TRANSPORT, cl.getOptionValue(TRANSPORT.getLongOpt()));
}
if (cl.getOptionValues(SERVICE_YAML_NONREQUIRED_OPTION.getLongOpt()) != null) {
toolOptions.set(ToolOptions.CONFIG_FILES, Lists.newArrayList(cl.getOptionValues(SERVICE_YAML_NONREQUIRED_OPTION.getLongOpt())));
checkFiles(toolOptions.get(ToolOptions.CONFIG_FILES));
}
if (cl.getOptionValues(GAPIC_YAML_NONREQUIRED_OPTION.getLongOpt()) != null) {
toolOptions.set(GapicGeneratorApp.GENERATOR_CONFIG_FILES, Lists.newArrayList(cl.getOptionValues(GAPIC_YAML_NONREQUIRED_OPTION.getLongOpt())));
checkFiles(toolOptions.get(GapicGeneratorApp.GENERATOR_CONFIG_FILES));
}
if (cl.getOptionValues(SAMPLE_YAML_NONREQUIRED_OPTION.getLongOpt()) != null) {
toolOptions.set(GapicGeneratorApp.SAMPLE_CONFIG_FILES, Lists.newArrayList(cl.getOptionValues(SAMPLE_YAML_NONREQUIRED_OPTION.getLongOpt())));
checkFiles(toolOptions.get(GapicGeneratorApp.SAMPLE_CONFIG_FILES));
}
if (!Strings.isNullOrEmpty(toolOptions.get(GapicGeneratorApp.PACKAGE_CONFIG2_FILE))) {
checkFile(toolOptions.get(GapicGeneratorApp.PACKAGE_CONFIG2_FILE));
}
if (cl.getOptionValues(enabledArtifactsOption.getLongOpt()) != null) {
toolOptions.set(GapicGeneratorApp.ENABLED_ARTIFACTS, Lists.newArrayList(cl.getOptionValues(enabledArtifactsOption.getLongOpt())));
}
if (!Strings.isNullOrEmpty(toolOptions.get(GapicGeneratorApp.GRPC_SERVICE_CONFIG))) {
checkFile(toolOptions.get(GapicGeneratorApp.GRPC_SERVICE_CONFIG));
}
toolOptions.set(GapicGeneratorApp.DEV_SAMPLES, cl.hasOption(devSamplesOption.getLongOpt()));
String outputPath = toolOptions.get(GapicGeneratorApp.OUTPUT_FILE);
GapicGeneratorApp codeGen = new GapicGeneratorApp(toolOptions, artifactType, new FileGapicWriter(outputPath));
int exitCode = codeGen.run();
System.exit(exitCode);
}
use of com.google.api.codegen.gapic.GapicGeneratorApp in project toolkit by googleapis.
the class ProtocGeneratorMain method generate.
@VisibleForTesting
public static // CodeGeneratorResponse.
CodeGeneratorResponse generate(CodeGeneratorRequest request) {
try {
ToolOptions toolOptions = parseOptions(request);
ProtocGapicWriter gapicWriter = new ProtocGapicWriter();
GapicGeneratorApp codeGen = new GapicGeneratorApp(toolOptions, DEFAULT_ARTIFACT_TYPE, gapicWriter);
codeGen.run();
CodeGeneratorResponse response = gapicWriter.getCodegenResponse();
if (response == null) {
throw new RuntimeException(collectDiags(codeGen));
}
return response;
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
return PluginProtos.CodeGeneratorResponse.newBuilder().setError(sw.toString()).build();
}
}
Aggregations