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()) {
// 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<String> replaceVariables(List<String> commands) {
List<String> resolvedCommands = new ArrayList<>();
for (String command : commands) {
boolean replaced;
do {
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;
}
private String replaceVariables(String input) {
if (input == null) return null;
String result = input;
boolean replaced;
do {
replaced = false;
for (Map.Entry<String, String> 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<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.