Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support latest with filter on local and global #633

Merged
merged 22 commits into from Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
537472c
Move command that returns the latest version to a utility function
klane May 14, 2020
9c82d11
Add support for filtering installed versions with asdf list
klane May 14, 2020
e1331cd
Add support for using the latest installed version of a tool
klane May 14, 2020
c1bcade
Add tests for new features
klane May 14, 2020
7291d97
Update documentation with new features
klane May 14, 2020
5a084b2
Fix shellcheck errors
klane May 14, 2020
ad53811
Add error messages when filtering yields no compatible versions
klane May 14, 2020
8f5fb89
Remove unnecessary echo flags
klane May 14, 2020
23575a2
Rearrange asdf list error messages to retain original behavior
klane May 14, 2020
163aca7
Improve version filtering error messages and exit codes
klane May 14, 2020
1bb167d
Add tests for version filtering error messages
klane May 14, 2020
be68403
Rename variable to account for previous name change
klane May 14, 2020
b787fe8
Move changes to latest version in CHANGELOG
klane May 14, 2020
00bcb8b
Replace sed with awk to get the latest version of a tool
klane May 14, 2020
81c96e7
Merge branch 'master' into use-latest
jthegedus Jul 6, 2021
b2333e9
Merge branch 'master' into use-latest
jthegedus Jul 6, 2021
c7272f0
chore: rm changelog changes
jthegedus Jul 6, 2021
73f9c58
chore: replace echo with printf
jthegedus Jul 6, 2021
5c4cda6
fix: error when latest cmd with invlid version
jthegedus Jul 6, 2021
6754115
chore: revert command-latest
jthegedus Jul 6, 2021
4e849c7
chore: revert command-latest
jthegedus Jul 6, 2021
495b1d0
fix: local & global with latest and filters
jthegedus Jul 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/core-manage-versions.md
Expand Up @@ -28,6 +28,13 @@ asdf list <name>
# asdf list erlang
```

Limit versions to those that begin with a given string.

```shell
asdf list <name> <version>
# asdf list erlang 17
```

## List All Available Versions

```shell
Expand Down Expand Up @@ -63,6 +70,10 @@ asdf global <name> <version> [<version>...]
asdf shell <name> <version> [<version>...]
asdf local <name> <version> [<version>...]
# asdf global elixir 1.2.4

asdf global <name> latest[:<version>]
asdf local <name> latest[:<version>]
# asdf global elixir latest
```

`global` writes the version to `$HOME/.tool-versions`.
Expand Down
11 changes: 9 additions & 2 deletions lib/commands/command-latest.bash
Expand Up @@ -8,11 +8,18 @@ latest_command() {

[[ -z $query ]] && query="$DEFAULT_QUERY"

local versions
# pattern from xxenv-latest (https://github.com/momo-lab/xxenv-latest)
asdf list-all "$plugin_name" "$query" |
versions=$(asdf list-all "$plugin_name" "$query" |
grep -vE "(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)" |
sed 's/^\s\+//' |
tail -1
tail -1)

if [ -z "${versions}" ]; then
exit 1
fi

printf "%s\n" "$versions"
}

latest_command "$@"
5 changes: 5 additions & 0 deletions lib/commands/command-list-all.bash
Expand Up @@ -32,6 +32,11 @@ list_all_command() {
output=$(cat "$std_out_file")
fi

if [ -z "$output" ]; then
display_error "No compatible versions available ($plugin_name $query)"
exit 1
fi

IFS=' ' read -r -a versions_list <<<"$output"

for version in "${versions_list[@]}"; do
Expand Down
18 changes: 15 additions & 3 deletions lib/commands/command-list.bash
Expand Up @@ -2,6 +2,7 @@

list_command() {
local plugin_name=$1
local query=$2

if [ -z "$plugin_name" ]; then
local plugins_path
Expand All @@ -11,20 +12,31 @@ list_command() {
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")
printf "%s\\n" "$plugin_name"
display_installed_versions "$plugin_name"
display_installed_versions "$plugin_name" "$query"
done
else
printf "%s\\n" 'No plugins installed'
fi
else
check_if_plugin_exists "$plugin_name"
display_installed_versions "$plugin_name"
display_installed_versions "$plugin_name" "$query"
fi
}

display_installed_versions() {
local plugin_name=$1
local query=$2
local versions
versions=$(list_installed_versions "$1")
versions=$(list_installed_versions "$plugin_name")

if [[ $query ]]; then
versions=$(printf "%s\n" "$versions" | grep -E "^\s*$query")

if [ -z "${versions}" ]; then
display_error "No compatible versions installed ($plugin_name $query)"
exit 1
fi
fi

if [ -n "${versions}" ]; then
for version in $versions; do
Expand Down
19 changes: 16 additions & 3 deletions lib/commands/version_commands.bash
Expand Up @@ -33,15 +33,28 @@ version_command() {
check_if_plugin_exists "$plugin_name"

declare -a resolved_versions
local version
for version in "${versions[@]}"; do
if [ "$version" = "latest" ]; then
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=$(asdf latest "$plugin_name" "${version_info[1]}")
elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then
version=$(asdf latest "$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
Comment on lines +52 to 56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check_if_version_exists handles the ref:<ref> & path:<dir> filters two calls deeper in the stack. Those I feel should be elevated to this level as latest:<filter> is handled here.

Copy link
Contributor

@jthegedus jthegedus Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not suggestion we do this now, but this codebase is becoming spaghetti as everything gets pushed down into utils.bash


resolved_versions+=("$version")
done

Expand Down
10 changes: 8 additions & 2 deletions test/latest_command.bats
Expand Up @@ -13,12 +13,18 @@ teardown() {

@test "latest_command shows latest stable version" {
run asdf latest dummy
[ "$(echo -e "2.0.0")" == "$output" ]
[ "$(echo "2.0.0")" == "$output" ]
[ "$status" -eq 0 ]
}

@test "latest_command with version shows latest stable version that matches the given string" {
run asdf latest dummy 1
[ "$(echo -e "1.1.0")" == "$output" ]
[ "$(echo "1.1.0")" == "$output" ]
[ "$status" -eq 0 ]
}

@test "latest_command with an invalid version should return an error" {
run asdf latest dummy 3
[ "$(echo "No compatible versions available (dummy 3)")" == "$output" ]
[ "$status" -eq 1 ]
}
24 changes: 23 additions & 1 deletion test/list_command.bats
Expand Up @@ -43,6 +43,23 @@ teardown() {
[ "$status" -eq 0 ]
}

@test "list_command with version filters installed versions" {
run asdf install dummy 1.0
run asdf install dummy 1.1
run asdf install dummy 2.0
run asdf list dummy 1
[ "$(echo -e " 1.0\n 1.1")" == "$output" ]
[ "$status" -eq 0 ]
}

@test "list_command with an invalid version should return an error" {
run asdf install dummy 1.0
run asdf install dummy 1.1
run asdf list dummy 2
[ "$(echo "No compatible versions installed (dummy 2)")" == "$output" ]
[ "$status" -eq 1 ]
}

@test "list_all_command lists available versions" {
run asdf list-all dummy
[ "$(echo -e "1.0.0\n1.1.0\n2.0.0")" == "$output" ]
Expand All @@ -55,6 +72,12 @@ teardown() {
[ "$status" -eq 0 ]
}

@test "list_all_command with an invalid version should return an error" {
run asdf list-all dummy 3
[ "$(echo "No compatible versions available (dummy 3)")" == "$output" ]
[ "$status" -eq 1 ]
}

@test "list_all_command fails when list-all script exits with non-zero code" {
run asdf list-all dummy-broken
echo $output
Expand All @@ -69,7 +92,6 @@ teardown() {
[[ "$output" == *"Attempting to list versions" ]]
}


@test "list_all_command ignores stderr when completing successfully" {
run asdf list-all dummy
[[ "$output" != *"ignore this error"* ]]
Expand Down
37 changes: 37 additions & 0 deletions test/version_commands.bats
Expand Up @@ -47,6 +47,24 @@ teardown() {
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
}

@test "local with latest should use the latest installed version" {
run asdf local "dummy" "latest"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 2.0.0" ]
}

@test "local with latest:version should use the latest valid installed version" {
run asdf local "dummy" "latest:1.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.0.0" ]
}

@test "local with latest:version should return an error for invalid versions" {
run asdf local "dummy" "latest:99"
[ "$status" -eq 1 ]
[ "$output" = "$(echo "No compatible versions available (dummy 99)")" ]
}

@test "local should allow multiple versions" {
run asdf local "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]
Expand Down Expand Up @@ -75,6 +93,7 @@ teardown() {

@test "local should overwrite the existing version if it's set" {
echo 'dummy 1.0.0' >> $PROJECT_DIR/.tool-versions

run asdf local "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
Expand Down Expand Up @@ -141,6 +160,24 @@ teardown() {
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
}

@test "global with latest should use the latest installed version" {
run asdf global "dummy" "latest"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "dummy 2.0.0" ]
}

@test "global with latest:version should use the latest valid installed version" {
run asdf global "dummy" "latest:1.0"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = "dummy 1.0.0" ]
}

@test "global with latest:version should return an error for invalid versions" {
run asdf global "dummy" "latest:99"
[ "$status" -eq 1 ]
[ "$output" = "$(echo "No compatible versions available (dummy 99)")" ]
}

@test "global should accept multiple versions" {
run asdf global "dummy" "1.1.0" "1.0.0"
[ "$status" -eq 0 ]
Expand Down