forked from sormuras/bach
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.java
More file actions
84 lines (77 loc) · 2.58 KB
/
bootstrap.java
File metadata and controls
84 lines (77 loc) · 2.58 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
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Comparator;
import java.util.spi.ToolProvider;
/** Compile and package Bach's main module. */
class bootstrap {
public static void main(String... args) throws Exception {
var module = "com.github.sormuras.bach";
var version = Files.readString(Path.of("VERSION")) + "+BOOTSTRAP";
var bin = Path.of(".bach/bin");
delete(bin, "com.github.sormuras.bach@*.jar");
deleteWorkspaces();
var classes = Path.of(".bach/workspace/.bootstrap");
run(
"javac",
"--module=" + module,
"--module-version=" + version + "+" + Instant.now(),
"--module-source-path=./*/main/java",
"-g",
"-parameters",
"-Werror",
"-Xlint",
"-encoding",
"UTF-8",
"-d",
classes.toString());
var file = module + "@" + version + ".jar";
var jar = Files.createDirectories(bin).resolve(file);
run(
"jar",
"--create",
"--file=" + jar,
"--main-class=" + module + ".Main",
"-C",
classes.resolve(module).toString(),
".",
"-C",
Path.of(module).resolve("main/java").toString(),
".");
System.out.println("<< " + Files.size(jar));
}
static void delete(Path directory, String glob) throws Exception {
try (var stream = Files.newDirectoryStream(directory, glob)) {
for (var path : stream) {
Files.delete(path);
System.out.println(">> " + path + " deleted");
}
}
}
static void deleteWorkspaces() throws Exception {
try (var stream = Files.walk(Path.of(""))) {
var selected =
stream
.filter(path -> path.toUri().toString().contains(".bach/workspace"))
.sorted(Comparator.reverseOrder())
.toArray(Path[]::new);
for (var path : selected) {
Files.delete(path);
// also delete non-empty ".bach" directory
if (path.endsWith(Path.of(".bach", "workspace")))
try {
Files.deleteIfExists(path.getParent());
} catch (DirectoryNotEmptyException ignore) {
}
}
System.out.println(">> " + selected.length + " workspace files deleted");
}
}
static void run(String name, String... args) {
System.out.println(">> " + name + " " + String.join(" ", args));
var tool = ToolProvider.findFirst(name).orElseThrow();
var code = tool.run(System.out, System.err, args);
if (code != 0) throw new Error("Non-zero exit code: " + code);
}
}