Correction bug test 16

This commit is contained in:
Louay DARDOURI 2025-03-15 18:39:45 +01:00
parent c2ff5983e5
commit 297ceba632

@ -28,7 +28,7 @@ public class BakefileParser {
* Format : "nom1 nom2 nom3 : dépendance1 dépendance2" * Format : "nom1 nom2 nom3 : dépendance1 dépendance2"
* La nouvelle regex gère plusieurs cibles séparées par des espaces * La nouvelle regex gère plusieurs cibles séparées par des espaces
*/ */
private static final Pattern TARGET_PATTERN = Pattern.compile("^([A-Za-z0-9_.\\-]+(?:\\s+[A-Za-z0-9_.\\-]+)*)\\s*:\\s*([^#]*?)\\s*(?:#.*)?$"); private static final Pattern TARGET_PATTERN = Pattern.compile("^([A-Za-z0-9_.\\-\\$\\(\\)\\{\\}]+(?:\\s+[A-Za-z0-9_.\\-\\$\\(\\)\\{\\}]+)*)\\s*:\\s*([^#]*?)\\s*(?:#.*)?$");
/** /**
* Regex pour détecter les lignes de commande associées à une target. * Regex pour détecter les lignes de commande associées à une target.
@ -225,173 +225,215 @@ public class BakefileParser {
} }
/** /**
* Analyser le fichier Bakefile pour extraire les règles de build. * Analyser le fichier Bakefile pour extraire les règles de build.
* @return Liste des règles extraites * @return Liste des règles extraites
*/ */
public List<Rule> parse() { public List<Rule> parse() {
List<Rule> rules = new ArrayList<>(); List<Rule> rules = new ArrayList<>();
Set<String> phonyTargets = new HashSet<>(); Set<String> phonyTargets = new HashSet<>();
if (!Files.exists(Paths.get(filename))) { if (!Files.exists(Paths.get(filename))) {
System.out.println("*** No targets specified and no makefile found. Stop."); System.out.println("*** No targets specified and no makefile found. Stop.");
System.exit(2); System.exit(2);
} }
try { try {
List<String> lines = Files.readAllLines(Paths.get(filename)); List<String> lines = Files.readAllLines(Paths.get(filename));
List<String> currentTargets = null; List<String> currentTargets = null;
List<String> dependencies = new ArrayList<>(); List<String> dependencies = new ArrayList<>();
List<String> commands = new ArrayList<>(); List<String> commands = new ArrayList<>();
List<List<String>> displayCommands = new ArrayList<>(); List<List<String>> displayCommands = new ArrayList<>();
// Variable pour suivre si la ligne précédente était une continuation
boolean previousLineContinues = false;
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i).replace("\r", "");
for (int i = 0; i < lines.size(); i++) { // Ignorer les lignes vides
String line = lines.get(i).replace("\r", ""); if (line.trim().isEmpty()) {
previousLineContinues = false; // Réinitialiser le flag de continuation
continue;
}
// Gérer les erreurs de format (espaces au lieu de tabulations)
// Mais uniquement si ce n'est pas une continuation de variable
if (line.matches("^ +.*$") && !previousLineContinues) {
System.err.println(filename + ":" + (i+1) + ": *** missing separator. Stop.");
System.exit(2);
}
// Vérifier si cette ligne se termine par un backslash (continuation)
previousLineContinues = line.trim().endsWith("\\");
// Matcher pour les déclarations .PHONY
Matcher phonyMatcher = PHONY_PATTERN.matcher(line);
if (phonyMatcher.matches()) {
String[] phonies = phonyMatcher.group(1).trim().split("\\s+");
Collections.addAll(phonyTargets, phonies);
continue;
}
// Matcher pour les déclarations de variables
Matcher varMatcher = VARIABLE_PATTERN.matcher(line);
if (varMatcher.matches()) {
String varName = varMatcher.group(1);
String varValue = varMatcher.group(2).trim();
// Ignorer les lignes vides // Vérifier si la ligne se termine par un backslash (continuation)
if (line.trim().isEmpty()) { if (varValue.endsWith("\\")) {
continue; StringBuilder fullValue = new StringBuilder(varValue.substring(0, varValue.length() - 1).trim());
} int j = i + 1;
// Gérer les erreurs de format (espaces au lieu de tabulations) while (j < lines.size()) {
if (line.matches("^ +.*$")) { String nextLine = lines.get(j).trim();
System.err.println(filename + ":" + (i+1) + ": *** missing separator. Stop.");
System.exit(2); if (nextLine.endsWith("\\")) {
} fullValue.append(" ").append(nextLine.substring(0, nextLine.length() - 1).trim());
j++;
// Matcher pour les déclarations .PHONY } else {
Matcher phonyMatcher = PHONY_PATTERN.matcher(line); fullValue.append(" ").append(nextLine);
if (phonyMatcher.matches()) { i = j; // Mettre à jour l'indice principal pour sauter les lignes traitées
String[] phonies = phonyMatcher.group(1).trim().split("\\s+"); break;
Collections.addAll(phonyTargets, phonies);
continue;
}
// Matcher pour les déclarations de variables
Matcher varMatcher = VARIABLE_PATTERN.matcher(line);
if (varMatcher.matches()) {
String varName = varMatcher.group(1);
String varValue = varMatcher.group(2).trim();
// Évaluer les variables référencées dans la valeur
varValue = replaceVariables(varValue);
variables.put(varName, varValue);
continue;
}
// Matcher pour les cibles et dépendances
Matcher targetMatcher = TARGET_PATTERN.matcher(line);
if (targetMatcher.matches()) {
// Si nous avions des cibles précédentes, créons les règles correspondantes
if (currentTargets != null) {
// Créer une règle pour chaque cible avec les mêmes dépendances et commandes
for (String target : currentTargets) {
String resolvedTarget = replaceVariables(target.trim());
rules.add(new Rule(
resolvedTarget,
replaceVariablesInList(dependencies),
replaceVariablesInList(commands),
displayCommands,
phonyTargets.contains(resolvedTarget)
));
if (firstTarget == null) {
firstTarget = resolvedTarget;
}
} }
} }
// Configuration pour les nouvelles cibles varValue = fullValue.toString();
String targetStr = targetMatcher.group(1);
currentTargets = splitTargets(targetStr);
String depStr = targetMatcher.group(2);
dependencies = splitDependencies(depStr);
commands = new ArrayList<>();
displayCommands = new ArrayList<>();
continue;
} }
// Matcher pour les lignes de commande // Évaluer les variables référencées dans la valeur
Matcher commandMatcher = COMMAND_PATTERN.matcher(line); varValue = replaceVariables(varValue);
if (commandMatcher.matches()) { variables.put(varName, varValue);
String command = commandMatcher.group(1);
if (BakeCLI.isDebug()) {
// Gérer la continuation de ligne System.out.println("Debug: Variable defined: " + varName + " = " + varValue);
if (command.endsWith("\\")) {
// Traiter la séquence complète de continuation
Object[] result = handleContinuationLines(lines, i);
String fullCommand = (String)result[0];
@SuppressWarnings("unchecked")
List<String> rawLines = (List<String>)result[1];
int linesUsed = (Integer)result[2];
// Ajouter la commande complète pour l'exécution
commands.add(fullCommand);
// Ajouter les lignes brutes pour l'affichage
displayCommands.add(rawLines);
// Ajuster i pour sauter les lignes traitées (moins 1 car la boucle for incrémente i)
i += linesUsed - 1;
} else {
String executableCommand = command;
commands.add(executableCommand);
// Pour l'affichage, préserver le formatage mais remplacer les variables
String displayLine = line;
// Ne pas modifier les lignes qui commencent par @ (silencieuses)
if (command.startsWith("@")) {
displayLine = line; // Garder le formatage complet pour les commandes silencieuses
} else {
// Remplacer les variables dans la partie de la commande uniquement (après la tabulation)
Matcher cmdMatcher = COMMAND_PATTERN.matcher(line);
if (cmdMatcher.matches()) {
String cmdPart = cmdMatcher.group(1);
String cmdWithVars = replaceVariables(cmdPart);
displayLine = "\t" + cmdWithVars;
}
}
List<String> singleLineDisplay = new ArrayList<>();
singleLineDisplay.add(displayLine);
displayCommands.add(singleLineDisplay);
}
continue;
}
}
// Traiter les dernières cibles
if (currentTargets != null) {
// Créer une règle pour chaque cible avec les mêmes dépendances et commandes
for (String target : currentTargets) {
String resolvedTarget = replaceVariables(target.trim());
rules.add(new Rule(
resolvedTarget,
replaceVariablesInList(dependencies),
replaceVariablesInList(commands),
displayCommands,
phonyTargets.contains(resolvedTarget)
));
if (firstTarget == null) {
firstTarget = resolvedTarget;
}
} }
continue;
} }
if (BakeCLI.isDebug()) { // Matcher pour les cibles et dépendances
System.out.println("Debug: Parsed " + rules.size() + " rules."); Matcher targetMatcher = TARGET_PATTERN.matcher(line);
for (Rule rule : rules) { if (targetMatcher.matches()) {
System.out.println("Debug: Rule: " + rule.getName()); // Si nous avions des cibles précédentes, créons les règles correspondantes
System.out.println("Debug: Commands: " + rule.getCommands().size()); if (currentTargets != null) {
for (String cmd : rule.getCommands()) { // Créer une règle pour chaque cible avec les mêmes dépendances et commandes
System.out.println("Debug: [" + cmd + "]"); for (String target : currentTargets) {
String resolvedTarget = replaceVariables(target.trim());
rules.add(new Rule(
resolvedTarget,
replaceVariablesInList(dependencies),
replaceVariablesInList(commands),
displayCommands,
phonyTargets.contains(resolvedTarget)
));
if (firstTarget == null) {
firstTarget = resolvedTarget;
}
} }
} }
// Configuration pour les nouvelles cibles
String targetStr = targetMatcher.group(1);
if (BakeCLI.isDebug()) {
System.out.println("Debug: Raw target(s): " + targetStr);
}
currentTargets = splitTargets(targetStr);
if (BakeCLI.isDebug()) {
System.out.println("Debug: Resolved targets: " + currentTargets);
}
String depStr = targetMatcher.group(2);
dependencies = splitDependencies(depStr);
commands = new ArrayList<>();
displayCommands = new ArrayList<>();
continue;
}
// Matcher pour les lignes de commande
Matcher commandMatcher = COMMAND_PATTERN.matcher(line);
if (commandMatcher.matches()) {
String command = commandMatcher.group(1);
// Gérer la continuation de ligne
if (command.endsWith("\\")) {
// Traiter la séquence complète de continuation
Object[] result = handleContinuationLines(lines, i);
String fullCommand = (String)result[0];
@SuppressWarnings("unchecked")
List<String> rawLines = (List<String>)result[1];
int linesUsed = (Integer)result[2];
// Ajouter la commande complète pour l'exécution
commands.add(fullCommand);
// Ajouter les lignes brutes pour l'affichage
displayCommands.add(rawLines);
// Ajuster i pour sauter les lignes traitées (moins 1 car la boucle for incrémente i)
i += linesUsed - 1;
} else {
String executableCommand = command;
commands.add(executableCommand);
// Pour l'affichage, préserver le formatage mais remplacer les variables
String displayLine = line;
// Ne pas modifier les lignes qui commencent par @ (silencieuses)
if (command.startsWith("@")) {
displayLine = line; // Garder le formatage complet pour les commandes silencieuses
} else {
// Remplacer les variables dans la partie de la commande uniquement (après la tabulation)
Matcher cmdMatcher = COMMAND_PATTERN.matcher(line);
if (cmdMatcher.matches()) {
String cmdPart = cmdMatcher.group(1);
String cmdWithVars = replaceVariables(cmdPart);
displayLine = "\t" + cmdWithVars;
}
}
List<String> singleLineDisplay = new ArrayList<>();
singleLineDisplay.add(displayLine);
displayCommands.add(singleLineDisplay);
}
continue;
} }
} catch (IOException e) {
e.printStackTrace();
} }
return rules;
// Traiter les dernières cibles
if (currentTargets != null) {
// Créer une règle pour chaque cible avec les mêmes dépendances et commandes
for (String target : currentTargets) {
String resolvedTarget = replaceVariables(target.trim());
rules.add(new Rule(
resolvedTarget,
replaceVariablesInList(dependencies),
replaceVariablesInList(commands),
displayCommands,
phonyTargets.contains(resolvedTarget)
));
if (firstTarget == null) {
firstTarget = resolvedTarget;
}
}
}
if (BakeCLI.isDebug()) {
System.out.println("Debug: First target is: " + firstTarget);
System.out.println("Debug: Parsed " + rules.size() + " rules.");
for (Rule rule : rules) {
System.out.println("Debug: Rule: " + rule.getName());
System.out.println("Debug: Commands: " + rule.getCommands().size());
for (String cmd : rule.getCommands()) {
System.out.println("Debug: [" + cmd + "]");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} }
return rules;
}
/** /**
* Récupérer la première cible * Récupérer la première cible