From c565bd4400d49382bc31b0a3c184f3f1127ea4c8 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Tue, 5 Sep 2023 20:58:18 +0100 Subject: [PATCH] test --- build.gradle | 1 - .../SPDF/config/ConfigInitializer.java | 95 +++++++++++++++---- 2 files changed, 77 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index b628a2d4..3d52f6f5 100644 --- a/build.gradle +++ b/build.gradle @@ -63,7 +63,6 @@ launch4j { } dependencies { - implementation 'org.yaml:snakeyaml:2.1' implementation 'org.springframework.boot:spring-boot-starter-web:3.1.2' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:3.1.2' diff --git a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java index 43d0ccfe..9760662a 100644 --- a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java +++ b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java @@ -1,10 +1,18 @@ package stirling.software.SPDF.config; + +import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; @@ -20,23 +28,74 @@ public class ConfigInitializer implements ApplicationContextInitializer templateLines; + try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) { + templateLines = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines().collect(Collectors.toList()); + } + + mergeYamlFiles(templateLines, destPath, destPath); + } + } + + public void mergeYamlFiles(List templateLines, Path userFilePath, Path outputPath) throws IOException { + List userLines = Files.readAllLines(userFilePath); + + List mergedLines = new ArrayList<>(); + boolean insideAutoGenerated = false; + + for (String line : templateLines) { + // Check if we've entered or left the AutomaticallyGenerated section + if (line.trim().equalsIgnoreCase("AutomaticallyGenerated:")) { + insideAutoGenerated = true; + mergedLines.add(line); + continue; + } else if (insideAutoGenerated && line.trim().isEmpty()) { + // We have reached the end of the AutomaticallyGenerated section + insideAutoGenerated = false; + mergedLines.add(line); + continue; + } + + if (insideAutoGenerated) { + // Add lines from user's settings if we are inside AutomaticallyGenerated + Optional userAutoGenValue = userLines.stream().filter(l -> l.trim().startsWith(line.split(":")[0].trim())).findFirst(); + if (userAutoGenValue.isPresent()) { + mergedLines.add(userAutoGenValue.get()); + continue; + } + } else { + // Outside of AutomaticallyGenerated, continue as before + if (line.contains(": ")) { + String key = line.split(": ")[0].trim(); + Optional userValue = userLines.stream().filter(l -> l.trim().startsWith(key)).findFirst(); + if (userValue.isPresent()) { + mergedLines.add(userValue.get()); + continue; + } + } + mergedLines.add(line); + } + } + + Files.write(outputPath, mergedLines, StandardCharsets.UTF_8); + } +} \ No newline at end of file