From 16f3fa9f0378aeec7f9c3cbbb14a6c3b42c80b3c Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Sun, 24 Jul 2016 09:03:43 +0900 Subject: [PATCH 01/13] Use dummy plugin for utils test --- test/utils.bats | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/test/utils.bats b/test/utils.bats index d8489cdf..7e0c76b0 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -4,6 +4,12 @@ load test_helpers setup() { setup_asdf_dir + install_dummy_plugin + install_dummy_version "0.1.0" + install_dummy_version "0.2.0" + + PROJECT_DIR=$BASE_DIR/project + mkdir -p $PROJECT_DIR } teardown() { @@ -11,22 +17,19 @@ teardown() { } @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" + run check_if_version_exists "inexistent" "1.0.0" [ "$status" -eq 1 ] - [ "$output" = "version 1.0.0 is not installed for foo" ] + [ "$output" = "version 1.0.0 is not installed for inexistent" ] } @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" + run check_if_version_exists "dummy" "1.0.0" [ "$status" -eq 1 ] - [ "$output" = "version 1.0.0 is not installed for foo" ] + [ "$output" = "version 1.0.0 is not installed for dummy" ] } @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" + run check_if_version_exists "dummy" "0.1.0" [ "$status" -eq 0 ] [ "$output" = "" ] } @@ -38,8 +41,7 @@ teardown() { } @test "check_if_plugin_exists should be noop if plugin exists" { - mkdir -p $ASDF_DIR/plugins/foo_bar - run check_if_plugin_exists "foo_bar" + run check_if_plugin_exists "dummy" [ "$status" -eq 0 ] [ "$output" = "" ] } From c6d814e247e5a8871cfef0184b87b53d9e77f556 Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Mon, 25 Jul 2016 09:12:40 +0900 Subject: [PATCH 02/13] Unify .tool-versions and legacy file search --- lib/utils.sh | 162 +++++++----------- .../dummy_plugin/bin/list-legacy-filenames | 3 + .../dummy_plugin/bin/parse-legacy-file | 3 + test/utils.bats | 89 +++++++++- 4 files changed, 157 insertions(+), 100 deletions(-) create mode 100644 test/fixtures/dummy_plugin/bin/list-legacy-filenames create mode 100644 test/fixtures/dummy_plugin/bin/parse-legacy-file diff --git a/lib/utils.sh b/lib/utils.sh index 0d8a98d9..03701c16 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -67,118 +67,82 @@ display_error() { echo >&2 $1 } -get_version_file_path_for() { - local tool_name=$1 - local legacy_version_file_support=$(get_asdf_config_value "legacy_version_file") +find_version_file_for() { + local plugin_name=$1 - if [ ! -f "$(pwd)/.tool-versions" ] && [ "$legacy_version_file_support" = "yes" ]; then - # Check for legacy version and return "" if it exists - local legacy_version=$(get_tool_version_from_legacy_file $tool_name $(pwd)) - if [ "$legacy_version" != "" ]; then - echo "" - return 1 - fi - fi - - echo $(get_asdf_versions_file_path) -} - -get_asdf_versions_file_path() { - local asdf_tool_versions_path="" + local plugin_path=$(get_plugin_path "$plugin_name") + local legacy_config=$(get_asdf_config_value "legacy_version_file") + local legacy_list_filenames_script="${plugin_path}/bin/list-legacy-filenames" local search_path=$(pwd) + if [ "$legacy_config" = "yes" ] && [ -f $legacy_list_filenames_script ]; then + local legacy_filenames=$(bash "$legacy_list_filenames_script") + echo $(search_filenames ".tool-versions $legacy_filenames" "$search_path") + else + echo $(search_filenames ".tool-versions" "$search_path") + fi +} + +search_filenames() { + local filenames=$1 + local search_path=$2 + while [ "$search_path" != "/" ]; do - if [ -f "$search_path/.tool-versions" ]; then - asdf_tool_versions_path="$search_path/.tool-versions" - break - fi + for filename in $filenames; do + if [ -f "$search_path/$filename" ]; then + echo "$search_path/$filename" + return 0 + fi + done search_path=$(dirname "$search_path") done - - echo $asdf_tool_versions_path } +parse_asdf_version_file() { + local file_path=$1 + local plugin_name=$2 + + cat $file_path | while read -r line || [[ -n "$line" ]]; do + local line_parts=($line) + + if [ "${line_parts[0]}" = "$plugin_name" ]; then + echo ${line_parts[1]} + return 0 + fi + done +} + +parse_legacy_version_file() { + local file_path=$1 + local plugin_name=$2 + + local plugin_path=$(get_plugin_path "$plugin_name") + local parse_legacy_script="${plugin_path}/bin/parse-legacy-file" + + if [ -f $parse_legacy_script ]; then + echo $(bash "$parse_legacy_script" "$file_path") + else + echo $(cat $file_path) + fi +} + +parse_version_file() { + local file_path=$1 + local plugin_name=$2 + + if [ $(basename $file_path) = ".tool-versions" ]; then + echo $(parse_asdf_version_file "$file_path" "$plugin_name") + else + echo $(parse_legacy_version_file "$file_path" "$plugin_name") + fi +} get_preset_version_for() { - local tool_name=$1 - local asdf_versions_path=$(get_asdf_versions_file_path) - local matching_tool_version="" - local legacy_version_file_support=$(get_asdf_config_value "legacy_version_file") - - # If .tool-versions is not in the working directory - if [ "$asdf_versions_path" != "$(pwd)/.tool-versions" ] && [ "$legacy_version_file_support" = "yes" ]; then - # Check for legacy version file - matching_tool_version=$(get_tool_version_from_legacy_file $tool_name $(pwd)) - fi - - # If no legacy version file, see if we can use a .tool-versions file higher in the directory tree - if [ "$matching_tool_version" = "" ] && [ "$asdf_versions_path" != "" ]; then - matching_tool_version=$(get_tool_version_from_file $asdf_versions_path $tool_name) - fi - - - # If there's no global .tool-versions file - # then we create it and return blank - if [ "$matching_tool_version" = "" ]; then - local global_tool_versions_path=$HOME/.tool-versions - if [ ! -f $global_tool_versions_path ]; then - touch $global_tool_versions_path - else - matching_tool_version=$(get_tool_version_from_file $global_tool_versions_path $tool_name) - fi - fi - - - echo $matching_tool_version -} - - -get_tool_version_from_file() { - local asdf_versions_path=$1 - local tool_name=$2 - local get_all_versions=$3 - local matching_tool_version="" - - local read_done=false - until $read_done; do - read tool_line || read_done=true - - if $read_done ; then - break; - fi - - IFS=' ' read -a tool_info <<< $tool_line - local t_name=$(echo "${tool_info[0]}" | xargs) - local t_version=$(echo "${tool_info[@]:1}" | xargs) - - if [ "$t_name" = "$tool_name" ] - then - matching_tool_version=$t_version - break; - fi - done < $asdf_versions_path - - echo $matching_tool_version -} - - -get_tool_version_from_legacy_file() { local plugin_name=$1 - local directory=$2 - local legacy_tool_version="" - local plugin_path=$(get_plugin_path $plugin_name) - local legacy_version_script="${plugin_path}/bin/get-version-from-legacy-file" - check_if_plugin_exists $plugin_name - - if [ -f $legacy_version_script ]; then - local legacy_tool_version=$(bash $legacy_version_script $directory) - fi - - # Should return the version/tag/commit/branch/path - echo $legacy_tool_version + local version_file_path=$(find_version_file_for "$plugin_name") + echo $(parse_version_file "$version_file_path" "$plugin_name") } - get_asdf_config_value_from_file() { local config_path=$1 local key=$2 diff --git a/test/fixtures/dummy_plugin/bin/list-legacy-filenames b/test/fixtures/dummy_plugin/bin/list-legacy-filenames new file mode 100644 index 00000000..ba4feb91 --- /dev/null +++ b/test/fixtures/dummy_plugin/bin/list-legacy-filenames @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo ".dummy-version .dummyrc" diff --git a/test/fixtures/dummy_plugin/bin/parse-legacy-file b/test/fixtures/dummy_plugin/bin/parse-legacy-file new file mode 100644 index 00000000..e130b252 --- /dev/null +++ b/test/fixtures/dummy_plugin/bin/parse-legacy-file @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo $(cat $1 | tr -d "dummy-") diff --git a/test/utils.bats b/test/utils.bats index 7e0c76b0..0bf16105 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -8,7 +8,7 @@ setup() { install_dummy_version "0.1.0" install_dummy_version "0.2.0" - PROJECT_DIR=$BASE_DIR/project + PROJECT_DIR=$HOME/project mkdir -p $PROJECT_DIR } @@ -45,3 +45,90 @@ teardown() { [ "$status" -eq 0 ] [ "$output" = "" ] } + +@test "find_version_file_for should return .tool-versions if legacy is disabled" { + cd $PROJECT_DIR + touch ".tool-versions" + touch ".dummy-version" + + run find_version_file_for "dummy" + [ "$status" -eq 0 ] + [ "$output" = "$PROJECT_DIR/.tool-versions" ] +} + +@test "find_version_file_for should return .tool-versions ++ plugin filenames if supported" { + cd $PROJECT_DIR + touch "$HOME/.tool-versions" + touch ".dummy-version" + echo 'legacy_version_file = yes' > $HOME/.asdfrc + + run find_version_file_for "dummy" + [ "$status" -eq 0 ] + [ "$output" = "$PROJECT_DIR/.dummy-version" ] +} + +@test "find_version_file_for should return .tool-versions if unsupported" { + cd $PROJECT_DIR + touch "$HOME/.tool-versions" + touch ".dummy-version" + echo 'legacy_version_file = yes' > $HOME/.asdfrc + rm $ASDF_DIR/plugins/dummy/bin/list-legacy-filenames + + run find_version_file_for "dummy" + [ "$status" -eq 0 ] + [ "$output" = "$HOME/.tool-versions" ] +} + +@test "search_filenames should return the path to the first filename found" { + touch "$PROJECT_DIR/.dummy-version" + touch "$PROJECT_DIR/.tool-versions" + + run search_filenames ".tool-versions .dummy-version" $PROJECT_DIR + [ "$status" -eq 0 ] + [ "$output" = "$PROJECT_DIR/.tool-versions" ] +} + +@test "search_filenames should walk parent directories" { + touch "$PROJECT_DIR/.dummy-version" + touch "$HOME/.tool-versions" + + run search_filenames ".tool-versions" $PROJECT_DIR + [ "$status" -eq 0 ] + [ "$output" = "$HOME/.tool-versions" ] +} + +@test "search_filenames should return nothing if not found" { + run search_filenames ".does-not-exist" $PROJECT_DIR + [ "$status" -eq 0 ] + [ "$output" = "" ] +} + +@test "parse_version_file parses the version from a .tool-version file" { + echo "dummy 0.2.0" > $HOME/.tool-versions + run parse_version_file $HOME/.tool-versions "dummy" + [ "$status" -eq 0 ] + [ "$output" = "0.2.0" ] +} + +@test "parse_version_file calls the plugin's parse-legacy-file if implemented" { + echo "dummy-0.2.0" > $HOME/.dummy-version + run parse_version_file $HOME/.dummy-version "dummy" + [ "$status" -eq 0 ] + [ "$output" = "0.2.0" ] +} + +@test "parse_version_file cats the legacy file if parse-legacy-file isn't implemented" { + echo "0.2.0" > $HOME/.dummy-version + rm $ASDF_DIR/plugins/dummy/bin/parse-legacy-file + run parse_version_file $HOME/.dummy-version "dummy" + [ "$status" -eq 0 ] + [ "$output" = "0.2.0" ] +} + +@test "get_preset_version_for returns the current version" { + cd $PROJECT_DIR + echo "dummy 0.2.0" > .tool-versions + run get_preset_version_for "dummy" + [ "$status" -eq 0 ] + [ "$output" = "0.2.0" ] +} From 4d75ddd50b23ff33a4fd13c17d9b60ea6c9bf887 Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Sun, 31 Jul 2016 08:48:57 +0900 Subject: [PATCH 03/13] Update current command with new util functions current will now print paths to legacy files and .tool-version files --- lib/commands/current.sh | 15 +++++--------- test/current_command.bats | 41 +++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/lib/commands/current.sh b/lib/commands/current.sh index d7359a04..3e1cf210 100644 --- a/lib/commands/current.sh +++ b/lib/commands/current.sh @@ -1,20 +1,15 @@ current_command() { local plugin_name=$1 - local version=$(get_preset_version_for $plugin_name) check_if_plugin_exists $plugin_name + local version_file_path=$(find_version_file_for $plugin_name) + local version=$(parse_version_file $version_file_path $plugin_name) + check_if_version_exists $plugin_name $version - if [ "$version" == "" ]; then + if [ -z "$version" ]; then echo "No version set for $plugin_name" exit 1 else - local version_file_path=$(get_version_file_path_for $plugin_name) - if [ "$version_file_path" == "" ]; then - echo "$version" - else - echo "$version (set by $version_file_path)" - fi - - exit 0 + echo "$version (set by $version_file_path)" fi } diff --git a/test/current_command.bats b/test/current_command.bats index 562baa4b..8491bafb 100644 --- a/test/current_command.bats +++ b/test/current_command.bats @@ -7,55 +7,54 @@ load test_helpers setup() { setup_asdf_dir install_dummy_plugin + install_dummy_version "1.1.0" + install_dummy_version "1.2.0" PROJECT_DIR=$HOME/project - OTHER_DIR=$HOME/other - mkdir -p $ASDF_DIR/installs/dummy/1.0.0 $ASDF_DIR/installs/dummy/1.1.0 $PROJECT_DIR $OTHER_DIR - - echo 'dummy 1.0.0' >> $HOME/.tool-versions - echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions - echo '1.2.0' >> $OTHER_DIR/.dummy-version + mkdir $PROJECT_DIR } teardown() { clean_asdf_dir } -@test "current should derive from the local .tool_versions when it exists" { +@test "current should derive from the current .tool-versions" { cd $PROJECT_DIR + echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions run current_command "dummy" [ "$status" -eq 0 ] [ "$output" = "1.1.0 (set by $PROJECT_DIR/.tool-versions)" ] } -@test "current should derive from the global .tool_versions when local doesn't exist" { - cd $OTHER_DIR - - run current_command "dummy" - [ "$status" -eq 0 ] - [ "$output" = "1.0.0 (set by $HOME/.tool-versions)" ] -} - -@test "current should derive from the legacy file if enabled and hide the file path" { +@test "current should derive from the legacy file if enabled" { + cd $PROJECT_DIR echo 'legacy_version_file = yes' > $HOME/.asdfrc - cd $OTHER_DIR + echo '1.2.0' >> $PROJECT_DIR/.dummy-version run current_command "dummy" [ "$status" -eq 0 ] - [ "$output" = "1.2.0" ] + [ "$output" = "1.2.0 (set by $PROJECT_DIR/.dummy-version)" ] } @test "current should error when the plugin doesn't exist" { run current_command "foobar" [ "$status" -eq 1 ] + [ "$output" = "No such plugin" ] } @test "current should error when no version is set" { - cd $OTHER_DIR - rm $HOME/.tool-versions + cd $PROJECT_DIR run current_command "dummy" [ "$status" -eq 1 ] - [ "$output" = "No version set for dummy" ] +} + +@test "current should error when a version is set that isn't installed" { + cd $PROJECT_DIR + echo 'dummy 9.9.9' >> $PROJECT_DIR/.tool-versions + + run current_command "dummy" + [ "$status" -eq 1 ] + [ "$output" = "version 9.9.9 is not installed for dummy" ] } From 511535b750d32a0a13ab5c99c1326a2da41ec095 Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Sun, 31 Jul 2016 09:35:37 +0900 Subject: [PATCH 04/13] Add warning to when using deprecated plugins This can be removed after we've given users enough time to update. --- lib/commands/current.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/commands/current.sh b/lib/commands/current.sh index 3e1cf210..8d73297f 100644 --- a/lib/commands/current.sh +++ b/lib/commands/current.sh @@ -6,6 +6,8 @@ current_command() { local version=$(parse_version_file $version_file_path $plugin_name) 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 @@ -13,3 +15,20 @@ current_command() { echo "$version (set by $version_file_path)" 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 +} From 280581c5661be8b0a4e042ce370f83da84dc32d4 Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Sun, 31 Jul 2016 09:56:47 +0900 Subject: [PATCH 05/13] Clean up utils Whitespace and remove dead function --- lib/utils.sh | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lib/utils.sh b/lib/utils.sh index 03701c16..c72f862f 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -2,7 +2,6 @@ asdf_version() { echo "0.2.0-dev" } - asdf_dir() { if [ -z $ASDF_DIR ]; then local current_script_path=${BASH_SOURCE[0]} @@ -12,7 +11,6 @@ asdf_dir() { echo $ASDF_DIR } - get_install_path() { local plugin=$1 local install_type=$2 @@ -27,7 +25,6 @@ get_install_path() { fi } - check_if_plugin_exists() { # Check if we have a non-empty argument if [ -z "${1+set}" ]; then @@ -51,18 +48,10 @@ check_if_version_exists() { fi } - -get_version_part() { - IFS='@' read -a version_info <<< "$1" - echo ${version_info[$2]} -} - - get_plugin_path() { echo $(asdf_dir)/plugins/$1 } - display_error() { echo >&2 $1 } From 333339b960bf8da944e0b2f16423bae34c25fe9a Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Sun, 31 Jul 2016 10:19:14 +0900 Subject: [PATCH 06/13] Update plugin docs with new legacy file api --- docs/creating-plugins.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/creating-plugins.md b/docs/creating-plugins.md index fece8b32..b51bbd5e 100644 --- a/docs/creating-plugins.md +++ b/docs/creating-plugins.md @@ -50,9 +50,19 @@ Setup the env to run the binaries in the package. Uninstalls a specific version of a tool. -#### bin/get_version_from_legacy_file +#### bin/list-legacy-filenames -Prints the version of the tool to use if a legacy version file is found in the current directory (e.g. rbenv's `.ruby-version`). Current directory is passed as the only argument to this script. +Register additional setter files for this plugin. Must print a string with a space-seperated list of filenames. + +``` +.ruby-version .rvmrc +``` + +Note: This will only apply for users who have enabled the `legacy_version_file` option in their `~/.asdfrc`. + +#### bin/parse-legacy-file + +This can be used to further parse the legacy file found by asdf. If `parse-legacy-file` isn't implemented, asdf will simply cat the file to determine the version. ### Custom shim templates From cec4e9a73782690b4d3d2a262b097e50b329201f Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Sun, 28 Aug 2016 13:15:56 +0900 Subject: [PATCH 07/13] Combine search and parse operations We need to search and parse simultaneously in case the tool-versions file doesn't contain a setter line for the plugin. --- lib/utils.sh | 72 ++++++++++++++++++-------------------- test/utils.bats | 92 ++++++++++++++----------------------------------- 2 files changed, 60 insertions(+), 104 deletions(-) diff --git a/lib/utils.sh b/lib/utils.sh index c72f862f..325b13e2 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -56,33 +56,36 @@ display_error() { echo >&2 $1 } -find_version_file_for() { +find_version_for() { local plugin_name=$1 + local search_path=$2 local plugin_path=$(get_plugin_path "$plugin_name") local legacy_config=$(get_asdf_config_value "legacy_version_file") local legacy_list_filenames_script="${plugin_path}/bin/list-legacy-filenames" - local search_path=$(pwd) + local legacy_filenames="" if [ "$legacy_config" = "yes" ] && [ -f $legacy_list_filenames_script ]; then - local legacy_filenames=$(bash "$legacy_list_filenames_script") - echo $(search_filenames ".tool-versions $legacy_filenames" "$search_path") - else - echo $(search_filenames ".tool-versions" "$search_path") + legacy_filenames=$(bash "$legacy_list_filenames_script") fi -} - -search_filenames() { - local filenames=$1 - local search_path=$2 while [ "$search_path" != "/" ]; do - for filename in $filenames; do - if [ -f "$search_path/$filename" ]; then - echo "$search_path/$filename" + local asdf_version=$(parse_asdf_version_file "$search_path/.tool-versions" $plugin_name) + + if [ -n "$asdf_version" ]; then + echo "$asdf_version" + return 0 + fi + + for filename in $legacy_filenames; do + local legacy_version=$(parse_legacy_version_file "$search_path/$filename" $plugin_name) + + if [ -n "$legacy_version" ]; then + echo "$legacy_version" return 0 fi done + search_path=$(dirname "$search_path") done } @@ -91,14 +94,16 @@ parse_asdf_version_file() { local file_path=$1 local plugin_name=$2 - cat $file_path | while read -r line || [[ -n "$line" ]]; do - local line_parts=($line) + if [ -f $file_path ]; then + cat $file_path | while read -r line || [[ -n "$line" ]]; do + local line_parts=($line) - if [ "${line_parts[0]}" = "$plugin_name" ]; then - echo ${line_parts[1]} - return 0 - fi - done + if [ "${line_parts[0]}" = "$plugin_name" ]; then + echo ${line_parts[1]} + return 0 + fi + done + fi } parse_legacy_version_file() { @@ -108,28 +113,19 @@ parse_legacy_version_file() { local plugin_path=$(get_plugin_path "$plugin_name") local parse_legacy_script="${plugin_path}/bin/parse-legacy-file" - if [ -f $parse_legacy_script ]; then - echo $(bash "$parse_legacy_script" "$file_path") - else - echo $(cat $file_path) - fi -} - -parse_version_file() { - local file_path=$1 - local plugin_name=$2 - - if [ $(basename $file_path) = ".tool-versions" ]; then - echo $(parse_asdf_version_file "$file_path" "$plugin_name") - else - echo $(parse_legacy_version_file "$file_path" "$plugin_name") + if [ -f $file_path ]; then + if [ -f $parse_legacy_script ]; then + echo $(bash "$parse_legacy_script" "$file_path") + else + echo $(cat $file_path) + fi fi } get_preset_version_for() { local plugin_name=$1 - local version_file_path=$(find_version_file_for "$plugin_name") - echo $(parse_version_file "$version_file_path" "$plugin_name") + + echo "$(find_version "$plugin_name" "$search_path")" } get_asdf_config_value_from_file() { diff --git a/test/utils.bats b/test/utils.bats index 0bf16105..d4f21296 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -46,83 +46,43 @@ teardown() { [ "$output" = "" ] } -@test "find_version_file_for should return .tool-versions if legacy is disabled" { - cd $PROJECT_DIR - touch ".tool-versions" - touch ".dummy-version" +@test "find_version should return .tool-versions if legacy is disabled" { + echo "dummy 0.1.0" > $PROJECT_DIR/.tool-versions + echo "0.2.0" > $PROJECT_DIR/.dummy-version - run find_version_file_for "dummy" + run find_version "dummy" $PROJECT_DIR [ "$status" -eq 0 ] - [ "$output" = "$PROJECT_DIR/.tool-versions" ] + [ "$output" = "0.1.0" ] } -@test "find_version_file_for should return .tool-versions ++ plugin filenames if supported" { - cd $PROJECT_DIR - touch "$HOME/.tool-versions" - touch ".dummy-version" - echo 'legacy_version_file = yes' > $HOME/.asdfrc +@test "find_version should return the legacy file if supported" { + echo "legacy_version_file = yes" > $HOME/.asdfrc + echo "dummy 0.1.0" > $HOME/.tool-versions + echo "0.2.0" > $PROJECT_DIR/.dummy-version - run find_version_file_for "dummy" + run find_version "dummy" $PROJECT_DIR [ "$status" -eq 0 ] - [ "$output" = "$PROJECT_DIR/.dummy-version" ] + [ "$output" = "0.2.0" ] } -@test "find_version_file_for should return .tool-versions if unsupported" { - cd $PROJECT_DIR - touch "$HOME/.tool-versions" - touch ".dummy-version" - echo 'legacy_version_file = yes' > $HOME/.asdfrc +@test "find_version skips .tool-version file that don't list the plugin" { + echo "dummy 0.1.0" > $HOME/.tool-versions + echo "another_plugin 0.3.0" > $PROJECT_DIR/.tool-versions + + run find_version "dummy" $PROJECT_DIR + [ "$status" -eq 0 ] + [ "$output" = "0.1.0" ] +} + +@test "find_version should return .tool-versions if unsupported" { + echo "dummy 0.1.0" > $HOME/.tool-versions + echo "0.2.0" > $PROJECT_DIR/.dummy-version + echo "legacy_version_file = yes" > $HOME/.asdfrc rm $ASDF_DIR/plugins/dummy/bin/list-legacy-filenames - run find_version_file_for "dummy" + run find_version "dummy" $PROJECT_DIR [ "$status" -eq 0 ] - [ "$output" = "$HOME/.tool-versions" ] -} - -@test "search_filenames should return the path to the first filename found" { - touch "$PROJECT_DIR/.dummy-version" - touch "$PROJECT_DIR/.tool-versions" - - run search_filenames ".tool-versions .dummy-version" $PROJECT_DIR - [ "$status" -eq 0 ] - [ "$output" = "$PROJECT_DIR/.tool-versions" ] -} - -@test "search_filenames should walk parent directories" { - touch "$PROJECT_DIR/.dummy-version" - touch "$HOME/.tool-versions" - - run search_filenames ".tool-versions" $PROJECT_DIR - [ "$status" -eq 0 ] - [ "$output" = "$HOME/.tool-versions" ] -} - -@test "search_filenames should return nothing if not found" { - run search_filenames ".does-not-exist" $PROJECT_DIR - [ "$status" -eq 0 ] - [ "$output" = "" ] -} - -@test "parse_version_file parses the version from a .tool-version file" { - echo "dummy 0.2.0" > $HOME/.tool-versions - run parse_version_file $HOME/.tool-versions "dummy" - [ "$status" -eq 0 ] - [ "$output" = "0.2.0" ] -} - -@test "parse_version_file calls the plugin's parse-legacy-file if implemented" { - echo "dummy-0.2.0" > $HOME/.dummy-version - run parse_version_file $HOME/.dummy-version "dummy" - [ "$status" -eq 0 ] - [ "$output" = "0.2.0" ] -} - -@test "parse_version_file cats the legacy file if parse-legacy-file isn't implemented" { - echo "0.2.0" > $HOME/.dummy-version - rm $ASDF_DIR/plugins/dummy/bin/parse-legacy-file - run parse_version_file $HOME/.dummy-version "dummy" - [ "$status" -eq 0 ] - [ "$output" = "0.2.0" ] + [ "$output" = "0.1.0" ] } @test "get_preset_version_for returns the current version" { From d505c0ee7127ae4a9516f6f3f8046e5e4865700e Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Sun, 28 Aug 2016 13:43:54 +0900 Subject: [PATCH 08/13] Return both version and path from find_version This allows the current command to print the setting file. --- lib/commands/current.sh | 9 ++++++--- lib/utils.sh | 11 +++++++---- test/utils.bats | 8 ++++---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/commands/current.sh b/lib/commands/current.sh index 8d73297f..1f55680c 100644 --- a/lib/commands/current.sh +++ b/lib/commands/current.sh @@ -2,10 +2,13 @@ current_command() { local plugin_name=$1 check_if_plugin_exists $plugin_name - local version_file_path=$(find_version_file_for $plugin_name) - local version=$(parse_version_file $version_file_path $plugin_name) - check_if_version_exists $plugin_name $version + 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 diff --git a/lib/utils.sh b/lib/utils.sh index 325b13e2..5f7cec6a 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -56,7 +56,7 @@ display_error() { echo >&2 $1 } -find_version_for() { +find_version() { local plugin_name=$1 local search_path=$2 @@ -73,7 +73,7 @@ find_version_for() { local asdf_version=$(parse_asdf_version_file "$search_path/.tool-versions" $plugin_name) if [ -n "$asdf_version" ]; then - echo "$asdf_version" + echo "$asdf_version:$search_path/.tool-versions" return 0 fi @@ -81,7 +81,7 @@ find_version_for() { local legacy_version=$(parse_legacy_version_file "$search_path/$filename" $plugin_name) if [ -n "$legacy_version" ]; then - echo "$legacy_version" + echo "$legacy_version:$search_path/$filename" return 0 fi done @@ -124,8 +124,11 @@ parse_legacy_version_file() { get_preset_version_for() { local plugin_name=$1 + local search_path=$(pwd) + local version_and_path=$(find_version "$plugin_name" "$search_path") + local version=$(cut -d ':' -f 1 <<< "$version_and_path"); - echo "$(find_version "$plugin_name" "$search_path")" + echo "$version" } get_asdf_config_value_from_file() { diff --git a/test/utils.bats b/test/utils.bats index d4f21296..5d5c8c66 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -52,7 +52,7 @@ teardown() { run find_version "dummy" $PROJECT_DIR [ "$status" -eq 0 ] - [ "$output" = "0.1.0" ] + [ "$output" = "0.1.0:$PROJECT_DIR/.tool-versions" ] } @test "find_version should return the legacy file if supported" { @@ -62,7 +62,7 @@ teardown() { run find_version "dummy" $PROJECT_DIR [ "$status" -eq 0 ] - [ "$output" = "0.2.0" ] + [ "$output" = "0.2.0:$PROJECT_DIR/.dummy-version" ] } @test "find_version skips .tool-version file that don't list the plugin" { @@ -71,7 +71,7 @@ teardown() { run find_version "dummy" $PROJECT_DIR [ "$status" -eq 0 ] - [ "$output" = "0.1.0" ] + [ "$output" = "0.1.0:$HOME/.tool-versions" ] } @test "find_version should return .tool-versions if unsupported" { @@ -82,7 +82,7 @@ teardown() { run find_version "dummy" $PROJECT_DIR [ "$status" -eq 0 ] - [ "$output" = "0.1.0" ] + [ "$output" = "0.1.0:$HOME/.tool-versions" ] } @test "get_preset_version_for returns the current version" { From 20739721ca5ea756b5a86af0f17c5f0ca48e0ea4 Mon Sep 17 00:00:00 2001 From: Kevin Rockwood Date: Tue, 30 Aug 2016 09:25:52 +0900 Subject: [PATCH 09/13] Add note about parse-legacy-file argument --- docs/creating-plugins.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/creating-plugins.md b/docs/creating-plugins.md index b51bbd5e..e7df57d0 100644 --- a/docs/creating-plugins.md +++ b/docs/creating-plugins.md @@ -62,7 +62,7 @@ Note: This will only apply for users who have enabled the `legacy_version_file` #### bin/parse-legacy-file -This can be used to further parse the legacy file found by asdf. If `parse-legacy-file` isn't implemented, asdf will simply cat the file to determine the version. +This can be used to further parse the legacy file found by asdf. If `parse-legacy-file` isn't implemented, asdf will simply cat the file to determine the version. The script will be passed the file path as its first argument. ### Custom shim templates From 181b5a769b8a5d91ddf2b6f6d4afa2c5c2527dd8 Mon Sep 17 00:00:00 2001 From: Stratus3D Date: Tue, 16 Aug 2016 10:52:37 -0400 Subject: [PATCH 10/13] Add test for 'asdf local' in a directory with a name containing whitespace. --- test/version_commands.bats | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/version_commands.bats b/test/version_commands.bats index 1471e818..f59d560d 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -45,6 +45,18 @@ teardown() { [ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ] } +@test "local should create a local .tool-versions file if it doesn't exist when the directory name contains whitespace" { + WHITESPACE_DIR="$PROJECT_DIR/whitespace\ dir" + mkdir -p "$WHITESPACE_DIR" + cd "$WHITESPACE_DIR" + + run local_command "dummy" "1.1.0" + + tool_version_contents=$(cat "$WHITESPACE_DIR/.tool-versions") + [ "$status" -eq 0 ] + [ "$tool_version_contents" = "dummy 1.1.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" From 5cc55d45fd5cc2b01907bd120d5e3e5a8a6aed88 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 13 Aug 2016 14:27:27 -0400 Subject: [PATCH 11/13] Add section to readme on the .asdfrc file. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index efd3dace..5124db05 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,18 @@ To install all the tools defined in a `.tool-versions` file run the `asdf instal You can view/modify the file by hand or use `asdf local` and `asdf global` to manage it. +## The `$HOME/.asdfrc` config file + +Add a `.asdfrc` file to your home directory and asdf will use the settings specified in the file. The file should be formatted like this: + +``` +legacy_version_file = yes +``` + +**Settings** + +* `legacy_version_file` - defaults to `no`. If set to yes it will cause plugins that support this feature to read the version files used by other version managers (e.g. `.ruby-version` in the case of Ruby's rbenv). + ## Credits Me ([@HashNuke](http://github.com/HashNuke)), High-fever, cold, cough. From 12a6316e4c59e186c2a7e7d16c7d0d1945e67f35 Mon Sep 17 00:00:00 2001 From: Stratus3D Date: Tue, 16 Aug 2016 10:54:36 -0400 Subject: [PATCH 12/13] Fix whitespace bug in 'asdf local'. --- lib/commands/version_commands.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/commands/version_commands.sh b/lib/commands/version_commands.sh index d608e024..d9c7d01e 100644 --- a/lib/commands/version_commands.sh +++ b/lib/commands/version_commands.sh @@ -18,10 +18,10 @@ version_command() { check_if_plugin_exists $plugin check_if_version_exists $plugin $version - if [ -f $file ] && grep $plugin $file > /dev/null; then - sed -i -e "s/$plugin .*/$plugin $version/" $file + if [ -f "$file" ] && grep $plugin "$file" > /dev/null; then + sed -i -e "s/$plugin .*/$plugin $version/" "$file" else - echo "$plugin $version" >> $file + echo "$plugin $version" >> "$file" fi } From 95bde8e86f043a61e797660e7bf7e3808044097f Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Tue, 30 Aug 2016 15:46:14 +0900 Subject: [PATCH 13/13] Fix local and global commands to accept multiple versions --- lib/commands/version_commands.sh | 16 ++++++++++------ test/version_commands.bats | 12 ++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/commands/version_commands.sh b/lib/commands/version_commands.sh index d9c7d01e..b6024998 100644 --- a/lib/commands/version_commands.sh +++ b/lib/commands/version_commands.sh @@ -1,14 +1,16 @@ version_command() { local cmd=$1 local plugin=$2 - local version=$3 - local file - if [ "$#" -ne 3 ]; then + if [ "$#" -lt "3" ]; then echo "Usage: asdf $cmd " exit 1 fi + shift 2 + local versions=$@ + + local file if [ $cmd = "global" ]; then file=$HOME/.tool-versions else @@ -16,12 +18,14 @@ version_command() { fi check_if_plugin_exists $plugin - check_if_version_exists $plugin $version + for version in $versions; do + check_if_version_exists $plugin $version + done if [ -f "$file" ] && grep $plugin "$file" > /dev/null; then - sed -i -e "s/$plugin .*/$plugin $version/" "$file" + sed -i -e "s/$plugin .*/$plugin $versions/" "$file" else - echo "$plugin $version" >> "$file" + echo "$plugin $versions" >> "$file" fi } diff --git a/test/version_commands.bats b/test/version_commands.bats index f59d560d..14d8c89e 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -45,6 +45,12 @@ teardown() { [ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ] } +@test "local should allow multiple versions" { + run local_command "dummy" "1.1.0" "1.0.0" + [ "$status" -eq 0 ] + [ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0 1.0.0" ] +} + @test "local should create a local .tool-versions file if it doesn't exist when the directory name contains whitespace" { WHITESPACE_DIR="$PROJECT_DIR/whitespace\ dir" mkdir -p "$WHITESPACE_DIR" @@ -70,6 +76,12 @@ teardown() { [ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ] } +@test "global should accept multiple versions" { + run global_command "dummy" "1.1.0" "1.0.0" + [ "$status" -eq 0 ] + [ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0 1.0.0" ] +} + @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"