Skip to content

Commit

Permalink
Merge pull request #113 from ascopes/task/log-version
Browse files Browse the repository at this point in the history
Log the protoc version info during builds
  • Loading branch information
ascopes authored Mar 8, 2024
2 parents b453d4c + 63bf984 commit fce4aff
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public ArgLineBuilder plugins(Collection<ResolvedPlugin> plugins, Path outputPat
return this;
}

public List<String> version() {
return List.of(args.get(0), "--version");
}

private ArgLineBuilder langOut(String type, Path outputPath, boolean lite) {
++outputTargetCount;
var flag = lite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.slf4j.LoggerFactory;

/**
* Executor for commands.
* Executor for {@code protoc} commands.
*
* @author Ashley Scopes
*/
Expand All @@ -36,30 +36,18 @@ public final class CommandLineExecutor {

@Inject
public CommandLineExecutor() {
// Static-only class.
// Nothing to do here.
}

public boolean execute(List<String> args) throws IOException {
public boolean execute(boolean silent, List<String> args) throws IOException {
log.info("Calling protoc with the following command line: {}", Shlex.quoteShellArgs(args));

var procBuilder = new ProcessBuilder(args);
procBuilder.environment().putAll(System.getenv());
procBuilder.inheritIO();

try {
var startTimeNs = System.nanoTime();
var proc = procBuilder.start();
var exitCode = proc.waitFor();
var elapsedTimeMs = ms(System.nanoTime() - startTimeNs);

if (exitCode == 0) {
log.info("Protoc completed after {}ms", elapsedTimeMs);
return true;
} else {
log.error("Protoc returned exit code {} after {}ms", exitCode, elapsedTimeMs);
return false;
}

return run(procBuilder);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
var newEx = new InterruptedIOException("Compilation was interrupted");
Expand All @@ -68,7 +56,18 @@ public boolean execute(List<String> args) throws IOException {
}
}

private static long ms(long ns) {
return ns / 1_000_000L;
private boolean run(ProcessBuilder procBuilder) throws InterruptedException, IOException {
var startTimeNs = System.nanoTime();
var proc = procBuilder.start();
var exitCode = proc.waitFor();
var elapsedTimeMs = (System.nanoTime() - startTimeNs) / 1_000_000L;

if (exitCode == 0) {
log.info("Protoc returned exit code 0 (success) after {}ms", elapsedTimeMs);
return true;
} else {
log.error("Protoc returned exit code {} (error) after {}ms", exitCode, elapsedTimeMs);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public SourceCodeGenerator(

public boolean generate(GenerationRequest request) throws ResolutionException, IOException {
var protocPath = discoverProtocPath(request);

var plugins = discoverPlugins(request);
var importPaths = discoverImportPaths(request);
var sourcePaths = discoverCompilableSources(request);
Expand Down Expand Up @@ -114,13 +115,23 @@ public boolean generate(GenerationRequest request) throws ResolutionException, I
.flatMap(Collection::stream)
.collect(Collectors.toCollection(LinkedHashSet::new));

return commandLineExecutor.execute(argLineBuilder.compile(sourceFiles));
if (!logProtocVersion(protocPath)) {
log.error("Unable to execute protoc. Ensure the binary is compatible for this platform!");
return false;
}

return commandLineExecutor.execute(false, argLineBuilder.compile(sourceFiles));
}

private Path discoverProtocPath(GenerationRequest request) throws ResolutionException {
return protocResolver.resolve(request.getMavenSession(), request.getProtocVersion());
}

private boolean logProtocVersion(Path protocPath) throws IOException {
var args = new ArgLineBuilder(protocPath).version();
return commandLineExecutor.execute(true, args);
}

private Collection<ResolvedPlugin> discoverPlugins(
GenerationRequest request
) throws IOException, ResolutionException {
Expand Down

0 comments on commit fce4aff

Please sign in to comment.