Skip to content

Commit 1d81c6d

Browse files
committed
refactor: added additional read/write methods to AppInfo
1 parent 8c8fa51 commit 1d81c6d

File tree

1 file changed

+77
-25
lines changed

1 file changed

+77
-25
lines changed

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

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,54 @@ public java.util.Set<String> getActionNames() {
5757

5858
/**
5959
* Reads the app.yml file in the current directory and returns its content as an AppInfo object.
60+
* If the file does not exist, an empty AppInfo object is returned.
6061
*
6162
* @return An instance of AppInfo
6263
* @throws IOException if an error occurred while reading or parsing the file
6364
*/
6465
@SuppressWarnings("unchecked")
6566
public static AppInfo read() throws IOException {
66-
Path prjJson = Paths.get(System.getProperty("user.dir"), APP_INFO_FILE);
67-
AppInfo appInfo = new AppInfo();
68-
if (Files.isRegularFile(prjJson)) {
69-
try (Reader in = Files.newBufferedReader(prjJson)) {
70-
Yaml yaml = new Yaml();
71-
appInfo.yaml = yaml.load(in);
67+
Path appInfoFile = Paths.get(System.getProperty("user.dir"), APP_INFO_FILE);
68+
return read(appInfoFile);
69+
}
70+
71+
/**
72+
* Reads the app.yml file in the current directory and returns its content as an AppInfo object.
73+
* If the file does not exist, an empty AppInfo object is returned.
74+
*
75+
* @param appInfoFile The path to the app.yml file
76+
* @return An instance of AppInfo
77+
* @throws IOException if an error occurred while reading or parsing the file
78+
*/
79+
@SuppressWarnings("unchecked")
80+
public static AppInfo read(Path appInfoFile) throws IOException {
81+
if (Files.isRegularFile(appInfoFile)) {
82+
try (Reader in = Files.newBufferedReader(appInfoFile)) {
83+
return read(in);
7284
}
7385
}
86+
return new AppInfo();
87+
}
88+
89+
/**
90+
* Reads the app.yml from the given Reader and returns its content as an AppInfo object.
91+
*
92+
* @param in The Reader to read the app.yml content from
93+
* @return An instance of AppInfo
94+
*/
95+
@SuppressWarnings("unchecked")
96+
public static AppInfo read(Reader in) {
97+
AppInfo appInfo = new AppInfo();
98+
Yaml yaml = new Yaml();
99+
appInfo.yaml = yaml.load(in);
74100
// Ensure yaml is never null
75101
if (appInfo.yaml == null) {
76102
appInfo.yaml = new LinkedHashMap<>();
77103
}
104+
// We now take any known information from the Yaml map and transfer it to their
105+
// respective fields in the AppInfo object, leaving unknown information untouched
78106
// WARNING awful code ahead
107+
// Parse dependencies section
79108
if (appInfo.yaml.containsKey("dependencies")
80109
&& appInfo.yaml.get("dependencies") instanceof Map) {
81110
Map<String, Object> deps = (Map<String, Object>) appInfo.yaml.get("dependencies");
@@ -109,25 +138,48 @@ public static AppInfo read() throws IOException {
109138
*/
110139
@SuppressWarnings("unchecked")
111140
public static void write(AppInfo appInfo) throws IOException {
112-
Path prjJson = Paths.get(System.getProperty("user.dir"), APP_INFO_FILE);
113-
try (Writer out = Files.newBufferedWriter(prjJson)) {
114-
DumperOptions dopts = new DumperOptions();
115-
dopts.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
116-
dopts.setPrettyFlow(true);
117-
Yaml yaml = new Yaml(dopts);
118-
// WARNING awful code ahead
119-
appInfo.yaml.put("dependencies", (Map<String, Object>) (Map) appInfo.dependencies);
120-
if (!appInfo.repositories.isEmpty()) {
121-
appInfo.yaml.put("repositories", (Map<String, Object>) (Map) appInfo.repositories);
122-
} else {
123-
appInfo.yaml.remove("repositories");
124-
}
125-
if (!appInfo.actions.isEmpty()) {
126-
appInfo.yaml.put("actions", (Map<String, Object>) (Map) appInfo.actions);
127-
} else {
128-
appInfo.yaml.remove("actions");
129-
}
130-
yaml.dump(appInfo.yaml, out);
141+
Path appInfoFile = Paths.get(System.getProperty("user.dir"), APP_INFO_FILE);
142+
write(appInfo, appInfoFile);
143+
}
144+
145+
/**
146+
* Writes the AppInfo object to the given path.
147+
*
148+
* @param appInfo The AppInfo object to write
149+
* @param appInfoFile The path to write the app.yml file to
150+
* @throws IOException if an error occurred while writing the file
151+
*/
152+
@SuppressWarnings("unchecked")
153+
public static void write(AppInfo appInfo, Path appInfoFile) throws IOException {
154+
try (Writer out = Files.newBufferedWriter(appInfoFile)) {
155+
write(appInfo, out);
156+
}
157+
}
158+
159+
/**
160+
* Writes the AppInfo object to the given Writer.
161+
*
162+
* @param appInfo The AppInfo object to write
163+
* @param out The Writer to write to
164+
*/
165+
@SuppressWarnings("unchecked")
166+
public static void write(AppInfo appInfo, Writer out) {
167+
DumperOptions dopts = new DumperOptions();
168+
dopts.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
169+
dopts.setPrettyFlow(true);
170+
Yaml yaml = new Yaml(dopts);
171+
// WARNING awful code ahead
172+
appInfo.yaml.put("dependencies", (Map<String, Object>) (Map) appInfo.dependencies);
173+
if (!appInfo.repositories.isEmpty()) {
174+
appInfo.yaml.put("repositories", (Map<String, Object>) (Map) appInfo.repositories);
175+
} else {
176+
appInfo.yaml.remove("repositories");
177+
}
178+
if (!appInfo.actions.isEmpty()) {
179+
appInfo.yaml.put("actions", (Map<String, Object>) (Map) appInfo.actions);
180+
} else {
181+
appInfo.yaml.remove("actions");
131182
}
183+
yaml.dump(appInfo.yaml, out);
132184
}
133185
}

0 commit comments

Comments
 (0)