Skip to content

Commit

Permalink
xolstice#5 Create args file in the build output directory
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-ivanov committed May 13, 2018
1 parent 40bde30 commit 54dee72
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ abstract class AbstractProtocMojo extends AbstractMojo {
)
private List<ArtifactRepository> remoteRepositories;

/**
* A directory where temporary files will be generated.
*
* @since 0.6.0
*/
@Parameter(
required = true,
readonly = true,
defaultValue = "${project.build.directory}"
)
private File tempDirectory;

/**
* A directory where native launchers for java protoc plugins will be generated.
*
Expand Down Expand Up @@ -674,6 +686,7 @@ protected void addProtocBuilderParameters(final Protoc.Builder protocBuilder) th
includeDependenciesInDescriptorSet,
includeSourceInfoInDescriptorSet);
}
protocBuilder.setTempDirectory(tempDirectory);
protocBuilder.useArgumentFile(useArgumentFile);
}

Expand Down
35 changes: 29 additions & 6 deletions src/main/java/org/xolstice/maven/plugin/protobuf/Protoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ final class Protoc {
private final StringStreamConsumer error;

/**
* A boolean indicating if the parameters to protoc should be passed in an argument file
* A directory where temporary files will be generated.
*/
private final File tempDirectory;

/**
* A boolean indicating if the parameters to protoc should be passed in an argument file.
*/
private final boolean useArgumentFile;

Expand All @@ -141,6 +146,7 @@ final class Protoc {
* @param nativePluginId a unique id of a native plugin.
* @param nativePluginExecutable path to the native plugin executable.
* @param nativePluginParameter an optional parameter for a native plugin.
* @param tempDirectory a directory where temporary files will be generated.
* @param useArgumentFile If {@code true}, parameters to protoc will be put in an argument file
*/
private Protoc(
Expand All @@ -160,6 +166,7 @@ private Protoc(
final String nativePluginId,
final String nativePluginExecutable,
final String nativePluginParameter,
final File tempDirectory,
final boolean useArgumentFile) {
this.executable = checkNotNull(executable, "executable");
this.protoPathElements = checkNotNull(protoPath, "protoPath");
Expand All @@ -177,6 +184,7 @@ private Protoc(
this.nativePluginId = nativePluginId;
this.nativePluginExecutable = nativePluginExecutable;
this.nativePluginParameter = nativePluginParameter;
this.tempDirectory = tempDirectory;
this.useArgumentFile = useArgumentFile;
this.error = new StringStreamConsumer();
this.output = new StringStreamConsumer();
Expand All @@ -185,8 +193,10 @@ private Protoc(
/**
* Invokes the {@code protoc} compiler using the configuration specified at construction.
*
* @param log logger instance.
* @return The exit status of {@code protoc}.
* @throws CommandLineException if command line environment cannot be set up.
* @throws InterruptedException if the execution was interrupted by the user.
*/
public int execute(final Log log) throws CommandLineException, InterruptedException {
final Commandline cl = new Commandline();
Expand Down Expand Up @@ -384,10 +394,10 @@ private static String fixUnicodeOutput(final String message) {
* @return the temporary file wth the arguments
* @throws IOException
*/
private static File createFileWithArguments(String[] args) throws IOException {
private File createFileWithArguments(String[] args) throws IOException {
PrintWriter writer = null;
try {
File tempFile = File.createTempFile("protoc", null);
final File tempFile = File.createTempFile("protoc", null, tempDirectory);
tempFile.deleteOnExit();

writer = new PrintWriter(tempFile, "UTF-8");
Expand Down Expand Up @@ -420,6 +430,8 @@ static final class Builder {

private final Set<ProtocPlugin> plugins;

private File tempDirectory;

private File pluginDirectory;

// TODO reorganise support for custom plugins
Expand Down Expand Up @@ -478,6 +490,13 @@ static final class Builder {
this.plugins = new LinkedHashSet<ProtocPlugin>();
}

public Builder setTempDirectory(final File directory) {
checkNotNull(directory);
checkArgument(directory.isDirectory(), "Temp directory " + directory + "does not exist");
tempDirectory = directory;
return this;
}

/**
* Sets the directory into which Java source files will be generated.
*
Expand Down Expand Up @@ -596,7 +615,7 @@ public Builder setPluginDirectory(final File directory) {
return this;
}

public void setNativePluginId(final String nativePluginId) {
public Builder setNativePluginId(final String nativePluginId) {
checkNotNull(nativePluginId, "'nativePluginId' is null");
checkArgument(!nativePluginId.isEmpty(), "'nativePluginId' is empty");
checkArgument(
Expand All @@ -607,17 +626,20 @@ public void setNativePluginId(final String nativePluginId) {
|| nativePluginId.equals("descriptor_set")),
"'nativePluginId' matches one of the built-in protoc plugins");
this.nativePluginId = nativePluginId;
return this;
}

public void setNativePluginExecutable(final String nativePluginExecutable) {
public Builder setNativePluginExecutable(final String nativePluginExecutable) {
checkNotNull(nativePluginExecutable, "'nativePluginExecutable' is null");
this.nativePluginExecutable = nativePluginExecutable;
return this;
}

public void setNativePluginParameter(final String nativePluginParameter) {
public Builder setNativePluginParameter(final String nativePluginParameter) {
checkNotNull(nativePluginParameter, "'nativePluginParameter' is null");
checkArgument(!nativePluginParameter.contains(":"), "'nativePluginParameter' contains illegal characters");
this.nativePluginParameter = nativePluginParameter;
return this;
}

public Builder withDescriptorSetFile(
Expand Down Expand Up @@ -736,6 +758,7 @@ public Protoc build() {
nativePluginId,
nativePluginExecutable,
nativePluginParameter,
tempDirectory,
useArgumentFile);
}
}
Expand Down

0 comments on commit 54dee72

Please sign in to comment.