From 362845b224791cdf0868fe6d07120cfa1521a4b9 Mon Sep 17 00:00:00 2001 From: Louay DARDOURI <louay.dardouri@etu.u-pec.fr> Date: Fri, 14 Mar 2025 22:47:20 +0100 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20de=20la=20m=C3=A9thode=20de?= =?UTF-8?q?=20remplacement=20des=20variables=20pour=20g=C3=A9rer=20les=20r?= =?UTF-8?q?=C3=A9f=C3=A9rences=20imbriqu=C3=A9es=20et=20ajout=20de=20tests?= =?UTF-8?q?=20pour=20les=20variables=20inexistantes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fr/monlouyan/bakefile/BakefileParser.java | 64 ++++++++----------- tests/test-29-variable-dont-exist/README.md | 26 ++++++++ .../test-29-variable-dont-exist/bake/Bakefile | 7 ++ .../test-29-variable-dont-exist/make/Makefile | 7 ++ 4 files changed, 68 insertions(+), 36 deletions(-) create mode 100644 tests/test-29-variable-dont-exist/README.md create mode 100644 tests/test-29-variable-dont-exist/bake/Bakefile create mode 100644 tests/test-29-variable-dont-exist/make/Makefile diff --git a/src/fr/monlouyan/bakefile/BakefileParser.java b/src/fr/monlouyan/bakefile/BakefileParser.java index b95728f..3a1f92f 100644 --- a/src/fr/monlouyan/bakefile/BakefileParser.java +++ b/src/fr/monlouyan/bakefile/BakefileParser.java @@ -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. diff --git a/tests/test-29-variable-dont-exist/README.md b/tests/test-29-variable-dont-exist/README.md new file mode 100644 index 0000000..bc2960d --- /dev/null +++ b/tests/test-29-variable-dont-exist/README.md @@ -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 \ No newline at end of file diff --git a/tests/test-29-variable-dont-exist/bake/Bakefile b/tests/test-29-variable-dont-exist/bake/Bakefile new file mode 100644 index 0000000..ae3225f --- /dev/null +++ b/tests/test-29-variable-dont-exist/bake/Bakefile @@ -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) \ No newline at end of file diff --git a/tests/test-29-variable-dont-exist/make/Makefile b/tests/test-29-variable-dont-exist/make/Makefile new file mode 100644 index 0000000..ae3225f --- /dev/null +++ b/tests/test-29-variable-dont-exist/make/Makefile @@ -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) \ No newline at end of file