Skip to content

Commit dd79e04

Browse files
feat: added gitlab support, simplified version sorting with ls-remote
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.
1 parent 62d1095 commit dd79e04

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

template/bin/list-all

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ plugin_dir=$(dirname "$(dirname "$current_script_path")")
88
# shellcheck source=../lib/utils.bash
99
source "${plugin_dir}/lib/utils.bash"
1010

11-
list_all_versions | sort_versions | xargs echo
11+
list_all_versions | xargs echo

template/lib/utils.bash

+34-16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
set -euo pipefail
44

5-
# TODO: Ensure this is the correct GitHub homepage where releases can be downloaded for <YOUR TOOL>.
6-
GH_REPO="<TOOL REPO>"
5+
GIT_VERSION=$(git --version)
6+
GIT_VERSION=${GIT_VERSION##* }
7+
# TODO: Ensure this is the correct GitHub/GitLab homepage where releases can be downloaded for <YOUR TOOL>.
8+
REPO="<TOOL REPO>"
79
TOOL_NAME="<YOUR TOOL>"
810
TOOL_TEST="<TOOL CHECK>"
11+
IS_GITHUB=$(
12+
[[ "$REPO" =~ "github" ]]
13+
echo $?
14+
)
15+
16+
git_supports_sort() {
17+
awk '{ split($0,a,"."); if ((a[1] < 2) || (a[2] < 18)) { print "1" } else { print "0" } }' <<<"$1"
18+
}
919

1020
fail() {
1121
echo -e "asdf-$TOOL_NAME: $*"
@@ -14,26 +24,30 @@ fail() {
1424

1525
curl_opts=(-fsSL)
1626

17-
# NOTE: You might want to remove this if <YOUR TOOL> is not hosted on GitHub releases.
27+
if [ $(git_supports_sort "${GIT_VERSION}") -eq 1 ]; then
28+
printf "must have git >= 2.18.0, have ${GIT_VERSION}\n"
29+
exit 1
30+
fi
31+
32+
# NOTE: You might want to remove this if <YOUR TOOL> is not hosted on GitHub or GitLab releases.
1833
if [ -n "${GITHUB_API_TOKEN:-}" ]; then
1934
curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITHUB_API_TOKEN")
35+
elif [ -n "${GITLAB_API_TOKEN:-}" ]; then
36+
curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITLAB_API_TOKEN")
2037
fi
2138

22-
sort_versions() {
23-
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' |
24-
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
25-
}
26-
27-
list_github_tags() {
28-
git ls-remote --tags --refs "$GH_REPO" |
29-
grep -o 'refs/tags/.*' | cut -d/ -f3- |
30-
sed 's/^v//' # NOTE: You might want to adapt this sed to remove non-version strings from tags
39+
list_remote_tags() {
40+
git -c 'versionsortsuffix=a' -c 'versionsortsuffix=b' \
41+
-c 'versionsortsuffix=r' -c 'versionsortsuffix=p' \
42+
-c 'versionsortsuffix=-' -c 'versionsortsuffix=_' \
43+
ls-remote --exit-code --tags --refs --sort="version:refname" "$REPO" |
44+
awk -F'[/v]' '{ print $NF }' || fail "no releases found"
3145
}
3246

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

3953
download_release() {
@@ -42,10 +56,14 @@ download_release() {
4256
filename="$2"
4357

4458
# TODO: Adapt the release URL convention for <YOUR TOOL>
45-
url="$GH_REPO/archive/v${version}.tar.gz"
46-
59+
if [ $IS_GITHUB -eq 0 ]; then
60+
url="$REPO/archive/v${version}.tar.gz"
61+
else
62+
url="$REPO/-/archive/${version}/${TOOL_NAME}-${version}.tar.gz"
63+
fi
64+
printf "%s: %s\n" "url" "$url"
4765
echo "* Downloading $TOOL_NAME release $version..."
48-
curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url"
66+
curl "${curl_opts[@]}" -o "$filename" "$url" || fail "Could not download $url"
4967
}
5068

5169
install_version() {

0 commit comments

Comments
 (0)