feat: limit dynamic resolve strategies

This commit is contained in:
Augusto Moura 2023-05-18 13:35:05 -03:00
parent 3d0239ade6
commit 385ff1349e
No known key found for this signature in database
GPG Key ID: BE5D1B3140B8A0A3
4 changed files with 67 additions and 46 deletions

View File

@ -15,9 +15,7 @@ install_nodejs() {
args+=(-c)
fi
if ! [ "${ASDF_NODEJS_SKIP_NODEBUILD_UPDATE-}" ]; then
try_to_update_nodebuild
fi
try_to_update_nodebuild
NODE_BUILD_CACHE_PATH="${NODE_BUILD_CACHE_PATH:-$ASDF_DOWNLOAD_PATH}" \
nodebuild_wrapped ${args+"${args[@]}"} "$version" "$install_path"

View File

@ -17,9 +17,7 @@ function print_only_fully_numeric_items() {
grep -E '^[0-9.]+$' <<< "$version_numbers"
}
if ! [ "${ASDF_NODEJS_SKIP_NODEBUILD_UPDATE-}" ]; then
try_to_update_nodebuild >&2
fi
try_to_update_nodebuild >&2
all_definitions_from_node_build="$(nodebuild_wrapped --definitions)"

View File

@ -1,37 +0,0 @@
#! /usr/bin/env bash
set -eu -o pipefail
# shellcheck source=../lib/utils.sh
source "$(dirname "${BASH_SOURCE[0]}")/../utils.sh"
list() {
local only_installed="$1"
if [ "$only_installed" ]; then
ASDF_NODEJS_SKIP_NODEBUILD_UPDATE=1 asdf list nodejs "$query" | cut -c3-
else
ASDF_NODEJS_SKIP_NODEBUILD_UPDATE=1 asdf list-all nodejs "$query"
fi
}
sieve_through_versions() {
local only_installed='' query=''
while (("$#")); do
case "$1" in
--installed)
only_installed=1
;;
*)
query="$1"
;;
esac
shift
done
list "$only_installed" | grep "^$query" | tail -n1
}
sieve_through_versions "$@"

View File

@ -17,7 +17,17 @@ plugin_name() {
}
asdf_data_dir() {
printf "%s\n" "${ASDF_DATA_DIR:-$HOME/.asdf}"
local data_dir
if [ "${ASDF_DATA_DIR-}" ]; then
data_dir="${ASDF_DATA_DIR}"
elif [ "${ASDF_DIR-}" ]; then
data_dir="$ASDF_DIR"
else
data_dir="$HOME/.asdf"
fi
printf "%s\n" "$data_dir"
}
export ASDF_NODEJS_CACHE_DIR="$(asdf_data_dir)/tmp/$ASDF_NODEJS_PLUGIN_NAME/cache"
@ -35,6 +45,10 @@ nodebuild_wrapped() {
}
try_to_update_nodebuild() {
if [ "${ASDF_NODEJS_SKIP_NODEBUILD_UPDATE-}" ]; then
return
fi
local exit_code=0
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-update-nodebuild.bash" 2>/dev/null || exit_code=$?
@ -48,6 +62,54 @@ wrong, try to manually update node-build by running: \`asdf %s update nodebuild\
fi
}
# Adapted from asdf-core https://github.com/asdf-vm/asdf/blob/684f4f058f24cc418f77825a59a22bacd16a9bee/lib/utils.bash#L95-L109
list_installed_versions() {
local plugin_name=$1
local plugin_installs_path
plugin_installs_path="$(asdf_data_dir)/installs/${plugin_name}"
if [ -d "$plugin_installs_path" ]; then
for install in "${plugin_installs_path}"/*/; do
[[ -e "$install" ]] || break
basename "$install" | sed 's/^ref-/ref:/'
done
fi
}
resolve_legacy_version() {
local strategy="$1" query="$2"
local resolved=
case "$strategy" in
latest_installed)
_list() {
ASDF_NODEJS_SKIP_NODEBUILD_UPDATE=1 list_installed_versions nodejs
}
;;
latest_available)
_list() {
ASDF_NODEJS_SKIP_NODEBUILD_UPDATE=1 "$ASDF_NODEJS_PLUGIN_DIR/bin/list-all" "$query" | tr ' ' '\n'
}
;;
*)
# Just return the original query
printf "%s\n" "$query"
return
esac
resolved=$(_list | grep "^$query" | tail -n1)
if [ "$resolved" ]; then
printf "%s\n" "$resolved"
else
# If no version is installed, fallback to latest_available, so `asdf install nodejs` works
resolve_legacy_version latest_available "$query"
fi
}
resolve_version() {
local query=
query=$(tr '[:upper:]' '[:lower:]' <<<"${1#v}")
@ -81,8 +143,8 @@ resolve_version() {
query="${nodejs_codenames[${#nodejs_codenames[@]} - 1]#*:}"
fi
if [ "${ASDF_NODEJS_RESOLVE_VERSION_CMD-}" ]; then
query=$(bash -c "$ASDF_NODEJS_RESOLVE_VERSION_CMD" -- "$query")
if [ "${ASDF_NODEJS_LEGACY_FILE_DYNAMIC_STRATEGY-}" ]; then
query=$(resolve_legacy_version "$ASDF_NODEJS_LEGACY_FILE_DYNAMIC_STRATEGY" "$query")
fi
printf "%s\n" "$query"