From 784d7e9f3427b2bebbbe5471fe36b86c41191392 Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Mon, 25 Apr 2016 00:11:33 +0900 Subject: [PATCH] Add support for local and global commands. --- README.md | 22 +++++++++ bin/asdf | 7 +++ completions/asdf.fish | 10 ++++ help.txt | 4 ++ lib/commands/version_commands.sh | 60 +++++++++++++++++++++++ lib/utils.sh | 10 ++++ test/utils.bats | 33 +++++++++++++ test/version_commands.bats | 83 ++++++++++++++++++++++++++++++++ 8 files changed, 229 insertions(+) create mode 100644 lib/commands/version_commands.sh create mode 100644 test/utils.bats create mode 100644 test/version_commands.bats diff --git a/README.md b/README.md index 8010cd93..2027abaa 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,26 @@ asdf list-all # 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 +asdf local +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. diff --git a/bin/asdf b/bin/asdf index 9cfedf8e..3afb7f18 100755 --- a/bin/asdf +++ b/bin/asdf @@ -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;; diff --git a/completions/asdf.fish b/completions/asdf.fish index 2b109423..6666bd25 100644 --- a/completions/asdf.fish +++ b/completions/asdf.fish @@ -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" diff --git a/help.txt b/help.txt index e18f0f00..df7df2da 100644 --- a/help.txt +++ b/help.txt @@ -13,6 +13,10 @@ MANAGE PACKAGES asdf uninstall Remove a specific version of a package asdf which Display 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 new file mode 100644 index 00000000..9fe7d955 --- /dev/null +++ b/lib/commands/version_commands.sh @@ -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" $@ +} diff --git a/lib/utils.sh b/lib/utils.sh index 3811a678..a06fcdf5 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -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" diff --git a/test/utils.bats b/test/utils.bats new file mode 100644 index 00000000..d64de922 --- /dev/null +++ b/test/utils.bats @@ -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" = "" ] +} diff --git a/test/version_commands.bats b/test/version_commands.bats new file mode 100644 index 00000000..ebdca95e --- /dev/null +++ b/test/version_commands.bats @@ -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" ] +}