Merge pull request #46 from tuvistavie/local-global-commands

Add support for local and global commands. Thank you @tuvistavie ~!
This commit is contained in:
Akash Manohar 2016-04-25 04:43:49 +05:30
commit 16369f770c
8 changed files with 229 additions and 0 deletions

View File

@ -127,6 +127,26 @@ asdf list-all <name>
# asdf list-all erlang
```
#### View current version
```bash
asdf local [name]
asdf global [name]
# asdf local
# asdf global
# asdf local elixir
# asdf global elixir
```
#### Set current version
```bash
asdf global <name> <version>
asdf local <name> <version>
asdf global elixir 1.2.4
```
## The `.tool-versions` file
Add a `.tool-versions` file to your project dir and versions of those tools will be used.
@ -147,6 +167,8 @@ The versions can be in the following format:
To install all the tools defined in a `.tool-versions` file run the `asdf install` command with no other arguments in the directory containing the `.tool-versions` file.
You can view/modify the file by hand or use `asdf local` and `asdf global` to manage it.
## Credits
Me ([@HashNuke](http://github.com/HashNuke)), High-fever, cold, cough.

View File

@ -7,6 +7,7 @@ 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/where.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
source $(dirname $(dirname $0))/lib/commands/reshim.sh
@ -40,6 +41,12 @@ case $1 in
"where")
where_command $callback_args;;
"local")
local_command $callback_args;;
"global")
global_command $callback_args;;
"list")
list_command $callback_args;;

View File

@ -78,6 +78,16 @@ complete -f -c asdf -n '__fish_asdf_needs_command' -a reshim -d "Recreate shims
complete -f -c asdf -n '__fish_asdf_using_command reshim; and __fish_asdf_arg_number 2' -a '(asdf plugin-list)'
complete -f -c asdf -n '__fish_asdf_using_command reshim; and __fish_asdf_arg_number 3' -a '(asdf list (__fish_asdf_arg_at 3))'
# local completion
complete -f -c asdf -n '__fish_asdf_needs_command' -a local -d "Set local version for a plugin"
complete -f -c asdf -n '__fish_asdf_using_command local; and __fish_asdf_arg_number 2' -a '(asdf plugin-list)'
complete -f -c asdf -n '__fish_asdf_using_command local; and __fish_asdf_arg_number 3' -a '(asdf list (__fish_asdf_arg_at 3))'
# global completion
complete -f -c asdf -n '__fish_asdf_needs_command' -a global -d "Set global version for a plugin"
complete -f -c asdf -n '__fish_asdf_using_command global; and __fish_asdf_arg_number 2' -a '(asdf plugin-list)'
complete -f -c asdf -n '__fish_asdf_using_command global; and __fish_asdf_arg_number 3' -a '(asdf list (__fish_asdf_arg_at 3))'
# misc
complete -f -c asdf -n '__fish_asdf_needs_command' -l "help" -d "Displays help"
complete -f -c asdf -n '__fish_asdf_needs_command' -l "version" -d "Displays asdf version"

View File

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

View File

@ -0,0 +1,60 @@
version_command() {
local cmd=$1
if [ $# -gt 3 ]; then
echo usage: $cmd [PLUGIN] [VERSION]
exit 1
fi
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 [ $# -ne 3 -a ! -f $file ]; then
echo $file does not exist
exit 1
fi
if [ $# -eq 1 ]; then
cat $file
exit 0
fi
local plugin=$2
check_if_plugin_exists $(get_plugin_path $plugin)
if [ $# -eq 2 ]; then
result=$(get_tool_version_from_file $file $plugin)
if [ -n "$result" ]; then
echo $result
exit 0
else
echo "version not set for $plugin"
exit 1
fi
fi
local version=$3
check_if_version_exists $plugin $version
if [ -f $file ] && grep $plugin $file > /dev/null; then
sed -i -e "s/$plugin .*/$plugin $version/" $file
else
echo "$plugin $version" >> $file
fi
}
local_command() {
version_command "local" $@
}
global_command() {
version_command "global" $@
}

View File

@ -36,6 +36,16 @@ check_if_plugin_exists() {
fi
}
check_if_version_exists() {
local plugin=$1
local version=$2
local version_dir=$(asdf_dir)/installs/$plugin/$version
if [ ! -d $version_dir ]; then
display_error "version $version is not installed for $plugin"
exit 1
fi
}
get_version_part() {
IFS='@' read -a version_info <<< "$1"

33
test/utils.bats Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env bats
. $(dirname $BATS_TEST_DIRNAME)/lib/utils.sh
setup() {
ASDF_DIR=$(mktemp -dt asdf.XXXX)
}
teardown() {
rm -rf $ASDF_DIR
unset ASDF_DIR
}
@test "check_if_version_exists should exit with 1 if plugin does not exist" {
mkdir -p $ASDF_DIR/installs
run check_if_version_exists "foo" "1.0.0"
[ "$status" -eq 1 ]
[ "$output" = "version 1.0.0 is not installed for foo" ]
}
@test "check_if_version_exists should exit with 1 if version does not exist" {
mkdir -p $ASDF_DIR/installs/foo
run check_if_version_exists "foo" "1.0.0"
[ "$status" -eq 1 ]
[ "$output" = "version 1.0.0 is not installed for foo" ]
}
@test "check_if_version_exists should be noop if version exists" {
mkdir -p $ASDF_DIR/installs/foo/1.0.0
run check_if_version_exists "foo" "1.0.0"
[ "$status" -eq 0 ]
[ "$output" = "" ]
}

View File

@ -0,0 +1,83 @@
#!/usr/bin/env bats
. $(dirname $BATS_TEST_DIRNAME)/lib/utils.sh
. $(dirname $BATS_TEST_DIRNAME)/lib/commands/version_commands.sh
setup() {
BASE_DIR=$(mktemp -dt asdf.XXXX)
HOME=$BASE_DIR/home
ASDF_DIR=$HOME/.asdf
OTHER_DIR=$BASE_DIR/other
mkdir -p $ASDF_DIR/plugins/foo $ASDF_DIR/plugins/bar $ASDF_DIR/installs/foo/1.0.0 $ASDF_DIR/installs/foo/1.1.0 $ASDF_DIR/installs/foo/1.2.0 $ASDF_DIR/installs/bar/1.0.0 $OTHER_DIR
cd $OTHER_DIR
echo 'foo 1.0.0' >> $HOME/.tool-versions
echo 'foo 1.1.0' >> .tool-versions
}
teardown() {
rm -rf $BASE_DIR
}
@test "local should emit an error when run in lookup mode and file does not exist" {
rm .tool-versions
run local_command
[ "$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 "foo"
[ "$status" -eq 1 ]
[ "$output" = "$HOME/.tool-versions does not exist" ]
}
@test "local should emit an error when plugin does not exist" {
run local_command "inexistent" "1.0.0"
[ "$status" -eq 1 ]
[ "$output" = "No such plugin" ]
}
@test "local should emit an error when plugin version does not exist" {
run local_command "foo" "0.0.1"
[ "$status" -eq 1 ]
[ "$output" = "version 0.0.1 is not installed for foo" ]
}
@test "local should return and set the local version" {
run local_command
[ "$status" -eq 0 ]
[ "$output" = "foo 1.1.0" ]
run local_command foo "1.2.0"
run local_command foo
[ "$status" -eq 0 ]
[ "$output" = "1.2.0" ]
run local_command bar
[ "$status" -eq 1 ]
run local_command bar 1.0.0
[ "$status" -eq 0 ]
run local_command bar
[ "$status" -eq 0 ]
[ "$output" = "1.0.0" ]
}
@test "global should return and set the global version" {
run global_command
[ "$status" -eq 0 ]
[ "$output" = "foo 1.0.0" ]
run global_command foo 1.2.0
[ "$status" -eq 0 ]
run global_command foo
[ "$status" -eq 0 ]
[ "$output" = "1.2.0" ]
}