Ajout de la méthode getRule dans BakeEngine et amélioration de la gestion des dépendances dans la classe Rule

This commit is contained in:
2025-02-08 23:15:07 +01:00
parent 2e51ca7491
commit 79f285d020
3 changed files with 84 additions and 95 deletions

View File

@@ -32,6 +32,12 @@ public class BakefileParser {
*/
private static final Pattern VARIABLE_PATTERN = Pattern.compile("^(\\w+)\\s*=\\s*([^#]*?)\\s*(?:#.*)?$");
/**
* Regex pour détecter les déclarations .PHONY
* Format : ".PHONY: clean all"
*/
private static final Pattern PHONY_PATTERN = Pattern.compile("^\\.PHONY:\\s*([^#]*?)\\s*(?:#.*)?$");
/**
* Première cible trouvée dans le fichier Bakefile.
*/
@@ -42,13 +48,12 @@ public class BakefileParser {
*/
private Map<String, String> variables = new HashMap<>();
public BakefileParser(String filename) {
this.filename = filename;
firstTarget = null;
}
private List<String> splitDependencies(String depStr) {
private List<String> splitDependencies(String depStr) {
if (depStr == null || depStr.trim().isEmpty()) {
return new ArrayList<>();
}
@@ -63,7 +68,6 @@ public class BakefileParser {
.collect(Collectors.toList());
}
public List<Rule> parse() {
List<Rule> rules = new ArrayList<>();
Set<String> phonyTargets = new HashSet<>();
@@ -86,6 +90,7 @@ public class BakefileParser {
Matcher varMatcher = VARIABLE_PATTERN.matcher(line);
Matcher targetMatcher = TARGET_PATTERN.matcher(line);
Matcher commandMatcher = COMMAND_PATTERN.matcher(line);
Matcher phonyMatcher = PHONY_PATTERN.matcher(line);
if (line.trim().isEmpty()) {
continue;
@@ -96,32 +101,35 @@ public class BakefileParser {
System.exit(1);
}
if (phonyMatcher.matches()) {
// Ajouter les cibles .PHONY à l'ensemble des cibles phony
String[] phonies = phonyMatcher.group(1).trim().split("\\s+");
Collections.addAll(phonyTargets, phonies);
continue;
}
if (varMatcher.matches()) {
// Stocke la variable en enlevant les espaces en début et fin
variables.put(varMatcher.group(1), varMatcher.group(2).trim());
} else if (targetMatcher.matches()) {
if (currentTarget != null) {
String resolvedTarget = replaceVariables(currentTarget.trim());
rules.add(new Rule(
replaceVariables(currentTarget.trim()),
splitDependencies(dependencies.stream()
.collect(Collectors.joining(" "))),
replaceVariablesInList(commands),
phonyTargets.contains(currentTarget.trim())
));
resolvedTarget,
splitDependencies(dependencies.stream()
.collect(Collectors.joining(" "))),
replaceVariablesInList(commands),
phonyTargets.contains(resolvedTarget)
));
// Ne définir firstTarget que si ce n'est pas une cible .PHONY
if (firstTarget == null && !phonyTargets.contains(resolvedTarget)) {
firstTarget = resolvedTarget;
}
}
currentTarget = targetMatcher.group(1);
String depStr = targetMatcher.group(2);
dependencies = splitDependencies(depStr);
if (firstTarget == null) {
firstTarget = replaceVariables(currentTarget.trim());
}
if (currentTarget.equals("clean")) {
phonyTargets.add(currentTarget);
}
String depStr = targetMatcher.group(2);
dependencies = splitDependencies(depStr);
commands = new ArrayList<>();
} else if (commandMatcher.matches()) {
commands.add(commandMatcher.group(1));
@@ -129,12 +137,18 @@ public class BakefileParser {
}
if (currentTarget != null) {
String resolvedTarget = replaceVariables(currentTarget.trim());
rules.add(new Rule(
replaceVariables(currentTarget.trim()),
resolvedTarget,
replaceVariablesInList(dependencies),
replaceVariablesInList(commands),
phonyTargets.contains(currentTarget.trim())
phonyTargets.contains(resolvedTarget)
));
// Vérifier une dernière fois pour firstTarget
if (firstTarget == null && !phonyTargets.contains(resolvedTarget)) {
firstTarget = resolvedTarget;
}
}
} catch (IOException e) {
@@ -143,7 +157,7 @@ public class BakefileParser {
return rules;
}
private String replaceVariables(String input) {
private String replaceVariables(String input) {
if (input == null) return null;
String result = input;
boolean replaced;
@@ -161,7 +175,7 @@ public class BakefileParser {
return result.trim();
}
private List<String> replaceVariablesInList(List<String> items) {
private List<String> replaceVariablesInList(List<String> items) {
List<String> resolved = new ArrayList<>();
for (String item : items) {
resolved.add(replaceVariables(item));
@@ -169,10 +183,11 @@ public class BakefileParser {
return resolved;
}
/**
* Permet de récupérer la première cible trouvée dans le fichier Bakefile.
* @return La première cible trouvée
*/
public static String getFirstTarget() { return firstTarget; }
}
public static String getFirstTarget() {
return firstTarget;
}
}