-
Notifications
You must be signed in to change notification settings - Fork 4
/
wemi
executable file
·585 lines (515 loc) · 22.4 KB
/
wemi
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#!/bin/sh
# Wemi Launcher - https://github.com/Darkyenus/wemi
# Target platform: Reasonably POSIX compliant systems (https://pubs.opengroup.org/onlinepubs/009695399/)
# Copyright 2019-20 Jan Polák
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# Configurable with:
# Environment variables:
# WEMI_JAVA - number, Java version number to use (e.g. 8, 9, 13) OR path to Java executable to use
# WEMI_DIST - path of a directory which contains .jar files needed to launch Wemi (useful for custom builds)
# WEMI_JAVA_OPTS - options passed to 'java' command used to launch Wemi
# WEMI_ROOT - root directory of the project, do not use unless you know what you are doing
# WEMI_USE_SYMLINKS (true|false) - override for symlink support detection (true by default, false on Windows)
# Environment variables for automatic JDK download, values must be understood by https://api.adoptopenjdk.net/
# WEMI_JDK_OS - name of this operating system, needed only if detection fails
# WEMI_JDK_ARCH - architecture of this machine, needed only if detection fails
# WEMI_JDK_IMPLEMENTATION - implementation of JDK, default is 'hotspot'
# WEMI_JDK_HEAP_SIZE - heap size for which the JDK is build, default is 'normal'
# WEMI_JDK_RELEASE - specific release number of requested JDK, default is 'latest'
# Parameters (must appear first, before parameters for Wemi itself):
# --java=<number> - same as WEMI_JAVA, with higher precedence
# --debug -|
# --debug=<port> -| launch Wemi with debugger on specified <port> or 5005,
# --debug-suspend -| optionally suspended until debugger connects
# --debug-suspend=<port> -|
# --print-java-exe - Print path to java executable that would be used and exit
# --print-wemi-home - Print path to directory with Wemi jars and exit
readonly wemi_version='0.15'
readonly java_min_version='8'
log() { echo "$@" 1>&2; }
fail() { log "$@"; exit 1; }
fail_unsatisfied() { log "Unsatisfied software dependency - install this software and try again: $*"; exit 2; }
# Parse command line options
print_java_exe=false
print_wemi_home=false
while true; do
if echo "$1" | grep -q -x -e '--java=.*' ; then
WEMI_JAVA="${1#--java=}"
elif echo "$1" | grep -E -q -x -e '--debug(-suspend|=[0-9]+|-suspend=[0-9]+)?'; then
suspend_='n'
# Parse --debug, --debug-suspend, --debug=<port> and --debug-suspend=<port> flags, which are mutually exclusive and
# must be the first flag to appear.
echo "$1" | grep -q -e 'suspend' && suspend_='y'
port_="$(echo "$1" | sed 's/[^0-9]*\([0-9]*\)[^0-9]*/\1/')" || fail "Failed to parse debug port ($?)"
WEMI_JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=${suspend_},address=${port_:-5005} ${WEMI_JAVA_OPTS}"
elif [ "$1" = '--print-java-exe' ]; then
print_java_exe=true
elif [ "$1" = '--print-wemi-home' ]; then
print_wemi_home=true
else
break
fi
shift
done
{
# Detect Java version (from WEMI_JAVA env-var (includes --java=<>, or default)
if [ -n "$WEMI_JAVA" ]; then
version_="${WEMI_JAVA}"
readonly java_version_forced=true
else
version_="$java_min_version"
readonly java_version_forced=false
fi
if echo "$version_" | grep -q -x -e '[0-9]*'; then
if [ -z "$version_" ] || [ "$version_" -lt "$java_min_version" ]; then
log "Requested version number '$version_' is too low, defaulting to $java_min_version"
version_="$java_min_version"
fi
readonly java_version="$version_"
readonly java_exe=''
else
readonly java_version=''
readonly java_exe="$version_"
fi
}
readonly wemi_home_dir=${WEMI_HOME:-"${HOME}/.wemi"}
if [ ! -e "$wemi_home_dir" ]; then
log "Creating '$wemi_home_dir' as a cache for Wemi related files"
mkdir -p "$wemi_home_dir" || fail "Wemi cache directory creation failed ($?)"
cat <<EOF >"$wemi_home_dir/readme.txt"
This directory is used by Wemi to store downloaded Wemi versions and JDKs.
Feel free do delete this directory to completely remove Wemi from your system.
To change the location of this directory, specify the new path in WEMI_HOME environment variable.
Then you can delete this directory or move it to the new location.
jdk/ contains downloaded OpenJDK Java distributions.
wemi/ contains downloaded Wemi distributions.
You can delete anything from these directories to reclaim disk space, as long as it is currently not used.
EOF
fi
# $1: Command name
# $?: Whether the command exists
command_exists() {
# Note: This is the most POSIX way to do it, but it is optional (the -v switch).
command -v "$1" > /dev/null 2>&1
}
# Downloads the resource at given URL to given file. May support continuations.
# $1: URL
# $2: file
download_to_file() {
mkdir -p "$(dirname "$2")" || log "Failed to ensure that parent directory for download exists ($?)"
if command_exists curl; then
curl -C - --fail --retry 3 --location --output "$2" --url "$1" || fail "Failed to fetch $1 to $2 (curl: $?)"
elif command_exists wget; then
# Download resume in wget is not supported, because it does not play well with custom output file
if [ -e "$2" ]; then
rm "$2" || log "Failed to delete old download file $2 (rm: $?)"
fi
wget --show-progress --output-document="$2" "$1" || fail "Failed to fetch $1 to $2 (wget: $?)"
else
fail_unsatisfied "curl or wget"
fi
}
# Downloads the resource at given URL to stdout.
# $1: URL
download_to_output() {
if command_exists curl; then
curl --fail --retry 3 --location --silent --show-error --url "$1" || fail "Failed to fetch $1 (curl: $?)"
elif command_exists wget; then
wget --quiet --output-document=- "$1" || fail "Failed to fetch $1 (wget: $?)"
else
fail_unsatisfied "curl or wget"
fi
}
# URL encode parameter $1
# NOTE: Usually does not handle Unicode characters correctly
url_encode() {
input_="$1"; output_=""
while [ -n "$input_" ]; do
# Take first character from input_
tail_="${input_#?}"; head="${input_%"$tail_"}"; input_="$tail_"
case "$head" in
[-_.~a-zA-Z0-9]) output_="$output_$head";;
*) output_="${output_}%$(printf '%02x' "'$head")";;
esac
done
echo "$output_"
}
# @1 `.zip` archive
# @2 directory with entries extracted
extract_zip() {
if command_exists unzip; then
unzip "$1" -d "$2" 1>/dev/null || fail "Failed to unzip '$1' (unzip: $?)"
else
fail_unsatisfied "unzip"
fi
}
# @1 `.tar.gz` archive
# @2 directory with entries extracted
extract_targz() {
# If we have gzip, use it, if we don't then hope that tar/pax supports the -z switch
# (gzip, tar and pax's -z switch are not POSIX compliant, so this tries things that are most likely to work first)
if command_exists gzip; then
if command_exists tar; then
# shellcheck disable=SC2015
mkdir "$2" 1>&2 &&
gzip -d -c < "$1" | tar -x -C "$2" 1>/dev/null || fail "Failed to extract '$1' (gzip|tar: $?)"
elif command_exists pax; then
# pax has two pecularities:
# 1. It always extracts (relative) to the current working directory
# 2. It permits absolute paths in archives and WILL extract to an absolute path
# Both can be fixed by prefixing the paths by the target directory manually.
gzip -d -c < "$1" | pax -r -s "#^#${2}/#" 1>/dev/null || fail "Failed to extract '$1' (gzip|pax: $?)"
else
fail_unsatisfied "tar or pax"
fi
else
if command_exists tar; then
# shellcheck disable=SC2015
mkdir "$2" &&
tar -x -z -C "$2" -f "$1" 1>/dev/null || fail "Failed to extract '$1' (tar: $?)"
elif command_exists pax; then
pax -r -z -s "#^#${2}/#" -f "$1" 1>/dev/null || fail "Failed to extract '$1' (pax: $?)"
else
fail_unsatisfied "tar or pax"
fi
fi
}
detect_os() {
if [ -n "$WEMI_JDK_OS" ]; then
echo "$WEMI_JDK_OS"
else
os_=$(uname)
# shellcheck disable=SC2039
os_=${os_:-${OSTYPE}}
os_=$(echo "$os_" | tr '[:upper:]' '[:lower:]') || fail "Failed to lowercase OS name ($?)" # to lowercase
case "$os_" in
*linux*) echo 'linux' ;;
*darwin*) echo 'mac' ;;
*windows* | *cygwin* | *mingw* | *msys*) echo 'windows' ;;
*sunos*) echo 'solaris' ;;
aix) echo 'aix' ;;
*) fail "Unrecognized or unsupported operating system: $os_ (specify manually via WEMI_JDK_OS)" ;;
esac
fi
}
detect_arch() {
if [ -n "$WEMI_JDK_ARCH" ]; then
echo "$WEMI_JDK_ARCH"
else
arch_=$(uname -m | tr '[:upper:]' '[:lower:]') # to lowercase
case "$arch_" in
*x86_64* | *amd64* | *i[3456]86-64*) echo 'x64' ;;
*x86* | *i[3456]86* | *i86pc*) echo 'x32' ;;
*ppc64le*) echo 'ppc64le' ;; # I guess?
*ppc64*) echo 'ppc64' ;;
*power* | *ppc*) fail "32-bit PowerPC architecture is not supported" ;;
*s390*) echo 's390x' ;;
*aarch64*) echo 'aarch64' ;; # 64-bit ARM
*arm*) echo 'arm32' ;;
*) fail "Unrecognized or unsupported CPU architecture: $arch_ (specify manually via WEMI_JDK_ARCH)" ;;
esac
fi
}
# Initialize JDK parameters
readonly jdk_version="openjdk${java_version}"
readonly jdk_implementation="${WEMI_JDK_IMPLEMENTATION:-hotspot}"
readonly jdk_heap_size="${WEMI_JDK_HEAP_SIZE:-normal}"
readonly jdk_release="${WEMI_JDK_RELEASE:-latest}"
readonly jdk_os="$(detect_os)" || fail "Failed to detect OS, specify it manually"
readonly jdk_arch="$(detect_arch)" || fail "Failed to detect architecture, specify it manually"
if [ "$WEMI_USE_SYMLINKS" = "false" ] || { [ "$jdk_os" = "windows" ] && [ ! "$WEMI_USE_SYMLINKS" = "true" ]; }; then
readonly use_symlinks="false"
else
readonly use_symlinks="true"
fi
fetch_jdk() {
# Contains all versions of this type of JDK
jdk_type_dir_="${wemi_home_dir}/jdk/${jdk_version}-${jdk_implementation}-${jdk_heap_size}"
# Download, extract and test a JDK distribution according to JDK_* constants, and the JDK release argument.
# Returns immediately if already downloaded.
# $1: JDK release (not 'latest')
# echo: Path to the JDK home
fetch_jdk_version() {
jdk_dir_="${jdk_type_dir_}/$1"
if [ -d "$jdk_dir_" ]; then
# This version already exists
echo "$jdk_dir_"
return 0
fi
# Download the archive to a temporary directory
jdk_dir_download_="${jdk_dir_}.download"
# Using the adoptopenjdk builds
log "Downloading ${jdk_version}(${jdk_implementation}) for ${jdk_os} (${jdk_arch}): ${1}"
jdk_url_="https://api.adoptopenjdk.net/v2/binary/releases/${jdk_version}?openjdk_impl=${jdk_implementation}&os=${jdk_os}&arch=${jdk_arch}&release=$(url_encode "$1")&type=jdk&heap_size=${jdk_heap_size}" || fail "Failed to build download URL ($?)"
download_to_file "$jdk_url_" "$jdk_dir_download_" 1>&2
# Now we need to decompress it
# Windows binaries are packaged as zip, others as tar.gz.
# This is a bit iffy, but getting the filename from curl/wget reliably is not easy.
jdk_dir_decompressed_="${jdk_dir_}.decompressed"
if [ "$jdk_os" = 'windows' ]; then
extract_zip "$jdk_dir_download_" "$jdk_dir_decompressed_"
else
extract_targz "$jdk_dir_download_" "$jdk_dir_decompressed_"
fi
# Unpack the directory (it is bundled in a different directory structure according to the platform)
# 'release' is a file inside the directory which we want
if release_file_="$(echo "${jdk_dir_decompressed_}/"*"/release")" && [ -f "$release_file_" ]; then
jdk_dir_decompressed_home_="${release_file_%/release}"
elif [ "$jdk_os" = 'mac' ] && release_file_="$(echo "${jdk_dir_decompressed_}/"*"/Contents/Home/release")" && [ -f "$release_file_" ]; then
jdk_dir_decompressed_home_="${release_file_%/release}"
else
fail "Failed to recognize downloaded JDK directory structure at '${jdk_dir_decompressed_}'"
fi
# Test the downloaded JDK
downloaded_java_="${jdk_dir_decompressed_home_}/bin/java"
if [ ! -f "$downloaded_java_" ]; then
fail "Downloaded JDK does not contain 'java' executable at the expected location (${downloaded_java_})"
fi
"$downloaded_java_" -version 2>/dev/null 1>&2 || fail "Downloaded JDK 'java' executable does not work ($?)"
# Move home into the valid directory
mv "$jdk_dir_decompressed_home_" "$jdk_dir_" 1>&2 || fail "Failed to move downloaded JDK into a correct destination ($?)"
# Delete original archive
rm "$jdk_dir_download_" 1>&2 || log "warning: Failed to delete downloaded file ${jdk_dir_download_} ($?)"
# Delete downloaded directory
rm -rf "${jdk_dir_decompressed_?:EMPTY_PATH}" 1>&2 || log "Failed to delete leftover downloaded files (rm returned $?)"
log "Downloaded a new JDK at: ${jdk_dir_}"
echo "$jdk_dir_"
}
fetch_latest_jdk_release() {
latest_info_url_="https://api.adoptopenjdk.net/v2/info/releases/${jdk_version}?openjdk_impl=${jdk_implementation}&os=${jdk_os}&arch=${jdk_arch}&release=latest&type=jdk&heap_size=${jdk_heap_size}"
# Get the json, remove all linebreaks and spaces (for easier processing)
latest_info_json_="$(download_to_output "$latest_info_url_" | tr -d '\n ')" || fail "Failed to download latest JDK release info"
# Delete everything except for the release name. Since the format is very simple, this should work, even if it is an abomination
echo "$latest_info_json_" | sed 's/.*"release_name":"\([^"][^"]*\)".*/\1/' || fail "Failed to extract latest JDK release info from JSON ($?)"
}
read_link() {
# $1 - path to check
# return: 0 if link is valid, 1 if it is broken or does not exist
# output: the path at which the destination is (may be $1 if it is a valid symlink)
if [ -d "$1" ]; then
# Directory or a valid symlink to a directory
echo "$1"
return 0
elif [ -f "$1" ]; then
# File
link_path_="$(cat "$1")" || return 1
if [ -d "$link_path_" ]; then
echo "$link_path_"
return 0
fi
fi
return 1
}
verify_link() {
# $1 - path to check
# return: 0 if link is valid or the file does not exist at all, 1 if it is broken
# Checks for symlinks and "manual links" (files which contain path)
if [ ! -e "$1" ] && [ ! -L "$1" ]; then
# File does not exist at all (and it isn't a broken symlink)
return 0
fi
read_link "$1" > /dev/null
}
fetch_latest_jdk() {
# Check whether 'latest' symlink exists and is fresh enough. If it does and is, download it.
# If it isn't, download and use the newest release.
latest_dir_="${jdk_type_dir_}/latest"
# When using symlinks, latest_dir_ is a symlink to latest release directory
# When not using symlinks, latest_dir_ is a plain file that contains the name of release directory
if ! verify_link "$latest_dir_"; then
fail "'${latest_dir_}' should be a symlink or a plain file, but isn't"
fi
# If the 'latest' exists and is less than 60 days old, no need to refresh it
if [ -z "$(find "$latest_dir_" -mtime -60 -print 2>/dev/null)" ]; then
log "Checking for JDK update"
# Get new latest version
if latest_release_="$(fetch_latest_jdk_release)"; then
if latest_release_dir_="$(fetch_jdk_version "$latest_release_")"; then
# Everything was successful, remove old symlink a create a new one
if [ -L "$latest_dir_" ]; then
unlink "$latest_dir_" || log "Failed to unlink previous 'latest' symlink ($?)"
elif [ -e "$latest_dir_" ]; then
rm "$latest_dir_" || log "Failed to delete previous 'latest' file ($?)"
fi
if [ "$use_symlinks" = "true" ]; then
ln -s "$latest_release_dir_" "$latest_dir_" || log "Failed to link ${latest_dir_} to ${latest_release_dir_} ($?)"
else
echo "$latest_release_dir_" > "$latest_dir_" || log "Failed to record ${latest_dir_} to ${latest_release_dir_} ($?)"
fi
else
log "Failed to fetch latest JDK release (${latest_release_})"
fi
else
log "Failed to fetch latest JDK release name"
fi
fi
read_link "$latest_dir_" || fail "JDK fetch failed, created link file is not valid"
}
if [ "$jdk_release" = 'latest' ]; then
fetch_latest_jdk
else
fetch_jdk_version "$jdk_release"
fi
}
can_use_java_from_path() {
if ! command_exists java; then
return 1
fi
if [ "$jdk_os" = 'mac' ] && command_exists '/usr/libexec/java_home' && [ -z "$JAVA_HOME" ]; then
# Java on macOS always exists on PATH, but that is just a proxy to the real JVM.
# Which JVM installation is used is controlled by either java_home command or JAVA_HOME env var.
# If JAVA_HOME is set, trust it, otherwise check that java_home returns a valid path.
# We also try to exec javac, to also check that it is a JDK.
if ! /usr/libexec/java_home --exec javac -version > /dev/null 2>&1; then
# No JDK installed (through java_home)
return 1
fi
elif [ "$java_version_forced" = 'true' ] && ! command_exists javac; then
# Not using java on PATH, because javac is not on PATH and explicit version has been specified
return 1
fi
if ! java_version_="$(java -version 2>&1 | head -n 1)"; then
log "Failed to get version of java on PATH, using downloaded openjdk"
return 1
fi
if echo "$java_version_" | grep -q -x -e '.* version ".*".*'; then
# Matching standard versioning (java version "1.8.0", openjdk version "11.0.3" 2019-10-15, openjdk version "9", etc.)
# https://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html
java_version_="$(echo "$java_version_" | sed 's/.* version "\(.*\)".*/\1/')" || fail "Failed to parse Java version ($?)"
java_version_major_="$(echo "$java_version_" | sed 's/\([0-9]*\).*/\1/')" || fail "Failed to parse Java version minor ($?)"
# Until java 9, the java version was in minor, from 9 onward it was in major
if [ "$java_version_major_" -gt 1 ]; then
java_version_number_="$java_version_major_"
else
java_version_number_="$(echo "$java_version_" | sed 's/[0-9]*\.\([0-9]*\).*/\1/')" || fail "Failed to parse Java version major ($?)"
fi
# If the user specified a specific version, use it no matter what.
if [ "$java_version_forced" = 'true' ]; then
if [ "$java_version_number_" -eq "$java_version" ]; then
return 0
else
return 1
fi
else # Otherwise: we can use it if the version is high enough
if [ "$java_version_number_" -ge "$java_min_version" ]; then
return 0
else
log "java on PATH is too old ($java_version_, at least $java_min_version required)"
return 1
fi
fi
else
log "java on PATH has unrecognized version ($java_version_)"
if [ "$java_version_forced" = 'true' ]; then
log "Using Wemi OpenJDK"
return 1
else
log "Using java on PATH - to force Wemi OpenJDK, put '--java=$java_min_version' as the first argument or set 'export WEMI_JAVA=$java_min_version' (or higher version)"
return 0
fi
fi
}
# Get command to execute 'java'
fetch_java() {
if [ -n "$java_exe" ]; then
echo "${java_exe}"
return 0
fi
if can_use_java_from_path; then
echo 'java'
return 0
fi
jdk_home_="$(fetch_jdk)" || fail "Failed to fetch JDK (release ${jdk_release})"
if [ ! -d "${jdk_home_?:EMPTY_JDK_HOME}" ]; then
fail "Failed to obtain JDK (release: ${jdk_release})"
fi
echo "${jdk_home_}/bin/java"
}
fetch_wemi() {
if [ -n "$WEMI_DIST" ]; then
if [ -d "$WEMI_DIST" ]; then
echo "$WEMI_DIST"
return 0
fi
fail "WEMI_DIST specified, but '${WEMI_DIST}' is not a directory (pwd=$(pwd))"
fi
# Contains the Wemi classpath jars
wemi_dist_dir_="${wemi_home_dir}/wemi/${wemi_version}"
# List of mirrors in order of preference
wemi_url_darkyen_="https://darkyen.com/wemi/${wemi_version}/wemi.tar.gz"
wemi_url_github_="https://github.com/Darkyenus/wemi/releases/download/${wemi_version}/wemi.tar.gz"
redownload_='false'
if [ "${wemi_version%-SNAPSHOT}" != "${wemi_version}" ]; then
# Snapshot, those are not uploaded to GitHub
wemi_url_github_=""
# If the snapshot directory exists and is less than 1 hour old, we should download a new snapshot
if [ -z "$(find "$wemi_dist_dir_" -type d -name "$(basename "$wemi_dist_dir_")" -mtime -1 -print 2>/dev/null)" ]; then
log "Checking for a newer Wemi snapshot"
redownload_='true'
fi
fi
if [ ! -e "$wemi_dist_dir_" ] || [ "$redownload_" = 'true' ]; then
wemi_dist_download_file_="${wemi_dist_dir_}.downloading"
log "Downloading Wemi distribution (version ${wemi_version})"
if ( download_to_file "$wemi_url_darkyen_" "$wemi_dist_download_file_" ) || ( [ -n "$wemi_url_github_" ] && ( download_to_file "$wemi_url_github_" "$wemi_dist_download_file_" ) ); then
# Success
rm -rf "${wemi_dist_dir_?:EMPTY_PATH}"
extract_targz "$wemi_dist_download_file_" "${wemi_dist_dir_}"
rm "$wemi_dist_download_file_" || log "Failed to remove downloaded Wemi archive (rm returned $?)"
elif [ -e "$wemi_dist_dir_" ]; then
# Failure, but we can use old version
log "Failed to download newer Wemi snapshot, continuing with the old one"
else
# Failure unfixable
fail "Failed to download Wemi. Make sure that your internet connection works and then try again."
fi
fi
echo "$wemi_dist_dir_"
}
# Given a path $1, return its native form.
# Needed only on Windows, where cygwin & others use UNIX-like paths, which other programs don't understand
nativize_path() {
if [ "$jdk_os" = 'windows' ] && command_exists cygpath; then
cygpath -w "$1"
else
echo "$1"
fi
}
# Launch as a Wemi launcher
launch_wemi() {
java_exe_="$(fetch_java)" || fail "Failed to prepare java"
wemi_dist_dir_="$(fetch_wemi)" || fail "Failed to prepare Wemi"
java_options_="${WEMI_JAVA_OPTS}"
wemi_root_="$(dirname "$0")" || fail "Failed to derive Wemi root"
wemi_root_="${WEMI_ROOT:-$wemi_root_}"
wemi_dist_dir_="$(nativize_path "$wemi_dist_dir_")" || fail "Failed to nativize Wemi installation directory path"
wemi_root_="$(nativize_path "$wemi_root_")" || fail "Failed to nativize Wemi project root path"
while true; do
# shellcheck disable=SC2086
"$java_exe_" $java_options_ -classpath "${wemi_dist_dir_}/*" 'wemi.boot.Launch' --root="$wemi_root_" --reload-supported "$@"
exit_code_="$?"
if [ "$exit_code_" -ne 6 ]; then # Wemi reload-requested exit code
exit "$exit_code_"
fi
done
}
if [ "$print_java_exe" = 'true' ]; then
nativize_path "$(fetch_java)" || fail "Failed to fetch Java"
elif [ "$print_wemi_home" = 'true' ]; then
nativize_path "$(fetch_wemi)" || fail "Failed to fetch Wemi"
else
launch_wemi "$@"
fi