Skip to content

Commit d19fe67

Browse files
stephenhschoenobates
authored andcommitted
Add plugin zip as an archive, fix lombok in compile (#2)
* Add pack as an artifact. * Move lombok to compileOnly. * Use the gradle-all distribute b/c IntelliJ wants to. * Add protobuf-java dependencies. * Remove lombok from runtime libraries as it was mostly unused. It was also futzing with my IntelliJ setup, although admittedly that was b/c I'd turned off annotation processors b/c the don't work in Gradle with JDK11. * Remove gson dependency. * Use approval files instead of assertions against the AST. * Use json snapshots instead of binary. * Add tests for the spring side. * Format as two space indent. * Convert ServiceGenerator to javapoet. Java Poet's DSL is slightly too "Googly" for my personal paste, e.g. builder all the things, but it's better than raw string interpolation, and more much widely used/maintained than my own version of the same thing. * Move context munging to a helper method. * Convert Undertow RpcGenerator to javapoet. * Convert Spring RpcGenerator to javapoet. * Remove Buffer/Indent that are used with JavaPoet. * Remove unused package-info.javas. * Move getServiceInterface to BaseGenerator. * Remove writeService methods. * Add a BaseServerGenerator. * Move Types out of the undertow package. * Minor cleanup. * Remove unused Strings class. * Change TypeMapper.get to return a ClassName. * Revert "Remove gson dependency." This reverts commit f3a56c8. * Have runtime/undertow pull in the plugin so it can codegen output. This will be used soon/next-commit to do actual "standup the server and make a few calls" tests.
1 parent cfd83ad commit d19fe67

File tree

65 files changed

+1761
-1208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1761
-1208
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Sat Sep 08 13:15:19 CDT 2018
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip

plugin/build.gradle

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ group 'com.flit'
1616

1717
sourceCompatibility = 1.8
1818

19-
if (!(project.hasProperty("release") && project.properties['release'] == "true")) {
20-
project.version += ".snapshot." + project.scm.tstamp
19+
configurations {
20+
zip
2121
}
2222

2323
shadowJar {
@@ -39,17 +39,18 @@ dependencies {
3939
compile 'commons-io:commons-io:2.5'
4040
compile 'com.google.protobuf:protobuf-java:3.5.1'
4141
compile 'com.google.protobuf:protobuf-java-util:3.5.1'
42+
compile 'com.squareup:javapoet:1.11.1'
4243

4344
compileOnly('org.projectlombok:lombok:+')
4445

4546
testCompile 'com.github.javaparser:javaparser-core:3.6.9'
4647
testCompile 'junit:junit:4.12'
48+
testCompile 'com.approvaltests:approvaltests:2.0.0'
4749
}
4850

49-
shadowJar.dependsOn build
51+
shadowJar.dependsOn compileJava
5052

5153
task pack(type: Zip) {
52-
5354
archiveName = "flit-plugin.zip"
5455
destinationDir = file("${buildDir}/package")
5556

@@ -62,4 +63,9 @@ task pack(type: Zip) {
6263
}
6364
}
6465

65-
pack.dependsOn shadowJar
66+
pack.dependsOn shadowJar
67+
68+
artifacts {
69+
zip pack
70+
}
71+

plugin/src/main/java/com/flit/protoc/Main.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,9 @@
88
*/
99
public class Main {
1010

11-
public static void main(String[] args) throws Exception {
12-
13-
Plugin plugin = new Plugin(
14-
PluginProtos.CodeGeneratorRequest
15-
.newBuilder()
16-
.mergeFrom(System.in)
17-
.build()
18-
);
19-
20-
plugin.process().writeTo(System.out);
21-
}
22-
11+
public static void main(String[] args) throws Exception {
12+
Plugin plugin = new Plugin(PluginProtos.CodeGeneratorRequest.newBuilder().mergeFrom(System.in).build());
13+
plugin.process().writeTo(System.out);
14+
}
2315

2416
}

plugin/src/main/java/com/flit/protoc/Parameter.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,30 @@
99
import java.util.function.Function;
1010
import java.util.stream.Collectors;
1111

12-
@Getter
13-
@ToString
14-
public class Parameter {
12+
@Getter @ToString public class Parameter {
1513

16-
public static final String PARAM_TARGET = "target";
17-
public static final String PARAM_CLIENT = "client";
18-
public static final String PARAM_TYPE = "type";
19-
public static final String PARAM_CONTEXT = "context";
14+
public static final String PARAM_TARGET = "target";
15+
public static final String PARAM_CLIENT = "client";
16+
public static final String PARAM_TYPE = "type";
17+
public static final String PARAM_CONTEXT = "context";
2018

21-
private String key;
22-
private String value;
19+
private String key;
20+
private String value;
2321

24-
public Parameter(String[] strings) {
25-
this.key = strings[0];
26-
this.value = strings[1];
27-
}
28-
29-
public static Map<String, Parameter> of(String value) {
30-
if (value == null || (value = value.trim()).isEmpty()) {
31-
throw new GeneratorException("Empty value passed to parameter builder");
32-
}
22+
public Parameter(String[] strings) {
23+
this.key = strings[0];
24+
this.value = strings[1];
25+
}
3326

34-
return Arrays.stream(value.split(","))
35-
.map(p -> p.split("=", 2))
36-
.filter(s -> s.length == 2 && !s[0].trim().isEmpty() && !s[1].trim().isEmpty())
37-
.map(Parameter::new)
38-
.collect(Collectors.toMap(Parameter::getKey, Function.identity()));
27+
public static Map<String, Parameter> of(String value) {
28+
if (value == null || (value = value.trim()).isEmpty()) {
29+
throw new GeneratorException("Empty value passed to parameter builder");
3930
}
31+
32+
return Arrays.stream(value.split(","))
33+
.map(p -> p.split("=", 2))
34+
.filter(s -> s.length == 2 && !s[0].trim().isEmpty() && !s[1].trim().isEmpty())
35+
.map(Parameter::new)
36+
.collect(Collectors.toMap(Parameter::getKey, Function.identity()));
37+
}
4038
}

plugin/src/main/java/com/flit/protoc/Plugin.java

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import com.flit.protoc.gen.GeneratorException;
55
import com.flit.protoc.gen.server.spring.SpringGenerator;
66
import com.flit.protoc.gen.server.undertow.UndertowGenerator;
7-
import com.google.protobuf.compiler.PluginProtos;
7+
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
8+
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
89

910
import java.util.Map;
1011

@@ -13,60 +14,47 @@
1314

1415
public class Plugin {
1516

16-
private final PluginProtos.CodeGeneratorRequest request;
17+
private final CodeGeneratorRequest request;
1718

18-
public Plugin(PluginProtos.CodeGeneratorRequest request) {
19-
this.request = request;
20-
}
21-
22-
public PluginProtos.CodeGeneratorResponse process() {
23-
if (!request.hasParameter()) {
24-
return PluginProtos.CodeGeneratorResponse
25-
.newBuilder()
26-
.setError("Usage: --flit_out=target=server,type=[spring|undertow]:<PATH>")
27-
.build();
28-
}
29-
30-
Map<String, Parameter> params = Parameter.of(request.getParameter());
19+
public Plugin(CodeGeneratorRequest request) {
20+
this.request = request;
21+
}
3122

32-
try {
33-
PluginProtos.CodeGeneratorResponse.Builder builder = PluginProtos.CodeGeneratorResponse.newBuilder();
34-
35-
resolveGenerator(params).generate(request, params).forEach(builder::addFile);
36-
37-
return builder.build();
38-
} catch (GeneratorException e) {
39-
return PluginProtos.CodeGeneratorResponse
40-
.newBuilder()
41-
.setError(e.getMessage())
42-
.build();
43-
}
23+
public CodeGeneratorResponse process() {
24+
if (!request.hasParameter()) {
25+
return CodeGeneratorResponse.newBuilder().setError("Usage: --flit_out=target=server,type=[spring|undertow]:<PATH>").build();
4426
}
4527

46-
private Generator resolveGenerator(Map<String, Parameter> params) {
47-
48-
if (!params.containsKey(PARAM_TARGET)) {
49-
throw new GeneratorException("No argument specified for target");
50-
}
51-
52-
if (!params.containsKey(PARAM_TYPE)) {
53-
throw new GeneratorException("No argument specified for type");
54-
}
28+
Map<String, Parameter> params = Parameter.of(request.getParameter());
29+
try {
30+
CodeGeneratorResponse.Builder builder = CodeGeneratorResponse.newBuilder();
31+
resolveGenerator(params).generate(request, params).forEach(builder::addFile);
32+
return builder.build();
33+
} catch (GeneratorException e) {
34+
return CodeGeneratorResponse.newBuilder().setError(e.getMessage()).build();
35+
}
36+
}
5537

56-
switch (params.get(PARAM_TARGET).getValue()) {
57-
case "server":
58-
switch (params.get(PARAM_TYPE).getValue()) {
59-
case "boot":
60-
case "spring":
61-
return new SpringGenerator();
62-
case "undertow":
63-
return new UndertowGenerator();
64-
default:
65-
throw new GeneratorException("Unknown server type: " + params.get(PARAM_TYPE).getValue());
66-
}
67-
default:
68-
throw new GeneratorException("Unknown target type: " + params.get(PARAM_TARGET).getValue());
38+
private Generator resolveGenerator(Map<String, Parameter> params) {
39+
if (!params.containsKey(PARAM_TARGET)) {
40+
throw new GeneratorException("No argument specified for target");
41+
}
42+
if (!params.containsKey(PARAM_TYPE)) {
43+
throw new GeneratorException("No argument specified for type");
44+
}
45+
switch (params.get(PARAM_TARGET).getValue()) {
46+
case "server":
47+
switch (params.get(PARAM_TYPE).getValue()) {
48+
case "boot":
49+
case "spring":
50+
return new SpringGenerator();
51+
case "undertow":
52+
return new UndertowGenerator();
53+
default:
54+
throw new GeneratorException("Unknown server type: " + params.get(PARAM_TYPE).getValue());
6955
}
70-
56+
default:
57+
throw new GeneratorException("Unknown target type: " + params.get(PARAM_TARGET).getValue());
7158
}
59+
}
7260
}

plugin/src/main/java/com/flit/protoc/gen/Buffer.java

Lines changed: 0 additions & 103 deletions
This file was deleted.
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.flit.protoc.gen;
22

33
import com.flit.protoc.Parameter;
4-
import com.google.protobuf.compiler.PluginProtos;
4+
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
5+
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
56

67
import java.util.List;
78
import java.util.Map;
@@ -11,14 +12,14 @@
1112
*/
1213
public interface Generator {
1314

14-
/**
15-
* This is the main entry point to code generation: the implementation is decided in the {@link com.flit.protoc.Plugin}
16-
* class from the given parameters.
17-
*
18-
* @param request The inbound protoc request
19-
* @param params The plugin parameters
20-
*
21-
* @return The list of files to be added to the output.
22-
*/
23-
List<PluginProtos.CodeGeneratorResponse.File> generate(PluginProtos.CodeGeneratorRequest request, Map<String, Parameter> params);
15+
/**
16+
* This is the main entry point to code generation: the implementation is decided in the {@link com.flit.protoc.Plugin}
17+
* class from the given parameters.
18+
*
19+
* @param request The inbound protoc request
20+
* @param params The plugin parameters
21+
*
22+
* @return The list of files to be added to the output.
23+
*/
24+
List<CodeGeneratorResponse.File> generate(CodeGeneratorRequest request, Map<String, Parameter> params);
2425
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.flit.protoc.gen;
22

33
public class GeneratorException extends RuntimeException {
4-
public GeneratorException(String message) {
5-
super(message);
6-
}
4+
public GeneratorException(String message) {
5+
super(message);
6+
}
77
}

0 commit comments

Comments
 (0)