Merge branch 'rockwood-legacy_file_redux'

This commit is contained in:
Trevor Brown 2016-08-30 19:13:32 -04:00
commit b0b30d0464
7 changed files with 184 additions and 151 deletions

View File

@ -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. The script will be passed the file path as its first argument.
### Custom shim templates

View File

@ -1,20 +1,37 @@
current_command() {
local plugin_name=$1
local version=$(get_preset_version_for $plugin_name)
check_if_plugin_exists $plugin_name
if [ "$version" == "" ]; then
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
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
}
# 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
}

View File

@ -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,134 +48,89 @@ 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
}
get_version_file_path_for() {
local tool_name=$1
local legacy_version_file_support=$(get_asdf_config_value "legacy_version_file")
find_version() {
local plugin_name=$1
local search_path=$2
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
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 legacy_filenames=""
if [ "$legacy_config" = "yes" ] && [ -f $legacy_list_filenames_script ]; then
legacy_filenames=$(bash "$legacy_list_filenames_script")
fi
echo $(get_asdf_versions_file_path)
}
get_asdf_versions_file_path() {
local asdf_tool_versions_path=""
local search_path=$(pwd)
while [ "$search_path" != "/" ]; do
if [ -f "$search_path/.tool-versions" ]; then
asdf_tool_versions_path="$search_path/.tool-versions"
break
local asdf_version=$(parse_asdf_version_file "$search_path/.tool-versions" $plugin_name)
if [ -n "$asdf_version" ]; then
echo "$asdf_version:$search_path/.tool-versions"
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:$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
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
fi
}
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 $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 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
local search_path=$(pwd)
local version_and_path=$(find_version "$plugin_name" "$search_path")
local version=$(cut -d ':' -f 1 <<< "$version_and_path");
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
echo "$version"
}
get_asdf_config_value_from_file() {
local config_path=$1
local key=$2

View File

@ -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" ]
}

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo ".dummy-version .dummyrc"

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo $(cat $1 | tr -d "dummy-")

View File

@ -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=$HOME/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,54 @@ 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" = "" ]
}
@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 "dummy" $PROJECT_DIR
[ "$status" -eq 0 ]
[ "$output" = "0.1.0:$PROJECT_DIR/.tool-versions" ]
}
@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 "dummy" $PROJECT_DIR
[ "$status" -eq 0 ]
[ "$output" = "0.2.0:$PROJECT_DIR/.dummy-version" ]
}
@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:$HOME/.tool-versions" ]
}
@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 "dummy" $PROJECT_DIR
[ "$status" -eq 0 ]
[ "$output" = "0.1.0:$HOME/.tool-versions" ]
}
@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" ]
}