74 lines
2.9 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) {
// 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
}
// Si la règle a besoin d'être mise à jour et qu'il n'y a pas de commandes
if (ruleNeedsUpdate && rule.getCommands().isEmpty()) {
return; // On ne fait rien mais on ne montre pas de message
}
for (String command : rule.getCommands()) {
if (isCircular){
System.out.println(command);
}
// 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);
pb.inheritIO();
Process process = pb.start();
// Attendre la fin du processus
int exitCode = process.waitFor();
if (exitCode != 0) {
System.err.println("bake: *** [" + rule.getName() + "] Error " + exitCode);
System.exit(exitCode);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
System.exit(1);
2025-02-04 17:31:25 +01:00
}
}
}
// On n'affiche le message "up to date" que si :
// 1. C'est la première cible
// 2. Aucune règle n'avait besoin d'être mise à jour
// 3. On a des commandes à exécuter
if (rule.getName().equals(BakefileParser.getFirstTarget()) && !needsUpdate && !rule.getCommands().isEmpty()) {
System.out.println("bake: `" + rule.getName() + "' is up to date.");
}
// On affiche "Nothing to be done" uniquement si :
// 1. C'est la première cible
// 2. Aucune règle n'avait besoin d'être mise à jour
// 3. On n'a PAS de commandes à exécuter
else if (rule.getName().equals(BakefileParser.getFirstTarget()) && !needsUpdate && rule.getCommands().isEmpty()) {
System.out.println("bake: Nothing to be done for '" + rule.getName() + "'.");
}
}
}