Skip to content

Commit e18f1cd

Browse files
committed
Add VertxGrpcGeneratorCommand for CLI argument parsing and enhance gRPC plugin flexibility
1 parent cfaf0af commit e18f1cd

5 files changed

Lines changed: 157 additions & 7 deletions

File tree

vertx-grpc-docs/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@
9595
<options>grpc-client=true,grpc-service=true,grpc-io=true</options>
9696
</jvmMavenPlugin>
9797
</jvmMavenPlugins>
98+
<!--jvmMavenPlugins>
99+
<jvmMavenPlugin>
100+
<groupId>io.vertx</groupId>
101+
<artifactId>vertx-grpc-protoc-plugin2</artifactId>
102+
<version>${project.version}</version>
103+
<mainClass>io.vertx.grpc.plugin.VertxGrpcGenerator</mainClass>
104+
<jvmArgs>
105+
<jvmArg>-grpc-client</jvmArg>
106+
<jvmArg>-grpc-service</jvmArg>
107+
<jvmArg>-grpc-io</jvmArg>
108+
</jvmArgs>
109+
</jvmMavenPlugin>
110+
</jvmMavenPlugins-->
98111
</configuration>
99112
<executions>
100113
<execution>

vertx-grpc-docs/src/main/asciidoc/plugin.adoc

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Using the definition above we need to compile it.
3838

3939
You can compile the proto file using the `protoc` compiler if you https://github.com/google/protobuf/tree/master/java#installation---without-maven[like], or you can integrate it in your build.
4040

41-
If you're using Apache Maven you need to add the plugin:
41+
If you're using Apache Maven you can choose from 2 plugins `io.github.ascopes` (Recommended) or `org.xolstice.maven.plugin`:
4242

4343
[source,xml]
4444
----
@@ -60,6 +60,12 @@ If you're using Apache Maven you need to add the plugin:
6060
<version>${vertx.version}</version>
6161
<mainClass>io.vertx.grpc.plugin.VertxGrpcGenerator</mainClass>
6262
<options>grpc-client=true,grpc-service=true,grpc-io=true</options>
63+
<!-- OR You can use jvmArgs -->
64+
<jvmArgs>
65+
<jvmArg>--grpc-client=true</jvmArg>
66+
<jvmArg>--grpc-service=true</jvmArg>
67+
<jvmArg>--grpc-io=true</jvmArg>
68+
</jvmArgs>
6369
</jvmMavenPlugin>
6470
</jvmMavenPlugins>
6571
</configuration>
@@ -72,6 +78,55 @@ This plugin uses `protocVersion` instead of platform-specific artifacts and take
7278

7379
For more details, see the https://ascopes.github.io/protobuf-maven-plugin/[ascopes protobuf-maven-plugin documentation].
7480

81+
[source,xml]
82+
----
83+
<plugin>
84+
<groupId>org.xolstice.maven.plugins</groupId>
85+
<artifactId>protobuf-maven-plugin</artifactId>
86+
<version>0.6.1</version>
87+
<configuration>
88+
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
89+
<pluginId>grpc-java</pluginId>
90+
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
91+
<protocPlugins>
92+
<protocPlugin>
93+
<id>vertx-grpc-protoc-plugin2</id>
94+
<groupId>io.vertx</groupId>
95+
<artifactId>vertx-grpc-protoc-plugin2</artifactId>
96+
<version>${stack.version}</version>
97+
<mainClass>io.vertx.grpc.plugin.VertxGrpcGenerator</mainClass>
98+
<args>
99+
<arg>--grpc-client=true</arg>
100+
<arg>--grpc-service=true</arg>
101+
<arg>--grpc-io=true</arg>
102+
</args>
103+
</protocPlugin>
104+
</protocPlugins>
105+
</configuration>
106+
<executions>
107+
<execution>
108+
<id>compile</id>
109+
<configuration>
110+
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
111+
<clearOutputDirectory>false</clearOutputDirectory>
112+
</configuration>
113+
<goals>
114+
<goal>compile</goal>
115+
<goal>compile-custom</goal>
116+
</goals>
117+
</execution>
118+
</executions>
119+
</plugin>
120+
----
121+
122+
NOTE: The xolstice is no longer supported as per https://github.com/xolstice/protobuf-maven-plugin/issues/121#issuecomment-2832430955. Users are encouraged to use ascopes plugin.
123+
124+
For more details, see the https://www.xolstice.org/protobuf-maven-plugin/[xolstice protobuf-maven-plugin documentation].
125+
126+
The `${os.detected.classifier}` property is used to make the build OS independent, on OSX it is replaced
127+
by _osx-x86_64_ and so on. To use it you need to add the os-maven-plugin[https://github.com/trustin/os-maven-plugin]
128+
in the `build` section of your `pom.xml`:
129+
75130
[source,xml]
76131
----
77132
<build>
@@ -104,12 +159,14 @@ By default, the plugin generates both client and service files. If you need only
104159

105160
=== Arguments:
106161

107-
- _grpc-client_: generate the client files
108-
- _grpc-service_: generate the service files
109-
- _grpc-io_: generate stub files compatible with `io.grpc:grpc-stub`, not generated by default. When using this option, you must also ensure the grpc-java plugin is configured (as shown in the Maven configuration above)
110-
- _grpc-transcoding_: whether to generate transcoding options for methods with HTTP annotations
111-
- _vertx-codegen_: whether to add Vert.x annotations to the generated classes (`@VertxGen`) By default, this is disabled
112-
- _service-prefix_: generate service classes with a prefix. For example, if you set it to `MyService`, the generated service class will be `MyServiceGreeterService` instead of `GreeterService`.
162+
- _[--]grpc-client_: generate the client files
163+
- _[--]grpc-service_: generate the service files
164+
- _[--]grpc-io_: generate stub files compatible with `io.grpc:grpc-stub`, not generated by default. When using this option, you must also ensure the grpc-java plugin is configured (as shown in the Maven configuration above)
165+
- _[--]grpc-transcoding_: whether to generate transcoding options for methods with HTTP annotations
166+
- _[--]vertx-codegen_: whether to add Vert.x annotations to the generated classes (`@VertxGen`) By default, this is disabled
167+
- _[--]service-prefix_: generate service classes with a prefix. For example, if you set it to `MyService`, the generated service class will be `MyServiceGreeterService` instead of `GreeterService`.
168+
169+
* [--] This means the argument can be prefixed with `--` when used as JVM arguments, but should be used without `--` when specified in the options tag. If possible, users should use plugin options as a more universal protoc plugin approach.
113170

114171
If no specific generation options are provided, both client and service files will be generated by default. By default, all extensions (currently only 'http') are supported.
115172

vertx-grpc-protoc-plugin2/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
<artifactId>compiler</artifactId>
4545
<version>0.9.14</version>
4646
</dependency>
47+
<dependency>
48+
<groupId>info.picocli</groupId>
49+
<artifactId>picocli</artifactId>
50+
<version>4.7.5</version>
51+
</dependency>
4752
</dependencies>
4853

4954
<build>

vertx-grpc-protoc-plugin2/src/main/java/io/vertx/grpc/plugin/VertxGrpcGenerator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.vertx.grpc.plugin.protoc.ProtocRequestConverter;
1313
import io.vertx.grpc.plugin.template.MustacheTemplateEngine;
1414
import io.vertx.grpc.plugin.template.TemplateEngine;
15+
import picocli.CommandLine;
1516

1617
import java.io.IOException;
1718
import java.util.ArrayList;
@@ -38,6 +39,18 @@ public static void main(String[] args) {
3839
// Read protoc request from stdin
3940
PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.parseFrom(ByteStreams.toByteArray(System.in), extensionRegistry);
4041

42+
// Override options if there are any args present
43+
if (args.length > 0) {
44+
if (!request.getParameter().isBlank()) {
45+
throw new IllegalArgumentException("Cannot specify both parameter and args");
46+
}
47+
48+
VertxGrpcGeneratorCommand command = new VertxGrpcGeneratorCommand();
49+
new CommandLine(command).execute(args);
50+
51+
request = request.toBuilder().setParameter(command.toString()).build();
52+
}
53+
4154
// Process the request
4255
PluginProtos.CodeGeneratorResponse response = processRequest(request);
4356

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.vertx.grpc.plugin;
2+
3+
import picocli.CommandLine;
4+
5+
import java.util.concurrent.Callable;
6+
7+
@CommandLine.Command(
8+
name = "vertx-grpc-generator",
9+
mixinStandardHelpOptions = true,
10+
version = "vertx-grpc-generator 1.0",
11+
description = "Generates Vert.x gRPC code from proto files."
12+
)
13+
public class VertxGrpcGeneratorCommand implements Callable<Integer> {
14+
15+
@CommandLine.Option(names = { "--grpc-client" }, description = "Generate gRPC client code")
16+
public boolean grpcClient = false;
17+
18+
@CommandLine.Option(names = { "--grpc-service" }, description = "Generate gRPC service code")
19+
public boolean grpcService = false;
20+
21+
@CommandLine.Option(names = { "--grpc-io" }, description = "Generate gRPC IO code")
22+
public boolean grpcIo = false;
23+
24+
@CommandLine.Option(names = { "--grpc-transcoding" }, description = "Whether to generate transcoding options for methods with HTTP annotations")
25+
public boolean grpcTranscoding = true;
26+
27+
@CommandLine.Option(names = { "--vertx-codegen" }, description = "Whether to generate vertx generator annotations")
28+
public boolean vertxCodegen = false;
29+
30+
@CommandLine.Option(
31+
names = { "--service-prefix" },
32+
description = "Generate service classes with a prefix. For example, if you set it to `MyService`, the generated service class will be `MyServiceGreeterService` instead of `GreeterService`."
33+
)
34+
public String servicePrefix = "";
35+
36+
@Override
37+
public Integer call() {
38+
if (!grpcClient && !grpcService && !grpcIo) {
39+
grpcClient = true;
40+
grpcService = true;
41+
}
42+
43+
return 0;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
StringBuilder sb = new StringBuilder();
49+
50+
sb.append("grpc-client=").append(grpcClient).append(", ");
51+
sb.append("grpc-service=").append(grpcService).append(", ");
52+
sb.append("grpc-io=").append(grpcIo).append(", ");
53+
sb.append("grpc-transcoding=").append(grpcTranscoding).append(", ");
54+
sb.append("vertx-codegen=").append(vertxCodegen).append(", ");
55+
56+
if (servicePrefix != null && !servicePrefix.isBlank()) {
57+
sb.append("service-prefix=").append(servicePrefix);
58+
}
59+
60+
return sb.toString();
61+
}
62+
}

0 commit comments

Comments
 (0)