Refactorisation du parser Bakefile : amélioration de la gestion des variables

This commit is contained in:
Louay DARDOURI 2025-02-08 20:42:11 +01:00
parent 0a9962269c
commit 7c2de35762

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