diff --git a/src/fr/monlouyan/bakefile/BakefileParser.java b/src/fr/monlouyan/bakefile/BakefileParser.java index 2b04238..8ce18b1 100644 --- a/src/fr/monlouyan/bakefile/BakefileParser.java +++ b/src/fr/monlouyan/bakefile/BakefileParser.java @@ -80,21 +80,26 @@ public class BakefileParser { } if (varMatcher.matches()) { - // Stocke la variable variables.put(varMatcher.group(1), varMatcher.group(2)); } else if (targetMatcher.matches()) { - if (firstTarget == null) { - firstTarget = targetMatcher.group(1); - } - if (currentTarget != null) { - rules.add(new Rule(currentTarget, dependencies, replaceVariables(commands), phonyTargets.contains(currentTarget))); + rules.add(new Rule( + replaceVariables(currentTarget), + replaceVariablesInList(dependencies), + replaceVariablesInList(commands), + phonyTargets.contains(currentTarget) + )); } currentTarget = targetMatcher.group(1); String depStr = targetMatcher.group(2).trim(); dependencies = depStr.isEmpty() ? new ArrayList<>() : new ArrayList<>(Arrays.asList(depStr.split("\\s+"))); - + + if (firstTarget == null) { + firstTarget = replaceVariables(currentTarget); + } + + if (currentTarget.equals("clean")) { phonyTargets.add(currentTarget); } @@ -106,7 +111,12 @@ public class BakefileParser { } if (currentTarget != null) { - rules.add(new Rule(currentTarget, dependencies, replaceVariables(commands), phonyTargets.contains(currentTarget))); + rules.add(new Rule( + replaceVariables(currentTarget), + replaceVariablesInList(dependencies), + replaceVariablesInList(commands), + phonyTargets.contains(currentTarget) + )); } } catch (IOException e) { @@ -115,32 +125,32 @@ public class BakefileParser { return rules; } - /** - * Remplace les variables dans une liste de commandes. - * Ex : "gcc $(FLAGS) -o main main.c" devient "gcc -ansi -pedantic -o main main.c" - * @param commands Liste des commandes à modifier - * @return Liste avec les variables remplacées - */ - private List replaceVariables(List commands) { - List resolvedCommands = new ArrayList<>(); - for (String command : commands) { - boolean replaced; - do { - replaced = false; - for (Map.Entry entry : variables.entrySet()) { - String key = "$(" + entry.getKey() + ")"; - if (command.contains(key)) { - command = command.replace(key, entry.getValue()); - replaced = true; - } + private String replaceVariables(String input) { + if (input == null) return null; + String result = input; + boolean replaced; + do { + replaced = false; + for (Map.Entry entry : variables.entrySet()) { + String key = "$(" + entry.getKey() + ")"; + if (result.contains(key)) { + result = result.replace(key, entry.getValue()); + replaced = true; } - } while (replaced); // Continue tant qu'on remplace des variables - resolvedCommands.add(command); - } - return resolvedCommands; + } + } while (replaced); + return result; } + private List replaceVariablesInList(List items) { + List resolved = new ArrayList<>(); + for (String item : items) { + resolved.add(replaceVariables(item)); + } + return resolved; + } + /** * Permet de récupérer la première cible trouvée dans le fichier Bakefile.