Merge pull request #178 from fcrespo82/which_command

Add which command.
This commit is contained in:
Trevor Brown 2017-07-04 09:15:48 -04:00 committed by GitHub
commit 37bb033328
5 changed files with 99 additions and 1 deletions

View File

@ -8,6 +8,7 @@ source $(dirname $(dirname $0))/lib/commands/install.sh
source $(dirname $(dirname $0))/lib/commands/uninstall.sh
source $(dirname $(dirname $0))/lib/commands/current.sh
source $(dirname $(dirname $0))/lib/commands/where.sh
source $(dirname $(dirname $0))/lib/commands/which.sh
source $(dirname $(dirname $0))/lib/commands/version_commands.sh
source $(dirname $(dirname $0))/lib/commands/list.sh
source $(dirname $(dirname $0))/lib/commands/list-all.sh
@ -46,6 +47,9 @@ case $1 in
"where")
where_command $callback_args;;
"which")
which_command $callback_args;;
"local")
local_command $callback_args;;

View File

@ -32,7 +32,7 @@ _asdf () {
fi
;;
*)
local cmds='plugin-add plugin-list plugin-remove plugin-update install uninstall update current where list list-all local global reshim'
local cmds='plugin-add plugin-list plugin-remove plugin-update install uninstall update current where which list list-all local global reshim'
COMPREPLY=($(compgen -W "$cmds" -- $cur))
;;
esac

View File

@ -13,6 +13,7 @@ MANAGE PACKAGES
asdf uninstall <name> <version> Remove a specific version of a package
asdf current <name> Display current version set or being used for package
asdf where <name> <version> Display install path for an installed version
asdf which <name> Display install path for current version
asdf local <name> <version> Set the package local version
asdf global <name> <version> Set the package global version
asdf list <name> List installed versions of a package

55
lib/commands/which.sh Normal file
View File

@ -0,0 +1,55 @@
current_version() {
local plugin_name=$1
check_if_plugin_exists $plugin_name
local search_path=$(pwd)
local version_and_path=$(find_version "$plugin_name" "$search_path")
local version=$(cut -d '|' -f 1 <<< "$version_and_path");
local version_file_path=$(cut -d '|' -f 2 <<< "$version_and_path");
check_if_version_exists $plugin_name $version
check_for_deprecated_plugin $plugin_name
if [ -z "$version" ]; then
echo "No version set for $plugin_name"
exit 1
else
echo "$version"
exit 0
fi
}
which_command() {
local plugin_name=$1
local plugin_path=$(get_plugin_path $plugin_name)
check_if_plugin_exists $plugin_name
local install_type="version"
local install_path=$(get_install_path $plugin_name $install_type $(current_version $plugin_name))
if [ -d $install_path ]; then
echo $install_path/bin/$plugin_name
exit 0
else
echo "Version not installed"
exit 1
fi
}
# Warn if the plugin isn't using the updated legacy file api.
check_for_deprecated_plugin() {
local plugin_name=$1
local plugin_path=$(get_plugin_path "$plugin_name")
local legacy_config=$(get_asdf_config_value "legacy_version_file")
local deprecated_script="${plugin_path}/bin/get-version-from-legacy-file"
local new_script="${plugin_path}/bin/list-legacy-filenames"
if [ "$legacy_config" = "yes" ] && [ -f $deprecated_script ] && [ ! -f $new_script ]; then
echo "Heads up! It looks like your $plugin_name plugin is out of date. You can update it with:"
echo ""
echo " asdf plugin-update $plugin_name"
echo ""
fi
}

38
test/which_command.bats Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env bats
load test_helpers
. $(dirname $BATS_TEST_DIRNAME)/lib/commands/which.sh
. $(dirname $BATS_TEST_DIRNAME)/lib/commands/install.sh
setup() {
setup_asdf_dir
install_dummy_plugin
run install_command dummy 1.0
PROJECT_DIR=$HOME/project
mkdir $PROJECT_DIR
}
teardown() {
clean_asdf_dir
}
@test "which should show dummy 1.0 main binary path" {
cd $PROJECT_DIR
echo 'dummy 1.0' >> $PROJECT_DIR/.tool-versions
run current_version "dummy"
[ "$output" = "1.0" ]
run which_command "dummy"
[ "$status" -eq 0 ]
[ "$output" = "$ASDF_DIR/installs/dummy/1.0/bin/dummy" ]
}
@test "which should error when the plugin doesn't exist" {
run which_command "foobar"
[ "$status" -eq 1 ]
[ "$output" = "No such plugin" ]
}