2023-08-26 17:30:49 +01:00
|
|
|
package stirling.software.SPDF.config;
|
2023-09-05 20:58:18 +01:00
|
|
|
|
2023-08-26 17:30:49 +01:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2024-05-03 20:43:48 +01:00
|
|
|
import java.net.URISyntaxException;
|
2023-08-26 17:30:49 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
2023-09-05 20:58:18 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2023-08-26 17:30:49 +01:00
|
|
|
|
|
|
|
import org.springframework.context.ApplicationContextInitializer;
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
|
|
|
|
public class ConfigInitializer
|
|
|
|
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-09-24 21:09:34 +01:00
|
|
|
@Override
|
|
|
|
public void initialize(ConfigurableApplicationContext applicationContext) {
|
|
|
|
try {
|
|
|
|
ensureConfigExists();
|
2024-05-03 20:43:48 +01:00
|
|
|
} catch (Exception e) {
|
2023-09-24 21:09:34 +01:00
|
|
|
throw new RuntimeException("Failed to initialize application configuration", e);
|
|
|
|
}
|
|
|
|
}
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2024-05-03 20:43:48 +01:00
|
|
|
public void ensureConfigExists() throws IOException, URISyntaxException {
|
2023-09-24 21:09:34 +01:00
|
|
|
// Define the path to the external config directory
|
|
|
|
Path destPath = Paths.get("configs", "settings.yml");
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-09-24 21:09:34 +01:00
|
|
|
// Check if the file already exists
|
|
|
|
if (Files.notExists(destPath)) {
|
|
|
|
// Ensure the destination directory exists
|
|
|
|
Files.createDirectories(destPath.getParent());
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-09-24 21:09:34 +01:00
|
|
|
// Copy the resource from classpath to the external directory
|
|
|
|
try (InputStream in =
|
|
|
|
getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
|
|
|
if (in != null) {
|
|
|
|
Files.copy(in, destPath);
|
|
|
|
} else {
|
|
|
|
throw new FileNotFoundException(
|
|
|
|
"Resource file not found: settings.yml.template");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-05-03 20:43:48 +01:00
|
|
|
Path templatePath =
|
|
|
|
Paths.get(
|
|
|
|
getClass()
|
|
|
|
.getClassLoader()
|
|
|
|
.getResource("settings.yml.template")
|
|
|
|
.toURI());
|
|
|
|
Path userPath = Paths.get("configs", "settings.yml");
|
|
|
|
|
|
|
|
List<String> templateLines = Files.readAllLines(templatePath);
|
|
|
|
List<String> userLines =
|
|
|
|
Files.exists(userPath) ? Files.readAllLines(userPath) : new ArrayList<>();
|
|
|
|
|
2024-05-19 11:54:45 +01:00
|
|
|
List<String> resultLines = new ArrayList<>();
|
2024-05-25 18:19:03 +02:00
|
|
|
int position = 0;
|
2024-05-19 11:54:45 +01:00
|
|
|
for (String templateLine : templateLines) {
|
|
|
|
// Check if the line is a comment
|
|
|
|
if (templateLine.trim().startsWith("#")) {
|
|
|
|
String entry = templateLine.trim().substring(1).trim();
|
|
|
|
if (!entry.isEmpty()) {
|
|
|
|
// Check if this comment has been uncommented in userLines
|
|
|
|
String key = entry.split(":")[0].trim();
|
2024-05-25 18:19:03 +02:00
|
|
|
addLine(resultLines, userLines, templateLine, key, position);
|
2024-05-19 11:54:45 +01:00
|
|
|
} else {
|
|
|
|
resultLines.add(templateLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if the line is a key-value pair
|
|
|
|
else if (templateLine.contains(":")) {
|
|
|
|
String key = templateLine.split(":")[0].trim();
|
2024-05-25 18:19:03 +02:00
|
|
|
addLine(resultLines, userLines, templateLine, key, position);
|
2024-05-19 11:54:45 +01:00
|
|
|
}
|
|
|
|
// Handle empty lines
|
|
|
|
else if (templateLine.trim().length() == 0) {
|
|
|
|
resultLines.add("");
|
|
|
|
}
|
2024-05-25 18:19:03 +02:00
|
|
|
position++;
|
2024-05-19 11:54:45 +01:00
|
|
|
}
|
2024-05-25 18:19:03 +02:00
|
|
|
|
|
|
|
// Write the result to the user settings file
|
2024-05-19 11:54:45 +01:00
|
|
|
Files.write(userPath, resultLines);
|
2024-05-03 20:43:48 +01:00
|
|
|
}
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2024-05-03 20:43:48 +01:00
|
|
|
Path customSettingsPath = Paths.get("configs", "custom_settings.yml");
|
|
|
|
if (!Files.exists(customSettingsPath)) {
|
|
|
|
Files.createFile(customSettingsPath);
|
|
|
|
}
|
|
|
|
}
|
2024-05-25 18:19:03 +02:00
|
|
|
|
|
|
|
// TODO check parent value instead of just indent lines for duplicate keys (like enabled etc)
|
|
|
|
private static void addLine(
|
|
|
|
List<String> resultLines,
|
|
|
|
List<String> userLines,
|
|
|
|
String templateLine,
|
|
|
|
String key,
|
|
|
|
int position) {
|
2024-05-19 11:54:45 +01:00
|
|
|
boolean added = false;
|
|
|
|
int templateIndentationLevel = getIndentationLevel(templateLine);
|
2024-05-25 18:19:03 +02:00
|
|
|
int pos = 0;
|
2024-05-19 11:54:45 +01:00
|
|
|
for (String settingsLine : userLines) {
|
2024-05-25 18:19:03 +02:00
|
|
|
if (settingsLine.trim().startsWith(key + ":") && position == pos) {
|
2024-05-19 11:54:45 +01:00
|
|
|
int settingsIndentationLevel = getIndentationLevel(settingsLine);
|
|
|
|
// Check if it is correct settingsLine and has the same parent as templateLine
|
|
|
|
if (settingsIndentationLevel == templateIndentationLevel) {
|
|
|
|
resultLines.add(settingsLine);
|
|
|
|
added = true;
|
|
|
|
break;
|
2024-05-03 22:23:21 +01:00
|
|
|
}
|
2024-05-03 20:43:48 +01:00
|
|
|
}
|
2024-05-25 18:19:03 +02:00
|
|
|
pos++;
|
2023-09-24 21:09:34 +01:00
|
|
|
}
|
2024-05-19 11:54:45 +01:00
|
|
|
if (!added) {
|
|
|
|
resultLines.add(templateLine);
|
2024-05-03 22:23:21 +01:00
|
|
|
}
|
2024-05-25 18:19:03 +02:00
|
|
|
}
|
2024-05-03 22:23:21 +01:00
|
|
|
|
2024-05-19 11:54:45 +01:00
|
|
|
private static int getIndentationLevel(String line) {
|
|
|
|
int indentationLevel = 0;
|
|
|
|
String trimmedLine = line.trim();
|
|
|
|
if (trimmedLine.startsWith("#")) {
|
2024-05-25 18:19:03 +02:00
|
|
|
line = trimmedLine.substring(1);
|
2024-05-03 20:43:48 +01:00
|
|
|
}
|
2024-05-19 11:54:45 +01:00
|
|
|
for (char c : line.toCharArray()) {
|
|
|
|
if (c == ' ') {
|
|
|
|
indentationLevel++;
|
|
|
|
} else {
|
|
|
|
break;
|
2023-09-24 21:09:34 +01:00
|
|
|
}
|
2024-02-17 13:10:00 +00:00
|
|
|
}
|
2024-05-19 11:54:45 +01:00
|
|
|
return indentationLevel;
|
2024-02-17 13:10:00 +00:00
|
|
|
}
|
2023-09-05 20:58:18 +01:00
|
|
|
}
|