Skip to content

Instantly share code, notes, and snippets.

@Frederick888
Last active September 7, 2020 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Frederick888/40f4ee2545e13b22ae91e801d257812c to your computer and use it in GitHub Desktop.
Save Frederick888/40f4ee2545e13b22ae91e801d257812c to your computer and use it in GitHub Desktop.
JetBrains Java SDK Management Scripts
#!/usr/bin/env bash
# shellcheck disable=SC2001
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
OS=$(uname)
case "$OS" in
Linux)
OS="${OS,,}"
;;
Darwin)
OS="osx"
;;
*)
printf 'Unsupported system\n'
exit 1
;;
esac
# get SDK version in the format of EDITIONuVERSIONbBUILD e.g. 11u06b722
function get_sdk_version() {
sed 's/jbr\?sdk-\?\([[:digit:]]\+\)[u_]\([[:digit:]]\+\)[_-]\?\([[:digit:]]\+\)\(-[[:alpha:]]\+-[xi][[:digit:]]\+-\)\?\(b[[:digit:]]\+\)\..*/\1u\2\3\5/' <<< "$1"
}
function compare_sdk_versions() {
readarray -d $'\t' L <<< "$(sed "s/^0*\([0-9]\+\)u0*\([0-9]\+\)b0*\([0-9]\+\)$/\1\t\2\t\3/" <<< "$1")"
readarray -d $'\t' R <<< "$(sed "s/^0*\([0-9]\+\)u0*\([0-9]\+\)b0*\([0-9]\+\)$/\1\t\2\t\3/" <<< "$2")"
for i in "${!L[@]}"; do
if [[ "${L[$i]}" -lt "${R[$i]}" ]]; then
printf -- '-1'
return 0
elif [[ "${L[$i]}" -gt "${R[$i]}" ]]; then
printf '1'
return 0
fi
done
printf '0'
}
function get_download_link() {
printf 'https://bintray.com/jetbrains/intellij-jbr/download_file?file_path=%s' "$1"
}
# list local SDKs
readarray -d $'\n' -t SDK_LIST <<< "$(find "$DIR" -maxdepth 1 -type f \( -name "jbrsdk-11*.tar.gz" -or -name "jbrsdk-8*.tar.gz" \) -exec basename '{}' \; | sort)"
printf 'Fetching file list...\n\n'
# list remote SDKs via Bintray API
for sdk_edition in '8' '11'; do
DOWNLOADABLE_FILES_VAR_NAME="DOWNLOADABLE_FILES_$sdk_edition"
BINTRAY_API="https://api.bintray.com/packages/jetbrains/intellij-jbr/jbrsdk$sdk_edition-$OS-x64/files"
readarray -d $'\n' -t "$DOWNLOADABLE_FILES_VAR_NAME" <<< "$(curl $BINTRAY_API 2>/dev/null | jq -r '[.[] | select(.path | contains("sdk") and (contains("checksum") | not))] | sort_by(.created) | reverse | .[].path')"
done
declare -a TO_DOWNLOAD=()
declare -a TO_REMOVE=()
# find latest builds for local SDKs
printf "INSTALLED\t\t\t\tLATEST\n"
for sdk in "${SDK_LIST[@]}"; do
SDK_VERSION=$(get_sdk_version "$sdk")
FILE_NAME=""
sdk_edition="$(cut -d 'u' -f 1 <<< "$SDK_VERSION")"
DOWNLOADABLE_FILES_VAR_NAME="DOWNLOADABLE_FILES_${sdk_edition}[@]"
for sdk_remote in "${!DOWNLOADABLE_FILES_VAR_NAME}"; do
SDK_REMOTE_VERSION=$(get_sdk_version "$sdk_remote")
if [[ "$SDK_VERSION" == "$SDK_REMOTE_VERSION" ]]; then
# find out the filename of the latest build in DOWNLOADABLE_FILES_x, which was sorted by creation date descendingly
FILE_NAME="$sdk_remote"
break
fi
done
if [[ "$sdk" == "$FILE_NAME" ]]; then
printf '%s\t(UP-TO-DATE)\n' "$sdk"
else
printf '%s\t%s\n' "$sdk" "$FILE_NAME"
DOWNLOAD_LINK=$(get_download_link "$FILE_NAME")
TO_DOWNLOAD+=( "$DOWNLOAD_LINK" )
TO_REMOVE+=( "$sdk" )
fi
done
# find the latest remote SDK
declare -A LATEST_INSTALLED
declare -A LATEST_AVAILABLE
declare -A LATEST_LINK
for sdk_edition in '8' '11'; do
for sdk in "${SDK_LIST[@]}"; do
SDK_VERSION=$(get_sdk_version "$sdk")
if [[ "$SDK_VERSION" == "$sdk_edition""u"* && ( -z "${LATEST_INSTALLED[$sdk_edition]}" || $(compare_sdk_versions "$SDK_VERSION" "${LATEST_INSTALLED[$sdk_edition]}") -gt 0 ) ]]; then
LATEST_INSTALLED[$sdk_edition]="$SDK_VERSION"
fi
done
DOWNLOADABLE_FILES_VAR_NAME="DOWNLOADABLE_FILES_${sdk_edition}[@]"
# we have to traverse through the whole list as new builds of old versions can be released after new versions
for sdk in "${!DOWNLOADABLE_FILES_VAR_NAME}"; do
SDK_VERSION=$(get_sdk_version "$sdk")
if [[ "$SDK_VERSION" == "$sdk_edition""u"* && ( -z "${LATEST_AVAILABLE[$sdk_edition]}" || $(compare_sdk_versions "$SDK_VERSION" "${LATEST_AVAILABLE[$sdk_edition]}") -gt 0 ) ]]; then
LATEST_AVAILABLE[$sdk_edition]="$SDK_VERSION"
LATEST_LINK[$sdk_edition]=$(get_download_link "$sdk")
fi
done
done
if [[ "${#TO_DOWNLOAD[@]}" -gt 0 ]]; then
printf '\n'
read -p "Upgrade all? [y/n] " -n 1 -r
printf '\n'
if [[ "$REPLY" != "y" ]]; then
TO_DOWNLOAD=()
TO_REMOVE=()
fi
fi
for sdk_edition in '8' '11'; do
if [[ -z "${LATEST_INSTALLED[$sdk_edition]}" || $(compare_sdk_versions "${LATEST_INSTALLED[$sdk_edition]}" "${LATEST_AVAILABLE[$sdk_edition]}") -lt 0 ]]; then
printf '\n'
read -p "Latest SDK $sdk_edition version ${LATEST_AVAILABLE[$sdk_edition]} is not installed yet. Download? [y/n] " -n 1 -r
printf '\n'
if [[ "$REPLY" == "y" ]]; then
TO_DOWNLOAD+=( "${LATEST_LINK[$sdk_edition]}" )
TO_REMOVE+=( "" )
fi
fi
done
if [[ "${#TO_DOWNLOAD[@]}" -eq 0 ]]; then
exit 0
fi
for i in "${!TO_DOWNLOAD[@]}"; do
aria2c -d "$DIR" -s4 -x4 "${TO_DOWNLOAD[$i]}"
# shellcheck disable=SC2181
if [[ $? -eq 0 && -n "${TO_REMOVE[$i]}" ]]; then
# delete old build if download was successful
rm "$DIR/${TO_REMOVE[$i]}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment