Skip to content

Commit c51ef83

Browse files
committed
devonfw#759: merge
1 parent 78c2fad commit c51ef83

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

cli/src/main/java/com/devonfw/tools/ide/commandlet/UpdateSettingsCommandlet.java

+50-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.file.StandardOpenOption;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.Map.Entry;
1213
import java.util.Properties;
1314
import java.util.stream.Collectors;
1415

@@ -46,35 +47,62 @@ public void run() {
4647

4748
Path target = file_path.getParent().resolve("ide.properties");
4849
Properties devonProperties = new Properties();
49-
try (InputStream devonInputStream = Files.newInputStream(file_path)) {
50-
devonProperties.load(devonInputStream);
51-
52-
for (String name : devonProperties.stringPropertyNames()) {
53-
if (name.contains("DEVON_")) {
54-
devonProperties.put(name.replace("DEVON_", ""), devonProperties.get(name));
55-
devonProperties.remove(name);
50+
try {
51+
List<String> readLines = Files.readAllLines(file_path);
52+
String[] split;
53+
for (String line : readLines) {
54+
if (!line.contains("#") && !line.isEmpty()) {
55+
if (line.contains("DEVON_")) {
56+
line.replace("DEVON_", "");
57+
}
58+
split = line.split("[ =]");
59+
if (split.length == 3) {
60+
devonProperties.put(split[1], new String[] { split[0], split[2] });
61+
}
62+
if (split.length == 2) {
63+
devonProperties.put(split[0], split[1]);
64+
}
5665
}
5766
}
67+
5868
} catch (IOException e) {
5969
throw new RuntimeException(e);
6070
}
6171

6272
if (context.getFileAccess().findFirst(file_path.getParent(), path -> path.getFileName().toString().equals("ide.properties"), false) != null) {
6373
try {
74+
List<String> readLines = Files.readAllLines(target);
75+
String[] split;
76+
6477
Properties ideProperties = new Properties();
65-
try (InputStream ideInputStream = Files.newInputStream(target)) {
66-
ideProperties.load(ideInputStream);
78+
for (String line : readLines) {
79+
if (!line.contains("#") && !line.isEmpty()) {
80+
split = line.split("[ =]");
81+
if (split.length == 3) {
82+
ideProperties.put(split[1], new String[] { split[0], split[2] });
83+
}
84+
if (split.length == 2) {
85+
ideProperties.put(split[0], split[1]);
86+
}
87+
}
6788
}
6889

6990
Properties mergedProperties = new Properties();
70-
mergedProperties.putAll(ideProperties.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));
71-
mergedProperties.putAll(devonProperties.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));
91+
mergedProperties.putAll(ideProperties.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue())));
92+
mergedProperties.putAll(devonProperties.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue())));
7293
for (String name : ideProperties.stringPropertyNames()) {
7394
mergedProperties.remove(name);
7495
}
7596

76-
Files.write(target, mergedProperties.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.toList()),
77-
StandardOpenOption.APPEND);
97+
for (Entry<Object, Object> set : mergedProperties.entrySet()) {
98+
if (set.getValue() instanceof String) {
99+
Files.write(target, ("\n" + set.getKey().toString() + "=" + set.getValue().toString()).getBytes(), StandardOpenOption.APPEND);
100+
}
101+
if (set.getValue() instanceof String[]) {
102+
String[] values = (String[]) set.getValue();
103+
Files.write(target, ("\n" + values[0] + " " + set.getKey().toString() + "=" + values[1]).getBytes(), StandardOpenOption.APPEND);
104+
}
105+
}
78106

79107
this.context.success("Successfully merged and updated ide.properties: " + file_path);
80108

@@ -103,8 +131,15 @@ public void run() {
103131
+ "# In case you are sitting behind a proxy these JVM options may help:\n"
104132
+ "#export JAVA_OPTS=-Dhttp.proxyHost=myproxy.com -Dhttp.proxyPort=8080\n";
105133
Files.write(file_path, comment.getBytes());
106-
Files.write(file_path, devonProperties.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.toList()),
107-
StandardOpenOption.APPEND);
134+
for (Entry<Object, Object> set : devonProperties.entrySet()) {
135+
if (set.getValue() instanceof String) {
136+
Files.write(file_path, ("\n" + set.getKey().toString() + "=" + set.getValue().toString()).getBytes(), StandardOpenOption.APPEND);
137+
}
138+
if (set.getValue() instanceof String[]) {
139+
String[] values = (String[]) set.getValue();
140+
Files.write(file_path, ("\n" + values[0] + " " + set.getKey().toString() + "=" + values[1]).getBytes(), StandardOpenOption.APPEND);
141+
}
142+
}
108143
Files.move(file_path, target);
109144
this.context.success("Updated file name: " + file_path + "\n-> " + target);
110145
} catch (IOException e) {

0 commit comments

Comments
 (0)