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;
|
|
|
|
}
|
|
|
|
|
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() + "'.");
|
2025-02-04 16:27:07 +01:00
|
|
|
return;
|
2025-02-04 17:31:25 +01:00
|
|
|
}
|
|
|
|
|
2025-02-04 19:27:56 +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;
|
|
|
|
}
|
|
|
|
|
2025-02-04 19:27:56 +01:00
|
|
|
|
2025-02-04 10:18:26 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2025-02-04 10:18:26 +01:00
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2025-02-04 17:31:25 +01:00
|
|
|
}
|