From ca1273e7d4b023bc053026df3295f1159e09deec Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Mon, 25 Jul 2016 00:47:17 +0900 Subject: [PATCH] Rename which to current (#79) * Rename `asdf which` -> `asdf current` * Output `set by $path` with current command * Use dummy plugin in current_command test * Hide "set by" message if derived from legacy file --- README.md | 6 +- bin/asdf | 6 +- completions/asdf.bash | 4 +- completions/asdf.fish | 6 +- help.txt | 2 +- lib/commands/current.sh | 20 ++++++ lib/commands/which.sh | 15 ----- lib/utils.sh | 15 +++++ test/current_command.bats | 61 +++++++++++++++++++ .../bin/get-version-from-legacy-file | 12 ++++ 10 files changed, 120 insertions(+), 27 deletions(-) create mode 100644 lib/commands/current.sh delete mode 100644 lib/commands/which.sh create mode 100644 test/current_command.bats create mode 100755 test/fixtures/dummy_plugin/bin/get-version-from-legacy-file diff --git a/README.md b/README.md index db4f6b91..05283295 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,9 @@ asdf plugin-update asdf install # asdf install erlang 17.3 -asdf which -# asdf which erlang -# 17.3 +asdf current +# asdf current erlang +# 17.3 (set by /Users/kim/.tool-versions) asdf uninstall # asdf uninstall erlang 17.3 diff --git a/bin/asdf b/bin/asdf index 5296f37d..8a1e04ba 100755 --- a/bin/asdf +++ b/bin/asdf @@ -5,7 +5,7 @@ source $(dirname $(dirname $0))/lib/utils.sh source $(dirname $(dirname $0))/lib/commands/help.sh source $(dirname $(dirname $0))/lib/commands/install.sh source $(dirname $(dirname $0))/lib/commands/uninstall.sh -source $(dirname $(dirname $0))/lib/commands/which.sh +source $(dirname $(dirname $0))/lib/commands/current.sh source $(dirname $(dirname $0))/lib/commands/where.sh source $(dirname $(dirname $0))/lib/commands/version_commands.sh source $(dirname $(dirname $0))/lib/commands/list.sh @@ -36,8 +36,8 @@ case $1 in "uninstall") uninstall_command $callback_args;; -"which") - which_command $callback_args;; +"current") + current_command $callback_args;; "where") where_command $callback_args;; diff --git a/completions/asdf.bash b/completions/asdf.bash index 16b0d905..db744aa0 100644 --- a/completions/asdf.bash +++ b/completions/asdf.bash @@ -12,7 +12,7 @@ _asdf () { plugin-update) COMPREPLY=($(compgen -W "$plugins --all" -- $cur)) ;; - plugin-remove|which|list|list-all) + plugin-remove|current|list|list-all) COMPREPLY=($(compgen -W "$plugins" -- $cur)) ;; install) @@ -32,7 +32,7 @@ _asdf () { fi ;; *) - local cmds='plugin-add plugin-list plugin-remove plugin-update install uninstall which where list list-all reshim' + local cmds='plugin-add plugin-list plugin-remove plugin-update install uninstall current where list list-all reshim' COMPREPLY=($(compgen -W "$cmds" -- $cur)) ;; esac diff --git a/completions/asdf.fish b/completions/asdf.fish index 621962e8..b2948ea6 100644 --- a/completions/asdf.fish +++ b/completions/asdf.fish @@ -56,9 +56,9 @@ complete -f -c asdf -n '__fish_asdf_needs_command' -a uninstall -d "Remove a spe complete -f -c asdf -n '__fish_asdf_using_command uninstall; and __fish_asdf_arg_number 2' -a '(asdf plugin-list)' complete -f -c asdf -n '__fish_asdf_using_command uninstall; and __fish_asdf_arg_number 3' -a '(asdf list (__fish_asdf_arg_at 3))' -# which completion -complete -f -c asdf -n '__fish_asdf_needs_command' -a which -d "Display version set or being used for package" -complete -f -c asdf -n '__fish_asdf_using_command which; and __fish_asdf_arg_number 2' -a '(asdf plugin-list)' +# current completion +complete -f -c asdf -n '__fish_asdf_needs_command' -a current -d "Display version set or being used for package" +complete -f -c asdf -n '__fish_asdf_using_command current; and __fish_asdf_arg_number 2' -a '(asdf plugin-list)' # where completion complete -f -c asdf -n '__fish_asdf_needs_command' -a where -d "Display install path for an installed version" diff --git a/help.txt b/help.txt index df7df2da..aaaf8b99 100644 --- a/help.txt +++ b/help.txt @@ -11,7 +11,7 @@ MANAGE PACKAGES with no arguments, install all the package versions listed in the .tool-versions file asdf uninstall Remove a specific version of a package - asdf which Display version set or being used for package + asdf current Display current version set or being used for package asdf where Display install path for an installed version asdf local [name] Display a package local version asdf local Set the package local version diff --git a/lib/commands/current.sh b/lib/commands/current.sh new file mode 100644 index 00000000..d7359a04 --- /dev/null +++ b/lib/commands/current.sh @@ -0,0 +1,20 @@ +current_command() { + local plugin_name=$1 + local version=$(get_preset_version_for $plugin_name) + + check_if_plugin_exists $plugin_name + + if [ "$version" == "" ]; then + echo "No version set for $plugin_name" + exit 1 + else + local version_file_path=$(get_version_file_path_for $plugin_name) + if [ "$version_file_path" == "" ]; then + echo "$version" + else + echo "$version (set by $version_file_path)" + fi + + exit 0 + fi +} diff --git a/lib/commands/which.sh b/lib/commands/which.sh deleted file mode 100644 index d9fb0191..00000000 --- a/lib/commands/which.sh +++ /dev/null @@ -1,15 +0,0 @@ -which_command() { - local plugin_name=$1 - local plugin_path=$(get_plugin_path $plugin_name) - check_if_plugin_exists $plugin_name - - full_version=$(get_preset_version_for $plugin_name) - - if [ "$full_version" == "" ]; then - echo "No version set for ${plugin_name}" - exit -1 - else - echo "$full_version" - exit 0 - fi -} diff --git a/lib/utils.sh b/lib/utils.sh index 00c12f02..727ad66b 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -67,6 +67,21 @@ display_error() { echo >&2 $1 } +get_version_file_path_for() { + local tool_name=$1 + local legacy_version_file_support=$(get_asdf_config_value "legacy_version_file") + + if [ ! -f "$(pwd)/.tool-versions" ] && [ "$legacy_version_file_support" = "yes" ]; then + # Check for legacy version and return "" if it exists + local legacy_version=$(get_tool_version_from_legacy_file $tool_name $(pwd)) + if [ "$legacy_version" != "" ]; then + echo "" + return 1 + fi + fi + + echo $(get_asdf_versions_file_path) +} get_asdf_versions_file_path() { local asdf_tool_versions_path="" diff --git a/test/current_command.bats b/test/current_command.bats new file mode 100644 index 00000000..562baa4b --- /dev/null +++ b/test/current_command.bats @@ -0,0 +1,61 @@ +#!/usr/bin/env bats + +load test_helpers + +. $(dirname $BATS_TEST_DIRNAME)/lib/commands/current.sh + +setup() { + setup_asdf_dir + install_dummy_plugin + + PROJECT_DIR=$HOME/project + OTHER_DIR=$HOME/other + mkdir -p $ASDF_DIR/installs/dummy/1.0.0 $ASDF_DIR/installs/dummy/1.1.0 $PROJECT_DIR $OTHER_DIR + + echo 'dummy 1.0.0' >> $HOME/.tool-versions + echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions + echo '1.2.0' >> $OTHER_DIR/.dummy-version +} + +teardown() { + clean_asdf_dir +} + +@test "current should derive from the local .tool_versions when it exists" { + cd $PROJECT_DIR + + run current_command "dummy" + [ "$status" -eq 0 ] + [ "$output" = "1.1.0 (set by $PROJECT_DIR/.tool-versions)" ] +} + +@test "current should derive from the global .tool_versions when local doesn't exist" { + cd $OTHER_DIR + + run current_command "dummy" + [ "$status" -eq 0 ] + [ "$output" = "1.0.0 (set by $HOME/.tool-versions)" ] +} + +@test "current should derive from the legacy file if enabled and hide the file path" { + echo 'legacy_version_file = yes' > $HOME/.asdfrc + cd $OTHER_DIR + + run current_command "dummy" + [ "$status" -eq 0 ] + [ "$output" = "1.2.0" ] +} + +@test "current should error when the plugin doesn't exist" { + run current_command "foobar" + [ "$status" -eq 1 ] +} + +@test "current should error when no version is set" { + cd $OTHER_DIR + rm $HOME/.tool-versions + + run current_command "dummy" + [ "$status" -eq 1 ] + [ "$output" = "No version set for dummy" ] +} diff --git a/test/fixtures/dummy_plugin/bin/get-version-from-legacy-file b/test/fixtures/dummy_plugin/bin/get-version-from-legacy-file new file mode 100755 index 00000000..235f6e59 --- /dev/null +++ b/test/fixtures/dummy_plugin/bin/get-version-from-legacy-file @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +get_legacy_version() { + current_directory=$1 + version_file="$current_directory/.dummy-version" + + if [ -f $version_file ]; then + cat $version_file + fi +} + +get_legacy_version $1 \ No newline at end of file