From 461f8f26ef1f3c5693e450aeef8774b7dad42cca Mon Sep 17 00:00:00 2001 From: Alberto Martinez de Murga Ramirez Date: Tue, 16 Nov 2021 23:52:12 +0900 Subject: [PATCH] feat(latest): adds the flag --all to the latest command As discussed in #1036, this patch adds a `--all` flag to the latest command which will show the list of plugins, their latest version and if this one is installed or not. This patch basically reuses the logic used in the list command and the latest command. The main different is the removal of the query paramenter as different plugins might need different queries. --- lib/commands/command-latest.bash | 49 ++++++++++++++++++++++++++++++++ test/latest_command.bats | 9 ++++++ 2 files changed, 58 insertions(+) diff --git a/lib/commands/command-latest.bash b/lib/commands/command-latest.bash index 3610dcf55..d086b5149 100644 --- a/lib/commands/command-latest.bash +++ b/lib/commands/command-latest.bash @@ -7,6 +7,10 @@ latest_command() { local query=$2 local plugin_path + if [ "$plugin_name" == "--all" ]; then + latest_all + fi + [[ -z $query ]] && query="$DEFAULT_QUERY" plugin_path=$(get_plugin_path "$plugin_name") @@ -35,4 +39,49 @@ latest_command() { printf "%s\n" "$versions" } +latest_all() { + local plugins_path + plugins_path=$(get_plugin_path) + + if ls "$plugins_path" &>/dev/null; then + for plugin_path in "$plugins_path"/*; do + plugin_name=$(basename "$plugin_path") + + # Retrieve the version of the plugin + local version + if [ -f "${plugin_path}/bin/latest-stable" ]; then + # We can't filter by a concrete query because different plugins might + # have different queries. + version=$("${plugin_path}"/bin/latest-stable "") + if [ -z "${version}" ]; then + version="unknown" + fi + else + # pattern from xxenv-latest (https://github.com/momo-lab/xxenv-latest) + version=$(asdf list-all "$plugin_name" | + grep -vE "(^Available version:|-src|-dev|-latest|-stm|[-\\.]rc|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)" | + sed 's/^[[:space:]]\+//' | + tail -1) + if [ -z "${version}" ]; then + version="unknown" + fi + fi + + local installed_status + installed_status="missing" + + local installed_versions + installed_versions=$(list_installed_versions "$plugin_name") + + if ! printf '%s\n' "$installed_versions" | grep -q "^$version\$"; then + installed_status="installed" + fi + printf "%s\\t%s\\t%s\\n" "$plugin_name" "$version" "$installed_status" + done + else + printf "%s\\n" 'No plugins installed' + fi + exit 0 +} + latest_command "$@" diff --git a/test/latest_command.bats b/test/latest_command.bats index dacd8622f..ea55a7dfb 100644 --- a/test/latest_command.bats +++ b/test/latest_command.bats @@ -59,3 +59,12 @@ teardown() { [ "$(echo "No compatible versions available (legacy-dummy 3)")" == "$output" ] [ "$status" -eq 1 ] } + +################################ +#### latest --all #### +################################ +@test "[latest_command - all plugins] shows the latest stable version of all plugins" { + run asdf latest --all + [ "$(echo -e "dummy\t2.0.0\tinstalled\nlegacy-dummy\t2.0.0\tinstalled\n")" == "$output" ] + [ "$status" -eq 0 ] +}