-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAppInfo.java
More file actions
233 lines (211 loc) · 8.02 KB
/
AppInfo.java
File metadata and controls
233 lines (211 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package org.codejive.jpm.config;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
/**
* Represents the contents of an app.yml file. There are methods for reading and writing instances
* from/to files.
*/
public class AppInfo {
private Map<String, Object> yaml = new LinkedHashMap<>();
private final List<String> dependencies = new ArrayList<>();
private final Map<String, String> repositories = new LinkedHashMap<>();
private final Map<String, String> actions = new LinkedHashMap<>();
/** The official name of the app.yml file. */
public static final String APP_INFO_FILE = "app.yml";
public String name() {
return (String) yaml.get("name");
}
public String description() {
return (String) yaml.get("description");
}
public String documentation() {
return (String) yaml.get("documentation");
}
public String java() {
return (String) yaml.get("java");
}
@SuppressWarnings("unchecked")
public List<String> authors() {
if (yaml.get("authors") instanceof List) {
return (List<String>) yaml.get("authors");
}
return null;
}
@SuppressWarnings("unchecked")
public List<String> contributors() {
if (yaml.get("contributors") instanceof List) {
return (List<String>) yaml.get("contributors");
}
return null;
}
public List<String> dependencies() {
return dependencies;
}
public Map<String, String> repositories() {
return repositories;
}
public Map<String, String> actions() {
return actions;
}
/**
* Returns the dependencies as an array of strings in the format "groupId:artifactId:version".
*
* @return An array of strings
*/
public String[] getDependencyGAVs() {
return dependencies.toArray(String[]::new);
}
/**
* Returns the action command for the given action name.
*
* @param actionName The name of the action
* @return The action command or null if not found
*/
public String getAction(String actionName) {
return actions != null ? actions.get(actionName) : null;
}
/**
* Returns all available action names.
*
* @return A set of action names
*/
public java.util.Set<String> getActionNames() {
return actions != null ? actions.keySet() : java.util.Collections.emptySet();
}
/**
* Reads the app.yml file in the current directory and returns its content as an AppInfo object.
* If the file does not exist, an empty AppInfo object is returned.
*
* @return An instance of AppInfo
* @throws IOException if an error occurred while reading or parsing the file
*/
@SuppressWarnings("unchecked")
public static AppInfo read() throws IOException {
Path appInfoFile = Paths.get(System.getProperty("user.dir"), APP_INFO_FILE);
return read(appInfoFile);
}
/**
* Reads the app.yml file in the current directory and returns its content as an AppInfo object.
* If the file does not exist, an empty AppInfo object is returned.
*
* @param appInfoFile The path to the app.yml file
* @return An instance of AppInfo
* @throws IOException if an error occurred while reading or parsing the file
*/
@SuppressWarnings("unchecked")
public static AppInfo read(Path appInfoFile) throws IOException {
if (Files.isRegularFile(appInfoFile)) {
try (Reader in = Files.newBufferedReader(appInfoFile)) {
return read(in);
}
}
return new AppInfo();
}
/**
* Reads the app.yml from the given Reader and returns its content as an AppInfo object.
*
* @param in The Reader to read the app.yml content from
* @return An instance of AppInfo
*/
@SuppressWarnings("unchecked")
public static AppInfo read(Reader in) {
AppInfo appInfo = new AppInfo();
Yaml yaml = new Yaml();
appInfo.yaml = yaml.load(in);
// Ensure yaml is never null
if (appInfo.yaml == null) {
appInfo.yaml = new LinkedHashMap<>();
}
// We now take any known information from the Yaml map and transfer it to their
// respective fields in the AppInfo object, leaving unknown information untouched
// WARNING awful code ahead
// Parse dependencies section
if (appInfo.yaml.containsKey("dependencies")) {
if (appInfo.yaml.get("dependencies") instanceof Map) {
Map<String, Object> deps = (Map<String, Object>) appInfo.yaml.get("dependencies");
for (Map.Entry<String, Object> entry : deps.entrySet()) {
appInfo.dependencies.add(entry.getKey() + ":" + entry.getValue());
}
} else if (appInfo.yaml.get("dependencies") instanceof List) {
List<String> deps = (List<String>) appInfo.yaml.get("dependencies");
appInfo.dependencies.addAll(deps);
}
}
// Parse repositories section
if (appInfo.yaml.containsKey("repositories")
&& appInfo.yaml.get("repositories") instanceof Map) {
Map<String, Object> deps = (Map<String, Object>) appInfo.yaml.get("repositories");
for (Map.Entry<String, Object> entry : deps.entrySet()) {
appInfo.repositories.put(entry.getKey(), entry.getValue().toString());
}
}
// Parse actions section
if (appInfo.yaml.containsKey("actions") && appInfo.yaml.get("actions") instanceof Map) {
Map<String, Object> actions = (Map<String, Object>) appInfo.yaml.get("actions");
for (Map.Entry<String, Object> entry : actions.entrySet()) {
appInfo.actions.put(entry.getKey(), entry.getValue().toString());
}
}
return appInfo;
}
/**
* Writes the AppInfo object to the app.yml file in the current directory.
*
* @param appInfo The AppInfo object to write
* @throws IOException if an error occurred while writing the file
*/
@SuppressWarnings("unchecked")
public static void write(AppInfo appInfo) throws IOException {
Path appInfoFile = Paths.get(System.getProperty("user.dir"), APP_INFO_FILE);
write(appInfo, appInfoFile);
}
/**
* Writes the AppInfo object to the given path.
*
* @param appInfo The AppInfo object to write
* @param appInfoFile The path to write the app.yml file to
* @throws IOException if an error occurred while writing the file
*/
@SuppressWarnings("unchecked")
public static void write(AppInfo appInfo, Path appInfoFile) throws IOException {
try (Writer out = Files.newBufferedWriter(appInfoFile)) {
write(appInfo, out);
}
}
/**
* Writes the AppInfo object to the given Writer.
*
* @param appInfo The AppInfo object to write
* @param out The Writer to write to
*/
@SuppressWarnings("unchecked")
public static void write(AppInfo appInfo, Writer out) {
DumperOptions dopts = new DumperOptions();
dopts.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dopts.setPrettyFlow(true);
Yaml yaml = new Yaml(dopts);
// WARNING awful code ahead
appInfo.yaml.put("dependencies", appInfo.dependencies);
if (!appInfo.repositories.isEmpty()) {
appInfo.yaml.put("repositories", (Map<String, Object>) (Map) appInfo.repositories);
} else {
appInfo.yaml.remove("repositories");
}
if (!appInfo.actions.isEmpty()) {
appInfo.yaml.put("actions", (Map<String, Object>) (Map) appInfo.actions);
} else {
appInfo.yaml.remove("actions");
}
yaml.dump(appInfo.yaml, out);
}
}