Skip to content

Commit

Permalink
Merge pull request #5 from nim-works/initial
Browse files Browse the repository at this point in the history
feat: initial tweaks
  • Loading branch information
disruptek authored Jan 21, 2024
2 parents e3c817c + 532cd14 commit d8fe94c
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 29 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">

# asdf-nimskull [![Build](https://github.com/disruptek/asdf-nimskull/actions/workflows/build.yml/badge.svg)](https://github.com/disruptek/asdf-nimskull/actions/workflows/build.yml) [![Lint](https://github.com/disruptek/asdf-nimskull/actions/workflows/lint.yml/badge.svg)](https://github.com/disruptek/asdf-nimskull/actions/workflows/lint.yml)
# asdf-nimskull [![Build](https://github.com/nim-works/asdf-nimskull/actions/workflows/build.yml/badge.svg)](https://github.com/nim-works/asdf-nimskull/actions/workflows/build.yml) [![Lint](https://github.com/nim-works/asdf-nimskull/actions/workflows/lint.yml/badge.svg)](https://github.com/nim-works/asdf-nimskull/actions/workflows/lint.yml)

[nimskull](https://nim-works.github.io/nimskull/) plugin for the [asdf version manager](https://asdf-vm.com).

Expand All @@ -27,7 +27,7 @@ Plugin:
```shell
asdf plugin add nimskull
# or
asdf plugin add nimskull https://github.com/disruptek/asdf-nimskull.git
asdf plugin add nimskull https://github.com/nim-works/asdf-nimskull.git
```

nimskull:
Expand All @@ -53,8 +53,8 @@ install & manage versions.

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

[Thanks goes to these contributors](https://github.com/disruptek/asdf-nimskull/graphs/contributors)!
[Thanks goes to these contributors](https://github.com/nim-works/asdf-nimskull/graphs/contributors)!

# License

See [LICENSE](LICENSE) © [Smooth Operator](https://github.com/disruptek/)
MIT
12 changes: 6 additions & 6 deletions bin/download
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ source "${plugin_dir}/lib/utils.bash"

mkdir -p "$ASDF_DOWNLOAD_PATH"

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

# Download tar.gz file to the download directory
# Download tar.zst file to the download directory
download_release "$ASDF_INSTALL_VERSION" "$release_file"

# Extract contents of tar.gz file into the download directory
tar -xzf "$release_file" -C "$ASDF_DOWNLOAD_PATH" --strip-components=1 || fail "Could not extract $release_file"
# Extract contents of tar.zst file into the download directory
echo "* Extracting $TOOL_NAME-$ASDF_INSTALL_VERSION.tar.zst ..."
tar --zstd -xf "$release_file" -C "$ASDF_DOWNLOAD_PATH" --strip-components=1 || fail "Could not extract $release_file"

# Remove the tar.gz file since we don't need to keep it
# Remove the tar.zst file since we don't need to keep it
rm "$release_file"
11 changes: 11 additions & 0 deletions bin/help.deps
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -euo pipefail

echo curl
echo jq
echo sed
echo awk
echo zstd
echo tar
echo coreutils
11 changes: 11 additions & 0 deletions bin/help.overview
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -euo pipefail

printf "nimskull binary release distribution\n"
printf "https://github.com/nim-works/nimskull\n\n"

printf "supported architectures:\n"
printf "\tx86_64-linux-gnu\n"
printf "\tx86_64-apple-darwin\n\n"
printf "operating system requirements:\n\n"
2 changes: 1 addition & 1 deletion bin/latest-stable
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi
# when there are releases redirect_url="REPO/releases/tag/v<VERSION>"
redirect_url=$(curl "${curl_opts[@]}" "$GH_REPO/releases/latest" | sed -n -e "s|^location: *||p" | sed -n -e "s|\r||p")
version=
printf "redirect url: %s\n" "$redirect_url" >&2
#printf "redirect url: %s\n" "$redirect_url" >&2
if [[ "$redirect_url" == "$GH_REPO/releases" ]]; then
version="$(list_all_versions | sort_versions | tail -n1 | xargs echo)"
else
Expand Down
3 changes: 1 addition & 2 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Testing Locally:
```shell
asdf plugin test <plugin-name> <plugin-url> [--asdf-tool-version <version>] [--asdf-plugin-gitref <git-ref>] [test-command*]

# TODO: adapt this
asdf plugin test nimskull https://github.com/disruptek/asdf-nimskull.git "nim --version"
asdf plugin test nimskull https://github.com/nim-works/asdf-nimskull.git "nim --version"
```

Tests are automatically run in GitHub Actions on push and PR.
112 changes: 96 additions & 16 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

set -euo pipefail

# TODO: Ensure this is the correct GitHub homepage where releases can be downloaded for nimskull.
GH_REPO="https://github.com/nim-works/nimskull"
TOOL_NAME="nimskull"
TOOL_TEST="nim --version"
Expand All @@ -14,14 +13,13 @@ fail() {

curl_opts=(-fsSL)

# NOTE: You might want to remove this if nimskull is not hosted on GitHub releases.
if [ -n "${GITHUB_API_TOKEN:-}" ]; then
curl_opts=("${curl_opts[@]}" -H "Authorization: token $GITHUB_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}'
sed 'h; s/[+-]/./g; s/$/.z/; G; s/\n/ /' |
LC_ALL=C sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4 -k 5,5n | awk '{print $2}'
}

list_github_tags() {
Expand All @@ -31,27 +29,107 @@ list_github_tags() {
}

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

detect_target_triple() {
# Get the OS name (Darwin or Linux)
local os_name
os_name=$(uname -s)

# Get the machine hardware name (x86_64)
local machine_hw_name
machine_hw_name=$(uname -m)

# Normalize and construct the target triple
case "$os_name" in
Linux)
local os_type="linux-gnu"
;;
Darwin)
local os_type="apple-darwin"
;;
*)
fail "Unsupported operating system '$os_name'"
;;
esac

# Construct the target triple
echo "${machine_hw_name}-${os_type}"
}

check_sha256() {
local src="$1"
local dst="$2"
local target
target=$(detect_target_triple) || fail "Unable to detect target triple"

case "$target" in
x86_64-linux-gnu)
# compare sha256 of src and dst on linux; fail if mismatch
sha256sum --check --status <(echo "$src $dst") || fail "Checksum mismatch for $dst"
;;
x86_64-apple-darwin)
# compare sha256 of src and dst on macos; fail if mismatch
shasum -a 256 --check --status <(echo "$src $dst") || fail "Checksum mismatch for $dst"
;;
*)
fail "Unsupported target triple '$target'"
;;
esac
}

download_release() {
local version filename url
version="$1"
filename="$2"

# TODO: Adapt the release URL convention for nimskull
url="$GH_REPO/archive/v${version}.tar.gz"

echo "* Downloading $TOOL_NAME release $version..."
target=$(detect_target_triple) || fail "Unable to detect target triple"

# Map the target triple to the platform
local platform
case "$target" in
x86_64-linux-gnu)
platform="linux_amd64"
;;
x86_64-apple-darwin)
platform="macosx_amd64"
;;
*)
fail "Unsupported target triple '$target'"
;;
esac

echo "* Downloading $TOOL_NAME release $version manifest..."
url="$GH_REPO/releases/download/${version}/manifest.json"
local manifest
manifest=$(curl --fail --silent --location "$url")

# sample input manifest:
# {"manifestVersion":0,"version":"0.1.0-dev.21167","source":
# {"name":"nim-0.1.0-dev.21167.tar.zst","sha256":"619b160b64822ccbe23e8069620cbf3720d6a0c355bb7c11e9e9ef764ab3a10d"},
# "binaries":[
# {"target":"x86_64-linux-gnu","name":"nim-0.1.0-dev.21167-linux_amd64.tar.zst","sha256":"f2d141c362c294c28036a44af4394afca9a74f5e88f5c913426a58592da02f45"},
# {"target":"x86_64-apple-darwin","name":"nim-0.1.0-dev.21167-macosx_amd64.tar.zst","sha256":"2618608b4d5fc9b9f9da8b5cbba1547688761d1e84d6c64ffb1af6e5050c3e89"}]}
#
# we want to retrieve the tarball name for the target triple, so we'll
# use jq to select it from the binaries array in the object:
#
local tarball
tarball=$(echo "$manifest" | jq -r ".binaries[] | select(.target == \"$target\") | .name")
checksum=$(echo "$manifest" | jq -r ".binaries[] | select(.target == \"$target\") | .sha256")
echo "* Downloading $TOOL_NAME release $version for $target..."
url="$GH_REPO/releases/download/${version}/${tarball}"
curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url"

check_sha256 "$checksum" "$filename" || fail "Checksum mismatch for $filename"
echo "* SHA256 checksum verified for ${version}-${platform} artifact."
}

install_version() {
local install_type="$1"
local version="$2"
local install_path="${3%/bin}/bin"
local install_path="${3%/bin}"

if [ "$install_type" != "version" ]; then
fail "asdf-$TOOL_NAME supports release installs only"
Expand All @@ -61,12 +139,14 @@ install_version() {
mkdir -p "$install_path"
cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path"

# TODO: Assert nimskull executable exists.
local tool_cmd
tool_cmd="$(echo "$TOOL_TEST" | cut -d' ' -f1)"
test -x "$install_path/$tool_cmd" || fail "Expected $install_path/$tool_cmd to be executable."
# Assert nimskull executable exists.
local executable
executable="${install_path}/bin/$(echo "$TOOL_TEST" | cut -d' ' -f1)"
test -x "$executable" || fail "Expected $executable to be executable."

echo "$TOOL_NAME $version installation was successful!"
echo "* Installation successful; compiler --version output:"
# Assert nimskull executable can be run by the operating system.
"$executable" --version || fail "Could not run $executable --version"
) || (
rm -rf "$install_path"
fail "An error occurred while installing $TOOL_NAME $version."
Expand Down

0 comments on commit d8fe94c

Please sign in to comment.