diff --git a/README.md b/README.md index 05283295..84785cb4 100644 --- a/README.md +++ b/README.md @@ -132,19 +132,11 @@ asdf list-all #### View current version ```bash -asdf local [name] -asdf global [name] -# asdf local -# asdf global -# asdf local elixir -# asdf global elixir +asdf current +# asdf current erlang +# 17.3 (set by /Users/kim/.tool-versions) ``` -`global` reads from `$HOME/.tool-versions`. - -`local` reads from `$PWD/.tool-versions` if it exists, -or searches recursively in the parent directories until it finds a `.tool-versions` file. - #### Set current version ```bash diff --git a/help.txt b/help.txt index aaaf8b99..b58ed1a8 100644 --- a/help.txt +++ b/help.txt @@ -13,9 +13,7 @@ MANAGE PACKAGES asdf uninstall Remove a specific version of a 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 - asdf global [name] Display a package global version asdf global Set the package global version asdf list List installed versions of a package asdf list-all List all versions of a package diff --git a/lib/commands/version_commands.sh b/lib/commands/version_commands.sh index 62b67cd0..d608e024 100644 --- a/lib/commands/version_commands.sh +++ b/lib/commands/version_commands.sh @@ -1,70 +1,21 @@ -get_plugin_version() { - local cmd=$1 - local file=$2 - local plugin=$3 - local legacy_version_file_support=$(get_asdf_config_value "legacy_version_file") - local result - - if [ $cmd = "local" -a "$legacy_version_file_support" = "yes" -a \ - \( ! -f $file -o $file = "$HOME/.tool-versions" \) ]; then - result=$(get_tool_version_from_legacy_file $plugin $(pwd)) - if [ -n "$result" ]; then - echo $result - exit 0 - fi - fi - - if [ -f $file ]; then - result=$(get_tool_version_from_file $file $plugin) - fi - - if [ -n "$result" ]; then - echo $result - exit 0 - fi - - - echo "version not set for $plugin" - exit 1 -} - version_command() { local cmd=$1 - + local plugin=$2 + local version=$3 local file - if [ $cmd = "global" ]; then - file=$HOME/.tool-versions - else - file=$(get_asdf_versions_file_path) - if [ -z "$file" ]; then - file=.tool-versions - fi - fi - if [ $# -eq 1 -a ! -f $file ]; then - echo $file does not exist + if [ "$#" -ne 3 ]; then + echo "Usage: asdf $cmd " exit 1 fi - if [ $# -eq 1 ]; then - cat $file - exit 0 - fi - - local plugin=$2 - - check_if_plugin_exists $plugin - - if [ $# -eq 2 ]; then - get_plugin_version $cmd $file $plugin - fi - - local version=${@:3} - - if [ $cmd = "local" ]; then + if [ $cmd = "global" ]; then + file=$HOME/.tool-versions + else file=$(pwd)/.tool-versions fi + check_if_plugin_exists $plugin check_if_version_exists $plugin $version if [ -f $file ] && grep $plugin $file > /dev/null; then diff --git a/test/version_commands.bats b/test/version_commands.bats index 7399af6e..1471e818 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -9,14 +9,10 @@ setup() { install_dummy_plugin install_dummy_version "1.0.0" install_dummy_version "1.1.0" - install_dummy_version "1.2.0" - PROJECT_DIR=$BASE_DIR/project + PROJECT_DIR=$HOME/project mkdir -p $PROJECT_DIR - echo 'dummy 1.0.0' >> $HOME/.tool-versions - echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions - cd $PROJECT_DIR } @@ -24,19 +20,11 @@ teardown() { clean_asdf_dir } - -@test "local should emit an error when run in lookup mode and file does not exist" { - rm .tool-versions - run local_command +# Warn users who invoke the old style command without arguments. +@test "local should emit an error when called with incorrect arity" { + run local_command "dummy" [ "$status" -eq 1 ] - [ "$output" = ".tool-versions does not exist" ] -} - -@test "global should emit an error when run in lookup mode and file does not exist" { - rm $HOME/.tool-versions - run global_command "dummy" - [ "$status" -eq 1 ] - [ "$output" = "version not set for dummy" ] + [ "$output" = "Usage: asdf local " ] } @test "local should emit an error when plugin does not exist" { @@ -51,69 +39,28 @@ teardown() { [ "$output" = "version 0.0.1 is not installed for dummy" ] } -@test "local should return and set the local version" { - - run local_command +@test "local should create a local .tool-versions file if it doesn't exist" { + run local_command "dummy" "1.1.0" [ "$status" -eq 0 ] - [ "$output" = "dummy 1.1.0" ] - - run local_command dummy "1.2.0" - - run local_command dummy - [ "$status" -eq 0 ] - [ "$output" = "1.2.0" ] - - rm .tool-versions - run local_command dummy 1.2.0 - [ -f .tool-versions ] - - run local_command dummy - [ "$status" -eq 0 ] - [ "$output" = "1.2.0" ] - run global_command dummy - [ "$output" = "1.0.0" ] - - mkdir $BASE_DIR/other && cd $BASE_DIR/other - - run local_command dummy - [ "$status" -eq 1 ] - - run local_command dummy 1.0.0 - [ "$status" -eq 0 ] - - run local_command dummy - [ "$status" -eq 0 ] - [ "$output" = "1.0.0" ] + [ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ] } -@test "local should fallback to legacy-file when enabled" { - echo 'legacy_version_file = yes' > $HOME/.asdfrc - echo '1.3.0' > .dummy-version - rm .tool-versions - run local_command dummy - - [ "$status" -eq 0 ] - [ "$output" = "1.3.0" ] +@test "local should overwrite the existing version if it's set" { + echo 'dummy 1.0.0' >> $PROJECT_DIR/.tool-versions + run local_command "dummy" "1.1.0" + [ "$status" -eq 0 ] + [ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ] } -@test "local should ignore legacy-file when disabled" { - rm .tool-versions - run local_command dummy - - [ "$status" -eq 1 ] - [ "$output" = "version not set for dummy" ] +@test "global should create a global .tool-versions file if it doesn't exist" { + run global_command "dummy" "1.1.0" + [ "$status" -eq 0 ] + [ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ] } - -@test "global should return and set the global version" { - run global_command +@test "global should overwrite the existing version if it's set" { + echo 'dummy 1.0.0' >> $HOME/.tool-versions + run global_command "dummy" "1.1.0" [ "$status" -eq 0 ] - [ "$output" = "dummy 1.0.0" ] - - run global_command dummy 1.2.0 - [ "$status" -eq 0 ] - - run global_command dummy - [ "$status" -eq 0 ] - [ "$output" = "1.2.0" ] + [ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ] }