Skip to content

Commit b32f3d5

Browse files
committed
refactor!: now using yaml instead of json
1 parent ac3992b commit b32f3d5

File tree

9 files changed

+68
-59
lines changed

9 files changed

+68
-59
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ target/
22
!.mvn/wrapper/maven-wrapper.jar
33
!**/src/main/**/target/
44
!**/src/test/**/target/
5+
deps/
56

67
### IntelliJ IDEA ###
78
.idea/
@@ -36,4 +37,4 @@ build/
3637
.vscode/
3738

3839
### Mac OS ###
39-
.DS_Store
40+
.DS_Store

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Let's see what that did:
5959
```shell
6060
$ tree
6161
.
62-
├── app.json
62+
├── app.yml
6363
├── deps
6464
│   └── jfiglet-0.0.9.jar -> /home/user/.m2/repository/com/github/lalyos/jfiglet/0.0.9/jfiglet-0.0.9.jar
6565
└── HelloWorld.java
@@ -79,13 +79,13 @@ $ javac -cp deps/* HelloWorld.java
7979
|_| |_|\___|_|_|\___( ) \_/\_/ \___/|_| |_|\__,_(_)
8080
```
8181
82-
But if we look again at the above output of `tree`, we also see an `app.json` file.
82+
But if we look again at the above output of `tree`, we also see an `app.yml` file.
8383
This file is used by `jpm` to keep track of the dependencies of your project. If you want to share your project
84-
with someone else, you can simply share the `app.json` file along with the code, and they can run `jpm install`
84+
with someone else, you can simply share the `app.yml` file along with the code, and they can run `jpm install`
8585
to get the required dependencies to run the code.
8686
8787
_NB: We could have used `jpm copy` instead of `jpm install` to copy the dependencies but that would not have created
88-
the `app.json` file._
88+
the `app.yml` file._
8989
9090
### JBang
9191
@@ -123,20 +123,20 @@ Commands:
123123
jpm search httpclient
124124
125125
install, i This adds the given artifacts to the list of dependencies
126-
available in the app.json file. It then behaves just like 'copy
126+
available in the app.yml file. It then behaves just like 'copy
127127
--sync' and copies all artifacts in that list and all their
128128
dependencies to the target directory while at the same time
129129
removing any artifacts that are no longer needed (ie the ones
130-
that are not mentioned in the app.json file). If no artifacts
131-
are passed the app.json file will be left untouched and only
130+
that are not mentioned in the app.yml file). If no artifacts
131+
are passed the app.yml file will be left untouched and only
132132
the existing dependencies in the file will be copied.
133133
134134
Example:
135135
jpm install org.apache.httpcomponents:httpclient:4.5.14
136136
137137
path, p Resolves one or more artifacts and prints the full classpath to
138138
standard output. If no artifacts are passed the classpath for
139-
the dependencies defined in the app.json file will be printed
139+
the dependencies defined in the app.yml file will be printed
140140
instead.
141141
142142
Example:

app.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

app.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dependencies:
2+
eu.maveniverse.maven.mima:context: "2.4.15"
3+
eu.maveniverse.maven.mima.runtime:standalone-static: "2.4.15"
4+
info.picocli:picocli: "4.7.6"
5+
org.yaml:snakeyaml: "2.3"
6+
org.jline:jline-console-ui: "3.29.0"
7+
org.jline:jline-terminal-jni: "3.29.0"
8+
org.slf4j:slf4j-api: "2.0.13"
9+
org.slf4j:slf4j-log4j12: "2.0.13"
10+
org.slf4j:slf4j-simple: "2.0.13"

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<mainClass>org.codejive.jpm.Main</mainClass>
3535
<version.mima>2.4.15</version.mima>
3636
<version.picocli>4.7.6</version.picocli>
37-
<version.gson>2.11.0</version.gson>
37+
<version.snakeyaml>2.3</version.snakeyaml>
3838
<version.jline>3.29.0</version.jline>
3939
<version.slf4j>2.0.13</version.slf4j>
4040
<version.spotless>2.43.0</version.spotless>
@@ -58,9 +58,9 @@
5858
<version>${version.picocli}</version>
5959
</dependency>
6060
<dependency>
61-
<groupId>com.google.code.gson</groupId>
62-
<artifactId>gson</artifactId>
63-
<version>${version.gson}</version>
61+
<groupId>org.yaml</groupId>
62+
<artifactId>snakeyaml</artifactId>
63+
<version>${version.snakeyaml}</version>
6464
</dependency>
6565
<dependency>
6666
<groupId>org.jline</groupId>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ private static String artifactGav(Artifact artifact) {
106106

107107
/**
108108
* Installs the given artifacts to the target directory while also registering them as
109-
* dependencies in the app.json file in the current directory. If no artifacts are given, all
110-
* dependencies in the app.json file will be installed. NB: "installation" in this context
109+
* dependencies in the app.yml file in the current directory. If no artifacts are given, all
110+
* dependencies in the app.yml file will be installed. NB: "installation" in this context
111111
* basically means sync-copying the artifacts to the target directory.
112112
*
113113
* @param artifactNames The artifacts to install.
@@ -139,7 +139,7 @@ public SyncStats install(String[] artifactNames)
139139

140140
/**
141141
* Returns the paths of the given artifacts. If no artifacts are given, the paths for all
142-
* dependencies in the app.json file will be returned instead.
142+
* dependencies in the app.yml file will be returned instead.
143143
*
144144
* @param artifactNames The artifacts to get the paths for.
145145
* @return A list of paths.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public Integer call() throws Exception {
153153
break;
154154
}
155155
String finalAction = selectFinalAction(prompt);
156-
if ("quit".equals(finalAction)) {
156+
if (!"again".equals(finalAction)) {
157157
break;
158158
}
159159
artifactPattern = null;
@@ -238,12 +238,12 @@ String selectFinalAction(ConsolePrompt prompt) throws IOException {
238238
.createListPrompt()
239239
.name("action")
240240
.message("Next step:")
241-
.newItem("again")
242-
.text("Search again")
243-
.add()
244241
.newItem("quit")
245242
.text("Quit")
246243
.add()
244+
.newItem("again")
245+
.text("Search again")
246+
.add()
247247
.addPrompt();
248248
Map<String, PromptResultItemIF> result = prompt.prompt(promptBuilder.build());
249249
return getSelectedId(result, "action");
@@ -259,9 +259,9 @@ private static String getSelectedId(
259259
name = "install",
260260
aliases = {"i"},
261261
description =
262-
"This adds the given artifacts to the list of dependencies available in the app.json file. "
263-
+ "It then behaves just like 'copy --sync' and copies all artifacts in that list and all their dependencies to the target directory while at the same time removing any artifacts that are no longer needed (ie the ones that are not mentioned in the app.json file). "
264-
+ "If no artifacts are passed the app.json file will be left untouched and only the existing dependencies in the file will be copied.\n\n"
262+
"This adds the given artifacts to the list of dependencies available in the app.yml file. "
263+
+ "It then behaves just like 'copy --sync' and copies all artifacts in that list and all their dependencies to the target directory while at the same time removing any artifacts that are no longer needed (ie the ones that are not mentioned in the app.yml file). "
264+
+ "If no artifacts are passed the app.yml file will be left untouched and only the existing dependencies in the file will be copied.\n\n"
265265
+ "Example:\n jpm install org.apache.httpcomponents:httpclient:4.5.14\n")
266266
static class Install implements Callable<Integer> {
267267
@Mixin QuietMixin quietMixin;
@@ -287,7 +287,7 @@ public Integer call() throws Exception {
287287
aliases = {"p"},
288288
description =
289289
"Resolves one or more artifacts and prints the full classpath to standard output. "
290-
+ "If no artifacts are passed the classpath for the dependencies defined in the app.json file will be printed instead.\n\n"
290+
+ "If no artifacts are passed the classpath for the dependencies defined in the app.yml file will be printed instead.\n\n"
291291
+ "Example:\n jpm path org.apache.httpcomponents:httpclient:4.5.14\n")
292292
static class PrintPath implements Callable<Integer> {
293293
@Mixin OptionalArtifactsMixin optionalArtifactsMixin;
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.codejive.jpm.json;
22

3-
import com.google.gson.Gson;
4-
import com.google.gson.GsonBuilder;
53
import java.io.IOException;
64
import java.io.Reader;
75
import java.io.Writer;
@@ -10,17 +8,19 @@
108
import java.nio.file.Paths;
119
import java.util.Map;
1210
import java.util.TreeMap;
11+
import org.yaml.snakeyaml.DumperOptions;
12+
import org.yaml.snakeyaml.Yaml;
1313

1414
/**
15-
* Represents the contents of an app.json file. There are methods for reading and writing instances
15+
* Represents the contents of an app.yml file. There are methods for reading and writing instances
1616
* from/to files.
1717
*/
1818
public class AppInfo {
19-
private Map<String, Object> json = new TreeMap<>();
19+
private Map<String, Object> yaml = new TreeMap<>();
2020
public Map<String, String> dependencies = new TreeMap<>();
2121

22-
/** The official name of the app.json file. */
23-
public static final String APP_INFO_FILE = "app.json";
22+
/** The official name of the app.yml file. */
23+
public static final String APP_INFO_FILE = "app.yml";
2424

2525
/**
2626
* Returns the dependencies as an array of strings in the format "groupId:artifactId:version".
@@ -34,8 +34,7 @@ public String[] getDependencyGAVs() {
3434
}
3535

3636
/**
37-
* Reads the app.json file in the current directory and returns its content as an AppInfo
38-
* object.
37+
* Reads the app.yml file in the current directory and returns its content as an AppInfo object.
3938
*
4039
* @return An instance of AppInfo
4140
* @throws IOException if an error occurred while reading or parsing the file
@@ -45,33 +44,39 @@ public static AppInfo read() throws IOException {
4544
AppInfo appInfo = new AppInfo();
4645
if (Files.isRegularFile(prjJson)) {
4746
try (Reader in = Files.newBufferedReader(prjJson)) {
48-
Gson parser = new GsonBuilder().create();
49-
appInfo.json = parser.fromJson(in, Map.class);
47+
Yaml yaml = new Yaml();
48+
appInfo.yaml = yaml.load(in);
5049
}
5150
} else {
5251
appInfo = new AppInfo();
5352
}
5453
// WARNING awful code ahead
55-
if (appInfo.json.containsKey("dependencies")
56-
&& appInfo.json.get("dependencies") instanceof Map) {
57-
appInfo.dependencies.putAll((Map<String, String>) appInfo.json.get("dependencies"));
54+
if (appInfo.yaml.containsKey("dependencies")
55+
&& appInfo.yaml.get("dependencies") instanceof Map) {
56+
Map<String, Object> deps = (Map<String, Object>) appInfo.yaml.get("dependencies");
57+
for (Map.Entry<String, Object> entry : deps.entrySet()) {
58+
appInfo.dependencies.put(entry.getKey(), entry.getValue().toString());
59+
}
5860
}
5961
return appInfo;
6062
}
6163

6264
/**
63-
* Writes the AppInfo object to the app.json file in the current directory.
65+
* Writes the AppInfo object to the app.yml file in the current directory.
6466
*
6567
* @param appInfo The AppInfo object to write
6668
* @throws IOException if an error occurred while writing the file
6769
*/
6870
public static void write(AppInfo appInfo) throws IOException {
6971
Path prjJson = Paths.get(APP_INFO_FILE);
7072
try (Writer out = Files.newBufferedWriter(prjJson)) {
71-
Gson parser = new GsonBuilder().setPrettyPrinting().create();
73+
DumperOptions dopts = new DumperOptions();
74+
dopts.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
75+
dopts.setPrettyFlow(true);
76+
Yaml yaml = new Yaml(dopts);
7277
// WARNING awful code ahead
73-
appInfo.json.put("dependencies", (Map<String, Object>) (Map) appInfo.dependencies);
74-
parser.toJson(appInfo.json, out);
78+
appInfo.yaml.put("dependencies", (Map<String, Object>) (Map) appInfo.dependencies);
79+
yaml.dump(appInfo.yaml, out);
7580
}
7681
}
7782
}

src/main/java/org/codejive/jpm/util/SearchUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.codejive.jpm.util;
22

3-
import com.google.gson.Gson;
4-
import com.google.gson.GsonBuilder;
53
import java.io.IOException;
64
import java.io.InputStream;
75
import java.io.InputStreamReader;
@@ -13,6 +11,11 @@
1311
import org.apache.http.impl.client.CloseableHttpClient;
1412
import org.apache.http.impl.client.HttpClients;
1513
import org.eclipse.aether.artifact.DefaultArtifact;
14+
import org.yaml.snakeyaml.DumperOptions;
15+
import org.yaml.snakeyaml.LoaderOptions;
16+
import org.yaml.snakeyaml.Yaml;
17+
import org.yaml.snakeyaml.constructor.Constructor;
18+
import org.yaml.snakeyaml.representer.Representer;
1619

1720
/** Utility class for searching Maven artifacts. */
1821
public class SearchUtils {
@@ -81,10 +84,14 @@ private static SearchResult select(String query, int start, int count) throws IO
8184
try (CloseableHttpClient httpClient = HttpClients.custom().setUserAgent(agent).build()) {
8285
HttpGet request = new HttpGet(searchUrl);
8386
try (CloseableHttpResponse response = httpClient.execute(request)) {
84-
Gson gson = new GsonBuilder().create();
87+
DumperOptions dopts = new DumperOptions();
88+
Constructor cons = new Constructor(MvnSearchResult.class, new LoaderOptions());
89+
Representer representer = new Representer(dopts);
90+
representer.getPropertyUtils().setSkipMissingProperties(true);
91+
Yaml yaml = new Yaml(cons, representer, dopts);
8592
InputStream ins = response.getEntity().getContent();
8693
InputStreamReader rdr = new InputStreamReader(ins);
87-
MvnSearchResult result = gson.fromJson(rdr, MvnSearchResult.class);
94+
MvnSearchResult result = yaml.load(rdr);
8895
if (result.responseHeader.status != 0) {
8996
throw new IOException("Search failed");
9097
}

0 commit comments

Comments
 (0)