Merge pull request #83 from rockwood/unify_version_getter

Unify Version Getter
This commit is contained in:
Daniel Perez 2016-07-28 01:44:15 +09:00 committed by GitHub
commit 3d5a5b9040
5 changed files with 44 additions and 149 deletions

View File

@ -132,19 +132,11 @@ asdf list-all <name>
#### View current version
```bash
asdf local [name]
asdf global [name]
# asdf local
# asdf global
# asdf local elixir
# asdf global elixir
asdf current <name>
# 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

View File

@ -13,9 +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 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

@ -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 <name> <version>"
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

View File

@ -12,6 +12,11 @@ install_dummy_plugin() {
cp -r $BATS_TEST_DIRNAME/fixtures/dummy_plugin $ASDF_DIR/plugins/dummy
}
install_dummy_version() {
dummy_version=$1
mkdir -p $ASDF_DIR/installs/dummy/$dummy_version
}
clean_asdf_dir() {
rm -rf $BASE_DIR
unset ASDF_DIR

View File

@ -6,32 +6,25 @@ load test_helpers
setup() {
setup_asdf_dir
install_dummy_plugin
install_dummy_version "1.0.0"
install_dummy_version "1.1.0"
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
PROJECT_DIR=$HOME/project
mkdir -p $PROJECT_DIR
cd $OTHER_DIR
echo 'foo 1.0.0' >> $HOME/.tool-versions
echo 'foo 1.1.0' >> .tool-versions
cd $PROJECT_DIR
}
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 "foo"
[ "$status" -eq 1 ]
[ "$output" = "version not set for foo" ]
[ "$output" = "Usage: asdf local <name> <version>" ]
}
@test "local should emit an error when plugin does not exist" {
@ -41,77 +34,33 @@ teardown() {
}
@test "local should emit an error when plugin version does not exist" {
run local_command "foo" "0.0.1"
run local_command "dummy" "0.0.1"
[ "$status" -eq 1 ]
[ "$output" = "version 0.0.1 is not installed for foo" ]
[ "$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" = "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" ]
rm .tool-versions
run local_command foo 1.2.0
[ -f .tool-versions ]
run local_command foo
[ "$status" -eq 0 ]
[ "$output" = "1.2.0" ]
run global_command foo
[ "$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
mkdir -p $ASDF_DIR/plugins/foo/bin
echo 'echo 1.0.0' > $ASDF_DIR/plugins/foo/bin/get-version-from-legacy-file
rm .tool-versions
run local_command foo
[ "$status" -eq 0 ]
[ "$output" = "1.0.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" {
mkdir -p $ASDF_DIR/plugins/foo/bin
echo 'cat 1.0.0' > $ASDF_DIR/plugins/foo/bin/get-version-from-legacy-file
rm .tool-versions
run local_command foo
[ "$status" -eq 1 ]
[ "$output" = "version not set for foo" ]
@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" = "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" ]
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
}