From 8264b8476551e823a1e1f6250059f813fb3cc5d1 Mon Sep 17 00:00:00 2001 From: zimagen Date: Mon, 18 Dec 2023 20:13:36 +0900 Subject: [PATCH] Initial commit --- .editorconfig | 8 +++ .github/workflows/workflow.yml | 27 ++++++++ LICENSE | 21 +++++++ README.md | 18 ++++++ bin/install | 111 +++++++++++++++++++++++++++++++++ bin/list-all | 21 +++++++ 6 files changed, 206 insertions(+) create mode 100644 .editorconfig create mode 100644 .github/workflows/workflow.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100755 bin/install create mode 100755 bin/list-all diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1923d41 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml new file mode 100644 index 0000000..7ff094a --- /dev/null +++ b/.github/workflows/workflow.yml @@ -0,0 +1,27 @@ +name: Main Workflow + +on: + push: + paths-ignore: + - '**.md' + pull_request: + paths-ignore: + - '**.md' + # schedule: + # - cron: "0 0 * * 5" + +jobs: + test: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + + runs-on: ${{ matrix.os }} + + steps: + - name: asdf_plugin_test + uses: asdf-vm/actions/plugin-test@v2 + with: + command: sd --help + env: + GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a05d19a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Genki Yajima + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c80179c --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# asdf-sd + +[![Main Workflow](https://github.com/zimagen/asdf-sd/actions/workflows/workflow.yml/badge.svg)](https://github.com/zimagen/asdf-sd/actions/workflows/workflow.yml) + +[`sd`][util] plugin for [asdf](https://github.com/asdf-vm/asdf) version manager. + +## Install + +``` +asdf plugin-add sd https://github.com/zimagen/asdf-sd.git +asdf install sd latest +``` + +## Use + +Check [asdf](https://github.com/asdf-vm/asdf) readme for instructions on how to install & manage versions of sd. + +[util]: https://github.com/chmln/sd diff --git a/bin/install b/bin/install new file mode 100755 index 0000000..6b2196e --- /dev/null +++ b/bin/install @@ -0,0 +1,111 @@ +#!/usr/bin/env bash + +set -e +set -o pipefail +# set -x + +ASDF_INSTALL_TYPE=${ASDF_INSTALL_TYPE:-version} +[ -n "$ASDF_INSTALL_VERSION" ] || (>&2 echo 'Missing ASDF_INSTALL_VERSION' && exit 1) +[ -n "$ASDF_INSTALL_PATH" ] || (>&2 echo 'Missing ASDF_INSTALL_PATH' && exit 1) + +install_plugin() { + local install_type=$1 + local version=$2 + local install_path=$3 + local download_url="$(get_download_url $install_type $version)" + local tmp_download_file=$(mktemp) + + local bin_install_path="$install_path/bin" + mkdir -p "${bin_install_path}" + + echo "Downloading sd from $download_url" + curl -L -s "$download_url" -o "$tmp_download_file" + pushd $bin_install_path > /dev/null + tar zxf "$tmp_download_file" || exit 1 + mv sd-*/sd ./ + popd > /dev/null +} + +get_platform() { + local unameOut="$(uname -s)" + case "${unameOut}" in + Linux*) echo "linux";; + Darwin*) echo "mac";; + CYGWIN*) echo "win";; + MINGW*) echo "win";; + *) echo "unknown" + esac + if [ "$unameOut" == "CYGWIN*" ] || [ "$unameOut" == "MINGW*" ]; then + echo "Windows is not supported" >&2 + exit 1 + fi +} + +get_arch() { + local arch=$(uname -m) + case $arch in + amd64 | x86_64) + echo "x86_64" + ;; + arm64 | aarch64) + echo "arm64" + ;; + arm) + echo "arm" + ;; + *) + echo "i386 is not supported" >&2 + exit 1 + esac +} + +get_download_url() { + local install_type=$1 + local tag=$2 + local version=$2 + + local platform=$(get_platform) + local arch=$(get_arch) + local extra="" + + # platform and arch mapping + case $platform in + linux) + platform="unknown-linux" + ;; + mac) + platform="apple-darwin" + ;; + win) + platform="pc-windows" + ;; + esac + + case $arch in + x86_64) + arch="x86_64" + ;; + arm64 | aarch64) + arch="aarch64" + ;; + arm) + arch="arm" + ;; + *) + arch="i386" + esac + + # in case the version is in some other format such as FOOBAR.X.X.X + if [[ "$tag" =~ ^[0-9] ]]; then + tag="v${tag}" + fi + + local file_extension="tar.gz" + if [ "$platform" == "pc-windows" ]; then + file_extension="zip" + fi + + echo "https://github.com/chmln/sd/releases/download/${tag}/sd-${tag}-${arch}-${platform}.${file_extension}" +} + +install_plugin "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH" diff --git a/bin/list-all b/bin/list-all new file mode 100755 index 0000000..0555e03 --- /dev/null +++ b/bin/list-all @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -eo pipefail + +repo=chmln/sd + +cmd="curl --silent --location" +releases_path="https://api.github.com/repos/$repo/releases" + +if [ -n "$GITHUB_API_TOKEN" ]; then + cmd="$cmd --header '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}' +} + +versions=$(eval "$cmd $releases_path" | grep -oE "tag_name\":\s?\".*\"," | sed 's/tag_name\": *\"//;s/\",//' | sed 's/^[v]//' | sort_versions | xargs) + +echo "$versions"