Correction de bug #2
This commit is contained in:
parent
635ff0a728
commit
06cd424919
src/fr/monlouyan/bakefile
@ -48,6 +48,12 @@ public class BakefileParser {
|
|||||||
*/
|
*/
|
||||||
private static final Pattern PHONY_PATTERN = Pattern.compile("^\\.PHONY:\\s*([^#]*?)\\s*(?:#.*)?$");
|
private static final Pattern PHONY_PATTERN = Pattern.compile("^\\.PHONY:\\s*([^#]*?)\\s*(?:#.*)?$");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regex pour détecter les lignes de continuation.
|
||||||
|
* Format : " gcc -o program program.c \"
|
||||||
|
*/
|
||||||
|
private static final Pattern CONTINUATION_PATTERN = Pattern.compile("^(.*)\\\\\\s*$");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Regex pour détecter les références de variables.
|
* Regex pour détecter les références de variables.
|
||||||
* Format : "${VAR}" ou "$(VAR)"
|
* Format : "${VAR}" ou "$(VAR)"
|
||||||
@ -73,6 +79,59 @@ public class BakefileParser {
|
|||||||
firstTarget = null;
|
firstTarget = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gérer les lignes de continuation.
|
||||||
|
* @param lines Liste des lignes du fichier Bakefile
|
||||||
|
* @param startIndex Index de la première ligne de continuation
|
||||||
|
* @return Tableau contenant la commande combinée, les lignes brutes et le nombre de lignes traitées
|
||||||
|
*/
|
||||||
|
private Object[] handleContinuationLines(List<String> lines, int startIndex) {
|
||||||
|
StringBuilder combinedLine = new StringBuilder();
|
||||||
|
List<String> rawLines = new ArrayList<>();
|
||||||
|
int i = startIndex;
|
||||||
|
|
||||||
|
// Ajouter la première ligne avec son backslash
|
||||||
|
String firstLine = lines.get(i);
|
||||||
|
rawLines.add(firstLine); // Garder la ligne telle quelle
|
||||||
|
|
||||||
|
Matcher contMatcher = CONTINUATION_PATTERN.matcher(firstLine);
|
||||||
|
if (contMatcher.matches()) {
|
||||||
|
combinedLine.append(contMatcher.group(1).trim()).append(" ");
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Traiter les lignes suivantes
|
||||||
|
while (i < lines.size()) {
|
||||||
|
String line = lines.get(i);
|
||||||
|
rawLines.add(line); // Garder la ligne telle quelle
|
||||||
|
|
||||||
|
contMatcher = CONTINUATION_PATTERN.matcher(line);
|
||||||
|
if (contMatcher.matches()) {
|
||||||
|
// Ajouter sans le backslash à la commande combinée
|
||||||
|
combinedLine.append(contMatcher.group(1).trim()).append(" ");
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
// Dernière ligne de la séquence (sans backslash)
|
||||||
|
combinedLine.append(line.trim());
|
||||||
|
i++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BakeCLI.isDebug()) {
|
||||||
|
System.out.println("Debug: Combined " + (i - startIndex) + " lines into: [" +
|
||||||
|
combinedLine.toString().trim() + "]");
|
||||||
|
System.out.println("Debug: Raw lines preserved: " + rawLines.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retourner la commande combinée, les lignes brutes et le nombre de lignes
|
||||||
|
return new Object[] {
|
||||||
|
combinedLine.toString().trim(), // Commande combinée pour exécution
|
||||||
|
rawLines, // Lignes brutes pour affichage
|
||||||
|
i - startIndex // Nombre de lignes traitées
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remplacer les variables dans une chaîne.
|
* Remplacer les variables dans une chaîne.
|
||||||
* @param input Chaîne à traiter
|
* @param input Chaîne à traiter
|
||||||
@ -178,8 +237,7 @@ public class BakefileParser {
|
|||||||
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<>();
|
||||||
boolean inContinuedCommand = false;
|
List<List<String>> displayCommands = new ArrayList<>();
|
||||||
StringBuilder continuedCommand = new StringBuilder();
|
|
||||||
|
|
||||||
for (int i = 0; i < lines.size(); i++) {
|
for (int i = 0; i < lines.size(); i++) {
|
||||||
String line = lines.get(i).replace("\r", "");
|
String line = lines.get(i).replace("\r", "");
|
||||||
@ -190,26 +248,11 @@ public class BakefileParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Gérer les erreurs de format (espaces au lieu de tabulations)
|
// Gérer les erreurs de format (espaces au lieu de tabulations)
|
||||||
if (line.matches("^ +.*$") && !inContinuedCommand) {
|
if (line.matches("^ +.*$")) {
|
||||||
System.err.println(filename + ":" + (i+1) + ": *** missing separator. Stop.");
|
System.err.println(filename + ":" + (i+1) + ": *** missing separator. Stop.");
|
||||||
System.exit(2);
|
System.exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si nous sommes en train de traiter une ligne continuée
|
|
||||||
if (inContinuedCommand) {
|
|
||||||
if (line.endsWith("\\")) {
|
|
||||||
// Encore une continuation
|
|
||||||
continuedCommand.append(" ").append(line.substring(0, line.length() - 1).trim());
|
|
||||||
} else {
|
|
||||||
// Fin de la continuation
|
|
||||||
continuedCommand.append(" ").append(line.trim());
|
|
||||||
commands.add(continuedCommand.toString().trim());
|
|
||||||
inContinuedCommand = false;
|
|
||||||
continuedCommand = new StringBuilder();
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Matcher pour les déclarations .PHONY
|
// Matcher pour les déclarations .PHONY
|
||||||
Matcher phonyMatcher = PHONY_PATTERN.matcher(line);
|
Matcher phonyMatcher = PHONY_PATTERN.matcher(line);
|
||||||
if (phonyMatcher.matches()) {
|
if (phonyMatcher.matches()) {
|
||||||
@ -241,6 +284,7 @@ public class BakefileParser {
|
|||||||
resolvedTarget,
|
resolvedTarget,
|
||||||
replaceVariablesInList(dependencies),
|
replaceVariablesInList(dependencies),
|
||||||
replaceVariablesInList(commands),
|
replaceVariablesInList(commands),
|
||||||
|
displayCommands,
|
||||||
phonyTargets.contains(resolvedTarget)
|
phonyTargets.contains(resolvedTarget)
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -257,6 +301,7 @@ public class BakefileParser {
|
|||||||
String depStr = targetMatcher.group(2);
|
String depStr = targetMatcher.group(2);
|
||||||
dependencies = splitDependencies(depStr);
|
dependencies = splitDependencies(depStr);
|
||||||
commands = new ArrayList<>();
|
commands = new ArrayList<>();
|
||||||
|
displayCommands = new ArrayList<>();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,11 +312,29 @@ public class BakefileParser {
|
|||||||
|
|
||||||
// Gérer la continuation de ligne
|
// Gérer la continuation de ligne
|
||||||
if (command.endsWith("\\")) {
|
if (command.endsWith("\\")) {
|
||||||
inContinuedCommand = true;
|
// Traiter la séquence complète de continuation
|
||||||
continuedCommand = new StringBuilder(command.substring(0, command.length() - 1).trim());
|
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 {
|
} else {
|
||||||
commands.add(command);
|
commands.add(command);
|
||||||
|
// Pour les commandes simples, créer une liste avec une seule ligne
|
||||||
|
List<String> singleLineDisplay = new ArrayList<>();
|
||||||
|
singleLineDisplay.add(line);
|
||||||
|
displayCommands.add(singleLineDisplay);
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,6 +347,7 @@ public class BakefileParser {
|
|||||||
resolvedTarget,
|
resolvedTarget,
|
||||||
replaceVariablesInList(dependencies),
|
replaceVariablesInList(dependencies),
|
||||||
replaceVariablesInList(commands),
|
replaceVariablesInList(commands),
|
||||||
|
displayCommands,
|
||||||
phonyTargets.contains(resolvedTarget)
|
phonyTargets.contains(resolvedTarget)
|
||||||
));
|
));
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package fr.monlouyan.bakefile;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exécuteur des commandes définies dans les règles.
|
* Exécuteur des commandes définies dans les règles.
|
||||||
@ -70,7 +71,13 @@ public class CommandExecutor {
|
|||||||
return; // On ne fait rien mais on ne montre pas de message
|
return; // On ne fait rien mais on ne montre pas de message
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String command : rule.getCommands()) {
|
List<String> executableCommands = rule.getCommands();
|
||||||
|
List<List<String>> displayCommands = rule.getDisplayCommands();
|
||||||
|
|
||||||
|
for (int i = 0; i < executableCommands.size(); i++) {
|
||||||
|
String command = executableCommands.get(i);
|
||||||
|
List<String> displayLines = (i < displayCommands.size()) ? displayCommands.get(i) : null;
|
||||||
|
|
||||||
// Vérifier si la commande commence par @ (ne pas afficher la commande)
|
// Vérifier si la commande commence par @ (ne pas afficher la commande)
|
||||||
boolean silent = command.startsWith("@");
|
boolean silent = command.startsWith("@");
|
||||||
// Enlever le @ si présent pour exécuter la commande correctement
|
// Enlever le @ si présent pour exécuter la commande correctement
|
||||||
@ -83,8 +90,11 @@ public class CommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isCircular){
|
if (isCircular){
|
||||||
if (!silent) {
|
if (!silent && displayLines != null && !displayLines.isEmpty()) {
|
||||||
System.out.println(actualCommand);
|
// Afficher les lignes formatées
|
||||||
|
for (String line : displayLines) {
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +102,14 @@ public class CommandExecutor {
|
|||||||
if (ruleNeedsUpdate) {
|
if (ruleNeedsUpdate) {
|
||||||
try {
|
try {
|
||||||
if(!isCircular && !silent){
|
if(!isCircular && !silent){
|
||||||
System.out.println(actualCommand);
|
// Afficher les lignes formatées
|
||||||
|
if (displayLines != null && !displayLines.isEmpty()) {
|
||||||
|
for (String line : displayLines) {
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (debug) System.out.println("Debug: Executing " + actualCommand);
|
if (debug) System.out.println("Debug: Executing " + actualCommand);
|
||||||
ProcessBuilder pb = new ProcessBuilder("sh", "-c", actualCommand);
|
ProcessBuilder pb = new ProcessBuilder("sh", "-c", actualCommand);
|
||||||
|
@ -22,6 +22,9 @@ public class Rule {
|
|||||||
/*** Liste des commandes */
|
/*** Liste des commandes */
|
||||||
private List<String> commands;
|
private List<String> commands;
|
||||||
|
|
||||||
|
/*** Liste des commandes d'affichage (pour préserver la mise en page originale) */
|
||||||
|
private List<List<String>> displayCommands;
|
||||||
|
|
||||||
/*** true si la règle est phony, false sinon */
|
/*** true si la règle est phony, false sinon */
|
||||||
private boolean isPhony;
|
private boolean isPhony;
|
||||||
|
|
||||||
@ -30,12 +33,15 @@ public class Rule {
|
|||||||
* @param name Nom de la règle
|
* @param name Nom de la règle
|
||||||
* @param dependencies Liste des dépendances
|
* @param dependencies Liste des dépendances
|
||||||
* @param commands Liste des commandes
|
* @param commands Liste des commandes
|
||||||
|
* @param displayCommands Liste des commandes formatées pour affichage
|
||||||
* @param isPhony true si la règle est phony, false sinon
|
* @param isPhony true si la règle est phony, false sinon
|
||||||
*/
|
*/
|
||||||
public Rule(String name, List<String> dependencies, List<String> commands, boolean isPhony) {
|
public Rule(String name, List<String> dependencies, List<String> commands,
|
||||||
|
List<List<String>> displayCommands, boolean isPhony) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.dependencies = dependencies;
|
this.dependencies = dependencies;
|
||||||
this.commands = commands;
|
this.commands = commands;
|
||||||
|
this.displayCommands = displayCommands;
|
||||||
this.isPhony = isPhony;
|
this.isPhony = isPhony;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,6 +63,12 @@ public class Rule {
|
|||||||
*/
|
*/
|
||||||
public List<String> getCommands() { return commands; }
|
public List<String> getCommands() { return commands; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récupère la liste des commandes formatées pour affichage.
|
||||||
|
* @return La liste des commandes formatées
|
||||||
|
*/
|
||||||
|
public List<List<String>> getDisplayCommands() { return displayCommands; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie si la règle est phony.
|
* Vérifie si la règle est phony.
|
||||||
* @return true si la règle est phony, false sinon.
|
* @return true si la règle est phony, false sinon.
|
||||||
@ -76,6 +88,16 @@ public class Rule {
|
|||||||
public boolean needsUpdate() {
|
public boolean needsUpdate() {
|
||||||
if (BakeCLI.isDebug()){
|
if (BakeCLI.isDebug()){
|
||||||
System.out.println("Debug : Checking if rule " + name + " needs update");
|
System.out.println("Debug : Checking if rule " + name + " needs update");
|
||||||
|
System.out.println("Debug : Rule has " + commands.size() + " commands:");
|
||||||
|
for (int i = 0; i < commands.size(); i++) {
|
||||||
|
System.out.println("Debug : Command " + (i+1) + ": [" + commands.get(i) + "]");
|
||||||
|
}
|
||||||
|
System.out.println("Debug : Rule has " + dependencies.size() + " dependencies:");
|
||||||
|
for (String dep : dependencies) {
|
||||||
|
System.out.println("Debug : Dependency: [" + dep + "]");
|
||||||
|
}
|
||||||
|
File targetFile = new File(name);
|
||||||
|
System.out.println("Debug : Target file '" + name + "' exists: " + targetFile.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Les règles phony sont toujours mises à jour
|
// Les règles phony sont toujours mises à jour
|
||||||
|
Loading…
x
Reference in New Issue
Block a user