Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support to build protos in one directory in one call. #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 33 additions & 19 deletions src/main/java/com/github/os72/protocjar/maven/ProtocJarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,25 @@ private void processTarget(OutputTarget target) throws MojoExecutionException {

if (input.exists() && input.isDirectory()) {
Collection<File> protoFiles = FileUtils.listFiles(input, fileFilter, TrueFileFilter.INSTANCE);
for (File protoFile : protoFiles) {
if (target.cleanOutputFolder || buildContext.hasDelta(protoFile.getPath())) {
processFile(protoFile, protocVersion, targetType, target.pluginPath, target.outputDirectory, target.outputOptions);
}
else {
getLog().info("Not changed " + protoFile);

boolean changed = target.cleanOutputFolder;

if (!changed) {
for (File protoFile : protoFiles) {
if (buildContext.hasDelta(protoFile.getPath())) {
changed = true;
break;
}
}
}

if (changed) {
processFile(input, protoFiles, protocVersion, targetType, target.pluginPath,
target.outputDirectory, target.outputOptions);
}
else {
getLog().info("Not changed " + input);
}
}
else {
if (input.exists()) getLog().warn(input + " is not a directory");
Expand Down Expand Up @@ -660,18 +671,18 @@ private void addGeneratedSources(OutputTarget target) throws MojoExecutionExcept
}
}

private void processFile(File file, String version, String type, String pluginPath, File outputDir, String outputOptions) throws MojoExecutionException {
getLog().info(" Processing ("+ type + "): " + file.getName());
private void processFile(File inputDir, Collection<File> files, String version, String type, String pluginPath, File outputDir, String outputOptions) throws MojoExecutionException {
getLog().info(" Processing ("+ type + "): " + inputDir);

try {
buildContext.removeMessages(file);
buildContext.removeMessages(inputDir);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
TeeOutputStream outTee = new TeeOutputStream(System.out, out);
TeeOutputStream errTee = new TeeOutputStream(System.err, err);

int ret = 0;
Collection<String> cmd = buildCommand(file, version, type, pluginPath, outputDir, outputOptions);
Collection<String> cmd = buildCommand(inputDir, files, version, type, pluginPath, outputDir, outputOptions);
if (protocCommand == null) ret = Protoc.runProtoc(cmd.toArray(new String[0]), outTee, errTee);
else ret = Protoc.runProtoc(protocCommand, Arrays.asList(cmd.toArray(new String[0])), outTee, errTee);

Expand All @@ -684,7 +695,7 @@ private void processFile(File file, String version, String type, String pluginPa
int lineNum = 0;
int colNum = 0;
String msg = line;
if (line.contains(file.getName())) {
if (line.contains(inputDir.getName())) {
String[] parts = line.split(":", 4);
if (parts.length == 4) {
try {
Expand All @@ -693,30 +704,30 @@ private void processFile(File file, String version, String type, String pluginPa
msg = parts[3];
}
catch (Exception e) {
getLog().warn("Failed to parse protoc warning/error for " + file);
getLog().warn("Failed to parse protoc warning/error for " + inputDir);
}
}
}
buildContext.addMessage(file, lineNum, colNum, msg, severity, null);
buildContext.addMessage(inputDir, lineNum, colNum, msg, severity, null);
}
}

if (ret != 0) throw new MojoExecutionException("protoc-jar failed for " + file + ". Exit code " + ret);
if (ret != 0) throw new MojoExecutionException("protoc-jar failed for " + inputDir + ". Exit code " + ret);
}
catch (InterruptedException e) {
throw new MojoExecutionException("Interrupted", e);
}
catch (IOException e) {
throw new MojoExecutionException("Unable to execute protoc-jar for " + file, e);
throw new MojoExecutionException("Unable to execute protoc-jar for " + inputDir, e);
}
}

private Collection<String> buildCommand(File file, String version, String type, String pluginPath, File outputDir, String outputOptions) throws MojoExecutionException {
private Collection<String> buildCommand(File inputDir, Collection<File> files, String version, String type, String pluginPath, File outputDir, String outputOptions) throws MojoExecutionException {
Collection<String> cmd = new ArrayList<String>();
populateIncludes(cmd);
cmd.add("-I" + file.getParentFile().getAbsolutePath());
cmd.add("-I" + inputDir);
if ("descriptor".equals(type)) {
File outFile = new File(outputDir, file.getName());
File outFile = new File(outputDir, inputDir.getName());
cmd.add("--descriptor_set_out=" + FilenameUtils.removeExtension(outFile.toString()) + ".desc");
if (includeImports) {
cmd.add("--include_imports");
Expand All @@ -738,7 +749,10 @@ private Collection<String> buildCommand(File file, String version, String type,
cmd.add("--plugin=protoc-gen-" + type + "=" + pluginPath);
}
}
cmd.add(file.toString());

for (File file : files) {
cmd.add(file.toString());
}
if (version != null) cmd.add("-v" + version);
return cmd;
}
Expand Down