Skip to content

Commit

Permalink
Ensured exit status is set on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Jul 23, 2024
1 parent df9aee7 commit 8f4bb51
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/main/java/rife/bld/extension/ExecOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import rife.bld.BaseProject;
import rife.bld.operations.AbstractOperation;
import rife.bld.operations.exceptions.ExitStatusException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -89,37 +89,49 @@ public ExecOperation command(Collection<String> args) {
@Override
public void execute() throws Exception {
if (project_ == null) {
LOGGER.severe("A project must be specified.");
}

final File workDir = Objects.requireNonNullElseGet(workDir_,
() -> new File(project_.workDirectory().getAbsolutePath()));

if (LOGGER.isLoggable(Level.INFO)) {
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.SEVERE) && !silent()) {
LOGGER.severe("A project must be specified.");
}
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} else {
final File workDir = Objects.requireNonNullElseGet(workDir_,
() -> new File(project_.workDirectory().getAbsolutePath()));

if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info(String.join(" ", args_));
LOGGER.info("Working directory: " + workDir.getAbsolutePath());
}

var proc = pb.start();
var err = proc.waitFor(timeout_, TimeUnit.SECONDS);

if (!err) {
proc.destroy();
throw new IOException("The command timed out.");
} else if (proc.exitValue() != 0 && failOnExit_) {
throw new IOException("The command exit value/status is: " + proc.exitValue());
if (workDir.isDirectory()) {
var pb = new ProcessBuilder();
pb.inheritIO();
pb.command(args_.stream().toList());
pb.directory(workDir);

if (LOGGER.isLoggable(Level.INFO) && !silent()) {
LOGGER.info(String.join(" ", args_));
}

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);
}
}

Expand Down

0 comments on commit 8f4bb51

Please sign in to comment.