diff --git a/bin/asdf b/bin/asdf index 439fd1814..25279c5b1 100755 --- a/bin/asdf +++ b/bin/asdf @@ -7,6 +7,8 @@ fi # shellcheck source=lib/utils.bash . "$(dirname "$(dirname "$0")")/lib/utils.bash" +# shellcheck source=lib/functions/versions.bash +. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" find_cmd() { local cmd_dir="$1" diff --git a/lib/commands/command-current.bash b/lib/commands/command-current.bash index f4724626e..973d6cb8e 100644 --- a/lib/commands/command-current.bash +++ b/lib/commands/command-current.bash @@ -21,7 +21,8 @@ plugin_current_command() { local description="" IFS=' ' read -r -a versions <<<"$full_version" - for version in "${versions[@]}"; do + for version_spec in "${versions[@]}"; do + version="$(resolve_version_spec "$version_spec")" if ! (check_if_version_exists "$plugin_name" "$version"); then version_not_installed="$version" fi diff --git a/lib/functions/versions.bash b/lib/functions/versions.bash index 7d1d941d6..b796408a5 100644 --- a/lib/functions/versions.bash +++ b/lib/functions/versions.bash @@ -37,27 +37,7 @@ version_command() { declare -a resolved_versions local item for item in "${!versions[@]}"; do - IFS=':' read -r -a version_info <<<"${versions[$item]}" - if [ "${version_info[0]}" = "latest" ] && [ -n "${version_info[1]}" ]; then - version=$(latest_command "$plugin_name" "${version_info[1]}") - elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then - version=$(latest_command "$plugin_name") - else - # if branch handles ref: || path: || normal versions - version="${versions[$item]}" - fi - - # check_if_version_exists should probably handle if either param is empty string - if [ -z "$version" ]; then - exit 1 - fi - - if ! (check_if_version_exists "$plugin_name" "$version"); then - version_not_installed_text "$plugin_name" "$version" 1>&2 - exit 1 - fi - - resolved_versions+=("$version") + resolved_versions+=("$(resolve_version_spec "${versions[$item]}")") done if [ -f "$file" ] && grep -q "^$plugin_name " "$file"; then @@ -79,6 +59,32 @@ version_command() { fi } +resolve_version_spec() { + local version_spec=$1 + + IFS=':' read -r -a version_info <<<"$version_spec" + if [ "${version_info[0]}" = "latest" ] && [ -n "${version_info[1]}" ]; then + version=$(latest_command "$plugin_name" "${version_info[1]}") + elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then + version=$(latest_command "$plugin_name") + else + # if branch handles ref: || path: || normal versions + version="${versions[$item]}" + fi + + # check_if_version_exists should probably handle if either param is empty string + if [ -z "$version" ]; then + exit 1 + fi + + if ! (check_if_version_exists "$plugin_name" "$version"); then + version_not_installed_text "$plugin_name" "$version" 1>&2 + exit 1 + fi + + printf "%s\n" "$version" +} + list_all_command() { local plugin_name=$1 local query=$2 diff --git a/lib/utils.bash b/lib/utils.bash index 21978a929..8b40bf548 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -736,7 +736,8 @@ select_version() { version_and_path=$(find_versions "$plugin_name" "$search_path") IFS='|' read -r version_string _path <<<"$version_and_path" IFS=' ' read -r -a usable_plugin_versions <<<"$version_string" - for plugin_version in "${usable_plugin_versions[@]}"; do + for version_spec in "${usable_plugin_versions[@]}"; do + plugin_version="$(resolve_version_spec "$version_spec")" for plugin_and_version in "${shim_versions[@]}"; do local plugin_shim_name local plugin_shim_version diff --git a/test/current_command.bats b/test/current_command.bats index 687bc9da8..caf3659f0 100755 --- a/test/current_command.bats +++ b/test/current_command.bats @@ -47,6 +47,18 @@ teardown() { [ "$output" = "$expected" ] } +@test "current should not error on version specifications like 'latest:'" { + cd "$PROJECT_DIR" + echo "dummy 1.2.0 latest:1.1" >>"$PROJECT_DIR/.tool-versions" + expected="dummy 1.2.0 latest:1.1 $PROJECT_DIR/.tool-versions" + + asdf current "dummy" + echo "$expected" + run asdf current "dummy" + [ "$status" -eq 0 ] + [ "$output" = "$expected" ] +} + @test "current should derive from the legacy file if enabled" { cd "$PROJECT_DIR" echo 'legacy_version_file = yes' >"$HOME/.asdfrc" diff --git a/test/install_command.bats b/test/install_command.bats index bad99898a..418808762 100644 --- a/test/install_command.bats +++ b/test/install_command.bats @@ -45,6 +45,15 @@ teardown() { [ "$(cat "$ASDF_DIR/installs/dummy/1.2.0/version")" = "1.2.0" ] } +@test "install_command installs the 'latest:' version in .tool-versions" { + cd "$PROJECT_DIR" + echo -n 'dummy latest:1.1' >".tool-versions" + run asdf install dummy + ls "$ASDF_DIR/installs/dummy/" + [ "$status" -eq 0 ] + [ "$(cat "$ASDF_DIR/installs/dummy/1.1.0/version")" = "1.1.0" ] +} + @test "install_command set ASDF_CONCURRENCY" { run asdf install dummy 1.0.0 [ "$status" -eq 0 ]