Skip to content

Commit 5167aec

Browse files
committed
feat: implement scripts
1 parent 10782f9 commit 5167aec

File tree

6 files changed

+120
-35
lines changed

6 files changed

+120
-35
lines changed

LICENSE

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
TODO: INSERT YOUR NAME COPYRIGHT YEAR (if applicable to your license)
2-
31
MIT License
42

5-
Copyright (c) [year] [fullname]
3+
Copyright (c) 2025 3w36zj6
64

75
Permission is hereby granted, free of charge, to any person obtaining a copy
86
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22

33
# asdf-codon [![Build](https://github.com/3w36zj6/asdf-codon/actions/workflows/build.yml/badge.svg)](https://github.com/3w36zj6/asdf-codon/actions/workflows/build.yml) [![Lint](https://github.com/3w36zj6/asdf-codon/actions/workflows/lint.yml/badge.svg)](https://github.com/3w36zj6/asdf-codon/actions/workflows/lint.yml)
44

5-
[codon](https://github.com/3w36zj6/codon) plugin for the [asdf version manager](https://asdf-vm.com).
5+
[codon](https://github.com/exaloop/codon) plugin for the [asdf version manager](https://asdf-vm.com).
66

77
</div>
88

9-
# Contents
9+
## Contents
1010

1111
- [Dependencies](#dependencies)
1212
- [Install](#install)
1313
- [Contributing](#contributing)
1414
- [License](#license)
1515

16-
# Dependencies
16+
## Dependencies
1717

18-
**TODO: adapt this section**
18+
- `bash`, `curl`, `tar`, `grep`, `sed`, `awk`.
19+
- Codon provides pre-built binaries for Linux and macOS only (this plugin follows upstream releases).
1920

20-
- `bash`, `curl`, `tar`, and [POSIX utilities](https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html).
21-
- `SOME_ENV_VAR`: set this environment variable in your shell config to load the correct version of tool x.
21+
Optional:
2222

23-
# Install
23+
- `GITHUB_API_TOKEN`: set this to avoid GitHub API rate limits when running `asdf list-all codon`.
24+
25+
## Install
2426

2527
Plugin:
2628

@@ -49,12 +51,12 @@ codon --help
4951
Check [asdf](https://github.com/asdf-vm/asdf) readme for more instructions on how to
5052
install & manage versions.
5153

52-
# Contributing
54+
## Contributing
5355

5456
Contributions of any kind welcome! See the [contributing guide](contributing.md).
5557

5658
[Thanks goes to these contributors](https://github.com/3w36zj6/asdf-codon/graphs/contributors)!
5759

58-
# License
60+
## License
5961

6062
See [LICENSE](LICENSE) © [3w36zj6](https://github.com/3w36zj6/)

bin/download

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ source "${plugin_dir}/lib/utils.bash"
1010

1111
mkdir -p "$ASDF_DOWNLOAD_PATH"
1212

13-
# TODO: Adapt this to proper extension and adapt extracting strategy.
1413
release_file="$ASDF_DOWNLOAD_PATH/$TOOL_NAME-$ASDF_INSTALL_VERSION.tar.gz"
1514

1615
# Download tar.gz file to the download directory

bin/latest-stable

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ fi
1717
# curl of REPO/releases/latest is expected to be a 302 to another URL
1818
# when no releases redirect_url="REPO/releases"
1919
# when there are releases redirect_url="REPO/releases/tag/v<VERSION>"
20-
redirect_url=$(curl "${curl_opts[@]}" "$GH_REPO/releases/latest" | sed -n -e "s|^location: *||p" | sed -n -e "s|\r||p")
20+
redirect_url=$(curl "${curl_opts[@]}" "$GH_REPO/releases/latest" | sed -n -e "s|^[Ll]ocation: *||p" | sed -n -e "s|\r||p")
2121
version=
22-
printf "redirect url: %s\n" "$redirect_url" >&2
2322
if [[ "$redirect_url" == "$GH_REPO/releases" ]]; then
2423
version="$(list_all_versions | sort_versions | tail -n1 | xargs echo)"
2524
else

contributing.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Testing Locally:
55
```shell
66
asdf plugin test <plugin-name> <plugin-url> [--asdf-tool-version <version>] [--asdf-plugin-gitref <git-ref>] [test-command*]
77

8-
# TODO: adapt this
98
asdf plugin test codon https://github.com/3w36zj6/asdf-codon.git "codon --help"
109
```
1110

lib/utils.bash

Lines changed: 107 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
set -euo pipefail
44

5-
# TODO: Ensure this is the correct GitHub homepage where releases can be downloaded for codon.
6-
GH_REPO="https://github.com/3w36zj6/codon"
5+
# Codon upstream repository.
6+
GH_REPO="https://github.com/exaloop/codon"
77
TOOL_NAME="codon"
88
TOOL_TEST="codon --help"
99

@@ -24,47 +24,135 @@ sort_versions() {
2424
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
2525
}
2626

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
27+
resolve_version() {
28+
local version
29+
version="$1"
30+
if [[ "$version" == "latest" ]]; then
31+
version="$(list_all_versions | sort_versions | tail -n1 | xargs echo)"
32+
if [ -z "$version" ]; then
33+
fail "Could not determine the latest $TOOL_NAME version."
34+
fi
35+
fi
36+
printf "%s\n" "$version"
37+
}
38+
39+
get_os() {
40+
local os
41+
os="$(uname -s | awk '{print tolower($0)}')"
42+
case "$os" in
43+
linux | darwin)
44+
printf "%s\n" "$os"
45+
;;
46+
*)
47+
fail "Pre-built binaries only exist for Linux and macOS. Detected OS: $os"
48+
;;
49+
esac
50+
}
51+
52+
get_arch() {
53+
local arch
54+
arch="$(uname -m)"
55+
case "$arch" in
56+
x86_64 | aarch64)
57+
printf "%s\n" "$arch"
58+
;;
59+
amd64)
60+
printf "%s\n" "x86_64"
61+
;;
62+
arm64)
63+
printf "%s\n" "aarch64"
64+
;;
65+
*)
66+
fail "Unsupported architecture for pre-built Codon binaries: $arch"
67+
;;
68+
esac
69+
}
70+
71+
determine_asset_arch() {
72+
local os arch
73+
os="$1"
74+
arch="$2"
75+
if [[ "$os" == "darwin" && "$arch" == "aarch64" ]]; then
76+
printf "arm64\n"
77+
else
78+
printf "%s\n" "$arch"
79+
fi
80+
}
81+
82+
normalize_tag() {
83+
local version
84+
version="$1"
85+
if [[ "$version" == v* ]]; then
86+
printf "%s\n" "$version"
87+
else
88+
printf "v%s\n" "$version"
89+
fi
90+
}
91+
92+
list_github_releases() {
93+
local page url
94+
page=1
95+
while :; do
96+
url="https://api.github.com/repos/exaloop/codon/releases?per_page=100&page=$page"
97+
98+
# Extract tag_name values without requiring jq.
99+
# Example: "tag_name": "v0.18.0"
100+
local tags
101+
tags="$(curl "${curl_opts[@]}" "$url" |
102+
grep -oE '"tag_name"[[:space:]]*:[[:space:]]*"[^"]+"' |
103+
sed -E 's/.*"tag_name"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/' || true)"
104+
105+
if [ -z "$tags" ]; then
106+
break
107+
fi
108+
109+
printf "%s\n" "$tags"
110+
page=$((page + 1))
111+
done
31112
}
32113

33114
list_all_versions() {
34-
# TODO: Adapt this. By default we simply list the tag names from GitHub releases.
35-
# Change this function if codon has other means of determining installable versions.
36-
list_github_tags
115+
# Codon versions are published as GitHub Releases.
116+
# Output versions without the leading "v" (asdf convention).
117+
list_github_releases |
118+
sed 's/^v//' |
119+
grep -E '^[0-9]'
37120
}
38121

39122
download_release() {
40-
local version filename url
123+
local version filename url resolved_version
41124
version="$1"
42125
filename="$2"
43-
44-
# TODO: Adapt the release URL convention for codon
45-
url="$GH_REPO/archive/v${version}.tar.gz"
46-
47-
echo "* Downloading $TOOL_NAME release $version..."
126+
local os arch tag asset
127+
resolved_version="$(resolve_version "$version")"
128+
os="$(get_os)"
129+
arch="$(get_arch)"
130+
tag="$(normalize_tag "$resolved_version")"
131+
local asset_arch
132+
asset_arch="$(determine_asset_arch "$os" "$arch")"
133+
asset="codon-$os-$asset_arch.tar.gz"
134+
url="$GH_REPO/releases/download/$tag/$asset"
135+
136+
echo "* Downloading $TOOL_NAME release $resolved_version..."
48137
curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url"
49138
}
50139

51140
install_version() {
52141
local install_type="$1"
53142
local version="$2"
54-
local install_path="${3%/bin}/bin"
143+
local install_path="$3"
55144

56145
if [ "$install_type" != "version" ]; then
57146
fail "asdf-$TOOL_NAME supports release installs only"
58147
fi
59148

60149
(
61150
mkdir -p "$install_path"
62-
cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path"
151+
cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path/"
63152

64-
# TODO: Assert codon executable exists.
65153
local tool_cmd
66154
tool_cmd="$(echo "$TOOL_TEST" | cut -d' ' -f1)"
67-
test -x "$install_path/$tool_cmd" || fail "Expected $install_path/$tool_cmd to be executable."
155+
test -x "$install_path/bin/$tool_cmd" || fail "Expected $install_path/bin/$tool_cmd to be executable."
68156

69157
echo "$TOOL_NAME $version installation was successful!"
70158
) || (

0 commit comments

Comments
 (0)