package fr.monlouyan.bakefile; import java.io.IOException; public class CommandExecutor { private boolean debug; public CommandExecutor(boolean debug) { this.debug = debug; } public void execute(Rule rule) { if (rule.getCommands().isEmpty()) { System.out.println("bake: Nothing to be done for '" + rule.getName() + "'."); return; } if (!rule.needsUpdate()) { if (rule.getName().equals(BakefileParser.getFirstTarget())) { System.out.println("bake: '" + rule.getName() + "' is up to date."); } return; } try { 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; } } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }