feat: refactor version/aliases resolution

This commit is contained in:
Augusto Moura 2023-04-19 14:24:34 -03:00
parent 644ada3066
commit c406dac170
No known key found for this signature in database
GPG Key ID: BE5D1B3140B8A0A3
5 changed files with 71 additions and 156 deletions

View File

@ -6,87 +6,24 @@ set -eu -o pipefail
source "$(dirname "$0")/../lib/utils.sh"
install_nodejs() {
local install_type="$1" version_query="$2" install_path="$3"
local version=
version="$(resolve_version_query "$version_query")"
if [ "$version" != "$version_query" ]; then
install_aliased_version "$version" "$version_query" "$install_path"
else
install_canon_version "$install_type" "$version" "$install_path"
fi
}
try_to_update_nodebuild() {
local exit_code=0
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-update-nodebuild.bash" 2> /dev/null || exit_code=$?
if [ "$exit_code" != 0 ]; then
printf "
$(colored $YELLOW WARNING): Updating node-build failed with exit code %s. The installation will
try to continue with already installed local defintions. To debug what went
wrong, try to manually update node-build by running: \`asdf %s update nodebuild\`
\n" "$exit_code" "$ASDF_NODEJS_PLUGIN_NAME"
fi
}
install_canon_version() {
local install_type="$1" version="$2" install_path="$3"
local args=()
version=$(resolve_version "$version")
if [ "$install_type" = ref ] || [ "${ASDF_NODEJS_FORCE_COMPILE-}" ]; then
args+=(-c)
fi
try_to_update_nodebuild
if ! [ "${ASDF_NODEJS_SKIP_NODEBUILD_UPDATE-}" ]; then
try_to_update_nodebuild
fi
NODE_BUILD_CACHE_PATH="${NODE_BUILD_CACHE_PATH:-$ASDF_DOWNLOAD_PATH}" \
nodebuild_wrapped ${args+"${args[@]}"} "$version" "$install_path"
}
install_aliased_version() {
local version=$1
local version_query=$2
local install_path=$3
# install the true version and only symlink it to the alias
>&2 echo "Installing alias $version_query as $version"
asdf install "$(plugin_name)" "$version" \
|| die "Could not install version $version"
if [ -L "$install_path" ]; then
rm "$install_path"
else
rmdir "$install_path"
fi
>&2 echo "Linking \"$version_query\" to \"$version\""
ln -s "$(asdf where "$(plugin_name)" "$version")" "$install_path"
}
resolve_version_query() {
local version_query="$1"
local canon_version="$(
# Find the first candidate which the alias match, then print it version
print_index_tab \
| awk -F'\t' -v "alias=$version_query" '$1 == alias { print $2; exit }'
)"
if [ -z "$canon_version" ]; then
echo "$version_query"
else
echo "$canon_version"
fi
}
install_default_npm_packages() {
local default_npm_packages_file="${ASDF_NPM_DEFAULT_PACKAGES_FILE:=$HOME/.default-npm-packages}" filtered_packages=

View File

@ -17,12 +17,13 @@ function print_only_fully_numeric_items() {
grep -E '^[0-9.]+$' <<< "$version_numbers"
}
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-update-nodebuild.bash" &> /dev/null
if ! [ "${ASDF_NODEJS_SKIP_NODEBUILD_UPDATE-}" ]; then
try_to_update_nodebuild >&2
fi
all_definitions_from_node_build="$(nodebuild_wrapped --definitions)"
# Print
echo $(
print_only_fully_numeric_items "$all_definitions_from_node_build"
printf "%s\n" lts-{argon,boron,carbon,dubnium,erbium,fermium,gallium,hydrogen} lts
)

View File

@ -1,3 +1,3 @@
#!/usr/bin/env bash
echo ".nvmrc .node-version"
echo .nvmrc .node-version

View File

@ -1,4 +1,8 @@
#!/usr/bin/env bash
echo $(
sed '1 s/^v//; s/^lts\//lts-/; s/^lts-\*$/lts/' "$1"
)
set -eu -o pipefail
# shellcheck source=../lib/utils.sh
source "$(dirname "$0")/../lib/utils.sh"
resolve_version "$(cat "$1")"

View File

@ -3,8 +3,7 @@
# When in China, set $NODEJS_ORG_MIRROR:
# export NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node/
NODEJS_ORG_MIRROR="${NODEJS_ORG_MIRROR:-https://nodejs.org/dist/}"
if [ ${NODEJS_ORG_MIRROR: -1} != / ]
then
if [ ${NODEJS_ORG_MIRROR: -1} != / ]; then
NODEJS_ORG_MIRROR=$NODEJS_ORG_MIRROR/
fi
@ -31,86 +30,60 @@ colored() {
export RED=31 GREEN=32 YELLOW=33 BLUE=34 MAGENTA=35 CYAN=36
die() {
>&2 echo "$@"
exit 1
}
delete_on_exit() {
trap "rm -rf $@" EXIT
}
# Tab file needs to be piped as stdin
# Print all alias and correspondent versions in the format "$alias\t$version"
# Also prints versions as a alias of itself. Eg: "v10.0.0\tv10.0.0"
filter_version_candidates() {
local curr_line= aliases= definitions=
definitions=$(nodebuild_wrapped --definitions)
# Skip headers
IFS= read -r curr_line
while IFS= read -r curr_line; do
# Just expanding the string should work because tabs are considered array separators
local -a fields=($curr_line)
# Version without `v` prefix
local version="${fields[0]#v}"
# Lowercase lts codename, `-` if not a lts version
local lts_codename=$(echo "${fields[9]}" | tr '[:upper:]' '[:lower:]')
# If not available in nodebuild skip it
if ! grep -q "^$version$" <<< "$definitions"; then
continue
fi
if [ "$lts_codename" != - ]; then
# No lts read yet, so this must be the more recent
if ! grep -q "^lts:" <<< "$aliases"; then
printf "lts\t%s\n" "$version"
aliases="$aliases"$'\n'"lts:$version"
fi
# No lts read for this codename yet, so this must be the more recent
if ! grep -q "^$lts_codename:" <<< "$aliases"; then
printf "lts-$lts_codename\t%s\n" "$version"
aliases="$aliases"$'\n'"$lts_codename:$version"
fi
fi
printf "%s\t%s\n" "$version" "$version"
done
}
versions_cache_dir="$ASDF_NODEJS_CACHE_DIR/versions-tab"
mkdir -p "$versions_cache_dir"
etag_file="$versions_cache_dir/etag"
index_file="$versions_cache_dir/index"
print_index_tab(){
local temp_headers_file= index= curl_opts=()
temp_headers_file=$(mktemp)
delete_on_exit "$temp_headers_file"
if [ -r "$etag_file" ]; then
curl_opts=(--header "If-None-Match: $(cat "$etag_file")")
fi
index=$(curl --fail --silent --location --dump-header "$temp_headers_file" ${curl_opts+"${curl_opts[@]}"} "${NODEJS_ORG_MIRROR}index.tab")
if [ "$index" ]; then
awk 'tolower($1) == "etag:" { print $2 }' < "$temp_headers_file" > "$etag_file"
printf "%s\n" "$index" > "$index_file"
fi
# The `cat` indirection is for a bash3 printf broken pipe error
# https://github.com/asdf-vm/asdf-nodejs/issues/300
cat <(filter_version_candidates < "$index_file")
}
nodebuild_wrapped() {
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-nodebuild.bash" "$@"
}
try_to_update_nodebuild() {
local exit_code=0
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-update-nodebuild.bash" 2>/dev/null || exit_code=$?
if [ "$exit_code" != 0 ]; then
printf "
$(colored $YELLOW WARNING): Updating node-build failed with exit code %s. The installation will
try to continue with already installed local defintions. To debug what went
wrong, try to manually update node-build by running: \`asdf %s update nodebuild\`
\n" "$exit_code" "$ASDF_NODEJS_PLUGIN_NAME"
fi
}
resolve_version() {
local query=
query=$(tr '[:upper:]' '[:lower:]' <<<"${1#v}")
if [[ $query = lts-* ]]; then
query=$(tr - / <<<"$query")
fi
local nodejs_codenames=(
argon:4
boron:6
carbon:8
dubnium:10
erbium:12
fermium:14
gallium:16
hydrogen:18
)
for cod_version in "${nodejs_codenames[@]}"; do
local codename="${cod_version%:*}"
local version_number="${cod_version#*:}"
if [ "${query#lts/}" = "$codename" ]; then
query="$version_number"
break
fi
done
if [ "$query" = lts ] || [ "$query" = "lts/*" ]; then
query="${nodejs_codenames[${#nodejs_codenames[@]} - 1]#*:}"
fi
if [ "${ASDF_NODEJS_RESOLVE_VERSION_CMD-}" ]; then
query=$(bash -c "$ASDF_NODEJS_RESOLVE_VERSION_CMD" -- "$query")
fi
printf "%s\n" "$query"
}