Grpc plugin with multiple language targets


(Geoff Groos) #1

Hey everyone,

I’m wondering if anybody here has experience getting the grpc/protobuf plugins for gradle to generate code for more than just java.

I’ve tried this:

    protobuf {

        protoc {
            artifact = "com.google.protobuf:protoc:3.5.1-1"
        }
        plugins {
            grpc {
                artifact = 'io.grpc:protoc-gen-grpc-java:1.15.1'
            }
        }

        generateProtoTasks {
            all().each { task ->
                task.plugins {
                    grpc {
                        logger.warn("TODO: currently not generating grpc for csharp, because protoc plugins on gradle plugins are NFG.")
//                        def path = file("$projectDir/../.kinda-nuget/packages/grpc.tools/1.15.0/tools/windows_x64/grpc_csharp_plugin.exe").absolutePath
//                        option "protoc-gen-grpc=$path"
                    }
                }
                task.builtins {
                    java { }
                    csharp { }
                }
            }
        }
    }

Which generates protobuf and grpc code for java, but only protobuf code for csharp.

The grpc plugin is at version 0.7, so its probably a problem there.

What I was thinking I could do is drop into the command line and just execute command line strings that invoke the required protoc --generator grpc --lang csharp command line strings as specified in the grpc tutorials. The problem is my gradle foo: I’m just not sure on the syntax to create multiple tasks to accomplish this.

In this sense I suspect that the code would look something like

protobuf {

    protoc {
        artifact = "com.google.protobuf:protoc:3.5.1-1"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.15.1'
        }
    }

    generateProtoTasks {
        //use the regular configuration for java generation tasks
        javaTasks {
            plugins { grpc {} }
            builtins { java {} }
        }
        
        //define and add a task for Csharp using command line
        def csharpTask {
            exec "protoc.exe -I${ProtoSource} --csharp_out ${MyProjectName} --grpc_out ${MyProjectName} ${ProtoSource}/helloworld.proto --plugin=protoc-gen-grpc=${grpc_plugin_home}\grpc_csharp_plugin.exe"
        }
        
        tasks << csharpTask
    }
}

Many thanks!


(Geoff Groos) #2

I was able to get it working with a bit of fnangling: https://github.com/google/protobuf-gradle-plugin/issues/263

    protobuf {

        protoc {
            artifact = "com.google.protobuf:protoc:3.5.1-1"
        }
        plugins {
            grpc_java {
                artifact = 'io.grpc:protoc-gen-grpc-java:1.15.1'
            }
            grpc_csharp {
                path = file("$projectDir/../.kinda-nuget/packages/grpc.tools/1.15.0/tools/windows_x64/grpc_csharp_plugin.exe").absolutePath
            }
        }

        generateProtoTasks {
            all().each { task ->
                task.plugins {
                    grpc_java {}
                    grpc_csharp {}
                }
                task.builtins {
                    java { }
                    csharp {}
                }
            }
        }
    }

Now this has required me to check in the binary file for csharp generation into my repo, which makes it windows x64 specific, and also requires some megabytes. My next job is to push gradle to pull this nuget-published artificate dynamically like it does with maven artifacts.