8
8
package com .salesforce .jprotoc ;
9
9
10
10
import com .google .common .base .Charsets ;
11
- import com .google .common .base .Joiner ;
12
- import com .google .common .base .Preconditions ;
13
11
import com .google .common .base .Strings ;
14
12
import com .google .common .io .ByteStreams ;
15
13
import com .google .common .io .Files ;
26
24
import java .util .stream .Collectors ;
27
25
import java .util .stream .Stream ;
28
26
27
+ import static com .google .common .base .Preconditions .*;
28
+
29
29
/**
30
30
* ProtocPlugin is the main entry point for running one or more java-base protoc plugins. This class handles
31
31
* I/O marshaling and error reporting.
@@ -40,7 +40,7 @@ private ProtocPlugin() {
40
40
* @param generator The generator to run.
41
41
*/
42
42
public static void generate (@ Nonnull Generator generator ) {
43
- Preconditions . checkNotNull (generator , "generator" );
43
+ checkNotNull (generator , "generator" );
44
44
generate (Collections .singletonList (generator ));
45
45
}
46
46
@@ -61,9 +61,9 @@ public static void generate(@Nonnull List<Generator> generators) {
61
61
*/
62
62
public static void generate (
63
63
@ Nonnull List <Generator > generators , List <GeneratedExtension > extensions ) {
64
- Preconditions . checkNotNull (generators , "generators" );
65
- Preconditions . checkArgument (!generators .isEmpty (), "generators.isEmpty()" );
66
- Preconditions . checkNotNull (extensions , "extensions" );
64
+ checkNotNull (generators , "generators" );
65
+ checkArgument (!generators .isEmpty (), "generators.isEmpty()" );
66
+ checkNotNull (extensions , "extensions" );
67
67
68
68
// As per https://developers.google.com/protocol-buffers/docs/reference/java-generated#extension,
69
69
// extensions must be registered in order to be processed.
@@ -102,7 +102,7 @@ public static void generate(
102
102
* @param dumpPath The path to a descriptor dump on the filesystem.
103
103
*/
104
104
public static void debug (@ Nonnull Generator generator , @ Nonnull String dumpPath ) {
105
- Preconditions . checkNotNull (generator , "generator" );
105
+ checkNotNull (generator , "generator" );
106
106
debug (Collections .singletonList (generator ), dumpPath );
107
107
}
108
108
@@ -127,10 +127,10 @@ public static void debug(
127
127
@ Nonnull List <Generator > generators ,
128
128
List <GeneratedExtension > extensions ,
129
129
@ Nonnull String dumpPath ) {
130
- Preconditions . checkNotNull (generators , "generators" );
131
- Preconditions . checkArgument (!generators .isEmpty (), "generators.isEmpty()" );
132
- Preconditions . checkNotNull (extensions , "extensions" );
133
- Preconditions . checkNotNull (dumpPath , "dumpPath" );
130
+ checkNotNull (generators , "generators" );
131
+ checkArgument (!generators .isEmpty (), "generators.isEmpty()" );
132
+ checkNotNull (extensions , "extensions" );
133
+ checkNotNull (dumpPath , "dumpPath" );
134
134
135
135
// As per https://developers.google.com/protocol-buffers/docs/reference/java-generated#extension,
136
136
// extensions must be registered in order to be processed.
@@ -152,11 +152,20 @@ public static void debug(
152
152
}
153
153
154
154
// Write files if present
155
- Joiner dotJoiner = Joiner .on ('.' ).skipNulls ();
156
155
for (PluginProtos .CodeGeneratorResponse .File file : response .getFileList ()) {
157
- String name = dotJoiner .join (file .getName (), file .getInsertionPoint ());
158
-
159
- File outFile = new File (name );
156
+ File outFile ;
157
+ if (Strings .isNullOrEmpty (file .getInsertionPoint ())) {
158
+ outFile = new File (file .getName ());
159
+ } else {
160
+ // Append insertion point to file name
161
+ String name = Files .getNameWithoutExtension (file .getName ()) +
162
+ "-" +
163
+ file .getInsertionPoint () +
164
+ Files .getFileExtension (file .getName ());
165
+ outFile = new File (name );
166
+ }
167
+
168
+ Files .createParentDirs (outFile );
160
169
Files .write (file .getContent (), outFile , Charsets .UTF_8 );
161
170
Files .write (file .getContentBytes ().toByteArray (), outFile );
162
171
}
@@ -169,9 +178,9 @@ public static void debug(
169
178
private static PluginProtos .CodeGeneratorResponse generate (
170
179
@ Nonnull List <Generator > generators ,
171
180
@ Nonnull PluginProtos .CodeGeneratorRequest request ) {
172
- Preconditions . checkNotNull (generators , "generators" );
173
- Preconditions . checkArgument (!generators .isEmpty (), "generators.isEmpty()" );
174
- Preconditions . checkNotNull (request , "request" );
181
+ checkNotNull (generators , "generators" );
182
+ checkArgument (!generators .isEmpty (), "generators.isEmpty()" );
183
+ checkNotNull (request , "request" );
175
184
176
185
// Run each file generator, collecting the output
177
186
Stream <PluginProtos .CodeGeneratorResponse .File > oldWay = generators
0 commit comments