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) {
|
|
|
|
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 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
|
|
|
|
|
|
|
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();
|
2025-02-06 12:31:07 +01:00
|
|
|
|
|
|
|
// Lire et afficher la sortie standard (stdout)
|
|
|
|
new Thread(() -> {
|
|
|
|
try (var reader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()))) {
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
System.out.println(line);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
|
|
|
|
// Lire et afficher la sortie d'erreur (stderr)
|
|
|
|
new Thread(() -> {
|
|
|
|
try (var reader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getErrorStream()))) {
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
System.err.println(line);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|