Skip to content

Commit

Permalink
Install most recent point release of inexact versions
Browse files Browse the repository at this point in the history
This change will make it such that when installing a version like so:

`asdf install nodejs 16.10`

It would install 16.10.3, assuming 16.10.3 is the latest point release
at the time of installation.  This change allows asdf to work better
with legacy version specifiers such as what would be stored in `.nvmrc`.

For this to work properly, it would also (in the example above), symlink
16.10 to point to 16.10.3 (and likewise to link 16 to 16.10.3).  Such
symlinks are expected to point to the latest version installed that
satisfies the partial version number.

An uninstallation script has to be added so that symlinks aren't left
dangling at any point.

This fix addresses issue asdf-vm#295.
  • Loading branch information
Daniel Falk committed Jan 13, 2023
1 parent c9e5df4 commit ad1c77c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ install_canon_version() {

NODE_BUILD_CACHE_PATH="${NODE_BUILD_CACHE_PATH:-$ASDF_DOWNLOAD_PATH}" \
nodebuild_wrapped ${args+"${args[@]}"} "$version" "$install_path"

update_version_symlinks "$(dirname $install_path)"
}


Expand Down Expand Up @@ -80,7 +82,13 @@ resolve_version_query() {
)"

if [ -z "$canon_version" ]; then
echo "$version_query"
local latest_point_version="$(
# Find the latest point release (e.g., a version_query of 16.10 can find 16.10.2)
print_index_tab |
egrep "^${version_query}\.[0-9\.]+ " -m1 |
awk -F'\t' '{ print $2; exit }'
)"
echo "${latest_point_version:-$version_query}"
else
echo "$canon_version"
fi
Expand Down
15 changes: 15 additions & 0 deletions bin/uninstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

# Unoffical Bash "strict mode"
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail

IFS=$'\t\n' # Stricter IFS settings


# shellcheck source=bin/utils.sh
source "$(dirname "$0")/../lib/utils.sh"

rm -rf "$ASDF_INSTALL_PATH" || true

update_version_symlinks "$(dirname "$ASDF_INSTALL_PATH")"
31 changes: 31 additions & 0 deletions lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@ print_index_tab(){
cat <(filter_version_candidates < "$index_file")
}

# /path/to/installs/16.10.1 => "/path/to/installs/16.10"
# /path/to/installs/16.10 => "/path/to/installs/16"
# /path/to/installs/16 => ""
# /path/to/installs/ => ""
strip_last_version_part() {
echo "$1" | sed -n -e 's#^\(.\+\)\.[[:digit:]]\+$#\1#p'
}

update_version_symlinks() {
cd "$1"

find * -maxdepth 1 -type l -regex '[0-9.]*' -delete

for dir in $(find * -maxdepth 0 -type d | egrep '^[0-9\.]+$' | sort --version-sort); do
alias_path="$dir"

for i in $(seq 1 5); do
alias_path=$(strip_last_version_part "$alias_path")

[ -z "$alias_path" ] && break

# if the alias path already exists but isn't a link, ignore
[ -e "$alias_path" -a ! -L "$alias_path" ] && continue

# Overwrite the symlink
rm "$alias_path" 2> /dev/null || true
ln -s -f "$dir" "$alias_path"
done
done
}

nodebuild_wrapped() {
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-nodebuild.bash" "$@"
}

0 comments on commit ad1c77c

Please sign in to comment.