-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathBuilder.java
More file actions
197 lines (175 loc) · 6.69 KB
/
Copy pathBuilder.java
File metadata and controls
197 lines (175 loc) · 6.69 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
/*
* Copyright 2015-2026 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
// default package
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
/**
* Platform-agnostic builder
*/
@SuppressWarnings({ "WeakerAccess", "SameParameterValue" })
class Builder {
private static final String TARGET_OPTION = "--target=";
private static final String EXCLUDE_OPTION = "--exclude=";
public static void main(String[] args) {
var target = determineTarget(args);
var excludedProjects = determineExcludedProjects(args);
var status = new Builder().build(target, excludedProjects);
if (status != 0) {
throw new AssertionError("Expected exit status of zero, but got: " + status);
}
}
int status = 0;
int build(Target target, Set<String> excludedProjects) {
System.out.printf("|%n| Building all samples (%s)...%n|%n", target);
run(".", "java", "--version");
if (target == Target.TEST) {
checkLicense("src/eclipse-public-license-2.0.java", ".java", ".kt", ".scala", ".groovy");
}
var antTarget = target == Target.TEST ? "test" : "compile";
var gradleTask = target == Target.TEST ? "test" : "testClasses";
var mavenLifecycle = target == Target.TEST ? "test" : "test-compile";
var bazelTarget = target == Target.TEST ? "test" : "build";
var sbtTask = target == Target.TEST ? "test" : "Test / compile";
var modularAction = target == Target.TEST ? "src/build/Build.java" : "src/build/Compile.java";
// jupiter-starter
if (!isWindows()) { // TODO https://github.com/junit-team/junit-examples/issues/66
runProject(excludedProjects, "junit-jupiter-starter-ant", "build.sh", "clean", antTarget);
}
runProject(excludedProjects, "junit-jupiter-starter-gradle", "gradlew", gradleTask);
runProject(excludedProjects, "junit-jupiter-starter-gradle-groovy", "gradlew", gradleTask);
runProject(excludedProjects, "junit-jupiter-starter-gradle-kotlin", "gradlew", gradleTask);
runProject(excludedProjects, "junit-jupiter-starter-maven", "mvnw", "--batch-mode", "clean", mavenLifecycle);
runProject(excludedProjects, "junit-jupiter-starter-maven-kotlin", "mvnw", "--batch-mode", "clean", mavenLifecycle);
runProject(excludedProjects, "junit-jupiter-starter-bazel", "bazel", bazelTarget, "//...");
runProject(excludedProjects, "junit-jupiter-starter-sbt", "sbt", sbtTask);
// jupiter-extensions
runProject(excludedProjects, "junit-jupiter-extensions", "gradlew", gradleTask);
// migration
runProject(excludedProjects, "junit-migration-gradle", "gradlew", gradleTask);
runProject(excludedProjects, "junit-migration-maven", "mvnw", "--batch-mode", "clean", mavenLifecycle);
runProject(excludedProjects, "junit-multiple-engines", "gradlew", gradleTask);
// modular
runProject(excludedProjects, "junit-modular-world", "java", modularAction);
// source launcher
runProject(excludedProjects, "junit-source-launcher", "java", "lib/DownloadRequiredModules.java");
runProject(excludedProjects, "junit-source-launcher",
"java",
"--module-path", "lib",
"--add-modules", "org.junit.start",
"src/HelloTests.java");
System.out.printf("%n%n%n|%n| Done. Build exits with status = %d.%n|%n", status);
return status;
}
private static Target determineTarget(String[] args) {
for (var arg : args) {
if (arg.startsWith(TARGET_OPTION)) {
return Target.valueOf(arg.substring(TARGET_OPTION.length()).toUpperCase(Locale.ROOT));
}
}
return Target.TEST;
}
private static Set<String> determineExcludedProjects(String[] args) {
Set<String> excludedProjects = new HashSet<>();
for (var arg : args) {
if (arg.startsWith(EXCLUDE_OPTION)) {
excludedProjects.addAll(Set.of(arg.substring(EXCLUDE_OPTION.length()).split(",")));
}
}
return Set.copyOf(excludedProjects);
}
void runProject(Set<String> excludedProjects, String project, String executable, String... args) {
if (excludedProjects.contains(project)) {
System.out.printf("%n%n%n|%n| %s is excluded.%n|%n", project);
return;
}
run(project, executable, args);
}
void run(String directory, String executable, String... args) {
if (status != 0) {
return;
}
System.out.printf("%n%n%n|%n| %s%n|%n", directory);
System.out.printf("| %s %s%n|%n", executable, String.join(" ", args));
var path = Paths.get(directory);
var isWindows = isWindows();
if (!isWindows) {
if (Files.isExecutable(path.resolve(executable))) {
executable = "./" + executable;
}
}
var processBuilder = new ProcessBuilder(isWindows ? "cmd.exe" : executable);
if (isWindows) {
processBuilder.command().add("/C");
processBuilder.command().add(executable);
}
Arrays.stream(args).forEach(processBuilder.command()::add);
processBuilder.directory(path.toFile());
processBuilder.redirectErrorStream(true);
try {
var process = processBuilder.start();
process.getInputStream().transferTo(System.out);
status = process.waitFor();
} catch (Exception exception) {
System.out.printf("%n%n%n| %s failed to build!%n", directory);
exception.printStackTrace(System.err);
status = 1;
}
}
boolean isWindows() {
return System.getProperty("os.name").toLowerCase(Locale.ROOT).startsWith("win");
}
void checkLicense(String blueprint, String... extensions) {
if (status != 0) {
return;
}
System.out.printf("%n%n%n|%n| check license: %s%n|%n", blueprint);
try {
var expected = Files.readAllLines(Paths.get(blueprint));
var errors = 0;
try (var paths = Files.walk(Paths.get("."))
.filter(path -> Arrays.stream(extensions).anyMatch(extension -> path.getFileName().toString().endsWith(extension)))
.filter(path -> !path.getFileName().toString().equals("MavenWrapperDownloader.java"))) {
for (var path : paths.toList()) {
if (checkLicense(path, expected)) {
continue;
}
System.out.printf("| %s%n", path);
errors++;
}
}
if (errors > 0) {
System.out.printf("| %d file(s) with no or false license.%n", errors);
status = 1;
}
}
catch (Exception exception) {
System.out.printf("%n%n%n| License `%s` check failed!%n", blueprint);
exception.printStackTrace(System.err);
status = 1;
}
}
boolean checkLicense(Path path, List<String> expected) throws Exception {
var actual = Files.readAllLines(path);
if (actual.size() >= expected.size()) {
actual = actual.subList(0, expected.size());
return actual.equals(expected);
}
return false;
}
enum Target {
COMPILE, TEST
}
}