Skip to content

Commit eb759df

Browse files
committed
Liberica now also has macOS Integration support
With the help of BellLabs (bell-sw/Liberica#42) we now also have Liberica macOS integration support. It doesn't work for all versions we can install, so it logs a warning in case it's not possible to use their "trick". For this to work we depend on @joschi java-metadata project. This project provides the shasum (and with that the check if the .pkg version exists). Other things done: - Fail if commands fail - Fixed unzip check - All error messages go to stderr - Aligned indent to 4 spaces - Removed all echo's in favor of printf in line with asdf-vm/asdf#806 - Use the bash [[ conditions
1 parent 0010d1f commit eb759df

3 files changed

Lines changed: 81 additions & 36 deletions

File tree

.github/workflows/tests.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ jobs:
3737
. asdf/asdf.sh
3838
asdf plugin-test java "$GITHUB_WORKSPACE" --asdf-plugin-gitref "$GITHUB_SHA" --asdf-tool-version adoptopenjdk-8.0.252+9.1.openj9-0.20.0 java -version
3939
- name: macOS Check java_home integration
40+
if: matrix.os == 'macOS-latest'
4041
env:
4142
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4243
run: |
4344
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
4445
echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}"
4546
. asdf/asdf.sh
4647
asdf plugin-test java "$GITHUB_WORKSPACE" --asdf-plugin-gitref "$GITHUB_SHA" --asdf-tool-version zulu-8.52.0.23 /usr/libexec/java_home -V 2>&1 | grep "Zulu 8.52.0.23"
48+
- name: macOS Check java_home integration Liberica
4749
if: matrix.os == 'macOS-latest'
50+
env:
51+
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52+
run: |
53+
export ASDF_CONFIG_FILE=${HOME}"/.asdfrc"
54+
echo "java_macos_integration_enable = yes" > "${ASDF_CONFIG_FILE}"
55+
. asdf/asdf.sh
56+
asdf plugin-test java "$GITHUB_WORKSPACE" --asdf-plugin-gitref "$GITHUB_SHA" --asdf-tool-version liberica-11.0.4 /usr/libexec/java_home -V 2>&1 | grep "OpenJDK 11.0.4-BellSoft"

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ Setting java_macos_integration_enable to yes on `.asdfrc` file enables this inte
6161
java_macos_integration_enable = yes
6262
```
6363

64-
_Note: Not all distributions of Java JDK packages offer this integration (eg. liberica). This option only works for packages that **do offer** that integration._
64+
### Liberica
65+
The Liberica distributions this plugin uses do not [contain the needed files for the macOS integration](https://github.com/bell-sw/Liberica/issues/42).
66+
The plugin will try to download the `.pkg` distribution for the same version, but this does not always exist.
67+
If it doesn't exist this plugin cannot provide the macOS Integration for that version. It will log a warning when that happens.

bin/functions

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/usr/bin/env bash
2+
# If a command fails we want to abort right away, and we do not want to use unset variables
3+
set -euo pipefail
4+
25
# shellcheck source=/dev/null
3-
source "$ASDF_DIR/lib/utils.bash"
6+
source "${ASDF_DIR}/lib/utils.bash"
47
CACHE_DIR="${TMPDIR:-/tmp}/asdf-java.cache"
58

6-
if [ ! -d "${CACHE_DIR}" ]
9+
if [[ ! -d "${CACHE_DIR}" ]]
710
then
811
mkdir -p "${CACHE_DIR}"
912
fi
@@ -24,7 +27,7 @@ case "${KERNEL_NAME}" in
2427
STAT_OPTS=('-c' '%Z')
2528
TEMP_DIR=$(mktemp -dp /tmp asdf-java.XXXXXXXX)
2629
;;
27-
*) echo "Unknown operating system: ${KERNEL_NAME}"
30+
*) >&2 printf "Unknown operating system: %s\n" "${KERNEL_NAME}"
2831
exit 1
2932
esac
3033

@@ -35,17 +38,17 @@ case "${MACHINE}" in
3538
x86_64) ARCHITECTURE="x86_64" ;;
3639
aarch64|arm64) ARCHITECTURE="aarch64" ;;
3740
armv7l) ARCHITECTURE="arm32-vfp-hflt" ;;
38-
*) echo "Unknown machine architecture: ${MACHINE}"
41+
*) >&2 printf "Unknown machine architecture: %s\n" "${MACHINE}"
3942
exit 1
4043
esac
4144

4245
function check-unzip() {
43-
USAGE="Install unzip to continue. Aborting."
46+
USAGE="Install unzip to continue. Aborting."
4447

45-
if ! [ -x "$(command -v unzip)" ]; then
46-
echo "${USAGE}" >&2
47-
exit 1;
48-
fi
48+
if ! [[ -x "$(command -v unzip)" ]]; then
49+
>&2 printf "%s\n" "${USAGE}"
50+
exit 1
51+
fi
4952
}
5053

5154
function retrieve-release-data() {
@@ -63,7 +66,7 @@ function list-all() {
6366
}
6467

6568
function list-legacy-filenames() {
66-
echo ".java-version"
69+
printf ".java-version\n"
6770
}
6871

6972
function install {
@@ -74,38 +77,64 @@ function install {
7477

7578
release_data=$(grep "^${ASDF_INSTALL_VERSION} " "${CACHE_DIR}/releases.tsv" | tail -n 1)
7679
if [[ -z "${release_data}" ]]; then
77-
echo "Unknown release: ${ASDF_INSTALL_VERSION}"
80+
>&2 printf "Unknown release: %s\n" "${ASDF_INSTALL_VERSION}"
7881
exit 1
7982
fi
8083

8184
package_filename=$(cut -d $'\t' -f 2 <<<"${release_data}")
8285
package_link=$(cut -d $'\t' -f 3 <<<"${release_data}")
8386
checksum=$(cut -d $'\t' -f 4 <<<"${release_data}")
8487

85-
if [[ "${package_filename}" =~ "zip$" ]]; then
88+
if [[ "${package_filename: -4}" == ".zip" ]]; then
8689
check-unzip
8790
fi
8891

89-
cd "${TEMP_DIR}" || return 1
90-
if ! curl -LO -# -w "${package_filename}\n" "${package_link}"; then
91-
exit 1
92+
cd "${TEMP_DIR}"
93+
94+
if [[ "${OS}" == "macosx" &&
95+
"${ASDF_INSTALL_VERSION}" == "liberica"* &&
96+
"$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]]; then
97+
# change package_filename, link and checksum
98+
new_package_filename="${package_filename/tar.gz/pkg}"
99+
new_package_filename="${new_package_filename/zip/pkg}"
100+
http_status=$(curl -s -o sha256 -w "%{http_code}" "https://joschi.github.io/java-metadata/checksums/liberica/${new_package_filename}.sha256")
101+
new_checksum=$(cat sha256)
102+
rm sha256
103+
if [[ "${http_status}" = "200" && "${#new_checksum}" -gt 0 ]]; then
104+
package_filename=${new_package_filename}
105+
package_link=${package_link/tar.gz/pkg}
106+
package_link=${package_link/zip/pkg}
107+
IFS=" " read -r -a checksum_parts <<< "${new_checksum}"
108+
checksum=${checksum_parts[0]}
109+
else
110+
>&2 printf "Could not find a PKG version of %s so the macOS java_home integration will not work for this version\nSee https://github.com/halcyon/asdf-java/blob/master/README.md for more information\n" "${ASDF_INSTALL_VERSION}"
111+
fi
92112
fi
93113

114+
curl -LO -# -w "${package_filename}\n" "${package_link}"
115+
94116
${SHA256SUM} -c <<<"${checksum} ${package_filename}"
95117

96118
case "${package_filename}" in
97-
*zip) unzip "${package_filename}"
119+
*zip) unzip -q "${package_filename}"
98120
;;
99121
*tar.gz) tar xf "${package_filename}"
100122
;;
101-
*) echo "Cannot extract ${package_filename}"
123+
*pkg) mkdir "${ASDF_INSTALL_VERSION}"
124+
cd "${ASDF_INSTALL_VERSION}"
125+
pkgutil --expand "../${package_filename}" tmp
126+
gunzip -dc < tmp/*.pkg/Payload | cpio -i
127+
rm -r tmp
128+
cd ..
129+
;;
130+
*) >&2 printf "Cannot extract %s\n" "${package_filename}"
102131
exit 1
103132
;;
104133
esac
105134

106135
read -r -a dirs <<<"$(ls -d ./*/)"
107-
cd "${dirs[0]}" || return 1
108-
if [ ! -d "${ASDF_INSTALL_PATH}" ]; then
136+
cd "${dirs[0]}"
137+
if [[ ! -d "${ASDF_INSTALL_PATH}" ]]; then
109138
mkdir -p "${ASDF_INSTALL_PATH}"
110139
fi
111140

@@ -114,21 +143,26 @@ function install {
114143
case ${ASDF_INSTALL_VERSION} in
115144
zulu*)
116145
mv ./* "${ASDF_INSTALL_PATH}"
117-
if [ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]; then
146+
if [[ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]]; then
118147
local macOS_integration_path
119148
macOS_integration_path="$(dirname "$(dirname "$(dirname "$(absolute_dir_path "${ASDF_INSTALL_PATH}/bin/..")")")")"
120149
java_macos_integration_install "$macOS_integration_path"
121150
fi
122151
;;
123152
liberica*)
124-
mv ./* "${ASDF_INSTALL_PATH}"
125-
if [ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]; then
126-
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."
153+
if [[ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" &&
154+
"${package_filename}" = *".pkg" ]]; then
155+
mv Contents/Home/* "${ASDF_INSTALL_PATH}"
156+
local macOS_integration_path
157+
macOS_integration_path="$(absolute_dir_path ".")"
158+
java_macos_integration_install "$macOS_integration_path"
159+
else
160+
mv ./* "${ASDF_INSTALL_PATH}"
127161
fi
128162
;;
129-
*)
163+
*)
130164
mv Contents/Home/* "${ASDF_INSTALL_PATH}"
131-
if [ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]; then
165+
if [[ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]]; then
132166
local macOS_integration_path
133167
macOS_integration_path="$(absolute_dir_path ".")"
134168
java_macos_integration_install "$macOS_integration_path"
@@ -142,20 +176,18 @@ function install {
142176
function uninstall {
143177
case ${OS} in
144178
macosx)
145-
if [ -z "${ASDF_INSTALL_VERSION}" ]; then
146-
true
147-
else
148-
if [ "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]; then
149-
java_macos_integration_remove
150-
fi
151-
fi
179+
if [[ -n "${ASDF_INSTALL_VERSION}" && "$(get_asdf_config_value "java_macos_integration_enable")" = "yes" ]]; then
180+
java_macos_integration_remove
181+
fi
152182
esac
153183
rm -rf "${ASDF_INSTALL_PATH}"
154184
}
155185

156186
function java_macos_integration_remove {
157-
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}"
158-
sudo rm -rf "/Library/Java/JavaVirtualMachines/${ASDF_INSTALL_VERSION}"
187+
if [[ -d "/Library/Java/JavaVirtualMachines/${ASDF_INSTALL_VERSION}" ]]; then
188+
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}"
189+
sudo rm -rf "/Library/Java/JavaVirtualMachines/${ASDF_INSTALL_VERSION}"
190+
fi
159191
}
160192

161193
function java_macos_integration_install {
@@ -181,5 +213,6 @@ case "$(basename "${0}")" in
181213
;;
182214
install) install
183215
;;
184-
uninstall) uninstall ;;
216+
uninstall) uninstall
217+
;;
185218
esac

0 commit comments

Comments
 (0)