Skip to content

Commit 7517aa3

Browse files
committed
Remove getter feature on local and global commands
This simplifies the `local` and `global` commands. `asdf current` should be sufficient for getting the current version. closes asdf-vm#80
1 parent 0ce1810 commit 7517aa3

4 files changed

Lines changed: 32 additions & 144 deletions

File tree

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,11 @@ asdf list-all <name>
132132
#### View current version
133133

134134
```bash
135-
asdf local [name]
136-
asdf global [name]
137-
# asdf local
138-
# asdf global
139-
# asdf local elixir
140-
# asdf global elixir
135+
asdf current <name>
136+
# asdf current erlang
137+
# 17.3 (set by /Users/kim/.tool-versions)
141138
```
142139

143-
`global` reads from `$HOME/.tool-versions`.
144-
145-
`local` reads from `$PWD/.tool-versions` if it exists,
146-
or searches recursively in the parent directories until it finds a `.tool-versions` file.
147-
148140
#### Set current version
149141

150142
```bash

help.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ MANAGE PACKAGES
1313
asdf uninstall <name> <version> Remove a specific version of a package
1414
asdf current <name> Display current version set or being used for package
1515
asdf where <name> <version> Display install path for an installed version
16-
asdf local [name] Display a package local version
1716
asdf local <name> <version> Set the package local version
18-
asdf global [name] Display a package global version
1917
asdf global <name> <version> Set the package global version
2018
asdf list <name> List installed versions of a package
2119
asdf list-all <name> List all versions of a package

lib/commands/version_commands.sh

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,21 @@
1-
get_plugin_version() {
2-
local cmd=$1
3-
local file=$2
4-
local plugin=$3
5-
local legacy_version_file_support=$(get_asdf_config_value "legacy_version_file")
6-
local result
7-
8-
if [ $cmd = "local" -a "$legacy_version_file_support" = "yes" -a \
9-
\( ! -f $file -o $file = "$HOME/.tool-versions" \) ]; then
10-
result=$(get_tool_version_from_legacy_file $plugin $(pwd))
11-
if [ -n "$result" ]; then
12-
echo $result
13-
exit 0
14-
fi
15-
fi
16-
17-
if [ -f $file ]; then
18-
result=$(get_tool_version_from_file $file $plugin)
19-
fi
20-
21-
if [ -n "$result" ]; then
22-
echo $result
23-
exit 0
24-
fi
25-
26-
27-
echo "version not set for $plugin"
28-
exit 1
29-
}
30-
311
version_command() {
322
local cmd=$1
33-
3+
local plugin=$2
4+
local version=$3
345
local file
35-
if [ $cmd = "global" ]; then
36-
file=$HOME/.tool-versions
37-
else
38-
file=$(get_asdf_versions_file_path)
39-
if [ -z "$file" ]; then
40-
file=.tool-versions
41-
fi
42-
fi
436

44-
if [ $# -eq 1 -a ! -f $file ]; then
45-
echo $file does not exist
7+
if [ "$#" -ne 3 ]; then
8+
echo "Usage: asdf $cmd <name> <version>"
469
exit 1
4710
fi
4811

49-
if [ $# -eq 1 ]; then
50-
cat $file
51-
exit 0
52-
fi
53-
54-
local plugin=$2
55-
56-
check_if_plugin_exists $plugin
57-
58-
if [ $# -eq 2 ]; then
59-
get_plugin_version $cmd $file $plugin
60-
fi
61-
62-
local version=${@:3}
63-
64-
if [ $cmd = "local" ]; then
12+
if [ $cmd = "global" ]; then
13+
file=$HOME/.tool-versions
14+
else
6515
file=$(pwd)/.tool-versions
6616
fi
6717

18+
check_if_plugin_exists $plugin
6819
check_if_version_exists $plugin $version
6920

7021
if [ -f $file ] && grep $plugin $file > /dev/null; then

test/version_commands.bats

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,22 @@ setup() {
99
install_dummy_plugin
1010
install_dummy_version "1.0.0"
1111
install_dummy_version "1.1.0"
12-
install_dummy_version "1.2.0"
1312

14-
PROJECT_DIR=$BASE_DIR/project
13+
PROJECT_DIR=$HOME/project
1514
mkdir -p $PROJECT_DIR
1615

17-
echo 'dummy 1.0.0' >> $HOME/.tool-versions
18-
echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions
19-
2016
cd $PROJECT_DIR
2117
}
2218

2319
teardown() {
2420
clean_asdf_dir
2521
}
2622

27-
28-
@test "local should emit an error when run in lookup mode and file does not exist" {
29-
rm .tool-versions
30-
run local_command
31-
[ "$status" -eq 1 ]
32-
[ "$output" = ".tool-versions does not exist" ]
33-
}
34-
35-
@test "global should emit an error when run in lookup mode and file does not exist" {
36-
rm $HOME/.tool-versions
37-
run global_command "dummy"
23+
# Warn users who invoke the old style command without arguments.
24+
@test "local should emit an error when called with incorrect arity" {
25+
run local_command "dummy"
3826
[ "$status" -eq 1 ]
39-
[ "$output" = "version not set for dummy" ]
27+
[ "$output" = "Usage: asdf local <name> <version>" ]
4028
}
4129

4230
@test "local should emit an error when plugin does not exist" {
@@ -51,69 +39,28 @@ teardown() {
5139
[ "$output" = "version 0.0.1 is not installed for dummy" ]
5240
}
5341

54-
@test "local should return and set the local version" {
55-
56-
run local_command
57-
[ "$status" -eq 0 ]
58-
[ "$output" = "dummy 1.1.0" ]
59-
60-
run local_command dummy "1.2.0"
61-
62-
run local_command dummy
63-
[ "$status" -eq 0 ]
64-
[ "$output" = "1.2.0" ]
65-
66-
rm .tool-versions
67-
run local_command dummy 1.2.0
68-
[ -f .tool-versions ]
69-
70-
run local_command dummy
71-
[ "$status" -eq 0 ]
72-
[ "$output" = "1.2.0" ]
73-
run global_command dummy
74-
[ "$output" = "1.0.0" ]
75-
76-
mkdir $BASE_DIR/other && cd $BASE_DIR/other
77-
78-
run local_command dummy
79-
[ "$status" -eq 1 ]
80-
81-
run local_command dummy 1.0.0
82-
[ "$status" -eq 0 ]
83-
84-
run local_command dummy
42+
@test "local should create a local .tool-versions file if it doesn't exist" {
43+
run local_command "dummy" "1.1.0"
8544
[ "$status" -eq 0 ]
86-
[ "$output" = "1.0.0" ]
87-
}
88-
89-
@test "local should fallback to legacy-file when enabled" {
90-
echo 'legacy_version_file = yes' > $HOME/.asdfrc
91-
echo '1.3.0' > .dummy-version
92-
rm .tool-versions
93-
run local_command dummy
94-
95-
[ "$status" -eq 0 ]
96-
[ "$output" = "1.3.0" ]
45+
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
9746
}
9847

99-
@test "local should ignore legacy-file when disabled" {
100-
rm .tool-versions
101-
run local_command dummy
102-
103-
[ "$status" -eq 1 ]
104-
[ "$output" = "version not set for dummy" ]
105-
}
106-
107-
108-
@test "global should return and set the global version" {
109-
run global_command
48+
@test "local should overwrite the existing version if it's set" {
49+
echo 'dummy 1.0.0' >> $PROJECT_DIR/.tool-versions
50+
run local_command "dummy" "1.1.0"
11051
[ "$status" -eq 0 ]
111-
[ "$output" = "dummy 1.0.0" ]
52+
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
53+
}
11254

113-
run global_command dummy 1.2.0
55+
@test "global should create a global .tool-versions file if it doesn't exist" {
56+
run global_command "dummy" "1.1.0"
11457
[ "$status" -eq 0 ]
58+
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
59+
}
11560

116-
run global_command dummy
61+
@test "global should overwrite the existing version if it's set" {
62+
echo 'dummy 1.0.0' >> $HOME/.tool-versions
63+
run global_command "dummy" "1.1.0"
11764
[ "$status" -eq 0 ]
118-
[ "$output" = "1.2.0" ]
65+
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
11966
}

0 commit comments

Comments
 (0)