-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathamper-from-sources
executable file
·299 lines (248 loc) · 10.5 KB
/
amper-from-sources
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
#!/bin/bash
#
# Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
#
### Runs amper cli from sources
set -e -u -o pipefail
script_dir="$(dirname -- "$0")"
script_dir="$(cd -- "$script_dir" && pwd)"
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"
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
temp_file="$cache_dir/download-file-$$.bin"
echo "Downloading $moniker... (only happens on the first run of this version)"
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" 2>&1
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" 2>&1
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 || {
echo "$2 (downloaded from $1):" >&2
echo "expected checksum $3 but got: $(shasum --binary -a "$sha_size" "$2" | awk '{print $1}')" >&2
die "ERROR: Checksum mismatch for $1"
}
return 0
fi
shaNsumCommand="sha${sha_size}sum"
if command -v "$shaNsumCommand" >/dev/null 2>&1; then
echo "$3 *$2" | $shaNsumCommand -w -c || {
echo "$2 (downloaded from $1):" >&2
echo "expected checksum $3 but got: $($shaNsumCommand "$2" | awk '{print $1}')" >&2
die "ERROR: Checksum mismatch for $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"
jbr_os="osx"
default_amper_cache_dir="$HOME/Library/Caches/Amper"
;;
Linux* )
simpleOs="linux"
jbr_os="linux"
default_amper_cache_dir="$HOME/.cache/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"
jbr_os="windows"
if command -v cygpath >/dev/null 2>&1; then
default_amper_cache_dir=$(cygpath -u "$LOCALAPPDATA\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
# TODO should we respect --shared-caches-root instead of (or in addition to) this env var?
amper_cache_dir="${AMPER_BOOTSTRAP_CACHE_DIR:-$default_amper_cache_dir}"
# ********** Provision JRE for Amper **********
if [ "x${AMPER_JAVA_HOME:-}" = "x" ]; then
case $arch in
x86_64 | x64) jbr_arch="x64" ;;
aarch64 | arm64) jbr_arch="aarch64" ;;
*) die "Unsupported architecture $arch" ;;
esac
# Auto-updated from syncVersions.main.kts, do not modify directly here
jbr_version=21.0.6
jbr_build=b895.97
# URL for JBR (vanilla) - see https://github.com/JetBrains/JetBrainsRuntime/releases
jbr_url="$AMPER_JRE_DOWNLOAD_ROOT/cache-redirector.jetbrains.com/intellij-jbr/jbr-$jbr_version-$jbr_os-$jbr_arch-$jbr_build.tar.gz"
jbr_target_dir="$amper_cache_dir/jbr-$jbr_version-$jbr_os-$jbr_arch-$jbr_build"
platform="$jbr_os $jbr_arch"
case $platform in
"osx x64") jbr_sha512=eeef2a2f82c16d7a0bebb1771b0e122aeb78c2bb1414d48767ea4b2d52086152f149bb5a076c9f98ef1f56dee7b40da1118fa01ba32a53576fffc4d1145044fd ;;
"osx aarch64") jbr_sha512=00afc547ff9e5e408446ece3c8eabef2d0b29909a8be4774c8bd3e296f823e1a902717f5f44313f1a634e22b093c80de8d8ac760869d61fb4647a73711793e0f ;;
"linux x64") jbr_sha512=7f0693316c8ac9a6c323d19682effeb33d5a69da8b79c86253d02e206dd0d2afe2ca49be86c58cc8115ad8626e70ec11ee9008282886d7102aa328722e8f032e ;;
"linux aarch64") jbr_sha512=62c2c1a241278951523bace44742a23bb4b48c713e53c1aaad4204af1f4057cca186a17a840a523b440c739b44af4815e7f388c4a6c167a4e7023da4295f6207 ;;
"windows x64") jbr_sha512=7e71a463327a92e6974b3d1013efde00f9d852660d5a18eae5765534b6d3cf0de471f72fd30d3caae910253b8b0df7202e2a76f0435e84ad80d13fb298a84c48 ;;
"windows aarch64") jbr_sha512=188bb92c35bc31b8ec9596701b498797c6578fb8513f1a854a2c8501ff3d2883a1fc74d24c45322526cdaaeb86940fffaf9729f39ba8dd52dd0f2b6f63da35fe ;;
*) die "Unsupported platform $platform" ;;
esac
download_and_extract "JetBrains Runtime v$jbr_version$jbr_build" "$jbr_url" "$jbr_sha512" 512 "$amper_cache_dir" "$jbr_target_dir"
AMPER_JAVA_HOME=
for d in "$jbr_target_dir" "$jbr_target_dir"/* "$jbr_target_dir"/Contents/Home "$jbr_target_dir"/*/Contents/Home; do
if [ -e "$d/bin/java" ]; then
AMPER_JAVA_HOME="$d"
fi
done
if [ "x${AMPER_JAVA_HOME:-}" = "x" ]; then
die "Unable to find bin/java under $jbr_target_dir"
fi
fi
java_exe="$AMPER_JAVA_HOME/bin/java"
if [ '!' -x "$java_exe" ]; then
die "Unable to find bin/java executable at $java_exe"
fi
# ********** Build Amper from sources **********
case "$(uname)" in
# man stat on Mac OS X
# N The name of the file.
# z The size of file in bytes (st_size).
# a, m, c, B
# The time file was last accessed or modified, or when the
# inode was last changed, or the birth time of the inode
# (st_atime, st_mtime, st_ctime, st_birthtime).
Darwin*) stat_format=(-f '%N %m %z') ;;
# man stat on Ubuntu and Windows (Git Bash)
# %n file name
# %s total size, in bytes
# %Y time of last data modification, seconds since Epoch
Linux* | CYGWIN* | MSYS* | MINGW*) stat_format=(--format='%n %s %Y') ;;
*)
echo "Unsupported system: $(uname)"
exit 1
;;
esac
# Prints a status of all sources files impacting the Amper build
current_state() {
# scan sources directory
find "$script_dir/sources" '(' -name build -o -name build-from-sources -o -name .git -o -name .idea ')' -prune -o -type f -print0 | \
xargs -0 stat "${stat_format[@]}"
stat "${stat_format[@]}" "$script_dir/project.yaml"
}
# Do not rebuild Amper if nothing changed
up_to_date_file="$script_dir/build/up-to-date-from-sources-wrapper.txt"
current="$(current_state)"
if [ ! -f "$up_to_date_file" ] || [ "$(cat "$up_to_date_file")" != "$current" ]; then
# Amper needs a published Amper Android Gradle plugin support for the delegated Gradle builds
(cd "$script_dir" && ./amper --log-level=warn task :cli:unpackedDist && ./amper --log-level=warn publish -m gradle-plugin mavenLocal)
current_state >"$up_to_date_file"
if [ -n "$TERM" ] && tput clear >/dev/null 2>&1; then
clear
fi
fi
# ********** Script path detection **********
# We might need to resolve symbolic links here
wrapper_path=$(realpath "$0")
# ********** Launch Amper from unpacked dist **********
if [ "$simpleOs" = "windows" ]; then
# Can't cygpath the *
classpath="$(cygpath -w "$script_dir/build/tasks/_cli_unpackedDist/dist/lib")\*"
else
classpath="$script_dir/build/tasks/_cli_unpackedDist/dist/lib/*"
fi
jvm_args="-ea -XX:+EnableDynamicAgentLoading ${AMPER_JAVA_OPTIONS:-}"
# shellcheck disable=SC2086
time "$java_exe" \
"-Damper.wrapper.path=$wrapper_path" \
$jvm_args -cp "$classpath" org.jetbrains.amper.cli.MainKt \
"--build-output=$script_dir/build-from-sources" "$@"