diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/execute/ArgLineBuilder.java b/src/main/java/io/github/ascopes/protobufmavenplugin/execute/ArgLineBuilder.java index 65c107f8..9184b9fe 100644 --- a/src/main/java/io/github/ascopes/protobufmavenplugin/execute/ArgLineBuilder.java +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/execute/ArgLineBuilder.java @@ -84,6 +84,10 @@ public ArgLineBuilder plugins(Collection plugins, Path outputPat return this; } + public List version() { + return List.of(args.get(0), "--version"); + } + private ArgLineBuilder langOut(String type, Path outputPath, boolean lite) { ++outputTargetCount; var flag = lite diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/execute/CommandLineExecutor.java b/src/main/java/io/github/ascopes/protobufmavenplugin/execute/CommandLineExecutor.java index 4ccaa151..74342860 100644 --- a/src/main/java/io/github/ascopes/protobufmavenplugin/execute/CommandLineExecutor.java +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/execute/CommandLineExecutor.java @@ -25,7 +25,7 @@ import org.slf4j.LoggerFactory; /** - * Executor for commands. + * Executor for {@code protoc} commands. * * @author Ashley Scopes */ @@ -36,10 +36,10 @@ public final class CommandLineExecutor { @Inject public CommandLineExecutor() { - // Static-only class. + // Nothing to do here. } - public boolean execute(List args) throws IOException { + public boolean execute(boolean silent, List args) throws IOException { log.info("Calling protoc with the following command line: {}", Shlex.quoteShellArgs(args)); var procBuilder = new ProcessBuilder(args); @@ -47,19 +47,7 @@ public boolean execute(List args) throws IOException { 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"); @@ -68,7 +56,18 @@ public boolean execute(List 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; + } } } diff --git a/src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java b/src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java index 1c21c980..612265ed 100644 --- a/src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java +++ b/src/main/java/io/github/ascopes/protobufmavenplugin/generate/SourceCodeGenerator.java @@ -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); @@ -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 discoverPlugins( GenerationRequest request ) throws IOException, ResolutionException {