2025-02-04 10:18:26 +01:00
|
|
|
package fr.monlouyan.bakefile;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class CommandExecutor {
|
|
|
|
private boolean debug;
|
|
|
|
|
|
|
|
public CommandExecutor(boolean debug) {
|
|
|
|
this.debug = debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void execute(Target target) {
|
2025-02-04 16:27:07 +01:00
|
|
|
if (!target.needsUpdate()){
|
|
|
|
System.out.println("bake: '" + target.getName() + "' is up to date.");
|
|
|
|
return;
|
|
|
|
};
|
2025-02-04 10:18:26 +01:00
|
|
|
try {
|
2025-02-04 16:27:07 +01:00
|
|
|
System.out.println(target.getCommand());
|
2025-02-04 10:18:26 +01:00
|
|
|
ProcessBuilder pb = new ProcessBuilder("sh", "-c", target.getCommand());
|
|
|
|
Process process = pb.start();
|
|
|
|
int exitCode = process.waitFor();
|
|
|
|
if (debug) System.out.println("Executed: " + target.getCommand() + " with exit code " + exitCode);
|
|
|
|
if (exitCode != 0) System.err.println("Error executing " + target.getName());
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|