25 lines
859 B
Java
25 lines
859 B
Java
|
|
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) {
|
||
|
|
if (!target.needsUpdate()) return;
|
||
|
|
try {
|
||
|
|
System.out.println("Executing: " + target.getCommand());
|
||
|
|
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();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|