Files
Stirling-PDF/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
3.3 KiB
Java
Raw Normal View History

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;
import java.net.URL;
2023-08-26 17:30:49 +01:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
2023-08-26 17:30:49 +01:00
2024-12-17 10:26:18 +01:00
import lombok.extern.slf4j.Slf4j;
2025-01-31 11:00:03 +00:00
/**
* A naive, line-based approach to merging "settings.yml" with "settings.yml.template" while
* preserving exact whitespace, blank lines, and inline comments -- but we only rewrite the file if
* the merged content actually differs.
*/
2024-12-17 10:26:18 +01:00
@Slf4j
2025-01-06 12:41:30 +00:00
public class ConfigInitializer {
2023-12-30 19:11:27 +00:00
2024-05-03 20:43:48 +01:00
public void ensureConfigExists() throws IOException, URISyntaxException {
2025-01-31 11:00:03 +00:00
// 1) If settings file doesn't exist, create from template
2025-01-06 12:41:30 +00:00
Path destPath = Paths.get(InstallationPathConfig.getSettingsPath());
2023-09-24 21:09:34 +01:00
if (Files.notExists(destPath)) {
Files.createDirectories(destPath.getParent());
try (InputStream in =
getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
2025-01-31 11:00:03 +00:00
if (in == null) {
2023-09-24 21:09:34 +01:00
throw new FileNotFoundException(
"Resource file not found: settings.yml.template");
}
2025-01-31 11:00:03 +00:00
Files.copy(in, destPath);
2023-09-24 21:09:34 +01:00
}
2025-01-06 12:41:30 +00:00
log.info("Created settings file from template");
2023-09-24 21:09:34 +01:00
} else {
2025-01-31 11:00:03 +00:00
// 2) Merge existing file with the template
URL templateResource = getClass().getClassLoader().getResource("settings.yml.template");
if (templateResource == null) {
throw new IOException("Resource not found: settings.yml.template");
}
2025-01-31 11:00:03 +00:00
// Copy template to a temp location so we can read lines
Path tempTemplatePath = Files.createTempFile("settings.yml", ".template");
2025-01-31 11:00:03 +00:00
try (InputStream in = templateResource.openStream()) {
Files.copy(in, tempTemplatePath, StandardCopyOption.REPLACE_EXISTING);
}
// Copy setting.yaml to a temp location so we can read lines
Path settingTempPath = Files.createTempFile("settings", ".yaml");
try (InputStream in = Files.newInputStream(destPath)) {
Files.copy(in, settingTempPath, StandardCopyOption.REPLACE_EXISTING);
}
2025-01-31 11:00:03 +00:00
YamlHelper settingsTemplateFile = new YamlHelper(tempTemplatePath);
YamlHelper settingsFile = new YamlHelper(settingTempPath);
2025-01-31 11:00:03 +00:00
boolean changesMade =
settingsTemplateFile.updateValuesFromYaml(settingsFile, settingsTemplateFile);
if (changesMade) {
settingsTemplateFile.save(destPath);
2025-01-31 11:00:03 +00:00
log.info("Settings file updated based on template changes.");
} else {
log.info("No changes detected; settings file left as-is.");
2024-05-03 20:43:48 +01:00
}
2025-01-31 11:00:03 +00:00
Files.deleteIfExists(tempTemplatePath);
Files.deleteIfExists(settingTempPath);
2023-09-24 21:09:34 +01:00
}
2025-01-31 11:00:03 +00:00
// 3) Ensure custom settings file exists
2025-01-06 12:41:30 +00:00
Path customSettingsPath = Paths.get(InstallationPathConfig.getCustomSettingsPath());
if (Files.notExists(customSettingsPath)) {
Files.createFile(customSettingsPath);
log.info("Created custom_settings file: {}", customSettingsPath.toString());
2024-05-03 22:23:21 +01:00
}
2024-05-25 18:19:03 +02:00
}
2023-09-05 20:58:18 +01:00
}