Ensured exit status is set on failure

This commit is contained in:
Erik C. Thauvin 2024-07-22 17:04:02 -07:00
parent df9aee7ca3
commit 8f4bb51dd2
Signed by: erik
GPG key ID: 776702A6A2DA330E

View file

@ -18,9 +18,9 @@ package rife.bld.extension;
import rife.bld.BaseProject; import rife.bld.BaseProject;
import rife.bld.operations.AbstractOperation; import rife.bld.operations.AbstractOperation;
import rife.bld.operations.exceptions.ExitStatusException;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -89,37 +89,49 @@ public class ExecOperation extends AbstractOperation<ExecOperation> {
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
if (project_ == null) { if (project_ == null) {
LOGGER.severe("A project must be specified."); if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
} LOGGER.severe("A project must be specified.");
}
final File workDir = Objects.requireNonNullElseGet(workDir_, throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
() -> new File(project_.workDirectory().getAbsolutePath())); } else {
final File workDir = Objects.requireNonNullElseGet(workDir_,
if (LOGGER.isLoggable(Level.INFO)) { () -> new File(project_.workDirectory().getAbsolutePath()));
LOGGER.info("Working directory: " + workDir.getAbsolutePath());
}
if (workDir.isDirectory()) {
var pb = new ProcessBuilder();
pb.inheritIO();
pb.command(args_.stream().toList());
pb.directory(workDir);
if (LOGGER.isLoggable(Level.INFO)) { if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info(String.join(" ", args_)); LOGGER.info("Working directory: " + workDir.getAbsolutePath());
} }
var proc = pb.start(); if (workDir.isDirectory()) {
var err = proc.waitFor(timeout_, TimeUnit.SECONDS); var pb = new ProcessBuilder();
pb.inheritIO();
pb.command(args_.stream().toList());
pb.directory(workDir);
if (!err) { if (LOGGER.isLoggable(Level.INFO) && !silent()) {
proc.destroy(); LOGGER.info(String.join(" ", args_));
throw new IOException("The command timed out."); }
} else if (proc.exitValue() != 0 && failOnExit_) {
throw new IOException("The command exit value/status is: " + proc.exitValue()); var proc = pb.start();
var err = proc.waitFor(timeout_, TimeUnit.SECONDS);
if (!err) {
proc.destroy();
if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
LOGGER.severe("The command timed out.");
}
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} else if (proc.exitValue() != 0 && failOnExit_) {
if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
LOGGER.severe("The command exit value/status is: " + proc.exitValue());
}
ExitStatusException.throwOnFailure(proc.exitValue());
}
} else {
if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
LOGGER.severe("Invalid working directory: " + workDir);
}
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} }
} else {
throw new IOException("Invalid working directory: " + workDir);
} }
} }