2025-02-04 10:18:26 +01:00
|
|
|
package fr.monlouyan.bakefile;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class CommandExecutor {
|
|
|
|
private boolean debug;
|
2025-02-04 21:25:16 +01:00
|
|
|
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) {
|
2025-02-04 10:18:26 +01:00
|
|
|
this.debug = debug;
|
2025-02-04 21:25:16 +01:00
|
|
|
this.isCircular = isCircular;
|
2025-02-04 10:18:26 +01:00
|
|
|
}
|
|
|
|
|
2025-02-04 17:31:25 +01:00
|
|
|
public void execute(Rule rule) {
|
2025-02-04 21:25:16 +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
|
|
|
}
|
2025-02-04 21:25:16 +01:00
|
|
|
|
2025-02-06 21:13:55 +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
|
|
|
|
}
|
|
|
|
|
2025-02-04 21:25:16 +01:00
|
|
|
for (String command : rule.getCommands()) {
|
|
|
|
if (isCircular){
|
|
|
|
System.out.println(command);
|
|
|
|
}
|
|
|
|
|
2025-02-06 21:13:55 +01:00
|
|
|
// On n'exécute que si nécessaire
|
2025-02-04 21:25:16 +01:00
|
|
|
if (ruleNeedsUpdate) {
|
|
|
|
try {
|
|
|
|
if(!isCircular){
|
|
|
|
System.out.println(command);
|
|
|
|
}
|
|
|
|
if (debug) System.out.println("Debug: Executing " + command);
|
|
|
|
ProcessBuilder pb = new ProcessBuilder("sh", "-c", command);
|
2025-02-09 17:16:48 +01:00
|
|
|
pb.inheritIO();
|
2025-02-04 21:25:16 +01:00
|
|
|
Process process = pb.start();
|
2025-02-06 12:31:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Attendre la fin du processus
|
2025-02-04 21:25:16 +01:00
|
|
|
int exitCode = process.waitFor();
|
2025-02-06 12:31:07 +01:00
|
|
|
|
2025-02-04 21:25:16 +01:00
|
|
|
if (exitCode != 0) {
|
2025-02-06 12:31:07 +01:00
|
|
|
System.err.println("bake: *** [" + rule.getName() + "] Error " + exitCode);
|
|
|
|
System.exit(exitCode);
|
2025-02-04 21:25:16 +01:00
|
|
|
}
|
2025-02-06 12:31:07 +01:00
|
|
|
|
2025-02-04 21:25:16 +01:00
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(1);
|
2025-02-04 17:31:25 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-04 10:18:26 +01:00
|
|
|
}
|
2025-02-04 21:25:16 +01:00
|
|
|
|
2025-02-06 21:13:55 +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()) {
|
2025-02-06 18:29:54 +01:00
|
|
|
System.out.println("bake: `" + rule.getName() + "' is up to date.");
|
2025-02-04 21:25:16 +01:00
|
|
|
}
|
2025-02-06 21:13:55 +01:00
|
|
|
// 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() + "'.");
|
|
|
|
}
|
2025-02-04 21:25:16 +01:00
|
|
|
}
|
|
|
|
}
|