-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbang
executable file
·158 lines (143 loc) · 5.29 KB
/
jbang
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
#!/usr/bin/env bash
#
# To run this script remotely type this in your shell
# (where <args>... are the arguments you want to pass to Jbang):
# curl -Ls https://sh.jbang.dev | bash -s - <args>...
#
# The Java version to install when it's not installed on the system yet
javaVersion=${JBANG_DEFAULT_JAVA_VERSION:-11}
absolute_path() {
# if the given path to the jbang launcher is absolute (i.e. it is either starting with / or a
# 'letter:/' when using gitbash on windows) it is returned unchanged, otherwise we construct an absolute path
[[ $1 = /* ]] || [[ $1 =~ ^[A-z]:/ ]] && echo "$1" || echo "$PWD/${1#./}"
}
resolve_symlink() {
if [[ $OSTYPE != darwin* ]]; then minusFarg="-f"; fi
sym_resolved=$(readlink ${minusFarg} $1)
if [[ -n $sym_resolved ]]; then
echo $sym_resolved
else
echo $1
fi
}
download() {
if [ -x "$(command -v curl)" ]; then
curl -sLf -H "Accept: application/gzip, application/octet-stream" -o "$2" $1
retval=$?
elif [ -x "$(command -v wget)" ]; then
wget -q --header="Accept: application/gzip, application/octet-stream" -O "$2" $1
retval=$?
else
echo "Error: curl or wget not found, please make sure one of them is installed" 1>&2
exit 1
fi
}
abs_jbang_path=/usr/local/Cellar/jbang/0.51.1/libexec/bin/jbang.jar
case "$(uname -s)" in
Linux*)
os=linux;;
Darwin*)
os=mac;;
CYGWIN*|MINGW*)
os=windows;;
*) echo "Unsupported Operating System: $(uname -s)" 1>&2; exit 1;;
esac
case "$(uname -m)" in
i?86)
arch=x32;;
x86_64|amd64)
arch=x64;;
aarch64)
arch=aarch64;;
*)
echo "Unsupported Architecture: $(uname -m)" 1>&2; exit 1;;
esac
## when mingw/git bash or cygwin fall out to just running the bat file.
if [[ $os == windows ]]; then
$(dirname $abs_jbang_path)/jbang.cmd $*
exit $?
fi
if [[ -z "$JBANG_DIR" ]]; then JBDIR="$HOME/.jbang"; else JBDIR="$JBANG_DIR"; fi
if [[ -z "$JBANG_CACHE_DIR" ]]; then TDIR="$JBDIR/cache"; else TDIR="$JBANG_CACHE_DIR"; fi
## resolve application jar path from script location
if [ -f "$(dirname $abs_jbang_path)/jbang.jar" ]; then
jarPath=$(dirname $abs_jbang_path)/jbang.jar
elif [ -f "$(dirname $abs_jbang_path)/.jbang/jbang.jar" ]; then
jarPath=$(dirname $abs_jbang_path)/.jbang/jbang.jar
else
if [ ! -f "$JBDIR/bin/jbang.jar" ]; then
echo "Downloading JBang..." 1>&2
mkdir -p "$TDIR/urls"
jburl="https://github.com/jbangdev/jbang/releases/latest/download/jbang.tar"
download $jburl "$TDIR/urls/jbang.tar"
if [ $retval -ne 0 ]; then echo "Error downloading JBang" 1>&2; exit $retval; fi
echo "Installing JBang..." 1>&2
rm -rf "$TDIR/urls/jbang"
tar xf "$TDIR/urls/jbang.tar" -C "$TDIR/urls"
if [ $retval -ne 0 ]; then echo "Error installing JBang" 1>&2; exit $retval; fi
rm -rf "$JBDIR/bin"
mv "$TDIR/urls/jbang/bin" "$JBDIR"
fi
eval "exec $JBDIR/bin/jbang $*"
fi
# Find/get a JDK
unset JAVA_EXEC
if [[ -n "$JAVA_HOME" ]]; then
# Determine if a (working) JDK is available in JAVA_HOME
if [ -x "$(command -v $JAVA_HOME/bin/javac)" ]; then
JAVA_EXEC="$JAVA_HOME/bin/java";
else
echo "JAVA_HOME is set but does not seem to point to a valid Java JDK" 1>&2
fi
fi
if [[ -z "$JAVA_EXEC" ]]; then
# Determine if a (working) JDK is available on the PATH
if [ -x "$(command -v javac)" ]; then
JAVA_EXEC="java";
elif [ -x "$JBDIR/currentjdk/bin/javac" ]; then
export JAVA_HOME="$JBDIR/currentjdk"
JAVA_EXEC="$JBDIR/currentjdk/bin/java";
else
export JAVA_HOME="$TDIR/jdks/$javaVersion"
JAVA_EXEC="$JAVA_HOME/bin/java"
# Check if we installed a JDK before
if [ ! -d "$TDIR/jdks/$javaVersion" ]; then
# If not, download and install it
mkdir -p "$TDIR/jdks"
echo "Downloading JDK $javaVersion. Be patient, this can take several minutes..." 1>&2
jdkurl="https://api.adoptopenjdk.net/v3/binary/latest/$javaVersion/ga/$os/$arch/jdk/hotspot/normal/adoptopenjdk"
download $jdkurl "$TDIR/bootstrap-jdk.tgz"
if [ $retval -ne 0 ]; then echo "Error downloading JDK" 1>&2; exit $retval; fi
echo "Installing JDK $javaVersion..." 1>&2
rm -rf "$TDIR/jdks/$javaVersion.tmp/"
mkdir -p "$TDIR/jdks/$javaVersion.tmp"
tar xf "$TDIR/bootstrap-jdk.tgz" -C "$TDIR/jdks/$javaVersion.tmp" --strip-components=1
retval=$?
if [[ $os == mac && $retval -eq 0 ]]; then
mv "$TDIR/jdks/$javaVersion.tmp/Contents/Home/"* "$TDIR/jdks/$javaVersion.tmp/"
retval=$?
fi
if [ $retval -ne 0 ]; then
# Check if the JDK was installed properly
javac -version > /dev/null 2>&1
retval=$?
fi
if [ $retval -ne 0 ]; then echo "Error installing JDK" 1>&2; exit $retval; fi
# Activate the downloaded JDK giving it its proper name
mv "$TDIR/jdks/$javaVersion.tmp" "$TDIR/jdks/$javaVersion"
# Set the current JDK
${JAVA_EXEC} -classpath ${jarPath} dev.jbang.Main jdk default $javaVersion
fi
fi
fi
## https://stackoverflow.com/questions/1668649/how-to-keep-quotes-in-bash-arguments
## attempt to ensure each argument keeps its original quoting
## run it using command substitution to have just the user process once jbang is done
output=$(CLICOLOR_FORCE=1 ${JAVA_EXEC} ${JBANG_JAVA_OPTIONS} -classpath ${jarPath} dev.jbang.Main "$@")
err=$?
if [ $err -eq 255 ]; then
eval "exec $output"
else
echo "$output"
exit $err
fi