fix for config #376
This commit is contained in:
parent
af5bbd8838
commit
ece00956d9
2 changed files with 99 additions and 71 deletions
|
@ -8,7 +8,7 @@ plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'stirling.software'
|
group = 'stirling.software'
|
||||||
version = '0.14.2'
|
version = '0.14.3'
|
||||||
sourceCompatibility = '17'
|
sourceCompatibility = '17'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
|
|
@ -12,6 +12,8 @@ import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContextInitializer;
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
|
@ -19,83 +21,109 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
public class ConfigInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
public class ConfigInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||||
try {
|
try {
|
||||||
ensureConfigExists();
|
ensureConfigExists();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("Failed to initialize application configuration", e);
|
throw new RuntimeException("Failed to initialize application configuration", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ensureConfigExists() throws IOException {
|
public void ensureConfigExists() throws IOException {
|
||||||
// Define the path to the external config directory
|
// Define the path to the external config directory
|
||||||
Path destPath = Paths.get("configs", "settings.yml");
|
Path destPath = Paths.get("configs", "settings.yml");
|
||||||
|
|
||||||
// Check if the file already exists
|
// Check if the file already exists
|
||||||
if (Files.notExists(destPath)) {
|
if (Files.notExists(destPath)) {
|
||||||
// Ensure the destination directory exists
|
// Ensure the destination directory exists
|
||||||
Files.createDirectories(destPath.getParent());
|
Files.createDirectories(destPath.getParent());
|
||||||
|
|
||||||
// Copy the resource from classpath to the external directory
|
// Copy the resource from classpath to the external directory
|
||||||
try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
||||||
if (in != null) {
|
if (in != null) {
|
||||||
Files.copy(in, destPath);
|
Files.copy(in, destPath);
|
||||||
} else {
|
} else {
|
||||||
throw new FileNotFoundException("Resource file not found: settings.yml.template");
|
throw new FileNotFoundException("Resource file not found: settings.yml.template");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If user file exists, we need to merge it with the template from the classpath
|
// If user file exists, we need to merge it with the template from the classpath
|
||||||
List<String> templateLines;
|
List<String> templateLines;
|
||||||
try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
||||||
templateLines = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
|
templateLines = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines()
|
||||||
}
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
mergeYamlFiles(templateLines, destPath, destPath);
|
mergeYamlFiles(templateLines, destPath, destPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void mergeYamlFiles(List<String> templateLines, Path userFilePath, Path outputPath) throws IOException {
|
public void mergeYamlFiles(List<String> templateLines, Path userFilePath, Path outputPath) throws IOException {
|
||||||
List<String> userLines = Files.readAllLines(userFilePath);
|
List<String> userLines = Files.readAllLines(userFilePath);
|
||||||
|
List<String> mergedLines = new ArrayList<>();
|
||||||
|
boolean insideAutoGenerated = false;
|
||||||
|
boolean beforeFirstKey = true;
|
||||||
|
|
||||||
List<String> mergedLines = new ArrayList<>();
|
Function<String, Boolean> isCommented = line -> line.trim().startsWith("#");
|
||||||
boolean insideAutoGenerated = false;
|
Function<String, String> extractKey = line -> {
|
||||||
|
String[] parts = line.split(":");
|
||||||
|
return parts.length > 0 ? parts[0].trim().replace("#", "").trim() : "";
|
||||||
|
};
|
||||||
|
|
||||||
for (String line : templateLines) {
|
Set<String> userKeys = userLines.stream().map(extractKey).collect(Collectors.toSet());
|
||||||
// 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) {
|
for (String line : templateLines) {
|
||||||
// Add lines from user's settings if we are inside AutomaticallyGenerated
|
String key = extractKey.apply(line);
|
||||||
Optional<String> userAutoGenValue = userLines.stream().filter(l -> l.trim().startsWith(line.split(":")[0].trim())).findFirst();
|
|
||||||
if (userAutoGenValue.isPresent()) {
|
if (line.trim().equalsIgnoreCase("AutomaticallyGenerated:")) {
|
||||||
mergedLines.add(userAutoGenValue.get());
|
insideAutoGenerated = true;
|
||||||
continue;
|
mergedLines.add(line);
|
||||||
}
|
continue;
|
||||||
} else {
|
} else if (insideAutoGenerated && line.trim().isEmpty()) {
|
||||||
// Outside of AutomaticallyGenerated, continue as before
|
insideAutoGenerated = false;
|
||||||
if (line.contains(": ")) {
|
mergedLines.add(line);
|
||||||
String key = line.split(": ")[0].trim();
|
continue;
|
||||||
Optional<String> userValue = userLines.stream().filter(l -> l.trim().startsWith(key)).findFirst();
|
}
|
||||||
if (userValue.isPresent()) {
|
|
||||||
mergedLines.add(userValue.get());
|
if (beforeFirstKey && (isCommented.apply(line) || line.trim().isEmpty())) {
|
||||||
continue;
|
// Handle top comments and empty lines before the first key.
|
||||||
}
|
mergedLines.add(line);
|
||||||
}
|
continue;
|
||||||
mergedLines.add(line);
|
}
|
||||||
}
|
|
||||||
}
|
if (!key.isEmpty())
|
||||||
|
beforeFirstKey = false;
|
||||||
|
|
||||||
|
if (userKeys.contains(key)) {
|
||||||
|
// If user has any version (commented or uncommented) of this key, skip the
|
||||||
|
// template line
|
||||||
|
Optional<String> userValue = userLines.stream()
|
||||||
|
.filter(l -> extractKey.apply(l).equalsIgnoreCase(key) && !isCommented.apply(l)).findFirst();
|
||||||
|
if (userValue.isPresent())
|
||||||
|
mergedLines.add(userValue.get());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isCommented.apply(line) || line.trim().isEmpty() || !userKeys.contains(key)) {
|
||||||
|
mergedLines.add(line); // If line is commented, empty or key not present in user's file, retain the
|
||||||
|
// template line
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add any additional uncommented user lines that are not present in the
|
||||||
|
// template
|
||||||
|
for (String userLine : userLines) {
|
||||||
|
String userKey = extractKey.apply(userLine);
|
||||||
|
boolean isPresentInTemplate = templateLines.stream().map(extractKey)
|
||||||
|
.anyMatch(templateKey -> templateKey.equalsIgnoreCase(userKey));
|
||||||
|
if (!isPresentInTemplate && !isCommented.apply(userLine)) {
|
||||||
|
mergedLines.add(userLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Files.write(outputPath, mergedLines, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
Files.write(outputPath, mergedLines, StandardCharsets.UTF_8);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue