Improve current command.

* Allow to be called without arguments
* Fix error in path display
* Add --short option
This commit is contained in:
Daniel Perez 2016-08-02 16:06:58 +09:00
parent 3d5a5b9040
commit 72630a0b10
5 changed files with 95 additions and 20 deletions

View File

@ -11,7 +11,7 @@ MANAGE PACKAGES
with no arguments, install all the package with no arguments, install all the package
versions listed in the .tool-versions file versions listed in the .tool-versions file
asdf uninstall <name> <version> Remove a specific version of a package asdf uninstall <name> <version> Remove a specific version of a package
asdf current <name> Display current version set or being used for package asdf current [--short] [<name>] Display current version set or being used
asdf where <name> <version> Display install path for an installed version asdf where <name> <version> Display install path for an installed version
asdf local <name> <version> Set the package local version asdf local <name> <version> Set the package local version
asdf global <name> <version> Set the package global version asdf global <name> <version> Set the package global version

View File

@ -1,20 +1,46 @@
current_command() { current_command() {
local plugin_name=$1 local exit_code=0
local short=false
local plugin_names=""
while [ $# -gt 0 ]
do
case "$1" in
--short) short=true ;;
-*) ;;
*)
plugin_names="$1" ;;
esac
shift
done
if [ -z "$plugin_names" ]; then
read -a plugin_names <<< $(plugin_list_command)
fi
for plugin_name in "${plugin_names[@]}"; do
local version=$(get_preset_version_for $plugin_name) local version=$(get_preset_version_for $plugin_name)
check_if_plugin_exists $plugin_name check_if_plugin_exists $plugin_name
if [ "$version" == "" ]; then if [ "$version" == "" ]; then
echo "No version set for $plugin_name" echo "No version set for $plugin_name"
exit 1 exit_code=1
else
if [ "$short" = true ]; then
echo "$version"
else else
local version_file_path=$(get_version_file_path_for $plugin_name) local version_file_path=$(get_version_file_path_for $plugin_name)
if [ "$version_file_path" == "" ]; then if [ "$version_file_path" == "" ]; then
echo "$version" echo "$plugin_name $version"
else else
echo "$version (set by $version_file_path)" echo "$plugin_name $version (set by $version_file_path)"
fi fi
fi
fi
done
exit 0 exit $exit_code
fi
} }

View File

@ -80,15 +80,18 @@ get_version_file_path_for() {
fi fi
fi fi
echo $(get_asdf_versions_file_path) echo $(get_asdf_versions_file_path $tool_name)
} }
get_asdf_versions_file_path() { get_asdf_versions_file_path() {
local tool_name=$1
local asdf_tool_versions_path="" local asdf_tool_versions_path=""
local search_path=$(pwd) local search_path=$(pwd)
while [ "$search_path" != "/" ]; do while [ "$search_path" != "/" ]; do
if [ -f "$search_path/.tool-versions" ]; then local file="$search_path/.tool-versions"
if [ -f $file ] && \
( [ -z "$tool_name" ] || grep -q "$tool_name" "$file" ); then
asdf_tool_versions_path="$search_path/.tool-versions" asdf_tool_versions_path="$search_path/.tool-versions"
break break
fi fi

View File

@ -2,6 +2,7 @@
load test_helpers load test_helpers
. $(dirname $BATS_TEST_DIRNAME)/lib/commands/plugin-list.sh
. $(dirname $BATS_TEST_DIRNAME)/lib/commands/current.sh . $(dirname $BATS_TEST_DIRNAME)/lib/commands/current.sh
setup() { setup() {
@ -11,8 +12,10 @@ setup() {
PROJECT_DIR=$HOME/project PROJECT_DIR=$HOME/project
OTHER_DIR=$HOME/other OTHER_DIR=$HOME/other
mkdir -p $ASDF_DIR/installs/dummy/1.0.0 $ASDF_DIR/installs/dummy/1.1.0 $PROJECT_DIR $OTHER_DIR mkdir -p $ASDF_DIR/installs/dummy/1.0.0 $ASDF_DIR/installs/dummy/1.1.0 $PROJECT_DIR $OTHER_DIR
mkdir -p $ASDF_DIR/installs/other/1.0.0 $ASDF_DIR/plugins/other
echo 'dummy 1.0.0' >> $HOME/.tool-versions echo 'dummy 1.0.0' >> $HOME/.tool-versions
echo 'other 1.0.0' >> $HOME/.tool-versions
echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions
echo '1.2.0' >> $OTHER_DIR/.dummy-version echo '1.2.0' >> $OTHER_DIR/.dummy-version
} }
@ -21,12 +24,21 @@ teardown() {
clean_asdf_dir clean_asdf_dir
} }
@test "current without arguments should print all versions" {
cd $PROJECT_DIR
run current_command
expected=$(echo -e "dummy 1.1.0 (set by $PROJECT_DIR/.tool-versions)\nother 1.0.0 (set by $HOME/.tool-versions)")
[ "$status" -eq 0 ]
[ "$output" = "$expected" ]
}
@test "current should derive from the local .tool_versions when it exists" { @test "current should derive from the local .tool_versions when it exists" {
cd $PROJECT_DIR cd $PROJECT_DIR
run current_command "dummy" run current_command "dummy"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" = "1.1.0 (set by $PROJECT_DIR/.tool-versions)" ] [ "$output" = "dummy 1.1.0 (set by $PROJECT_DIR/.tool-versions)" ]
} }
@test "current should derive from the global .tool_versions when local doesn't exist" { @test "current should derive from the global .tool_versions when local doesn't exist" {
@ -34,7 +46,7 @@ teardown() {
run current_command "dummy" run current_command "dummy"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" = "1.0.0 (set by $HOME/.tool-versions)" ] [ "$output" = "dummy 1.0.0 (set by $HOME/.tool-versions)" ]
} }
@test "current should derive from the legacy file if enabled and hide the file path" { @test "current should derive from the legacy file if enabled and hide the file path" {
@ -43,7 +55,7 @@ teardown() {
run current_command "dummy" run current_command "dummy"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" = "1.2.0" ] [ "$output" = "dummy 1.2.0" ]
} }
@test "current should error when the plugin doesn't exist" { @test "current should error when the plugin doesn't exist" {

View File

@ -4,6 +4,14 @@ load test_helpers
setup() { setup() {
setup_asdf_dir setup_asdf_dir
install_dummy_plugin
PROJECT_DIR=$HOME/project
mkdir -p $PROJECT_DIR
echo 'dummy 1.0.0' >> $HOME/.tool-versions
echo 'other 1.0.0' >> $HOME/.tool-versions
echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions
} }
teardown() { teardown() {
@ -43,3 +51,29 @@ teardown() {
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" = "" ] [ "$output" = "" ]
} }
@test "get_asdf_versions_file_path should return closest .tool-versions when no args provided" {
cd $PROJECT_DIR
run get_asdf_versions_file_path
[ "$status" -eq 0 ]
[ "$output" = "$PROJECT_DIR/.tool-versions" ]
}
@test "get_asdf_versions_file_path should return closest .tool-versions if contains plugin" {
cd $PROJECT_DIR
run get_asdf_versions_file_path "dummy"
[ "$status" -eq 0 ]
[ "$output" = "$PROJECT_DIR/.tool-versions" ]
}
@test "get_asdf_versions_file_path should return correct .tool-versions if closest does not contain plugin" {
cd $PROJECT_DIR
run get_asdf_versions_file_path "other"
[ "$status" -eq 0 ]
[ "$output" = "$HOME/.tool-versions" ]
}