-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
quarkusbuilditemdoc.java
executable file
·228 lines (205 loc) · 8.73 KB
/
quarkusbuilditemdoc.java
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
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.5.0
//DEPS org.jboss.forge.roaster:roaster-jdt:2.22.2.Final
//DEPS org.eclipse.collections:eclipse-collections:10.4.0
//DEPS org.yaml:snakeyaml:1.27
//DEPS io.fabric8:maven-model-helper:16
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import io.fabric8.maven.Maven;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.factory.Multimaps;
import org.eclipse.collections.impl.tuple.Tuples;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.JavaDocCapable;
import org.jboss.forge.roaster.model.source.FieldSource;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.yaml.snakeyaml.Yaml;
import picocli.CommandLine;
import picocli.CommandLine.Command;
@Command(name = "builditemdoc", mixinStandardHelpOptions = true, version = "builditemdoc 0.1",
description = "builditemdoc made with jbang")
class quarkusbuilditemdoc implements Callable<Integer> {
@CommandLine.Option(names = {"--outputFile"})
public Path outputFile;
@CommandLine.Option(names = {"--dirs"}, required = true, arity = "1..*")
public List<Path> paths;
private PrintStream out = System.out;
public static void main(String... args) {
int exitCode = new CommandLine(new quarkusbuilditemdoc()).execute(args);
System.exit(exitCode);
}
// jbang .github/quarkusbuilditemdoc.java --outputFile target/asciidoc/generated/config/quarkus-all-build-items.adoc --dirs core/deployment core/test-extension extensions
@Override
public Integer call() throws Exception {
if (outputFile != null) {
Files.createDirectories(outputFile.getParent());
out = new PrintStream(Files.newOutputStream(outputFile));
}
final Multimap<String, Pair<Path, JavaClassSource>> multimap = collect();
Map<String, String> names = extractNames(Paths.get("."), multimap.keySet());
// Print Core first
{
printTableHeader(names.remove("Core"));
for (Pair<Path, JavaClassSource> source : multimap.get("Core")) {
printTableRow(source);
}
printTableFooter();
}
names.forEach((key, name) -> {
printTableHeader(name);
for (Pair<Path, JavaClassSource> source : multimap.get(key)) {
printTableRow(source);
}
printTableFooter();
});
return 0;
}
private String getJavaDoc(JavaDocCapable<?> source) {
if (!source.hasJavaDoc()) {
return "<i>No Javadoc found</i>";
}
return source.getJavaDoc().getText();
}
private Multimap<String, Pair<Path, JavaClassSource>> collect() throws IOException {
MutableMultimap<String, Pair<Path, JavaClassSource>> multimap = Multimaps.mutable.sortedSet
.with(Comparator.comparing(o -> o.getTwo().getName()));
String line;
for (Path path : paths) {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith("BuildItem.java")) {
process(multimap, file);
}
return FileVisitResult.CONTINUE;
}
});
}
return multimap;
}
private void process(MutableMultimap<String, Pair<Path, JavaClassSource>> multimap, Path path) throws IOException {
JavaClassSource source = Roaster.parse(JavaClassSource.class, path.toFile());
// Ignore deprecated annotations and non-public classes
if (!source.hasAnnotation(Deprecated.class) && source.isPublic()) {
String name;
Path pom = findPom(path);
if (pom != null) {
name = Maven.readModel(pom).getName();
} else {
String pathString = path.toString();
int spiIdx = pathString.indexOf("/spi/src");
int runtimeIdx = pathString.indexOf("/runtime/src");
int deploymentIdx = pathString.indexOf("/deployment/src");
int idx = Math.max(Math.max(spiIdx, runtimeIdx), deploymentIdx);
int extensionsIdx = pathString.indexOf("extensions/");
int startIdx = 0;
if (extensionsIdx != -1) {
startIdx = extensionsIdx + 11;
}
if (idx == -1) {
name = pathString.substring(startIdx, pathString.indexOf("/", startIdx + 1));
} else {
name = pathString.substring(startIdx, idx);
}
}
// sanitize name
name = name.replace("Quarkus - ", "")
.replace(" - Deployment", "");
Pair<Path, JavaClassSource> pair = Tuples.pair(path, source);
multimap.put(name, pair);
}
}
private Path findPom(Path path) {
Path pom = null;
Path parent = path;
while (pom == null && (parent = parent.getParent()) != null) {
Path resolve = parent.resolve("pom.xml");
if (Files.exists(resolve)) {
pom = resolve;
}
}
return pom;
}
private Map<String, String> extractNames(Path root, Iterable<String> extensionDirs) throws IOException {
Map<String, String> names = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Yaml yaml = new Yaml();
for (String extension : extensionDirs) {
Path yamlPath = root.resolve("extensions/" + extension + "/runtime/src/main/resources/META-INF/quarkus-extension.yaml");
if (Files.exists(yamlPath)) {
try (InputStream is = Files.newInputStream(yamlPath)) {
Map<String, String> map = yaml.load(is);
names.put(extension, map.get("name"));
}
} else {
names.put(extension, extension);
}
}
return names;
}
private void printTableHeader(String title) {
out.println("== " + title);
out.println("[%header,cols=2*]");
out.println("|===");
out.println("|Class Name |Attributes ");
}
private void printTableRow(Pair<Path, JavaClassSource> pair) {
//TODO: Use tagged version?
Path root = Paths.get(".").toAbsolutePath().normalize();
String link = "https://github.com/quarkusio/quarkus/blob/main/" + root.relativize(pair.getOne().normalize());
JavaClassSource source = pair.getTwo();
String className = source.getQualifiedName();
String attributes = buildAttributes(source);
String description = getJavaDoc(source);
out.println("a| " + link + "[`" + className + "`, window=\"_blank\"] :: +++" +
javadocToHTML(description) + "+++");
out.println("a| " + attributes);
}
private String buildAttributes(JavaClassSource source) {
StringBuilder sb = new StringBuilder();
for (FieldSource<JavaClassSource> field : source.getFields()) {
if (field.isStatic()) {
continue;
}
sb.append("`" + field.getType().getName() + " " + field.getName() + "` :: ");
sb.append("+++" + javadocToHTML(getJavaDoc(field)) + "+++");
sb.append("\n");
}
return sb.length() == 0 ? "None" : sb.toString();
}
private void printTableFooter() {
out.println("|===");
}
private String javadocToHTML(String content) {
return content
.replaceAll("\\{?@see ", "<pre>")
.replaceAll("\\{?@code ", "<pre>")
.replaceAll("\\{?@link ", "<pre>")
.replaceAll(" ?}", "</pre>");
}
private String javadocToAsciidoc(String content) {
return content
.replaceAll("<p>", "\n")
.replaceAll("</p>", "\n")
.replaceAll("\\{?@see ", "```")
.replaceAll("\\{?@code ", "```")
.replaceAll("\\{?@link ", "```")
.replaceAll("<pre>", "```\n")
.replaceAll("</pre>", "\n```")
.replaceAll(" ?}", "```");
}
}