asdf-java/bin/functions

182 lines
5.8 KiB
Plaintext
Raw Normal View History

2019-09-10 02:30:31 +08:00
#!/usr/bin/env bash
2020-02-29 23:21:47 +08:00
set -Eeuo pipefail
2019-09-10 02:30:31 +08:00
2020-02-26 22:25:44 +08:00
PLUGIN_HOME="$(dirname "$(dirname "${0}")")"
CACHE_DIR="${TMPDIR:-/tmp/}asdf-java.cache"
2020-02-26 22:25:44 +08:00
trap 'rm -rf ${TEMP_DIR}' EXIT
if [ ! -d "${CACHE_DIR}" ]
then
mkdir -p "${CACHE_DIR}"
fi
2019-09-11 00:57:28 +08:00
case $(uname -s) in
Darwin) OS="mac"
2019-09-10 03:44:40 +08:00
SHA256SUM="gsha256sum"
STAT="/usr/bin/stat -f %c ${CACHE_DIR}/*"
TEMP_DIR=$(/usr/bin/mktemp -dt asdf-java)
2019-09-10 02:30:31 +08:00
;;
2019-09-11 00:57:28 +08:00
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() {
2020-02-26 22:25:44 +08:00
USAGE="Install jq to continue. Aborting."
if ! [ -x "$(command -v jq)" ]; then
2020-02-26 22:25:44 +08:00
echo "${USAGE}" >&2
exit 1;
fi
}
function retrieve-adoptopenjdk() {
2019-09-10 02:30:31 +08:00
URLS=("https://api.adoptopenjdk.net/v2/info/releases/{openjdk8}?type=jdk"
"https://api.adoptopenjdk.net/v2/info/releases/{openjdk9}?type=jdk"
"https://api.adoptopenjdk.net/v2/info/releases/{openjdk10}?type=jdk"
"https://api.adoptopenjdk.net/v2/info/releases/{openjdk11}?type=jdk"
2019-09-18 08:32:46 +08:00
"https://api.adoptopenjdk.net/v2/info/releases/{openjdk12}?type=jdk"
"https://api.adoptopenjdk.net/v2/info/releases/{openjdk13}?type=jdk")
2019-09-10 02:30:31 +08:00
2020-02-26 22:25:44 +08:00
# shellcheck disable=SC2046
if [[ -z "$(ls -A "${CACHE_DIR}"/adopt-*.json 2>/dev/null)" ]] || [[ $(set -- $(${STAT}) && echo "${1}") -le $(( $(date +%s) - 3600)) ]]
2019-09-10 02:30:31 +08:00
then
for url in "${URLS[@]}"
do
2020-02-29 23:21:47 +08:00
curl -skL "${url}" -# -w "%{filename_effective}\n" -o "${CACHE_DIR}/adopt-#1.json" > /dev/null 2>&1
2019-09-10 02:30:31 +08:00
done
2020-02-26 22:25:44 +08:00
for i in "${CACHE_DIR}"/adopt-*.json
2019-09-10 02:30:31 +08:00
do
2020-02-26 22:25:44 +08:00
[[ -e "$i" ]] || break
2019-09-10 02:30:31 +08:00
jq '(.[].release_name) |= sub("jdk-";"adopt-openjdk-") | (.[].release_name) |= sub("^jdk";"adopt-openjdk-")' \
2020-02-26 22:25:44 +08:00
"${i}" > "${i}.temp"
mv "${i}.temp" "${i}"
2019-09-10 02:30:31 +08:00
done
fi
}
function retrieve-sapmachine() {
2020-02-29 23:21:47 +08:00
args=('-skfL')
2020-02-26 22:25:44 +08:00
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"))
| {
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",
openjdk_impl: "hotspot",
heap_size: "normal"
}
]}
]'
2020-02-26 22:25:44 +08:00
# shellcheck disable=SC2046
if [[ ! -r "${cache_file}" ]] || [[ $(set -- $(${STAT}) && echo "${1}") -le $(( $(date +%s) - 3600)) ]]
then
2020-02-29 23:21:47 +08:00
curl "${args[@]}" '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
}
2019-09-10 02:30:31 +08:00
function all-json() {
check-jq
2020-02-26 22:25:44 +08:00
jq -s 'add' "${CACHE_DIR}"/*.json "${PLUGIN_HOME}"/corretto/corretto.json "${PLUGIN_HOME}"/zulu/zulu.json
2019-09-10 02:30:31 +08:00
}
function list-all() {
check-jq
retrieve-adoptopenjdk
retrieve-sapmachine
2019-09-10 02:30:31 +08:00
local hotspot="map(select(.binaries[].openjdk_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\"))"
2020-02-26 22:25:44 +08:00
# shellcheck disable=SC2046
2019-09-10 02:30:31 +08:00
echo $(all-json | jq -r "${hotspot}" ; all-json | jq -r "${openj9_normal_heap}" ; all-json | jq -r "${openj9_large_heap}")
}
2019-11-22 23:18:54 +08:00
function list-legacy-filenames() {
echo ".java-version"
}
2019-09-10 02:30:31 +08:00
function install {
check-jq
2020-02-26 22:25:44 +08:00
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
2019-09-11 00:57:28 +08:00
local select_release="map(select(.release_name==\"${RELEASE}\")) | unique[] | .binaries[]"
local select_binary="select(.os==\"${OS}\" and .architecture==\"${ARCHITECTURE}\" and .heap_size==\"${HEAP_SIZE}\")"
2020-02-26 22:25:44 +08:00
local binary
local binary_link
local checksum_link
binary=$(all-json | jq "$select_release | $select_binary")
binary_link=$(set -- "$(echo "${binary}" | jq -r ".binary_link")" ; echo "${1}")
checksum_link=$(set -- "$(echo "${binary}" | jq -r ".checksum_link")" ; echo "${1}")
cd "${TEMP_DIR}"
2020-02-29 23:21:47 +08:00
if ! curl -kLO -# -w "%{filename_effective}\n" "${binary_link}";
2019-09-10 02:30:31 +08:00
then
exit 1
fi
2020-02-29 23:21:47 +08:00
if ! curl -kLO -# -w "%{filename_effective}\n" "${checksum_link}";
2019-09-10 02:30:31 +08:00
then
exit 1
fi
2020-02-26 22:25:44 +08:00
${SHA256SUM} -c "$(basename "${checksum_link}")"
2019-09-10 02:30:31 +08:00
2020-02-26 22:25:44 +08:00
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
2019-09-10 02:30:31 +08:00
2019-09-13 04:40:16 +08:00
case ${OS} in
2019-09-19 04:12:14 +08:00
mac) case ${RELEASE} in
2020-02-26 22:25:44 +08:00
azul*) mv ./* "${ASDF_INSTALL_PATH}" ;;
*) mv Contents/Home/* "${ASDF_INSTALL_PATH}" ;;
2019-09-10 02:30:31 +08:00
esac ;;
2020-02-26 22:25:44 +08:00
*) mv ./* "${ASDF_INSTALL_PATH}" ;;
2019-09-10 02:30:31 +08:00
esac
}
2020-02-26 22:25:44 +08:00
case "$(basename "${0}")" in
2019-09-10 02:30:31 +08:00
list-all) list-all
;;
2019-11-22 23:18:54 +08:00
list-legacy-filenames) list-legacy-filenames
;;
2019-09-10 02:30:31 +08:00
install) install
;;
esac