forked from binkley/modern-java-practices
-
Notifications
You must be signed in to change notification settings - Fork 1
/
batect
executable file
·205 lines (163 loc) · 6.92 KB
/
batect
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
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
{
set -euo pipefail
# This file is part of Batect.
# Do not modify this file. It will be overwritten next time you upgrade Batect.
# You should commit this file to version control alongside the rest of your project. It should not be installed globally.
# For more information, visit https://github.com/batect/batect.
VERSION="0.85.0"
CHECKSUM="${BATECT_DOWNLOAD_CHECKSUM:-901ed73295be75d295cec1d06315f7026b36ccb1666660b8af432cfbbc7feae8}"
DOWNLOAD_URL_ROOT=${BATECT_DOWNLOAD_URL_ROOT:-"https://updates.batect.dev/v1/files"}
DOWNLOAD_URL=${BATECT_DOWNLOAD_URL:-"$DOWNLOAD_URL_ROOT/$VERSION/batect-$VERSION.jar"}
QUIET_DOWNLOAD=${BATECT_QUIET_DOWNLOAD:-false}
BATECT_WRAPPER_CACHE_DIR=${BATECT_CACHE_DIR:-"$HOME/.batect/cache"}
VERSION_CACHE_DIR="$BATECT_WRAPPER_CACHE_DIR/$VERSION"
JAR_PATH="$VERSION_CACHE_DIR/batect-$VERSION.jar"
BATECT_WRAPPER_DID_DOWNLOAD=false
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
function main() {
if ! haveVersionCachedLocally; then
download
BATECT_WRAPPER_DID_DOWNLOAD=true
fi
checkChecksum
runApplication "$@"
}
function haveVersionCachedLocally() {
[ -f "$JAR_PATH" ]
}
function download() {
checkForCurl
mkdir -p "$VERSION_CACHE_DIR"
temp_file=$(mktemp)
if [[ $QUIET_DOWNLOAD == 'true' ]]; then
curl --silent --fail --show-error --location --output "$temp_file" --retry 3 --retry-connrefused "$DOWNLOAD_URL"
else
echo "Downloading Batect version $VERSION from $DOWNLOAD_URL..."
curl -# --fail --show-error --location --output "$temp_file" --retry 3 --retry-connrefused "$DOWNLOAD_URL"
fi
mv "$temp_file" "$JAR_PATH"
}
function checkChecksum() {
local_checksum=$(getLocalChecksum)
if [[ "$local_checksum" != "$CHECKSUM" ]]; then
echo "The downloaded version of Batect does not have the expected checksum. Delete '$JAR_PATH' and then re-run this script to download it again."
exit 1
fi
}
function getLocalChecksum() {
if [[ "$(uname)" == "Darwin" ]]; then
shasum -a 256 "$JAR_PATH" | cut -d' ' -f1
else
sha256sum "$JAR_PATH" | cut -d' ' -f1
fi
}
function runApplication() {
java_path=$(getPathToJava)
checkForJava "$java_path"
java_version_info=$(getJavaVersionInfo "$java_path")
checkJavaVersion "$java_version_info"
java_version=$(extractJavaVersion "$java_version_info")
java_version_major=$(extractJavaMajorVersion "$java_version")
if (( java_version_major >= 9 )); then
JAVA_OPTS=(--add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED)
else
JAVA_OPTS=()
fi
if [[ "$(uname -o 2>&1)" == "Msys" ]] && hash winpty 2>/dev/null && [ -t /dev/stdin ]; then
GIT_BASH_PTY_WORKAROUND=(winpty)
else
GIT_BASH_PTY_WORKAROUND=()
fi
BATECT_WRAPPER_SCRIPT_DIR="$SCRIPT_PATH" \
BATECT_WRAPPER_CACHE_DIR="$BATECT_WRAPPER_CACHE_DIR" \
BATECT_WRAPPER_DID_DOWNLOAD="$BATECT_WRAPPER_DID_DOWNLOAD" \
HOSTNAME="$HOSTNAME" \
exec \
${GIT_BASH_PTY_WORKAROUND[@]+"${GIT_BASH_PTY_WORKAROUND[@]}"} \
"$java_path" \
-Djava.net.useSystemProxies=true \
${JAVA_OPTS[@]+"${JAVA_OPTS[@]}"} \
-jar "$JAR_PATH" \
"$@"
}
function checkForCurl() {
if ! hash curl 2>/dev/null; then
echo "curl is not installed or not on your PATH. Please install it and try again." >&2
exit 1
fi
}
function getPathToJava() {
if useJavaHome; then
echo "$JAVA_HOME/bin/java"
else
echo "java"
fi
}
function useJavaHome() {
test -n "${JAVA_HOME+x}"
}
function checkForJava() {
local java_path="$1"
if ! hash "$java_path" 2>/dev/null; then
showJavaNotInstalledError
fi
}
function showJavaNotInstalledError() {
if useJavaHome; then
echo "JAVA_HOME is set to '$JAVA_HOME', but there is no Java executable at '$JAVA_HOME/bin/java'." >&2
else
echo "Java is not installed or not on your PATH. Please install it and try again." >&2
fi
exit 1
}
function checkJavaVersion() {
java_version_info="$1"
java_version=$(extractJavaVersion "$java_version_info")
java_version_major=$(extractJavaMajorVersion "$java_version")
java_version_minor=$(extractJavaMinorVersion "$java_version")
if (( java_version_major < 1 || ( java_version_major == 1 && java_version_minor <= 7 ) )); then
if useJavaHome; then
echo "The version of Java that is available in JAVA_HOME is version $java_version, but version 1.8 or greater is required." >&2
echo "If you have a newer version of Java installed, please make sure JAVA_HOME is set correctly." >&2
echo "JAVA_HOME takes precedence over any versions of Java available on your PATH." >&2
else
echo "The version of Java that is available on your PATH is version $java_version, but version 1.8 or greater is required." >&2
echo "If you have a newer version of Java installed, please make sure your PATH is set correctly." >&2
fi
exit 1
fi
if ! javaIs64Bit "$java_version_info"; then
if useJavaHome; then
echo "The version of Java that is available in JAVA_HOME is a 32-bit version, but Batect requires a 64-bit Java runtime." >&2
echo "If you have a 64-bit version of Java installed, please make sure JAVA_HOME is set correctly." >&2
echo "JAVA_HOME takes precedence over any versions of Java available on your PATH." >&2
else
echo "The version of Java that is available on your PATH is a 32-bit version, but Batect requires a 64-bit Java runtime." >&2
echo "If you have a 64-bit version of Java installed, please make sure your PATH is set correctly." >&2
fi
exit 1
fi
}
function getJavaVersionInfo() {
local java_path="$1"
"$java_path" -version 2>&1 || showJavaNotInstalledError
}
function extractJavaVersion() {
echo "$1" | grep version | sed -En ';s/.* version "([0-9]+)(\.([0-9]+))?.*".*/\1.\3/p;'
}
function extractJavaMajorVersion() {
java_version=$1
echo "${java_version%.*}"
}
function extractJavaMinorVersion() {
java_version=$1
java_version_minor="${java_version#*.}"
echo "${java_version_minor:-0}"
}
function javaIs64Bit() {
echo "$1" | grep -q '64-[Bb]it'
}
main "$@"
exit $?
}