Search in sources :

Example 1 with GapicGeneratorApp

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);
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) FileGapicWriter(com.google.api.codegen.gapic.FileGapicWriter) Options(org.apache.commons.cli.Options) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) CommandLine(org.apache.commons.cli.CommandLine) GapicGeneratorApp(com.google.api.codegen.gapic.GapicGeneratorApp) DiscoGapicGeneratorApp(com.google.api.codegen.discogapic.DiscoGapicGeneratorApp) Option(org.apache.commons.cli.Option) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 2 with GapicGeneratorApp

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();
    }
}
Also used : StringWriter(java.io.StringWriter) GapicGeneratorApp(com.google.api.codegen.gapic.GapicGeneratorApp) CodeGeneratorResponse(com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) ProtocGapicWriter(com.google.api.codegen.gapic.ProtocGapicWriter) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

GapicGeneratorApp (com.google.api.codegen.gapic.GapicGeneratorApp)2 ToolOptions (com.google.api.tools.framework.tools.ToolOptions)2 DiscoGapicGeneratorApp (com.google.api.codegen.discogapic.DiscoGapicGeneratorApp)1 FileGapicWriter (com.google.api.codegen.gapic.FileGapicWriter)1 ProtocGapicWriter (com.google.api.codegen.gapic.ProtocGapicWriter)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CodeGeneratorResponse (com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 CommandLine (org.apache.commons.cli.CommandLine)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1 Option (org.apache.commons.cli.Option)1 Options (org.apache.commons.cli.Options)1