-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathamper
More file actions
executable file
·329 lines (280 loc) · 12.5 KB
/
amper
File metadata and controls
executable file
·329 lines (280 loc) · 12.5 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/sh
#
# Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
#
# Possible environment variables:
# AMPER_DOWNLOAD_ROOT Maven repository to download Amper dist from.
# default: https://packages.jetbrains.team/maven/p/amper/amper
# AMPER_JRE_DOWNLOAD_ROOT Url prefix to download Amper JRE from.
# default: https:/
# AMPER_BOOTSTRAP_CACHE_DIR Cache directory to store extracted JRE and Amper distribution
# AMPER_JAVA_HOME JRE to run Amper itself (optional, does not affect compilation)
# AMPER_JAVA_OPTIONS JVM options to pass to the JVM running Amper (does not affect the user's application)
# AMPER_NO_WELCOME_BANNER Disables the first-run welcome message if set to a non-empty value
set -e -u
# The version of the Amper distribution to provision and use
amper_version=0.9.2
# Establish chain of trust from here by specifying exact checksum of Amper distribution to be run
amper_sha256=33304bb301d0c5276ad4aa718ce8a10486fda4be45179779497d2036666bb16f
AMPER_DOWNLOAD_ROOT="${AMPER_DOWNLOAD_ROOT:-https://packages.jetbrains.team/maven/p/amper/amper}"
AMPER_JRE_DOWNLOAD_ROOT="${AMPER_JRE_DOWNLOAD_ROOT:-https:/}"
die() {
echo >&2
echo "$@" >&2
echo >&2
exit 1
}
download_and_extract() {
moniker="$1"
file_url="$2"
file_sha="$3"
sha_size="$4"
cache_dir="$5"
extract_dir="$6"
show_banner_on_cache_miss="$7"
if [ -e "$extract_dir/.flag" ] && [ "$(cat "$extract_dir/.flag")" = "${file_sha}" ]; then
# Everything is up-to-date in $extract_dir, do nothing
return 0;
fi
mkdir -p "$cache_dir"
# Take a lock for the download of this file
short_sha=$(echo "$file_sha" | cut -c1-32) # cannot use the ${short_sha:0:32} syntax in regular /bin/sh
download_lock_file="$cache_dir/download-${short_sha}.lock"
process_lock_file="$cache_dir/download-${short_sha}.$$.lock"
echo $$ >"$process_lock_file"
while ! ln "$process_lock_file" "$download_lock_file" 2>/dev/null; do
lock_owner=$(cat "$download_lock_file" 2>/dev/null || true)
if [ -n "$lock_owner" ] && ps -p "$lock_owner" >/dev/null; then
echo "Another Amper instance (pid $lock_owner) is downloading $moniker. Awaiting the result..."
sleep 1
elif [ -n "$lock_owner" ] && [ "$(cat "$download_lock_file" 2>/dev/null)" = "$lock_owner" ]; then
rm -f "$download_lock_file"
# We don't want to simply loop again here, because multiple concurrent processes may face this at the same time,
# which means the 'rm' command above from another script could delete our new valid lock file. Instead, we just
# ask the user to try again. This doesn't 100% eliminate the race, but the probability of issues is drastically
# reduced because it would involve 4 processes with perfect timing. We can revisit this later.
die "Another Amper instance (pid $lock_owner) locked the download of $moniker, but is no longer running. The lock file is now removed, please try again."
fi
done
# shellcheck disable=SC2064
trap "rm -f \"$download_lock_file\"" EXIT
rm -f "$process_lock_file"
unlock_and_cleanup() {
rm -f "$download_lock_file"
trap - EXIT
return 0
}
if [ -e "$extract_dir/.flag" ] && [ "$(cat "$extract_dir/.flag")" = "${file_sha}" ]; then
# Everything is up-to-date in $extract_dir, just release the lock
unlock_and_cleanup
return 0;
fi
if [ "$show_banner_on_cache_miss" = "true" ] && [ -z "${AMPER_NO_WELCOME_BANNER:-}" ]; then
echo
echo ' _____ Welcome to '
echo ' /:::::| ____ ___ ____ ____ __ ___ '
echo ' /::/|::| |::::\_|:::\ |:::::\ /::::\ |::|/:::| '
echo ' /::/ |::| |::|\:::|\::\ |::|\::\ /:/__\:\ |:::/ '
echo ' /::/__|::| |::| |::| |::| |::| |::|:::::::/ |::| '
echo ' /:::::::::| |::| |::| |::| |::|/::/ \::\__ |::| '
echo ' /::/ |::| |::| |::| |::| |:::::/ \::::| |::| '
echo ' |::| '
echo " |::| v.$amper_version "
echo
echo "This is the first run of this version, so we need to download the actual Amper distribution."
echo "Please give us a few seconds, subsequent runs will be faster."
echo
fi
echo "Downloading $moniker..."
temp_file="$cache_dir/download-file-$$.bin"
rm -f "$temp_file"
if command -v curl >/dev/null 2>&1; then
if [ -t 1 ]; then CURL_PROGRESS="--progress-bar"; else CURL_PROGRESS="--silent --show-error"; fi
# shellcheck disable=SC2086
curl $CURL_PROGRESS -L --fail --retry 5 --connect-timeout 30 --output "${temp_file}" "$file_url"
elif command -v wget >/dev/null 2>&1; then
if [ -t 1 ]; then WGET_PROGRESS=""; else WGET_PROGRESS="-nv"; fi
wget $WGET_PROGRESS --tries=5 --connect-timeout=30 --read-timeout=120 -O "${temp_file}" "$file_url"
else
die "ERROR: Please install 'wget' or 'curl', as Amper needs one of them to download $moniker"
fi
check_sha "$file_url" "$temp_file" "$file_sha" "$sha_size"
rm -rf "$extract_dir"
mkdir -p "$extract_dir"
case "$file_url" in
*".zip")
if command -v unzip >/dev/null 2>&1; then
unzip -q "$temp_file" -d "$extract_dir"
else
die "ERROR: Please install 'unzip', as Amper needs it to extract $moniker"
fi ;;
*)
if command -v tar >/dev/null 2>&1; then
tar -x -f "$temp_file" -C "$extract_dir"
else
die "ERROR: Please install 'tar', as Amper needs it to extract $moniker"
fi ;;
esac
rm -f "$temp_file"
echo "$file_sha" >"$extract_dir/.flag"
# Unlock and cleanup the lock file
unlock_and_cleanup
echo "Download complete."
echo
}
# usage: check_sha SOURCE_MONIKER FILE SHA_CHECKSUM SHA_SIZE
# $1 SOURCE_MONIKER (e.g. url)
# $2 FILE
# $3 SHA hex string
# $4 SHA size in bits (256, 512, ...)
check_sha() {
sha_size=$4
if command -v shasum >/dev/null 2>&1; then
echo "$3 *$2" | shasum -a "$sha_size" --status -c || {
die "ERROR: Checksum mismatch for $2 (downloaded from $1): expected checksum $3 but got $(shasum --binary -a "$sha_size" "$2" | awk '{print $1}')"
}
return 0
fi
shaNsumCommand="sha${sha_size}sum"
if command -v "$shaNsumCommand" >/dev/null 2>&1; then
echo "$3 *$2" | $shaNsumCommand -w -c || {
die "ERROR: Checksum mismatch for $2 (downloaded from $1): expected checksum $3 but got $($shaNsumCommand "$2" | awk '{print $1}')"
}
return 0
fi
echo "Both 'shasum' and 'sha${sha_size}sum' utilities are missing. Please install one of them"
return 1
}
# ********** System detection **********
kernelName=$(uname -s)
arch=$(uname -m)
case "$kernelName" in
Darwin* )
simpleOs="macos"
jre_os="macosx"
jre_archive_type="tar.gz"
default_amper_cache_dir="$HOME/Library/Caches/JetBrains/Amper"
;;
Linux* )
simpleOs="linux"
jre_os="linux"
jre_archive_type="tar.gz"
default_amper_cache_dir="$HOME/.cache/JetBrains/Amper"
# If linux runs in 32-bit mode, we want the "fake" 32-bit architecture, not the real hardware,
# because in this mode linux cannot run 64-bit binaries.
# shellcheck disable=SC2046
arch=$(linux$(getconf LONG_BIT) uname -m)
;;
CYGWIN* | MSYS* | MINGW* )
simpleOs="windows"
jre_os="win"
jre_archive_type=zip
if command -v cygpath >/dev/null 2>&1; then
default_amper_cache_dir=$(cygpath -u "$LOCALAPPDATA\JetBrains\Amper")
else
die "The 'cypath' command is not available, but Amper needs it. Use amper.bat instead, or try a Cygwin or MSYS environment."
fi
;;
*)
die "Unsupported platform $kernelName"
;;
esac
amper_cache_dir="${AMPER_BOOTSTRAP_CACHE_DIR:-$default_amper_cache_dir}"
# ********** Provision Amper distribution **********
amper_url="$AMPER_DOWNLOAD_ROOT/org/jetbrains/amper/amper-cli/$amper_version/amper-cli-$amper_version-dist.tgz"
amper_target_dir="$amper_cache_dir/amper-cli-$amper_version"
download_and_extract "Amper distribution v$amper_version" "$amper_url" "$amper_sha256" 256 "$amper_cache_dir" "$amper_target_dir" "true"
# ********** Provision JRE for Amper **********
if [ "x${AMPER_JAVA_HOME:-}" = "x" ]; then
case $arch in
x86_64 | x64) jre_arch="x64" ;;
aarch64 | arm64) jre_arch="aarch64" ;;
*) die "Unsupported architecture $arch" ;;
esac
# Auto-updated from syncVersions.main.kts, do not modify directly here
zulu_version=25.28.85
java_version=25.0.0
pkg_type=jre
platform="$jre_os $jre_arch"
case $platform in
"macosx x64") jre_sha256=a73455c80413daa31af6b09589e3655bb8b8b91e4aa884ca7c91dc5552b9e974 ;;
"macosx aarch64") jre_sha256=37316ebea9709eb4f8bc58f0ddd2f58e53720d3e7df6f78c64125915b44d322d ;;
"linux x64") jre_sha256=807e96e43db00af3390a591ed40f2c8c35f7f475fb14b6061dfb19db33702cba ;;
"linux aarch64") jre_sha256=ad75e426e3f101cfa018f65fde07d82b10337d4f85250ca988474d59891c5f50 ;;
"win x64") jre_sha256=d3c5db7864e6412ce3971c0b065def64942d7b0f3d02581f7f0472cac21fbba9 ;;
"win aarch64") jre_sha256=f5f6d8a913695649e8e2607fe0dc79c81953b2583013ac1fb977c63cb4935bfb; pkg_type=jdk ;;
*) die "Unsupported platform $platform" ;;
esac
# URL for the JRE (see https://api.azul.com/metadata/v1/zulu/packages?release_status=ga&include_fields=java_package_features,os,arch,hw_bitness,abi,java_package_type,sha256_hash,size,archive_type,lib_c_type&java_version=25&os=macos,linux,win)
# https://cdn.azul.com/zulu/bin/zulu25.28.85-ca-jre25.0.0-macosx_aarch64.tar.gz
# https://cdn.azul.com/zulu/bin/zulu25.28.85-ca-jre25.0.0-linux_x64.tar.gz
jre_url="$AMPER_JRE_DOWNLOAD_ROOT/cdn.azul.com/zulu/bin/zulu$zulu_version-ca-$pkg_type$java_version-${jre_os}_$jre_arch.$jre_archive_type"
jre_target_dir="$amper_cache_dir/zulu$zulu_version-ca-$pkg_type$java_version-${jre_os}_$jre_arch"
download_and_extract "Amper runtime v$zulu_version" "$jre_url" "$jre_sha256" 256 "$amper_cache_dir" "$jre_target_dir" "false"
effective_amper_java_home=
for d in "$jre_target_dir" "$jre_target_dir"/* "$jre_target_dir"/Contents/Home "$jre_target_dir"/*/Contents/Home; do
if [ -e "$d/bin/java" ]; then
effective_amper_java_home="$d"
fi
done
if [ "x${effective_amper_java_home:-}" = "x" ]; then
die "Unable to find bin/java under $jre_target_dir"
fi
else
effective_amper_java_home="$AMPER_JAVA_HOME"
fi
java_exe="$effective_amper_java_home/bin/java"
if [ '!' -x "$java_exe" ]; then
die "Unable to find bin/java executable at $java_exe"
fi
# ********** Script path detection **********
# We might need to resolve symbolic links here
wrapper_path=$(realpath "$0")
# ********** Launch Amper **********
# In this section we construct the command line by prepending arguments from biggest to lowest precedence:
# 1. basic main class, CLI arguments, and classpath
# 2. user JVM args (AMPER_JAVA_OPTIONS)
# 3. default JVM args (prepended last, which means they appear first, so they are overridden by user args)
# 1. Prepend basic launch arguments
if [ "$simpleOs" = "windows" ]; then
# Can't cygpath the '*' so it has to be outside
classpath="$(cygpath -w "$amper_target_dir")\lib\*"
else
classpath="$amper_target_dir/lib/*"
fi
set -- -cp "$classpath" org.jetbrains.amper.cli.MainKt "$@"
# 2. Prepend user JVM args from AMPER_JAVA_OPTS
#
# We use "xargs" to parse quoted JVM args from inside AMPER_JAVA_OPTS.
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
if [ -n "${AMPER_JAVA_OPTIONS:-}" ]; then
eval "set -- $(
printf '%s\n' "$AMPER_JAVA_OPTIONS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
fi
# 3. Prepend default JVM args
set -- \
@"$amper_target_dir/amper.args" \
"-Damper.wrapper.dist.sha256=$amper_sha256" \
"-Damper.dist.path=$amper_target_dir" \
"-Damper.wrapper.path=$wrapper_path" \
"$@"
# Then we can launch with the overridden $@ arguments
exec "$java_exe" "$@"