-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbootstrap.java
More file actions
executable file
·54 lines (42 loc) · 1.61 KB
/
bootstrap.java
File metadata and controls
executable file
·54 lines (42 loc) · 1.61 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
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 11+
//DESCRIPTION Bootstrap a jbang script to make it self-contained.
import static java.lang.System.*;
import static java.nio.file.Files.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
public class bootstrap {
public static void main(String... args) {
if(args.length != 1) {
err.println("Usage: jbang bootstrap.java <jbang script>");
exit(1);
}
var path = Path.of(args[0]);
if(notExists(path)) {
err.println("File " + path + " does not exist");
exit(1);
}
List<String> lines = null;
try {
lines = Files.readAllLines(path);
} catch(IOException e) {
err.println("Could not read from " + path);
exit(1);
}
String header = "///usr/bin/env bash -c \'command -v jbang >/dev/null 2>&1 || { echo \"Bootstrapping JBang...\" >&2; curl -Ls https://sh.jbang.dev | bash -s - app setup --quiet ; export PATH=\\\"$HOME/.jbang/bin:$PATH\\\"; }; exec jbang \"$0\" \"$@\"\' \"$0\" \"$@\"; exit $?";
if (lines.size() > 0 && lines.get(0).startsWith("///usr/bin/env jbang \"$0\" \"$@\" ; exit $?")) {
lines.set(0, header);
} else {
lines.add(0, header);
}
try {
write(path, lines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
} catch(IOException e) {
err.println("Could not write to " + path);
exit(1);
}
}
}