diff --git a/template/bin/list-all b/template/bin/list-all index 943371e..55bf7b1 100755 --- a/template/bin/list-all +++ b/template/bin/list-all @@ -8,4 +8,4 @@ plugin_dir=$(dirname "$(dirname "$current_script_path")") # shellcheck source=../lib/utils.bash source "${plugin_dir}/lib/utils.bash" -list_all_versions | sort_versions | xargs echo +list_all_versions | xargs echo diff --git a/template/lib/utils.bash b/template/lib/utils.bash index 85a1c69..2acb2fb 100755 --- a/template/lib/utils.bash +++ b/template/lib/utils.bash @@ -2,10 +2,21 @@ set -euo pipefail -# TODO: Ensure this is the correct GitHub homepage where releases can be downloaded for . -GH_REPO="" +GIT_REMINDER_FILE="${ASDF_INSTALL_PATH}/.upgradegit" +GIT_VERSION=$(git --version) +GIT_VERSION=${GIT_VERSION##* } +# TODO: Ensure this is the correct GitHub/GitLab homepage where releases can be downloaded for . +REPO="" TOOL_NAME="" TOOL_TEST="" +IS_GITHUB=$( + [[ "$REPO" =~ "github" ]] + echo $? +) + +git_supports_sort() { + awk '{ split($0,a,"."); if ((a[1] < 2) || (a[2] < 18)) { print "1" } else { print "0" } }' <<<"$1" +} fail() { echo -e "asdf-$TOOL_NAME: $*" @@ -14,26 +25,53 @@ fail() { curl_opts=(-fsSL) -# NOTE: You might want to remove this if is not hosted on GitHub releases. +if [ $(git_supports_sort "${GIT_VERSION}") -eq 0 ]; then + rm -f "${GIT_REMINDER_FILE}" + GIT_SUPPORTS_SORT=0 +else + GIT_SUPPORTS_SORT=1 + if [ ! -f "${GIT_REMINDER_FILE}" ]; then + printf "consider upgrading git to a version >= 2.18.0 for faster asdf - you have v${GIT_VERSION}\n" + touch "${GIT_REMINDER_FILE}" + fi +fi + +# NOTE: You might want to remove this if is not hosted on GitHub or GitLab releases. if [ -n "${GITHUB_API_TOKEN:-}" ]; then curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITHUB_API_TOKEN") +elif [ -n "${GITLAB_API_TOKEN:-}" ]; then + curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITLAB_API_TOKEN") fi -sort_versions() { - sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' | - LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}' -} - -list_github_tags() { - git ls-remote --tags --refs "$GH_REPO" | - grep -o 'refs/tags/.*' | cut -d/ -f3- | - sed 's/^v//' # NOTE: You might want to adapt this sed to remove non-version strings from tags +# NOTE: If doesn't issue releases according to something resembling semver, +# you will need to edit the awk regex. +list_remote_tags() { + if [ ${GIT_SUPPORTS_SORT} -eq 0 ]; then + git -c 'versionsort.suffix=a' -c 'versionsort.suffix=b' \ + -c 'versionsort.suffix=r' -c 'versionsort.suffix=p' \ + -c 'versionsort.suffix=-' -c 'versionsort.suffix=_' \ + ls-remote --exit-code --tags --refs --sort="version:refname" "$REPO" | + awk -F'[/v]' '$NF ~ /^[0-9]+.*/ { print $NF }' || fail "no releases found" + else + git ls-remote --exit-code --tags --refs "$REPO" | + awk -F'[/v]' '$NF ~ /^[0-9]+.*/ { print $NF }' || fail "no releases found" + fi } list_all_versions() { # TODO: Adapt this. By default we simply list the tag names from GitHub releases. # Change this function if has other means of determining installable versions. - list_github_tags + if [ ${GIT_SUPPORTS_SORT} -eq 0 ]; then + list_remote_tags + else + list_remote_tags | sort_versions + fi +} + +# NOTE: This is a fallback for if the user's installed version of git doesn't support sorting. +sort_versions() { + sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' | + LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}' } download_release() { @@ -42,10 +80,14 @@ download_release() { filename="$2" # TODO: Adapt the release URL convention for - url="$GH_REPO/archive/v${version}.tar.gz" - + if [ $IS_GITHUB -eq 0 ]; then + url="$REPO/archive/v${version}.tar.gz" + else + url="$REPO/-/archive/${version}/${TOOL_NAME}-${version}.tar.gz" + fi + printf "%s: %s\n" "url" "$url" echo "* Downloading $TOOL_NAME release $version..." - curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url" + curl "${curl_opts[@]}" -o "$filename" "$url" || fail "Could not download $url" } install_version() {