43 lines
1.4 KiB
Java
Raw Normal View History

package fr.monlouyan.bakefile;
import java.io.IOException;
public class CommandExecutor {
private boolean debug;
public CommandExecutor(boolean debug) {
this.debug = debug;
}
2025-02-04 17:31:25 +01:00
public void execute(Rule rule) {
if (rule.getCommands().isEmpty()) {
System.out.println("bake: Nothing to be done for '" + rule.getName() + "'.");
return;
2025-02-04 17:31:25 +01:00
}
if (!rule.needsUpdate()) {
if (rule.getName().equals(BakefileParser.getFirstTarget())) {
System.out.println("bake: '" + rule.getName() + "' is up to date.");
}
2025-02-04 17:31:25 +01:00
return;
}
try {
2025-02-04 17:31:25 +01:00
for (String command : rule.getCommands()) {
System.out.println(command); // Affichage de la commande executée
ProcessBuilder pb = new ProcessBuilder("sh", "-c", command);
Process process = pb.start();
int exitCode = process.waitFor();
if (debug) System.out.println("Executed: " + command + " with exit code " + exitCode);
if (exitCode != 0) {
System.err.println("Error executing " + rule.getName() + "");
break;
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
2025-02-04 17:31:25 +01:00
}