Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
package com.salesforce.jprotoc;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
Expand All @@ -26,6 +24,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.*;

/**
* ProtocPlugin is the main entry point for running one or more java-base protoc plugins. This class handles
* I/O marshaling and error reporting.
Expand All @@ -40,7 +40,7 @@ private ProtocPlugin() {
* @param generator The generator to run.
*/
public static void generate(@Nonnull Generator generator) {
Preconditions.checkNotNull(generator, "generator");
checkNotNull(generator, "generator");
generate(Collections.singletonList(generator));
}

Expand All @@ -61,9 +61,9 @@ public static void generate(@Nonnull List<Generator> generators) {
*/
public static void generate(
@Nonnull List<Generator> generators, List<GeneratedExtension> extensions) {
Preconditions.checkNotNull(generators, "generators");
Preconditions.checkArgument(!generators.isEmpty(), "generators.isEmpty()");
Preconditions.checkNotNull(extensions, "extensions");
checkNotNull(generators, "generators");
checkArgument(!generators.isEmpty(), "generators.isEmpty()");
checkNotNull(extensions, "extensions");

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

Expand All @@ -127,10 +127,10 @@ public static void debug(
@Nonnull List<Generator> generators,
List<GeneratedExtension> extensions,
@Nonnull String dumpPath) {
Preconditions.checkNotNull(generators, "generators");
Preconditions.checkArgument(!generators.isEmpty(), "generators.isEmpty()");
Preconditions.checkNotNull(extensions, "extensions");
Preconditions.checkNotNull(dumpPath, "dumpPath");
checkNotNull(generators, "generators");
checkArgument(!generators.isEmpty(), "generators.isEmpty()");
checkNotNull(extensions, "extensions");
checkNotNull(dumpPath, "dumpPath");

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

// Write files if present
Joiner dotJoiner = Joiner.on('.').skipNulls();
for (PluginProtos.CodeGeneratorResponse.File file : response.getFileList()) {
String name = dotJoiner.join(file.getName(), file.getInsertionPoint());

File outFile = new File(name);
File outFile;
if (Strings.isNullOrEmpty(file.getInsertionPoint())) {
outFile = new File(file.getName());
} else {
// Append insertion point to file name
String name = Files.getNameWithoutExtension(file.getName()) +
"-" +
file.getInsertionPoint() +
Files.getFileExtension(file.getName());
outFile = new File(name);
}

Files.createParentDirs(outFile);
Files.write(file.getContent(), outFile, Charsets.UTF_8);
Files.write(file.getContentBytes().toByteArray(), outFile);
}
Expand All @@ -169,9 +178,9 @@ public static void debug(
private static PluginProtos.CodeGeneratorResponse generate(
@Nonnull List<Generator> generators,
@Nonnull PluginProtos.CodeGeneratorRequest request) {
Preconditions.checkNotNull(generators, "generators");
Preconditions.checkArgument(!generators.isEmpty(), "generators.isEmpty()");
Preconditions.checkNotNull(request, "request");
checkNotNull(generators, "generators");
checkArgument(!generators.isEmpty(), "generators.isEmpty()");
checkNotNull(request, "request");

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