Skip to content

Commit 786df31

Browse files
committed
Fix how jprotoc handles debug output with subdirectories
1 parent 54d3bea commit 786df31

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

jprotoc/jprotoc/src/main/java/com/salesforce/jprotoc/ProtocPlugin.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
package com.salesforce.jprotoc;
99

1010
import com.google.common.base.Charsets;
11-
import com.google.common.base.Joiner;
12-
import com.google.common.base.Preconditions;
1311
import com.google.common.base.Strings;
1412
import com.google.common.io.ByteStreams;
1513
import com.google.common.io.Files;
@@ -26,6 +24,8 @@
2624
import java.util.stream.Collectors;
2725
import java.util.stream.Stream;
2826

27+
import static com.google.common.base.Preconditions.*;
28+
2929
/**
3030
* ProtocPlugin is the main entry point for running one or more java-base protoc plugins. This class handles
3131
* I/O marshaling and error reporting.
@@ -40,7 +40,7 @@ private ProtocPlugin() {
4040
* @param generator The generator to run.
4141
*/
4242
public static void generate(@Nonnull Generator generator) {
43-
Preconditions.checkNotNull(generator, "generator");
43+
checkNotNull(generator, "generator");
4444
generate(Collections.singletonList(generator));
4545
}
4646

@@ -61,9 +61,9 @@ public static void generate(@Nonnull List<Generator> generators) {
6161
*/
6262
public static void generate(
6363
@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");
6767

6868
// As per https://developers.google.com/protocol-buffers/docs/reference/java-generated#extension,
6969
// extensions must be registered in order to be processed.
@@ -102,7 +102,7 @@ public static void generate(
102102
* @param dumpPath The path to a descriptor dump on the filesystem.
103103
*/
104104
public static void debug(@Nonnull Generator generator, @Nonnull String dumpPath) {
105-
Preconditions.checkNotNull(generator, "generator");
105+
checkNotNull(generator, "generator");
106106
debug(Collections.singletonList(generator), dumpPath);
107107
}
108108

@@ -127,10 +127,10 @@ public static void debug(
127127
@Nonnull List<Generator> generators,
128128
List<GeneratedExtension> extensions,
129129
@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");
134134

135135
// As per https://developers.google.com/protocol-buffers/docs/reference/java-generated#extension,
136136
// extensions must be registered in order to be processed.
@@ -152,11 +152,20 @@ public static void debug(
152152
}
153153

154154
// Write files if present
155-
Joiner dotJoiner = Joiner.on('.').skipNulls();
156155
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);
160169
Files.write(file.getContent(), outFile, Charsets.UTF_8);
161170
Files.write(file.getContentBytes().toByteArray(), outFile);
162171
}
@@ -169,9 +178,9 @@ public static void debug(
169178
private static PluginProtos.CodeGeneratorResponse generate(
170179
@Nonnull List<Generator> generators,
171180
@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");
175184

176185
// Run each file generator, collecting the output
177186
Stream<PluginProtos.CodeGeneratorResponse.File> oldWay = generators

0 commit comments

Comments
 (0)