Skip to content

Commit 5b63b44

Browse files
committed
feat: improved json reading/writing
The code will only touch the `dependencies` section of the `app.json` file, leaving anything else untouched. This way we'll be future-proof to a certain extent.
1 parent 5cd4805 commit 5b63b44

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

app.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2+
"main": "test",
23
"dependencies": {
3-
"eu.maveniverse.maven.mima:context": "2.4.15",
4+
"com.google.code.gson:gson": "2.11.0",
45
"eu.maveniverse.maven.mima.runtime:standalone-static": "2.4.15",
6+
"eu.maveniverse.maven.mima:context": "2.4.15",
57
"info.picocli:picocli": "4.7.6",
6-
"com.google.code.gson:gson": "2.11.0",
78
"org.slf4j:slf4j-api": "2.0.13",
9+
"org.slf4j:slf4j-log4j12": "2.0.13",
810
"org.slf4j:slf4j-simple": "2.0.13"
911
}
10-
}
12+
}

src/main/java/org/codejive/jpm/Jpm.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ private static String[] getArtifacts(String[] artifactNames, AppInfo prj) {
9393
if (artifactNames.length > 0) {
9494
deps = artifactNames;
9595
} else {
96-
deps =
97-
prj.dependencies.entrySet().stream()
98-
.map(e -> e.getKey() + ":" + e.getValue())
99-
.toArray(String[]::new);
96+
deps = prj.getDependencyGAVs();
10097
}
10198
return deps;
10299
}

src/main/java/org/codejive/jpm/json/AppInfo.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,43 @@
1111
import java.util.TreeMap;
1212

1313
public class AppInfo {
14-
public Map<String, String> dependencies;
14+
private Map<String, Object> json;
15+
public Map<String, String> dependencies = new TreeMap<>();
1516

1617
public static final String APP_INFO_FILE = "app.json";
1718

19+
public String[] getDependencyGAVs() {
20+
return dependencies.entrySet().stream()
21+
.map(e -> e.getKey() + ":" + e.getValue())
22+
.toArray(String[]::new);
23+
}
24+
1825
public static AppInfo read() throws IOException {
1926
Path prjJson = Path.of(APP_INFO_FILE);
20-
AppInfo prj;
27+
AppInfo appInfo = new AppInfo();
2128
if (Files.isRegularFile(prjJson)) {
2229
try (Reader in = Files.newBufferedReader(prjJson)) {
2330
Gson parser = new GsonBuilder().create();
24-
prj = parser.fromJson(in, AppInfo.class);
31+
appInfo.json = parser.fromJson(in, Map.class);
2532
}
2633
} else {
27-
prj = new AppInfo();
34+
appInfo = new AppInfo();
2835
}
29-
if (prj.dependencies == null) {
30-
prj.dependencies = new TreeMap<>();
31-
} else {
32-
prj.dependencies = new TreeMap<>(prj.dependencies);
36+
// WARNING awful code ahead
37+
if (appInfo.json.containsKey("dependencies")
38+
&& appInfo.json.get("dependencies") instanceof Map) {
39+
appInfo.dependencies.putAll((Map<String, String>) appInfo.json.get("dependencies"));
3340
}
34-
return prj;
41+
return appInfo;
3542
}
3643

37-
public static void write(AppInfo prj) throws IOException {
44+
public static void write(AppInfo appInfo) throws IOException {
3845
Path prjJson = Path.of(APP_INFO_FILE);
3946
try (Writer out = Files.newBufferedWriter(prjJson)) {
4047
Gson parser = new GsonBuilder().setPrettyPrinting().create();
41-
parser.toJson(prj, out);
48+
// WARNING awful code ahead
49+
appInfo.json.put("dependencies", (Map<String, Object>) (Map) appInfo.dependencies);
50+
parser.toJson(appInfo.json, out);
4251
}
4352
}
4453
}

0 commit comments

Comments
 (0)