Amélioration de la méthode de remplacement des variables pour gérer les références imbriquées et ajout de tests pour les variables inexistantes
This commit is contained in:
parent
1800aae7cd
commit
362845b224
src/fr/monlouyan/bakefile
tests/test-29-variable-dont-exist
@ -121,45 +121,37 @@ public class BakefileParser {
|
||||
|
||||
/**
|
||||
* Remplacer les variables dans une chaîne.
|
||||
* Cette méthode remplace toutes les références de variables (${VAR} ou $(VAR))
|
||||
* par leur valeur. Si une variable n'est pas définie, elle est remplacée par une chaîne vide.
|
||||
*
|
||||
* @param input Chaîne à traiter
|
||||
* @return Chaîne avec les variables remplacées
|
||||
*/
|
||||
private String replaceVariables(String input) {
|
||||
if (input == null) return null;
|
||||
|
||||
String result = input;
|
||||
Set<String> processedVars = new HashSet<>();
|
||||
boolean changed;
|
||||
|
||||
do {
|
||||
changed = false;
|
||||
Matcher matcher = VARIABLE_REFERENCE.matcher(result);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
while (matcher.find()) {
|
||||
String varName = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
|
||||
if (!processedVars.contains(varName) && variables.containsKey(varName)) {
|
||||
String replacement = variables.get(varName);
|
||||
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
|
||||
changed = true;
|
||||
processedVars.add(varName);
|
||||
}
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
result = sb.toString();
|
||||
|
||||
// Si aucun changement n'a été fait dans ce passage, arrêter
|
||||
if (!changed) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Réinitialiser processedVars pour le prochain passage si nécessaire
|
||||
processedVars.clear();
|
||||
|
||||
} while (changed);
|
||||
|
||||
return result.trim();
|
||||
}
|
||||
private String replaceVariables(String input) {
|
||||
if (input == null) return null;
|
||||
|
||||
String result = input;
|
||||
|
||||
// Détecter et remplacer toutes les occurrences de variables
|
||||
Matcher matcher = VARIABLE_REFERENCE.matcher(result);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
while (matcher.find()) {
|
||||
String varName = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
|
||||
// Remplacer par la valeur de la variable si elle existe, sinon par une chaîne vide
|
||||
String replacement = variables.containsKey(varName) ? variables.get(varName) : "";
|
||||
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
result = sb.toString();
|
||||
|
||||
// Vérifier les références imbriquées de variables et continuer à remplacer si nécessaire
|
||||
if (VARIABLE_REFERENCE.matcher(result).find()) {
|
||||
result = replaceVariables(result); // Appel récursif pour gérer les variables imbriquées
|
||||
}
|
||||
|
||||
return result.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remplacer les variables dans une liste de chaînes.
|
||||
|
26
tests/test-29-variable-dont-exist/README.md
Normal file
26
tests/test-29-variable-dont-exist/README.md
Normal file
@ -0,0 +1,26 @@
|
||||
en gros ici on vérifie ce qu'il se passe quand on essaye d'utiliser une variable qui n'existe pas.
|
||||
|
||||
Exemple :
|
||||
|
||||
bake =
|
||||
moncef@MacBook-Pro-de-Moncef bake % bake
|
||||
Création du fichier
|
||||
touch test.txt
|
||||
Essai de création d'un fichier avec une variable qui n'existe pas
|
||||
touch $(NOM_FICHIER2)
|
||||
sh: NOM_FICHIER2: command not found
|
||||
usage: touch [-A [-][[hh]mm]SS] [-achm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]]
|
||||
[-d YYYY-MM-DDThh:mm:SS[.frac][tz]] file ...
|
||||
bake: *** [main] Error 1
|
||||
|
||||
|
||||
make =
|
||||
|
||||
moncef@MacBook-Pro-de-Moncef make % make
|
||||
Création du fichier
|
||||
touch test.txt
|
||||
Essai de création d'un fichier avec une variable qui n'existe pas
|
||||
touch
|
||||
usage: touch [-A [-][[hh]mm]SS] [-achm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]]
|
||||
[-d YYYY-MM-DDThh:mm:SS[.frac][tz]] file ...
|
||||
make: *** [main] Error 1
|
7
tests/test-29-variable-dont-exist/bake/Bakefile
Normal file
7
tests/test-29-variable-dont-exist/bake/Bakefile
Normal file
@ -0,0 +1,7 @@
|
||||
NOM_FICHIER = test.txt
|
||||
|
||||
main:
|
||||
@echo "Création du fichier"
|
||||
touch $(NOM_FICHIER)
|
||||
@echo "Essai de création d'un fichier avec une variable qui n'existe pas"
|
||||
touch $(NOM_FICHIER2)
|
7
tests/test-29-variable-dont-exist/make/Makefile
Normal file
7
tests/test-29-variable-dont-exist/make/Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
NOM_FICHIER = test.txt
|
||||
|
||||
main:
|
||||
@echo "Création du fichier"
|
||||
touch $(NOM_FICHIER)
|
||||
@echo "Essai de création d'un fichier avec une variable qui n'existe pas"
|
||||
touch $(NOM_FICHIER2)
|
Loading…
x
Reference in New Issue
Block a user