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.
|
* 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
|
* @param input Chaîne à traiter
|
||||||
* @return Chaîne avec les variables remplacées
|
* @return Chaîne avec les variables remplacées
|
||||||
*/
|
*/
|
||||||
private String replaceVariables(String input) {
|
private String replaceVariables(String input) {
|
||||||
if (input == null) return null;
|
if (input == null) return null;
|
||||||
|
|
||||||
String result = input;
|
String result = input;
|
||||||
Set<String> processedVars = new HashSet<>();
|
|
||||||
boolean changed;
|
// Détecter et remplacer toutes les occurrences de variables
|
||||||
|
Matcher matcher = VARIABLE_REFERENCE.matcher(result);
|
||||||
do {
|
StringBuffer sb = new StringBuffer();
|
||||||
changed = false;
|
|
||||||
Matcher matcher = VARIABLE_REFERENCE.matcher(result);
|
while (matcher.find()) {
|
||||||
StringBuffer sb = new StringBuffer();
|
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
|
||||||
while (matcher.find()) {
|
String replacement = variables.containsKey(varName) ? variables.get(varName) : "";
|
||||||
String varName = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
|
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
|
||||||
if (!processedVars.contains(varName) && variables.containsKey(varName)) {
|
}
|
||||||
String replacement = variables.get(varName);
|
matcher.appendTail(sb);
|
||||||
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
|
result = sb.toString();
|
||||||
changed = true;
|
|
||||||
processedVars.add(varName);
|
// 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
|
||||||
matcher.appendTail(sb);
|
}
|
||||||
result = sb.toString();
|
|
||||||
|
return result.trim();
|
||||||
// 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remplacer les variables dans une liste de chaînes.
|
* 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