Repository navigation
Expand file tree
/
Copy pathGenerateSteps.java
More file actions
424 lines (395 loc) · 20.3 KB
/
Copy pathGenerateSteps.java
File metadata and controls
424 lines (395 loc) · 20.3 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.zip.ZipInputStream;
/**
* Generates the application of the Vaadin tutorial as it is at the end of each step, from the code
* listings in vaadin/docs.
* <p>
* Usage: {@code java GenerateSteps.java <docs-dir> <output-dir>}
* <p>
* {@code <docs-dir>} is a checkout of vaadin/docs. The first step, Set Up Project, downloads the
* project skeleton that the step links to, and adds the dependencies and SQL files that the step
* lists. Every later step starts from the previous one, adds the dependencies that the step lists,
* and writes the files of its "Complete Code for This Step" section. Each step goes in
* {@code <output-dir>/<page>}, where {@code <page>} is the name of the step's page, such as
* {@code list-data}. The file {@code <output-dir>/steps.tsv} lists the pages and their titles, in
* the order of the tutorial.
*/
public class GenerateSteps {
static final String TUTORIAL_DIR = "articles/getting-started/tutorial";
static final String VERSION_FILE = "articles/_vaadin-version.adoc";
static final String DOCS_URL = "https://vaadin.com/docs/latest/getting-started/tutorial";
static final String REPOSITORY_URL = "https://github.com/vaadin/tutorial";
static final Pattern ATTRIBUTE = Pattern.compile("^:([\\w-]+):\\s*(.*)$");
static final Pattern ATTRIBUTE_REFERENCE = Pattern.compile("\\{([\\w-]+)}");
static final Pattern SKELETON_LINK = Pattern.compile("link:(https://start\\.vaadin\\.com/skeleton\\?[^\\[]+)\\[");
static final Pattern SQL_LINK = Pattern.compile("link:\\{articles}/([\\w/-]+\\.sql)\\[");
static final Pattern START_PROJECT_COMMENT = Pattern.compile("(?m)^[ \\t]*<!-- Project from https://start\\.vaadin\\.com/\\S* -->\\R");
static final Pattern LISTING_TITLE = Pattern.compile("^\\.([\\w.-]+\\.\\w+)$");
static final Pattern SOURCE_BLOCK = Pattern.compile("^\\[source,(\\w+).*]$");
static final Pattern CALLOUT = Pattern.compile("(?m)[ \\t]*(?://|#) <\\d+>[ \\t]*$|[ \\t]*/\\* <\\d+> \\*/[ \\t]*$|[ \\t]*<!-- <\\d+> -->[ \\t]*$");
static final Pattern DEPENDENCY = Pattern.compile("(?s)<dependency>.*?</dependency>");
static final Pattern GROUP_ID = Pattern.compile("<groupId>([^<]+)</groupId>");
static final Pattern ARTIFACT_ID = Pattern.compile("<artifactId>([^<]+)</artifactId>");
static final Pattern PACKAGE = Pattern.compile("(?m)^package ([\\w.]+);");
// Packages of the test-scoped dependencies. A class that imports them only compiles in the test sources.
static final Pattern TEST_IMPORT = Pattern.compile("(?m)^import (static )?(org\\.junit|org\\.mockito|org\\.assertj|org\\.hamcrest"
+ "|org\\.springframework\\.boot\\.test|org\\.springframework\\.test|com\\.vaadin\\.browserless|com\\.vaadin\\.testbench)\\.");
// The Set Up Project step tells the reader to save the SQL files in this directory.
static final String SQL_DIR = "src/main/resources";
// Files linked with {articles} are served from this directory of vaadin/docs.
static final String DOCS_RESOURCES_DIR = "src/main/resources/META-INF/resources";
record Step(String page, String title, int order, List<String> lines) {
String url() {
return DOCS_URL + "/" + page;
}
String branch() {
return "step/" + page;
}
}
record Listing(String fileName, String language, String code) {
}
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: java GenerateSteps.java <docs-dir> <output-dir>");
System.exit(2);
}
var docs = Path.of(args[0]);
var output = Path.of(args[1]);
if (Files.exists(output)) {
try (var files = Files.list(output)) {
if (files.findAny().isPresent()) {
throw new IllegalArgumentException(output + " is not empty");
}
}
}
var attributes = readAttributes(docs.resolve(VERSION_FILE));
var steps = readSteps(docs.resolve(TUTORIAL_DIR));
var tsv = new StringBuilder();
Path previous = null;
for (int i = 0; i < steps.size(); i++) {
var step = steps.get(i);
var next = i + 1 < steps.size() ? steps.get(i + 1) : null;
var project = output.resolve(step.page());
System.out.println("Generating " + step.title() + " in " + project);
if (previous == null) {
setUpProject(step, project, docs, attributes);
} else {
copyProject(previous, project);
addDependencies(step, project);
writeCompleteCode(step, project);
}
writeReadme(step, next, project, attributes);
tsv.append(step.page()).append('\t').append(step.title()).append('\n');
previous = project;
}
Files.writeString(output.resolve("steps.tsv"), tsv);
}
static Map<String, String> readAttributes(Path file) throws IOException {
var attributes = new HashMap<String, String>();
for (var line : Files.readAllLines(file)) {
var matcher = ATTRIBUTE.matcher(line);
if (matcher.matches()) {
attributes.put(matcher.group(1), matcher.group(2).trim());
}
}
return attributes;
}
static List<Step> readSteps(Path tutorial) throws IOException {
var steps = new ArrayList<Step>();
try (var files = Files.list(tutorial)) {
for (var file : files.filter(f -> f.toString().endsWith(".adoc")).toList()) {
var page = file.getFileName().toString().replaceFirst("\\.adoc$", "");
if (page.equals("index")) {
continue;
}
var lines = Files.readAllLines(file);
var frontMatter = readFrontMatter(lines, file);
steps.add(new Step(page, frontMatter.get("title"), Integer.parseInt(frontMatter.get("order")), lines));
}
}
steps.sort(Comparator.comparingInt(Step::order));
return steps;
}
static Map<String, String> readFrontMatter(List<String> lines, Path file) {
if (lines.isEmpty() || !lines.getFirst().equals("---")) {
throw new IllegalStateException(file + " has no front matter");
}
var frontMatter = new HashMap<String, String>();
for (var line : lines.subList(1, lines.size())) {
if (line.equals("---")) {
if (!frontMatter.containsKey("title") || !frontMatter.containsKey("order")) {
throw new IllegalStateException(file + " has no title or order");
}
return frontMatter;
}
var colon = line.indexOf(':');
if (colon > 0) {
frontMatter.put(line.substring(0, colon).trim(), line.substring(colon + 1).trim());
}
}
throw new IllegalStateException(file + " has unterminated front matter");
}
// Set Up Project
static void setUpProject(Step step, Path project, Path docs, Map<String, String> attributes) throws Exception {
var text = String.join("\n", step.lines());
var link = SKELETON_LINK.matcher(text);
if (!link.find()) {
throw new IllegalStateException(step.page() + " doesn't link to a project skeleton");
}
var url = resolveAttributes(link.group(1), attributes);
System.out.println(" Downloading " + url);
downloadSkeleton(URI.create(url), project);
var pom = project.resolve("pom.xml");
Files.writeString(pom, START_PROJECT_COMMENT.matcher(Files.readString(pom)).replaceAll(""));
addDependencies(step, project);
var sqlFiles = SQL_LINK.matcher(text);
int count = 0;
while (sqlFiles.find()) {
var source = docs.resolve(DOCS_RESOURCES_DIR).resolve(sqlFiles.group(1));
var target = project.resolve(SQL_DIR).resolve(source.getFileName().toString());
System.out.println(" Copying " + source.getFileName() + " to " + project.relativize(target));
Files.copy(source, target);
count++;
}
if (count == 0) {
throw new IllegalStateException(step.page() + " doesn't link to any SQL files");
}
}
static String resolveAttributes(String text, Map<String, String> attributes) {
return ATTRIBUTE_REFERENCE.matcher(text).replaceAll(match -> {
var value = attributes.get(match.group(1));
if (value == null) {
throw new IllegalStateException("Unknown attribute " + match.group());
}
return Matcher.quoteReplacement(value);
});
}
static void downloadSkeleton(URI uri, Path project) throws Exception {
var client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();
var response = client.send(HttpRequest.newBuilder(uri).build(), HttpResponse.BodyHandlers.ofInputStream());
if (response.statusCode() != 200) {
throw new IOException("Downloading " + uri + " failed with status " + response.statusCode());
}
Files.createDirectories(project);
try (var zip = new ZipInputStream(response.body())) {
for (var entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
// The skeleton is in a directory of its own, and comes with a Git repository.
var name = entry.getName().replaceFirst("^[^/]+/", "");
if (name.isEmpty() || name.equals(".git/") || name.startsWith(".git/")) {
continue;
}
var target = project.resolve(name).normalize();
if (!target.startsWith(project)) {
throw new IOException("Unexpected entry " + entry.getName());
}
if (entry.isDirectory()) {
Files.createDirectories(target);
} else {
Files.createDirectories(target.getParent());
Files.copy(zip, target);
makeScriptExecutable(target);
}
}
}
}
// Java's zip support doesn't expose file permissions. The Maven wrapper is the only script in the skeleton.
static void makeScriptExecutable(Path file) throws IOException {
try (InputStream in = Files.newInputStream(file)) {
var start = in.readNBytes(2);
if (start.length == 2 && start[0] == '#' && start[1] == '!') {
if (!file.toFile().setExecutable(true, false)) {
throw new IOException("Can't make " + file + " executable");
}
}
}
}
// Later steps
static void copyProject(Path from, Path to) throws IOException {
try (Stream<Path> files = Files.walk(from)) {
for (var source : files.toList()) {
var target = to.resolve(from.relativize(source).toString());
if (Files.isDirectory(source)) {
Files.createDirectories(target);
} else {
Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES);
}
}
}
}
// Adds the dependencies of the step's XML listings to the project's dependencies, unless the
// project already has them. They go after the last dependency that isn't scoped for tests.
static void addDependencies(Step step, Path project) throws IOException {
var pomFile = project.resolve("pom.xml");
var pom = Files.readString(pomFile);
for (var listing : listings(step.lines(), false)) {
if (!listing.language().equals("xml")) {
continue;
}
var dependencies = DEPENDENCY.matcher(listing.code());
while (dependencies.find()) {
var dependency = dependencies.group();
var id = dependencyId(dependency);
int start = pom.indexOf("<dependencies>", Math.max(0, pom.indexOf("</dependencyManagement>")));
int end = pom.indexOf("</dependencies>", start);
if (start < 0 || end < 0) {
throw new IllegalStateException("pom.xml has no dependencies");
}
var existing = DEPENDENCY.matcher(pom).region(start, end);
int insertAt = -1;
boolean found = false;
while (existing.find()) {
found |= dependencyId(existing.group()).equals(id);
if (!existing.group().contains("<scope>test</scope>")) {
insertAt = existing.end();
}
}
if (found) {
continue;
}
if (insertAt < 0) {
insertAt = start + "<dependencies>".length();
}
var lineStart = pom.lastIndexOf('\n', start) + 1;
var indent = pom.substring(lineStart, start) + " ";
var lines = new StringBuilder();
for (var line : dependency.split("\n")) {
lines.append('\n').append(indent).append(line);
}
System.out.println(" Adding dependency " + id);
pom = pom.substring(0, insertAt) + lines + pom.substring(insertAt);
}
}
Files.writeString(pomFile, pom);
}
static String dependencyId(String dependency) {
var groupId = GROUP_ID.matcher(dependency);
var artifactId = ARTIFACT_ID.matcher(dependency);
if (!groupId.find() || !artifactId.find()) {
throw new IllegalStateException("Dependency without groupId or artifactId: " + dependency);
}
return groupId.group(1) + ":" + artifactId.group(1);
}
static void writeCompleteCode(Step step, Path project) throws IOException {
var listings = listings(completeCodeSection(step), true);
if (listings.isEmpty()) {
throw new IllegalStateException(step.page() + " has no complete code");
}
for (var listing : listings) {
var target = targetFile(listing, project);
System.out.println(" Writing " + project.relativize(target));
Files.createDirectories(target.getParent());
Files.writeString(target, listing.code());
}
}
static List<String> completeCodeSection(Step step) {
var lines = step.lines();
int start = lines.indexOf("== Complete Code for This Step");
if (start < 0) {
throw new IllegalStateException(step.page() + " has no Complete Code for This Step section");
}
int end = start + 1;
while (end < lines.size() && !lines.get(end).startsWith("== ")) {
end++;
}
return lines.subList(start + 1, end);
}
// Returns the source blocks in the lines. If titled is true, every block must have a title
// that names the file, such as ".ProductCatalogView.java".
static List<Listing> listings(List<String> lines, boolean titled) {
var listings = new ArrayList<Listing>();
String title = null;
for (int i = 0; i < lines.size(); i++) {
var line = lines.get(i);
var titleMatcher = LISTING_TITLE.matcher(line);
if (titleMatcher.matches()) {
title = titleMatcher.group(1);
continue;
}
var source = SOURCE_BLOCK.matcher(line);
if (!source.matches()) {
continue;
}
if (i + 1 >= lines.size() || !lines.get(i + 1).equals("----")) {
throw new IllegalStateException("Source block without ---- delimiter: " + line);
}
int end = lines.subList(i + 2, lines.size()).indexOf("----");
if (end < 0) {
throw new IllegalStateException("Unterminated source block after " + title);
}
var code = String.join("\n", lines.subList(i + 2, i + 2 + end)) + "\n";
if (titled && title == null) {
throw new IllegalStateException("Source block without a file name: " + code);
}
listings.add(new Listing(title, source.group(1), CALLOUT.matcher(code).replaceAll("")));
title = null;
i += end + 2;
}
return listings;
}
// A Java class goes in the directory of its package, in the test sources if it uses a test
// library, such as a test or the browserless application context of the tests. Any other file
// replaces the only file of the same name in the project.
static Path targetFile(Listing listing, Path project) throws IOException {
if (listing.fileName().endsWith(".java")) {
var className = listing.fileName().replaceFirst("\\.java$", "");
if (!Pattern.compile("\\b(class|interface|record|enum)\\s+" + className + "\\b").matcher(listing.code()).find()) {
throw new IllegalStateException(listing.fileName() + " doesn't declare " + className);
}
var packageName = PACKAGE.matcher(listing.code());
if (!packageName.find()) {
throw new IllegalStateException(listing.fileName() + " has no package");
}
var sources = TEST_IMPORT.matcher(listing.code()).find() ? "src/test/java" : "src/main/java";
return project.resolve(sources).resolve(packageName.group(1).replace('.', '/')).resolve(listing.fileName());
}
try (Stream<Path> files = Files.walk(project)) {
var matches = files.filter(f -> f.getFileName().toString().equals(listing.fileName())).toList();
if (matches.size() != 1) {
throw new IllegalStateException("Expected one " + listing.fileName() + " in the project, found " + matches);
}
return matches.getFirst();
}
}
static void writeReadme(Step step, Step next, Path project, Map<String, String> attributes) throws IOException {
var readme = new StringBuilder();
readme.append("# Vaadin Tutorial: ").append(step.title()).append("\n\n");
readme.append("This branch holds the application of the [Vaadin tutorial](").append(DOCS_URL)
.append(") as it is at the end of the [").append(step.title()).append("](").append(step.url())
.append(") step.");
if (next != null) {
readme.append(" To continue the tutorial from here, follow the [").append(next.title()).append("](")
.append(next.url()).append(") step. The [`").append(next.branch()).append("`](")
.append(REPOSITORY_URL).append("/tree/").append(next.branch())
.append(") branch holds the result.\n\n");
} else {
readme.append(" This is the last step, so this branch holds the finished application. The [`main`](")
.append(REPOSITORY_URL).append(") branch has the same code.\n\n");
}
readme.append("To run the application, you need a Java Development Kit (JDK), version ")
.append(attributes.getOrDefault("vaadin-start-java-version", "21"))
.append(" or newer. Start the application with `./mvnw` (`mvnw` on Windows), and open http://localhost:8080 in your browser. ")
.append("To build the application for production and run its tests, run `./mvnw verify`.\n\n");
readme.append("This branch is generated from the code listings of the tutorial, and is replaced when they change. ")
.append("To fix the code, fix the tutorial in [vaadin/docs](https://github.com/vaadin/docs). ")
.append("The [README of the `main` branch](").append(REPOSITORY_URL)
.append("#step-branches) explains how the branches are generated.\n");
Files.writeString(project.resolve("README.md"), readme);
}
}