mirror of
https://github.com/halcyon/asdf-java.git
synced 2024-10-06 07:42:09 +08:00
b0dcb5ae05
Before deleting the temporary directory (`$TEMP_DIR`) at the end of the script, make sure that it exists so that we don't accidentally delete the user's home directory. Fixes #63
185 lines
6.6 KiB
Bash
Executable File
185 lines
6.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -e
|
|
set -Euo pipefail
|
|
|
|
PLUGIN_HOME="$(dirname "$(dirname "${0}")")"
|
|
CACHE_DIR="${TMPDIR:-/tmp}/asdf-java.cache"
|
|
trap 'test -d "${TEMP_DIR}" && rm -rf "${TEMP_DIR}"' EXIT
|
|
|
|
if [ ! -d "${CACHE_DIR}" ]
|
|
then
|
|
mkdir -p "${CACHE_DIR}"
|
|
fi
|
|
|
|
case $(uname -s) in
|
|
Darwin) OS="mac"
|
|
SHA256SUM="gsha256sum"
|
|
STAT="/usr/bin/stat -f %c ${CACHE_DIR}/*"
|
|
TEMP_DIR=$(/usr/bin/mktemp -dt asdf-java)
|
|
;;
|
|
Linux) OS="linux"
|
|
SHA256SUM="sha256sum"
|
|
STAT="stat -c %Z ${CACHE_DIR}/*"
|
|
TEMP_DIR=$(mktemp -dp /tmp asdf-java.XXXXXXXX)
|
|
;;
|
|
esac
|
|
|
|
case $(uname -m) in
|
|
x86_64) ARCHITECTURE="x64" ;;
|
|
esac
|
|
|
|
function check-jq() {
|
|
USAGE="Install jq to continue. Aborting."
|
|
|
|
if ! [ -x "$(command -v jq)" ]; then
|
|
echo "${USAGE}" >&2
|
|
exit 1;
|
|
fi
|
|
}
|
|
|
|
function retrieve-adoptopenjdk() {
|
|
URLS=("https://api.adoptopenjdk.net/v3/assets/feature_releases/{8}/ga?architecture=${ARCHITECTURE}&os=${OS}&image_type=jdk&page=0&page_size=100&project=jdk&sort_order=ASC&vendor=adoptopenjdk"
|
|
"https://api.adoptopenjdk.net/v3/assets/feature_releases/{9}/ga?architecture=${ARCHITECTURE}&os=${OS}&image_type=jdk&page=0&page_size=100&project=jdk&sort_order=ASC&vendor=adoptopenjdk"
|
|
"https://api.adoptopenjdk.net/v3/assets/feature_releases/{10}/ga?architecture=${ARCHITECTURE}&os=${OS}&image_type=jdk&page=0&page_size=100&project=jdk&sort_order=ASC&vendor=adoptopenjdk"
|
|
"https://api.adoptopenjdk.net/v3/assets/feature_releases/{11}/ga?architecture=${ARCHITECTURE}&os=${OS}&image_type=jdk&page=0&page_size=100&project=jdk&sort_order=ASC&vendor=adoptopenjdk"
|
|
"https://api.adoptopenjdk.net/v3/assets/feature_releases/{12}/ga?architecture=${ARCHITECTURE}&os=${OS}&image_type=jdk&page=0&page_size=100&project=jdk&sort_order=ASC&vendor=adoptopenjdk"
|
|
"https://api.adoptopenjdk.net/v3/assets/feature_releases/{13}/ga?architecture=${ARCHITECTURE}&os=${OS}&image_type=jdk&page=0&page_size=100&project=jdk&sort_order=ASC&vendor=adoptopenjdk")
|
|
|
|
# shellcheck disable=SC2046
|
|
if [[ -z "$(ls -A "${CACHE_DIR}"/adopt-*.json 2>/dev/null)" ]] || [[ $(set -- $(${STAT}) && echo "${1}") -le $(( $(date +%s) - 3600)) ]]
|
|
then
|
|
for url in "${URLS[@]}"
|
|
do
|
|
curl -f -s -H "Accept: application/json" -L "${url}" -# -w "%{filename_effective}\n" -o "${CACHE_DIR}/adopt-#1.json" > /dev/null 2>&1
|
|
done
|
|
for i in "${CACHE_DIR}"/adopt-*.json
|
|
do
|
|
[[ -e "$i" ]] || break
|
|
jq '(.[].release_name) |= sub("jdk-";"adopt-openjdk-") | (.[].release_name) |= sub("^jdk";"adopt-openjdk-")' \
|
|
"${i}" > "${i}.temp"
|
|
mv "${i}.temp" "${i}"
|
|
done
|
|
fi
|
|
}
|
|
|
|
function retrieve-sapmachine() {
|
|
args=('-s' '-f')
|
|
if [[ -n "${GITHUB_API_TOKEN:-}" ]]; then
|
|
args+=('-H' "Authorization: token $GITHUB_API_TOKEN")
|
|
fi
|
|
local cache_file="${CACHE_DIR}/sapmachine.json"
|
|
local filter='[
|
|
.[] | select(.prerelease == false and .draft == false)
|
|
| {
|
|
release_name: .name,
|
|
binaries: [ .assets[]
|
|
| select(.content_type == "application/x-tar")
|
|
| select(.name | startswith("sapmachine-jdk"))
|
|
| select(.name | endswith("linux-x64_bin.tar.gz") or endswith("osx-x64_bin.tar.gz"))
|
|
| {
|
|
package: {
|
|
binary_link: .browser_download_url,
|
|
checksum_link: (.browser_download_url | sub("tar\\.gz$"; "sha256.txt"))
|
|
},
|
|
os: (if .name | endswith("osx-x64_bin.tar.gz") then "mac" else "linux" end),
|
|
architecture: "x64",
|
|
jvm_impl: "hotspot",
|
|
heap_size: "normal"
|
|
}
|
|
]}
|
|
]'
|
|
# shellcheck disable=SC2046
|
|
if [[ ! -r "${cache_file}" ]] || [[ $(set -- $(${STAT}) && echo "${1}") -le $(( $(date +%s) - 3600)) ]]
|
|
then
|
|
curl "${args[@]}" -L 'https://api.github.com/repos/SAP/SapMachine/releases' -o "${cache_file}"
|
|
jq "${filter}" "${cache_file}" > "${cache_file}.temp"
|
|
mv "${cache_file}.temp" "${cache_file}"
|
|
fi
|
|
}
|
|
|
|
|
|
function all-json() {
|
|
check-jq
|
|
jq -s 'add' "${CACHE_DIR}"/*.json "${PLUGIN_HOME}"/corretto/corretto.json "${PLUGIN_HOME}"/zulu/zulu.json
|
|
}
|
|
|
|
|
|
function list-all() {
|
|
check-jq
|
|
retrieve-adoptopenjdk
|
|
retrieve-sapmachine
|
|
local hotspot="map(select(.binaries[].jvm_impl == \"hotspot\")) \
|
|
| map(.release_name) | unique[]"
|
|
local openj9_normal_heap="map(select(.binaries[].heap_size == \"normal\")) \
|
|
| map(.release_name) | unique[] | select(contains(\"openj9\"))"
|
|
local openj9_large_heap="map(select(.binaries[].heap_size == \"large\")) \
|
|
| map(.release_name + \"_large-heap\") | unique[] | select(contains(\"openj9\"))"
|
|
# shellcheck disable=SC2046
|
|
echo $(all-json | jq -r "${hotspot}" ; all-json | jq -r "${openj9_normal_heap}" ; all-json | jq -r "${openj9_large_heap}")
|
|
}
|
|
|
|
function list-legacy-filenames() {
|
|
echo ".java-version"
|
|
}
|
|
|
|
function install {
|
|
check-jq
|
|
case "${ASDF_INSTALL_VERSION}" in
|
|
*_large-heap) RELEASE="${ASDF_INSTALL_VERSION%%_large-heap}"
|
|
HEAP_SIZE="large"
|
|
;;
|
|
*) RELEASE="${ASDF_INSTALL_VERSION}"
|
|
HEAP_SIZE="normal"
|
|
;;
|
|
esac
|
|
|
|
retrieve-adoptopenjdk
|
|
retrieve-sapmachine
|
|
local select_release="map(select(.release_name==\"${RELEASE}\")) | unique[] | .binaries[]"
|
|
local select_binary="select(.os==\"${OS}\" and .architecture==\"${ARCHITECTURE}\" and .heap_size==\"${HEAP_SIZE}\")"
|
|
local binary
|
|
local binary_link
|
|
local checksum_link
|
|
binary=$(all-json | jq "$select_release | $select_binary")
|
|
binary_link=$(set -- "$(echo "${binary}" | jq -r ".package.link")" ; echo "${1}")
|
|
checksum_link=$(set -- "$(echo "${binary}" | jq -r ".package.checksum_link")" ; echo "${1}")
|
|
|
|
cd "${TEMP_DIR}"
|
|
if ! curl -LO -# -w "%{filename_effective}\n" "${binary_link}";
|
|
then
|
|
exit 1
|
|
fi
|
|
|
|
if ! curl -LO -# -w "%{filename_effective}\n" "${checksum_link}";
|
|
then
|
|
exit 1
|
|
fi
|
|
|
|
${SHA256SUM} -c "$(basename "${checksum_link}")"
|
|
|
|
tar -zxf "$(basename "${binary_link}")"
|
|
dir=$(set -- "$(ls -d ./*/)" ; echo "${1}")
|
|
cd "${dir}"
|
|
if [ ! -d "${ASDF_INSTALL_PATH}" ]
|
|
then
|
|
mkdir -p "${ASDF_INSTALL_PATH}"
|
|
fi
|
|
|
|
case ${OS} in
|
|
mac) case ${RELEASE} in
|
|
azul*) mv ./* "${ASDF_INSTALL_PATH}" ;;
|
|
*) mv Contents/Home/* "${ASDF_INSTALL_PATH}" ;;
|
|
esac ;;
|
|
*) mv ./* "${ASDF_INSTALL_PATH}" ;;
|
|
esac
|
|
}
|
|
|
|
case "$(basename "${0}")" in
|
|
list-all) list-all
|
|
;;
|
|
list-legacy-filenames) list-legacy-filenames
|
|
;;
|
|
install) install
|
|
;;
|
|
esac
|