-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringConfigurationPropertyDocumenter.java
More file actions
297 lines (262 loc) · 11.6 KB
/
Copy pathSpringConfigurationPropertyDocumenter.java
File metadata and controls
297 lines (262 loc) · 11.6 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.rodnansol:spring-configuration-property-documenter-core:0.2.3
//DEPS info.picocli:picocli:4.7.0
//JAVA 17
import org.rodnansol.core.generator.DocumentGenerationException;
import org.rodnansol.core.generator.reader.MetadataReader;
import org.rodnansol.core.generator.resolver.MetadataInputResolverContext;
import org.rodnansol.core.generator.template.HandlebarsTemplateCompiler;
import org.rodnansol.core.generator.template.TemplateCompiler;
import org.rodnansol.core.generator.template.TemplateCompilerFactory;
import org.rodnansol.core.generator.template.TemplateData;
import org.rodnansol.core.generator.template.TemplateType;
import org.rodnansol.core.generator.template.customization.TemplateCustomizationFactory;
import org.rodnansol.core.generator.writer.AggregationDocumenter;
import org.rodnansol.core.generator.writer.CombinedInput;
import org.rodnansol.core.generator.writer.CreateAggregationCommand;
import org.rodnansol.core.generator.writer.CreateDocumentCommand;
import org.rodnansol.core.generator.writer.CustomTemplate;
import org.rodnansol.core.generator.writer.Documenter;
import org.rodnansol.core.project.Project;
import org.rodnansol.core.project.ProjectFactory;
import org.rodnansol.core.project.ProjectType;
import picocli.CommandLine;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@CommandLine.Command(
mixinStandardHelpOptions = true,
version = SpringConfigurationPropertyDocumenter.VERSION,
description = "CLI tool that reads Jar or raw files to generate documentation about the Spring properties",
subcommands = {
SpringConfigurationPropertyDocumenter.GenerateCommand.class,
SpringConfigurationPropertyDocumenter.AggregatorCommand.class
})
public class SpringConfigurationPropertyDocumenter {
protected static final String VERSION = "0.2.3";
public static void main(String... args) {
int exitCode = new CommandLine(new SpringConfigurationPropertyDocumenter()).execute(args);
System.exit(exitCode);
}
@CommandLine.Command(name = "generate",
mixinStandardHelpOptions = true,
version = SpringConfigurationPropertyDocumenter.VERSION,
description =
"""
Documents the incoming input into a file.
Example: jbang spring-configuration-property-documenter@rodnansol document \\
-n "Header title" -i ~/module-a \\
-tt HTML \\
-o property-docs.html
"""
)
static class GenerateCommand implements Runnable {
/**
* Main header name in the aggregated output file.
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-n", "--name"}, description = "Main header name", arity = "1", defaultValue = "Spring Properties")
String documentName;
/**
* Custom template file.
*
* @since 0.2.1
*/
@CommandLine.Option(names = {"-t", "--template"}, description = "Custom template file", arity = "0..1")
String template;
/**
* List of the inputs, they can be proper JSON files, JAR/Zip files or only directories. The script will determine what to consume.
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-i", "--input"}, description = "Input file, directory or JAR file", arity = "1", required = true)
File input;
/**
* Name of the output file.
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-o", "--output"}, description = "Output file name", arity = "1", required = true)
File outputFile;
/**
* Desired template type, it can be MARKDOWN, ADOC or HTML
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-tt", "--template-type"},
description = """
Desired template type, supported:
${COMPLETION-CANDIDATES}
""",
arity = "1", defaultValue = "MARKDOWN")
TemplateType templateType;
/**
* Type of the project. Currently supported:
*
* <ul>
* <li>MAVEN</li>
* <li>GRADLE</li>
* </ul>
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-pt", "--project-type"},
description = """
Type of the project,
currently supported: MAVEN, GRADLE
""", arity = "1", defaultValue = "MAVEN")
ProjectType projectType;
/**
* Custom template compiler.
*
* @since 0.2.1
*/
@CommandLine.Option(names = {"-tc", "--template-compiler"}, description = "Template's compiler fully qualified name", arity = "0..1", defaultValue = "org.rodnansol.core.generator.template.HandlebarsTemplateCompiler")
String templateCompiler = HandlebarsTemplateCompiler.class.getName();
@Override
public void run() {
Project project = ProjectFactory.ofType(new File("."), documentName, projectType);
String singleTemplate = template != null && !template.isBlank() ? template : templateType.getSingleTemplate();
CreateDocumentCommand createDocumentCommand = new CreateDocumentCommand(project, documentName, input, singleTemplate, outputFile, TemplateCustomizationFactory.getDefaultTemplateCustomizationByType(templateType));
try {
new Documenter(MetadataReader.INSTANCE, TemplateCompilerFactory.getInstanceByClassName(templateCompiler), MetadataInputResolverContext.INSTANCE).readMetadataAndGenerateRenderedFile(createDocumentCommand);
} catch (IOException e) {
throw new DocumentGenerationException("Error during generating the documentation, please check the logs...", e);
}
}
}
@CommandLine.Command(name = "aggregate",
mixinStandardHelpOptions = true,
version = SpringConfigurationPropertyDocumenter.VERSION,
description =
"""
Aggregates the incoming inputs into one big file while.
Please specify the module names for each input with a sequence.
Example: jbang spring-configuration-property-documenter@rodnansol aggregate \\
-n "Multi module project" \\
-mn "Module A" -i ~/module-a \\
-mn "Module B" -i ~/module-b \\
-mn "Module C" -i ~/module-c \\
-tt HTML \\
-o aggregated-property-docs.html
"""
)
static class AggregatorCommand implements Runnable {
/**
* Main header name in the aggregated output file.
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-n", "--name"}, description = "Main header name", arity = "1", defaultValue = "Spring Properties")
String mainName;
/**
* List of the module names.
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-mn", "--module-names"}, description = "List of the module names", arity = "1..*", defaultValue = "Spring Properties")
List<String> moduleNames;
/**
* List of the inputs, they can be proper JSON files, JAR/Zip files or only directories. The script will determine what to consume.
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-i", "--inputs"}, description = "List of input files, directories or JAR files", arity = "1..*", required = true)
List<String> inputs;
/**
* Name of the output file.
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-o", "--output"}, description = "Output file name", arity = "1", required = true)
String outputFile;
/**
* Desired template type, it can be MARKDOWN, ADOC or HTML
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-tt", "--template-type"},
description = """
Desired template type, supported:
${COMPLETION-CANDIDATES}
""",
arity = "1", defaultValue = "MARKDOWN")
TemplateType templateType;
/**
* Type of the project. Currently supported:
*
* <ul>
* <li>MAVEN</li>
* <li>GRADLE</li>
* </ul>
*
* @since 0.1.0
*/
@CommandLine.Option(names = {"-pt", "--project-type"},
description = """
Type of the project,
currently supported: MAVEN, GRADLE
""", arity = "1", defaultValue = "MAVEN")
ProjectType projectType;
/**
* Custom header template file.
*
* @since 0.2.1
*/
@CommandLine.Option(names = {"-ht", "--header-template"}, description = "Custom header template file", arity = "0..1")
String headerTemplate;
/**
* Custom header template file.
*
* @since 0.2.1
*/
@CommandLine.Option(names = {"-ct", "--content-template"}, description = "Custom content template file", arity = "0..1")
String contentTemplate;
/**
* Custom header template file.
*
* @since 0.2.1
*/
@CommandLine.Option(names = {"-ft", "--footer-template"}, description = "Custom footer template file", arity = "0..1")
String footerTemplate;
/**
* Custom template compiler.
*
* @since 0.2.1
*/
@CommandLine.Option(names = {"-tc", "--template-compiler"}, description = "Template's compiler fully qualified name", arity = "0..1", defaultValue = "org.rodnansol.core.generator.template.HandlebarsTemplateCompiler")
String templateCompiler = HandlebarsTemplateCompiler.class.getName();
@Override
public void run() {
List<CombinedInput> combinedInputs = new ArrayList<>(inputs.size());
for (int i = 0; i < inputs.size(); i++) {
String input = inputs.get(i);
combinedInputs.add(new CombinedInput(new File(input), getModuleName(i)));
}
Project project = ProjectFactory.ofType(new File("."), mainName, projectType);
CreateAggregationCommand createAggregationCommand = new CreateAggregationCommand(project,
mainName,
combinedInputs,
templateType,
TemplateCustomizationFactory.getDefaultTemplateCustomizationByType(templateType),
new File(outputFile));
createAggregationCommand.setCustomTemplate(new CustomTemplate(headerTemplate, contentTemplate, footerTemplate));
new AggregationDocumenter(MetadataReader.INSTANCE, TemplateCompilerFactory.getInstanceByClassName(templateCompiler), MetadataInputResolverContext.INSTANCE).createDocumentsAndAggregate(createAggregationCommand);
}
private String getModuleName(int i) {
try {
return moduleNames.get(i);
} catch (IndexOutOfBoundsException e) {
return "Module " + i;
}
}
}
public static class CustomTemplateCompiler implements TemplateCompiler {
@Override
public String compileTemplate(String s, TemplateData templateData) throws DocumentGenerationException {
return "Jbang based template compiler \n";
}
}
}