config stuff
This commit is contained in:
parent
e30665e7c8
commit
fa746a2b51
1 changed files with 58 additions and 122 deletions
|
@ -61,12 +61,34 @@ public class ConfigInitializer
|
||||||
List<String> userLines =
|
List<String> userLines =
|
||||||
Files.exists(userPath) ? Files.readAllLines(userPath) : new ArrayList<>();
|
Files.exists(userPath) ? Files.readAllLines(userPath) : new ArrayList<>();
|
||||||
|
|
||||||
Map<String, String> templateEntries = extractEntries(templateLines);
|
List<String> resultLines = new ArrayList<>();
|
||||||
Map<String, String> userEntries = extractEntries(userLines);
|
|
||||||
|
|
||||||
List<String> mergedLines = mergeConfigs(templateLines, templateEntries, userEntries);
|
for (String templateLine : templateLines) {
|
||||||
mergedLines = cleanInvalidYamlEntries(mergedLines);
|
// Check if the line is a comment
|
||||||
Files.write(userPath, mergedLines);
|
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();
|
||||||
|
System.out.println("key=" + key + ", entry=" + entry );
|
||||||
|
addLine(resultLines, userLines, templateLine, key);
|
||||||
|
} else {
|
||||||
|
resultLines.add(templateLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Check if the line is a key-value pair
|
||||||
|
else if (templateLine.contains(":")) {
|
||||||
|
String key = templateLine.split(":")[0].trim();
|
||||||
|
addLine(resultLines, userLines, templateLine, key);
|
||||||
|
}
|
||||||
|
// Handle empty lines
|
||||||
|
else if (templateLine.trim().length() == 0) {
|
||||||
|
resultLines.add("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the result to the user settings file
|
||||||
|
Files.write(userPath, resultLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
Path customSettingsPath = Paths.get("configs", "custom_settings.yml");
|
Path customSettingsPath = Paths.get("configs", "custom_settings.yml");
|
||||||
|
@ -75,128 +97,42 @@ public class ConfigInitializer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, String> extractEntries(List<String> lines) {
|
|
||||||
Map<String, String> entries = new HashMap<>();
|
|
||||||
StringBuilder currentEntry = new StringBuilder();
|
|
||||||
String currentKey = null;
|
|
||||||
int blockIndent = -1;
|
|
||||||
|
|
||||||
for (String line : lines) {
|
|
||||||
if (line.trim().isEmpty()) {
|
|
||||||
if (currentKey != null) {
|
|
||||||
currentEntry.append(line).append("\n");
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int indentLevel = getIndentationLevel(line);
|
//TODO check parent value instead of just indent lines for duplicate keys (like enabled etc)
|
||||||
if (line.trim().startsWith("#")) {
|
private static void addLine(List<String> resultLines, List<String> userLines, String templateLine, String key) {
|
||||||
if (indentLevel <= blockIndent || blockIndent == -1) {
|
boolean added = false;
|
||||||
if (currentKey != null) {
|
int templateIndentationLevel = getIndentationLevel(templateLine);
|
||||||
entries.put(currentKey, currentEntry.toString().trim());
|
for (String settingsLine : userLines) {
|
||||||
currentEntry = new StringBuilder();
|
if(settingsLine.contains("oauth2") || settingsLine.contains("enabled") )
|
||||||
}
|
if (settingsLine.trim().startsWith(key + ":")) {
|
||||||
currentKey = line.trim().replaceAll("#", "").split(":")[0].trim();
|
int settingsIndentationLevel = getIndentationLevel(settingsLine);
|
||||||
blockIndent = indentLevel;
|
// Check if it is correct settingsLine and has the same parent as templateLine
|
||||||
}
|
if (settingsIndentationLevel == templateIndentationLevel) {
|
||||||
currentEntry.append(line).append("\n");
|
resultLines.add(settingsLine);
|
||||||
} else if (indentLevel == 0 || indentLevel <= blockIndent) {
|
added = true;
|
||||||
if (currentKey != null) {
|
break;
|
||||||
entries.put(currentKey, currentEntry.toString().trim());
|
|
||||||
currentEntry = new StringBuilder();
|
|
||||||
}
|
|
||||||
currentKey = line.split(":")[0].trim();
|
|
||||||
blockIndent = indentLevel;
|
|
||||||
currentEntry.append(line).append("\n");
|
|
||||||
} else {
|
|
||||||
currentEntry.append(line).append("\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentKey != null) {
|
|
||||||
entries.put(currentKey, currentEntry.toString().trim());
|
|
||||||
}
|
}
|
||||||
|
if (!added) {
|
||||||
return entries;
|
resultLines.add(templateLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> mergeConfigs(
|
|
||||||
List<String> templateLines,
|
|
||||||
Map<String, String> templateEntries,
|
|
||||||
Map<String, String> userEntries) {
|
|
||||||
List<String> mergedLines = new ArrayList<>();
|
|
||||||
Set<String> handledKeys = new HashSet<>();
|
|
||||||
|
|
||||||
String currentBlockKey = null;
|
|
||||||
int blockIndent = -1;
|
|
||||||
|
|
||||||
for (String line : templateLines) {
|
|
||||||
if (line.trim().isEmpty()) {
|
|
||||||
mergedLines.add(line);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int indentLevel = getIndentationLevel(line);
|
|
||||||
if (indentLevel == 0 || (indentLevel <= blockIndent && !line.trim().startsWith("#"))) {
|
|
||||||
currentBlockKey = line.split(":")[0].trim();
|
|
||||||
blockIndent = indentLevel;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userEntries.containsKey(currentBlockKey)
|
|
||||||
&& !handledKeys.contains(currentBlockKey)) {
|
|
||||||
mergedLines.add(userEntries.get(currentBlockKey));
|
|
||||||
handledKeys.add(currentBlockKey);
|
|
||||||
} else if (!handledKeys.contains(currentBlockKey)) {
|
|
||||||
mergedLines.add(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return mergedLines;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<String> cleanInvalidYamlEntries(List<String> lines) {
|
|
||||||
List<String> cleanedLines = new ArrayList<>();
|
|
||||||
for (int i = 0; i < lines.size(); i++) {
|
|
||||||
String line = lines.get(i);
|
|
||||||
String trimmedLine = line.trim();
|
|
||||||
|
|
||||||
if (trimmedLine.startsWith("#")
|
|
||||||
|| !trimmedLine.endsWith(":")
|
|
||||||
|| trimmedLine.contains(" ")) {
|
|
||||||
cleanedLines.add(line);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isKeyWithoutChildrenOrValue(i, lines)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanedLines.add(line);
|
|
||||||
}
|
|
||||||
return cleanedLines;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isKeyWithoutChildrenOrValue(int currentIndex, List<String> lines) {
|
|
||||||
if (currentIndex + 1 < lines.size()) {
|
|
||||||
String currentLine = lines.get(currentIndex);
|
|
||||||
String nextLine = lines.get(currentIndex + 1);
|
|
||||||
int currentIndentation = getIndentationLevel(currentLine);
|
|
||||||
int nextIndentation = getIndentationLevel(nextLine);
|
|
||||||
|
|
||||||
// If the next line is less or equally indented, it's not a child or value
|
|
||||||
return nextIndentation <= currentIndentation;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it's the last line, then it definitely has no children or value
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getIndentationLevel(String line) {
|
private static int getIndentationLevel(String line) {
|
||||||
int count = 0;
|
int indentationLevel = 0;
|
||||||
for (char ch : line.toCharArray()) {
|
String trimmedLine = line.trim();
|
||||||
if (ch == ' ') count++;
|
if (trimmedLine.startsWith("#")) {
|
||||||
else break;
|
line = trimmedLine.substring(1);
|
||||||
}
|
}
|
||||||
return count;
|
for (char c : line.toCharArray()) {
|
||||||
|
if (c == ' ') {
|
||||||
|
indentationLevel++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return indentationLevel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue