Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
146 lines (136 loc) · 5.8 KB
/
Copy pathMain.java
File metadata and controls
146 lines (136 loc) · 5.8 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
/*
* Copyright (c) 2023-2024 jwdeveloper [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package docs;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;
/**
* Entry point of the docs tool: {@code generate <repo root>} writes the API and schema reference pages under
* {@code docs/src/content/docs}; {@code check <repo root>} validates the authored pages.
*/
public final class Main {
static final String SOURCE_BASE_URL = "https://github.com/jwdeveloper/TikTokLiveJava/blob/master";
static final Path CONTENT_DIR = Path.of("docs", "src", "content", "docs");
static final Path PROTO_DIR = Path.of("API", "src", "main", "proto");
private static final Pattern PARENT_BLOCK = Pattern.compile("(?s)<parent>.*?</parent>");
private static final Pattern VERSION = Pattern.compile("<version>\\s*([^<\\s]+)\\s*</version>");
private Main() {}
public static void main(String[] args) {
System.exit(run(args, System.out, System.err));
}
/** Runs a command; exit code 0 on success, 1 on failure, 2 on usage errors. */
static int run(String[] args, PrintStream out, PrintStream err) {
if (args.length != 2) {
err.println("usage: docs-tools <generate|check> <repository root>");
return 2;
}
var repoRoot = Path.of(args[1]).toAbsolutePath().normalize();
switch (args[0]) {
case "generate":
try {
return generate(repoRoot, out);
} catch (RuntimeException e) {
err.println("generate failed: " + e.getMessage());
e.printStackTrace(err);
return 1;
}
case "check":
try {
return ContentCheck.run(repoRoot, out);
} catch (RuntimeException e) {
err.println("check failed: " + e.getMessage());
e.printStackTrace(err);
return 1;
}
default:
err.println("unknown command: " + args[0]);
return 2;
}
}
/** Wipes and rewrites {@code api/} and {@code schema/} under the content directory. */
static int generate(Path repoRoot, PrintStream out) {
var version = projectVersion(repoRoot.resolve("pom.xml"));
var units = JavaSources.load(repoRoot);
var protoFiles = protoFiles(repoRoot.resolve(PROTO_DIR));
var content = repoRoot.resolve(CONTENT_DIR);
var apiDir = content.resolve("api");
var schemaDir = content.resolve("schema");
wipe(apiDir);
wipe(schemaDir);
var schemaPages = SchemaPages.render(protoFiles, version, schemaDir);
var apiPages = ApiPages.render(units, version, SOURCE_BASE_URL, apiDir, SchemaPages.javaLinks(protoFiles));
out.println("Wrote " + apiPages.size() + " API pages to " + apiDir + " and " + schemaPages.size() + " schema pages to " + schemaDir);
return 0;
}
/** The project's own {@code <version>} in a pom.xml: the first one outside any {@code <parent>} block. */
static String projectVersion(Path pom) {
String text;
try {
text = Files.readString(pom);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
var m = VERSION.matcher(PARENT_BLOCK.matcher(text).replaceFirst(""));
if (!m.find()) {
throw new IllegalStateException("no <version> in " + pom);
}
return m.group(1);
}
private static List<ProtoFile> protoFiles(Path dir) {
List<Path> paths;
try (Stream<Path> files = Files.list(dir)) {
paths = files.filter(p -> p.getFileName().toString().endsWith(".proto")).sorted().toList();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (paths.isEmpty()) {
throw new IllegalStateException("no .proto files in " + dir);
}
var files = new ArrayList<ProtoFile>();
for (var path : paths) {
files.add(ProtoParser.parse(path));
}
return files;
}
private static void wipe(Path dir) {
try {
if (Files.exists(dir)) {
try (Stream<Path> walk = Files.walk(dir)) {
for (var path : walk.sorted(Comparator.reverseOrder()).toList()) {
Files.delete(path);
}
}
}
Files.createDirectories(dir);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}