Skip to content

Commit

Permalink
feat: added gitlab support, simplified version sorting with ls-remote
Browse files Browse the repository at this point in the history
This allows for easier access to both GitHub and GitLab releases,
without having to modify anything new.

It also simplifies sorting by using git ls-remote's built-in --sort
functionality. Since not all projects strictly follow semver, attempts
were made to normalize it, such as if there is no `-` between a patch
version and `rc`, `prerelease`, etc.

NOTE: this does require git >= 2.18.0, so that is checked for.

It also removes the -C - parameter from curl, which was attempting to
resume downloads, querying the server for the byte range to do so. This
is not universally supported, and if the server doesn't support it, the
download will fail.

Finally, where possible, it uses shell built-ins like parameter
substitution over calling external commands. If this isn't possible, it
minimizes the number of spawned subshells by combining commands rather
than piping. This speeds up the asdf ecosystem as a whole, by minimizing syscalls.
  • Loading branch information
stephanGarland committed Jan 15, 2023
1 parent 62d1095 commit b36b7a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion template/bin/list-all
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 34 additions & 16 deletions template/lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

set -euo pipefail

# TODO: Ensure this is the correct GitHub homepage where releases can be downloaded for <YOUR TOOL>.
GH_REPO="<TOOL REPO>"
GIT_VERSION=$(git --version)
GIT_VERSION=${GIT_VERSION##* }
# TODO: Ensure this is the correct GitHub/GitLab homepage where releases can be downloaded for <YOUR TOOL>.
REPO="<TOOL REPO>"
TOOL_NAME="<YOUR TOOL>"
TOOL_TEST="<TOOL CHECK>"
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: $*"
Expand All @@ -14,26 +24,30 @@ fail() {

curl_opts=(-fsSL)

# NOTE: You might want to remove this if <YOUR TOOL> is not hosted on GitHub releases.
if [ $(git_supports_sort "${GIT_VERSION}") -eq 1 ]; then
printf "must have git >= 2.18.0, have ${GIT_VERSION}\n"
exit 1
fi

# NOTE: You might want to remove this if <YOUR TOOL> 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
list_remote_tags() {
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]' '{ print $NF }' || fail "no releases found"
}

list_all_versions() {
# TODO: Adapt this. By default we simply list the tag names from GitHub releases.
# Change this function if <YOUR TOOL> has other means of determining installable versions.
list_github_tags
list_remote_tags
}

download_release() {
Expand All @@ -42,10 +56,14 @@ download_release() {
filename="$2"

# TODO: Adapt the release URL convention for <YOUR TOOL>
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() {
Expand Down

0 comments on commit b36b7a6

Please sign in to comment.