Skip to content

Commit 00f3c87

Browse files
authored
Merge pull request #20 from Sandro642/feature/schematic
Feature: update project version to 0.1.9-SNAPSHOT and enhance YAML te…
2 parents 1045a5f + 9359a67 commit 00f3c87

2 files changed

Lines changed: 115 additions & 41 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = 'fr.sandro642.github'
8-
version = '0.1.8-STABLE' // Version de votre projet
8+
version = '0.1.9-SNAPSHOT' // Version de votre projet
99

1010
// Ajoutez cette tâche à votre build.gradle
1111
tasks.register('printVersion') {

src/main/java/fr/sandro642/github/utils/YamlUtils.java

Lines changed: 114 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.io.*;
99
import java.nio.file.Files;
1010
import java.nio.file.Paths;
11+
import java.util.ArrayList;
12+
import java.util.List;
1113
import java.util.Map;
1214

1315
/**
@@ -80,8 +82,10 @@ public Map<String,String> getRoutes() {
8082
}
8183

8284
/**
83-
* Génère un template de fichier YAML si le fichier n'existe pas déjà.
84-
* @param type
85+
* Génère un template de fichier YAML si celui-ci n'existe pas, ou met à jour la section des routes si le fichier existe déjà.
86+
*
87+
* @param type Le type de ressource pour laquelle générer le template.
88+
* @param routes Les routes à ajouter ou mettre à jour dans le fichier YAML.
8589
*/
8690
public void generateTemplateIfNotExists(ResourceType type, Map<Enum<?>, String> routes) {
8791
String basePath;
@@ -99,45 +103,115 @@ public void generateTemplateIfNotExists(ResourceType type, Map<Enum<?>, String>
99103

100104
File file = new File(basePath, "infos.yml");
101105

102-
StringBuilder template = new StringBuilder(
103-
"# properties Connector API By Sandro642\n\n" +
104-
"urlPath: \"http://localhost:8080/api\"\n\n" +
105-
"routes:\n" +
106-
" #info: \"/info/version\"\n" +
107-
" #ping: \"/ping\"\n" +
108-
" #status: \"/status\"\n\n");
109-
110-
for (Map.Entry<Enum<?>, String> entry : routes.entrySet()) {
111-
template.append(" ")
112-
.append(entry.getKey().name().toLowerCase())
113-
.append(": \"")
114-
.append(entry.getValue())
115-
.append("\"\n");
116-
}
106+
if (file.exists()) {
107+
// Le fichier existe, on met à jour seulement la section routes
108+
try {
109+
// Lire le contenu existant
110+
List<String> lines = new ArrayList<>();
111+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
112+
String line;
113+
while ((line = reader.readLine()) != null) {
114+
lines.add(line);
115+
}
116+
}
117+
118+
// Trouver les indices de début et fin de la section routes
119+
int routesStartIndex = -1;
120+
int routesEndIndex = -1;
121+
122+
for (int i = 0; i < lines.size(); i++) {
123+
String line = lines.get(i).trim();
124+
if (line.equals("routes:")) {
125+
routesStartIndex = i;
126+
} else if (routesStartIndex != -1 && line.matches("^[a-zA-Z_][a-zA-Z0-9_]*:.*")) {
127+
// On a trouvé une nouvelle section (pas une route)
128+
if (!line.startsWith("#") && !line.matches("^\\s*[a-zA-Z_][a-zA-Z0-9_]*:\\s*\"/.*")) {
129+
routesEndIndex = i;
130+
break;
131+
}
132+
}
133+
}
134+
135+
// Si on n'a pas trouvé de fin, la section routes va jusqu'à la fin du fichier
136+
if (routesStartIndex != -1 && routesEndIndex == -1) {
137+
routesEndIndex = lines.size();
138+
}
139+
140+
if (routesStartIndex != -1) {
141+
// Supprimer l'ancienne section routes (garder juste la ligne "routes:")
142+
List<String> newLines = new ArrayList<>();
143+
newLines.addAll(lines.subList(0, routesStartIndex + 1));
144+
145+
// Ajouter les routes commentées par défaut
146+
newLines.add(" #info: \"/info/version\"");
147+
newLines.add(" #ping: \"/ping\"");
148+
newLines.add(" #status: \"/status\"");
149+
newLines.add("");
150+
151+
// Ajouter les nouvelles routes
152+
for (Map.Entry<Enum<?>, String> entry : routes.entrySet()) {
153+
newLines.add(" " + entry.getKey().name().toLowerCase() + ": \"" + entry.getValue() + "\"");
154+
}
155+
156+
// Ajouter le reste du fichier (section schema, etc.)
157+
if (routesEndIndex < lines.size()) {
158+
newLines.add(""); // Ligne vide avant la prochaine section
159+
newLines.addAll(lines.subList(routesEndIndex, lines.size()));
160+
}
161+
162+
// Écrire le fichier mis à jour
163+
try (FileWriter writer = new FileWriter(file)) {
164+
for (String line : newLines) {
165+
writer.write(line + "\n");
166+
}
167+
}
168+
}
169+
170+
} catch (IOException e) {
171+
throw new RuntimeException("Erreur lors de la mise à jour du fichier infos.yml", e);
172+
}
173+
} else {
174+
// Le fichier n'existe pas, on crée le template complet
175+
StringBuilder template = new StringBuilder(
176+
"# properties Connector API By Sandro642\n\n" +
177+
"urlPath: \"http://localhost:8080/api\"\n\n" +
178+
"routes:\n" +
179+
" #info: \"/info/version\"\n" +
180+
" #ping: \"/ping\"\n" +
181+
" #status: \"/status\"\n\n");
182+
183+
for (Map.Entry<Enum<?>, String> entry : routes.entrySet()) {
184+
template.append(" ")
185+
.append(entry.getKey().name().toLowerCase())
186+
.append(": \"")
187+
.append(entry.getValue())
188+
.append("\"\n");
189+
}
117190

118-
template.append("\nschema:\n" +
119-
" #Activer la création de schéma ?\n" +
120-
" enable: false\n\n" +
121-
" #Schéma par défaut:\n" +
122-
" #\tmsg : string\n" +
123-
" #\terr: boolean\n" +
124-
" #\tcode: integer\n" +
125-
" #\tdata: Map<String, Object>\n\n" +
126-
" #Composants à créer exemple, je vais créer plusieurs composant:\n" +
127-
" # msg : str\n" +
128-
" # status : bln\n" +
129-
" # code : int\n" +
130-
" # data_string-object : map / string pour une chaine de caractère et\n" +
131-
" # / object pour la récupération de n'importe\n" +
132-
" # / quel type de variable.\n" +
133-
" #\n" +
134-
" # Grâce à cela vous pourrez les appeler pour récupérer vos propres valeurs\n" +
135-
" # par rapport à votre schéma réponse API\n");
136-
137-
try (FileWriter writer = new FileWriter(file)) {
138-
writer.write(template.toString());
139-
} catch (IOException e) {
140-
throw new RuntimeException("Erreur lors de la création du template infos.yml", e);
191+
template.append("\nschema:\n" +
192+
" #Activer la création de schéma ?\n" +
193+
" enable: false\n\n" +
194+
" #Schéma par défaut:\n" +
195+
" #\tmsg : string\n" +
196+
" #\terr: boolean\n" +
197+
" #\tcode: integer\n" +
198+
" #\tdata: Map<String, Object>\n\n" +
199+
" #Composants à créer exemple, je vais créer plusieurs composant:\n" +
200+
" # msg : str\n" +
201+
" # status : bln\n" +
202+
" # code : int\n" +
203+
" # data_string-object : map / string pour une chaine de caractère et\n" +
204+
" # / object pour la récupération de n'importe\n" +
205+
" # / quel type de variable.\n" +
206+
" #\n" +
207+
" # Grâce à cela vous pourrez les appeler pour récupérer vos propres valeurs\n" +
208+
" # par rapport à votre schéma réponse API\n");
209+
210+
try (FileWriter writer = new FileWriter(file)) {
211+
writer.write(template.toString());
212+
} catch (IOException e) {
213+
throw new RuntimeException("Erreur lors de la création du template infos.yml", e);
214+
}
141215
}
142216
}
143217
}

0 commit comments

Comments
 (0)