From 672a624675926bee49c5cf26920c26ce3af09bcd Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Thu, 20 Apr 2023 10:35:18 +0100 Subject: [PATCH] fix: minor improvements for osx compat --- template/bin/download | 12 ++++++++---- template/bin/install | 2 +- template/bin/latest-stable | 4 +++- template/bin/list-all | 2 +- template/lib/utils.bash | 38 +++++++++++++++++++++++++++++++++++--- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/template/bin/download b/template/bin/download index b104db2..ceea50e 100755 --- a/template/bin/download +++ b/template/bin/download @@ -5,19 +5,23 @@ set -euo pipefail current_script_path=${BASH_SOURCE[0]} plugin_dir=$(dirname "$(dirname "$current_script_path")") -# shellcheck source=../lib/utils.bash +# shellcheck source=./lib/utils.bash source "${plugin_dir}/lib/utils.bash" -mkdir -p "$ASDF_DOWNLOAD_PATH" +download_base_path="$(sanitize_path "$ASDF_DOWNLOAD_PATH")" +mkdir -p "$download_base_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="$download_base_path"/"$TOOL_NAME".zip # Download tar.gz file to the download directory download_release "$ASDF_INSTALL_VERSION" "$release_file" +install_base_path="$(sanitize_path "$ASDF_DOWNLOAD_PATH")" +mkdir -p "$install_base_path" + # 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" +tar -xzf "$release_file" -C "$install_base_path" --strip-components=1 || fail "Could not extract $release_file" # Remove the tar.gz file since we don't need to keep it rm "$release_file" diff --git a/template/bin/install b/template/bin/install index 9737a63..6ca759a 100755 --- a/template/bin/install +++ b/template/bin/install @@ -5,7 +5,7 @@ set -euo pipefail current_script_path=${BASH_SOURCE[0]} plugin_dir=$(dirname "$(dirname "$current_script_path")") -# shellcheck source=../lib/utils.bash +# shellcheck source=./lib/utils.bash source "${plugin_dir}/lib/utils.bash" install_version "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH" diff --git a/template/bin/latest-stable b/template/bin/latest-stable index dd01f31..2b35df3 100755 --- a/template/bin/latest-stable +++ b/template/bin/latest-stable @@ -5,7 +5,7 @@ set -euo pipefail current_script_path=${BASH_SOURCE[0]} plugin_dir=$(dirname "$(dirname "$current_script_path")") -# shellcheck source=../lib/utils.bash +# shellcheck source=./lib/utils.bash . "${plugin_dir}/lib/utils.bash" curl_opts=(-sI) @@ -26,4 +26,6 @@ else version="$(printf "%s\n" "$redirect_url" | sed 's|.*/tag/v\{0,1\}||')" fi +version=$(get_version "${version}") + printf "%s\n" "$version" diff --git a/template/bin/list-all b/template/bin/list-all index 943371e..9b8b666 100755 --- a/template/bin/list-all +++ b/template/bin/list-all @@ -5,7 +5,7 @@ set -euo pipefail current_script_path=${BASH_SOURCE[0]} plugin_dir=$(dirname "$(dirname "$current_script_path")") -# shellcheck source=../lib/utils.bash +# shellcheck source=./lib/utils.bash source "${plugin_dir}/lib/utils.bash" list_all_versions | sort_versions | xargs echo diff --git a/template/lib/utils.bash b/template/lib/utils.bash index 887708c..7166e10 100755 --- a/template/lib/utils.bash +++ b/template/lib/utils.bash @@ -36,13 +36,44 @@ list_all_versions() { list_github_tags } +get_download_link() { + local version + version="$1" + + # seems like version can come as full download url or just the version + if [[ ${version} = https* ]]; then + (echo "${version}/" | sed 's|/tag/|/download/|') + else + (echo "${GH_REPO}/releases/download/v${version}/") + fi +} + +get_version() { + local version + version="$1" + + # seems like version can come as full download url or just the version + if [[ ${version} = https* ]]; then + (echo "${version//${GH_REPO}\/releases\/tag\/v/}") + else + (echo "${version}") + fi +} + +sanitize_path() { + local path + path="$1" + + echo "${path//${GH_REPO}\/releases\/tag\/v/}" +} + download_release() { local version filename url version="$1" filename="$2" # TODO: Adapt the release URL convention for - url="$GH_REPO/archive/v${version}.tar.gz" + url="$(get_download_link "${version}")" echo "* Downloading $TOOL_NAME release $version..." curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url" @@ -51,7 +82,8 @@ download_release() { install_version() { local install_type="$1" local version="$2" - local install_path="${3%/bin}/bin" + local install_path="$3" + install_path="$(sanitize_path "$install_path")" if [ "$install_type" != "version" ]; then fail "asdf-$TOOL_NAME supports release installs only" @@ -59,7 +91,7 @@ install_version() { ( mkdir -p "$install_path" - cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path" + cp -r "$(sanitize_path "$ASDF_DOWNLOAD_PATH")"/* "$install_path" # TODO: Assert executable exists. local tool_cmd