60 lines
2.2 KiB
Java
Raw Normal View History

package fr.monlouyan.bakefile;
import java.io.IOException;
public class CommandExecutor {
private boolean debug;
private boolean needsUpdate = false; // Pour tracker si quelque chose doit être mis à jour
private boolean isCircular = false; // Pour tracker si un cycle a été détecté
public CommandExecutor(boolean debug, boolean isCircular) {
this.debug = debug;
this.isCircular = isCircular;
}
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
}
// On vérifie d'abord si cette règle a besoin d'être mise à jour
boolean ruleNeedsUpdate = rule.needsUpdate();
if (ruleNeedsUpdate) {
needsUpdate = true; // Au moins une règle doit être mise à jour
2025-02-04 17:31:25 +01:00
}
for (String command : rule.getCommands()) {
if (isCircular){
System.out.println(command);
}
// Mais on n'exécute que si nécessaire
if (ruleNeedsUpdate) {
try {
if(!isCircular){
System.out.println(command);
}
if (debug) System.out.println("Debug: Executing " + command);
ProcessBuilder pb = new ProcessBuilder("sh", "-c", command);
Process process = pb.start();
int exitCode = process.waitFor();
if (exitCode != 0) {
System.err.println("Error executing " + rule.getName());
System.exit(1);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
System.exit(1);
2025-02-04 17:31:25 +01:00
}
}
}
// On affiche "up to date" seulement après avoir traité TOUTES les règles
// et seulement si AUCUNE règle n'avait besoin d'être mise à jour
if (rule.getName().equals(BakefileParser.getFirstTarget()) && !needsUpdate) {
System.out.println("bake: '" + rule.getName() + "' is up to date.");
}
}
}