Search in sources :

Example 1 with CodeGeneratorResponse

use of com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse in project toolkit by googleapis.

the class ProtocGeneratorMain method main.

public static void main(String[] args) {
    CodeGeneratorResponse response;
    CodeGeneratorRequest request;
    int exitCode = 0;
    try {
        request = PluginProtos.CodeGeneratorRequest.parseFrom(System.in);
    } catch (IOException e) {
        response = PluginProtos.CodeGeneratorResponse.newBuilder().setError(e.toString()).build();
        try {
            response.writeTo(System.out);
        } catch (IOException e2) {
            System.err.println("Unable to parse CodeGeneraterRequest from stdin.");
            e2.printStackTrace(System.err);
        }
        System.exit(1);
        return;
    }
    try {
        response = generate(request);
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        response = PluginProtos.CodeGeneratorResponse.newBuilder().setError(sw.toString()).build();
        exitCode = 1;
    }
    try {
        response.writeTo(System.out);
    } catch (IOException e) {
        System.err.println("Failed to write out CodeGeneratorResponse.");
        e.printStackTrace(System.err);
        System.exit(1);
    }
    System.out.flush();
    System.exit(exitCode);
}
Also used : StringWriter(java.io.StringWriter) CodeGeneratorResponse(com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse) IOException(java.io.IOException) CodeGeneratorRequest(com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 2 with CodeGeneratorResponse

use of com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse in project toolkit by googleapis.

the class ProtocGapicPluginGeneratorTest method testFailingGenerator.

@Test
public void testFailingGenerator() {
    CodeGeneratorRequest codeGeneratorRequest = CodeGeneratorRequest.newBuilder().addAllProtoFile(model.getFiles().stream().map(ProtoFile::getProto).collect(Collectors.toList())).addFileToGenerate("fuuuuudge.proto").setParameter("transport=rest").build();
    CodeGeneratorResponse response = ProtocGeneratorMain.generate(codeGeneratorRequest);
    Truth.assertThat(response).isNotNull();
    Truth.assertThat(response.getError()).isNotEmpty();
}
Also used : CodeGeneratorResponse(com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse) CodeGeneratorRequest(com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest) GapicCodeGeneratorAnnotationsTest(com.google.api.codegen.protoannotations.GapicCodeGeneratorAnnotationsTest) Test(org.junit.Test)

Example 3 with CodeGeneratorResponse

use of com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse in project toolkit by googleapis.

the class ProtocGapicPluginGeneratorTest method testGenerator.

@Test
public void testGenerator() {
    CodeGeneratorRequest codeGeneratorRequest = CodeGeneratorRequest.newBuilder().addAllProtoFile(model.getFiles().stream().map(ProtoFile::getProto).collect(Collectors.toList())).addFileToGenerate("multiple_services.proto").setParameter("language=java,transport=grpc").build();
    CodeGeneratorResponse response = ProtocGeneratorMain.generate(codeGeneratorRequest);
    // TODO(andrealin): Look into setting these up as baseline files.
    Truth.assertThat(response).isNotNull();
    Truth.assertThat(response.getError()).isEmpty();
    Truth.assertThat(response.getFileCount()).isEqualTo(15);
    Truth.assertThat(response.getFile(0).getContent()).contains("DecrementerServiceClient");
}
Also used : CodeGeneratorResponse(com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse) CodeGeneratorRequest(com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest) GapicCodeGeneratorAnnotationsTest(com.google.api.codegen.protoannotations.GapicCodeGeneratorAnnotationsTest) Test(org.junit.Test)

Example 4 with CodeGeneratorResponse

use of com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse 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

CodeGeneratorResponse (com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse)4 CodeGeneratorRequest (com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest)3 GapicCodeGeneratorAnnotationsTest (com.google.api.codegen.protoannotations.GapicCodeGeneratorAnnotationsTest)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Test (org.junit.Test)2 GapicGeneratorApp (com.google.api.codegen.gapic.GapicGeneratorApp)1 ProtocGapicWriter (com.google.api.codegen.gapic.ProtocGapicWriter)1 ToolOptions (com.google.api.tools.framework.tools.ToolOptions)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1