-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.java
More file actions
executable file
·121 lines (102 loc) · 4.24 KB
/
build.java
File metadata and controls
executable file
·121 lines (102 loc) · 4.24 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
///usr/bin/env java "$0" "$@" ; exit $?
void main(String[] args) throws Exception {
String version = args.length > 0 ? args[0] : "0.0.1-SNAPSHOT";
String timestamp = args.length > 1 ? args[1] : ZonedDateTime.now()
.withNano(0).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
String runtime = args.length > 2 ? args[2] : "local";
Path jmodsPath = args.length > 3 ? Path.of(args[3]) : Path.of(System.getProperty("java.home")).resolve("jmods");
ToolProvider javac = ToolProvider.findFirst("javac").orElseThrow();
ToolProvider jar = ToolProvider.findFirst("jar").orElseThrow();
ToolProvider jlink = ToolProvider.findFirst("jlink").orElseThrow();
Path outDir = Path.of("out");
del(outDir);
Path libDir = Path.of("lib");
Path modulesSrc = Path.of("modules");
Path modulesOut = outDir.resolve("modules");
Path jars = outDir.resolve("jars");
Path chariot = libDir.resolve("chariot.jar");
Path metaInf = outDir.resolve("META-INF");
for (Path dir : List.of(libDir, metaInf)) Files.createDirectories(dir);
Files.copy(Path.of("LICENSE"), metaInf.resolve("LICENSE"));
if (! Files.exists(chariot))
Files.copy(URI.create(
"https://repo1.maven.org/maven2/io/github/tors42/chariot/0.2.10/chariot-0.2.10.jar"
).toURL().openStream(), chariot);
List<String> modules = List.of(
"tba.api",
"tba",
"app",
"teambattle.api",
"teambattle",
"teambattle.replay",
"teambattle.http",
"example.language.french");
for (String module : modules) {
String filenamePrefix = module + "-" + version;
run(javac,
"--enable-preview",
"--release", "26",
"--module-path", libDir,
"--module-source-path", modulesSrc.resolve("*", "src"),
"--module", module,
"-d", modulesOut
);
Path resourcesDir = switch(modulesSrc.resolve(module).resolve("resources")) {
case Path path when Files.exists(path) -> path;
case Path _ -> Files.createDirectories(modulesOut.resolve(module).resolve("resources"));
};
run(jar,
"--create",
"--date", timestamp,
"--module-version", version,
"--no-manifest",
"--file", jars.resolve(filenamePrefix + ".jar"),
"-C", outDir, "META-INF",
"-C", resourcesDir, ".",
"-C", modulesOut.resolve(module), "."
);
}
createRuntime(
jlink,
List.of(jmodsPath, jars, libDir),
outDir.resolve("runtime").resolve(runtime),
modules,
"tba=app/app.App");
}
void createRuntime(ToolProvider jlink, List<Path> modulePath, Path output, List<String> modules, String launcher) {
run(jlink,
"--add-options", " --enable-preview",
"--compress", "zip-9",
"--no-man-pages",
"--no-header-files",
"--module-path", String.join(File.pathSeparator, modulePath.stream().map(Path::toString).toList()),
"--add-modules", String.join(",", modules),
"--launcher", launcher,
"--output", output
);
}
void del(Path dir) {
if (dir.toFile().exists()) {
try (Stream<Path> files = Files.walk(dir)) {
files.sorted(Collections.reverseOrder()).map(Path::toFile).forEach(File::delete);
} catch (Exception e) { throw new RuntimeException(e); }
}
}
void run(ToolProvider tool, Object... args) {
String[] stringArgs = Arrays.stream(args).map(Object::toString).toArray(String[]::new);
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
int exitCode = tool.run(new PrintWriter(out), new PrintWriter(err), stringArgs);
if (exitCode != 0) {
out.flush();
err.flush();
System.err.format("""
%s exited with code %d
args: %s
stdout: %s
stderr: %s%n""",
tool, exitCode, String.join(" ", stringArgs),
out, err);
System.exit(exitCode);
}
}