From 670c96d1a6d6d2c19ff63ce2ed14f784c340e9b9 Mon Sep 17 00:00:00 2001 From: Edwin Kofler Date: Fri, 6 Jan 2023 04:08:48 -0800 Subject: [PATCH] fix: Allow `path:` versions to use `~` (#1403) --- docs/manage/configuration.md | 2 +- docs/pt-br/manage/configuration.md | 2 +- docs/zh-hans/manage/configuration.md | 2 +- lib/functions/versions.bash | 2 +- lib/utils.bash | 27 +++++++++++++++++++++++++-- test/utils.bats | 15 +++++++++++++++ 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/docs/manage/configuration.md b/docs/manage/configuration.md index a49ce685..17e22f4d 100644 --- a/docs/manage/configuration.md +++ b/docs/manage/configuration.md @@ -31,7 +31,7 @@ The versions can be in the following format: - `10.15.0` - an actual version. Plugins that support downloading binaries, will download binaries. - `ref:v1.0.2-a` or `ref:39cb398vb39` - tag/commit/branch to download from github and compile -- `path:/src/elixir` - a path to custom compiled version of a tool to use. For use by language developers and such. +- `path:~/src/elixir` - a path to custom compiled version of a tool to use. For use by language developers and such. - `system` - this keyword causes asdf to passthrough to the version of the tool on the system that is not managed by asdf. ::: tip diff --git a/docs/pt-br/manage/configuration.md b/docs/pt-br/manage/configuration.md index 511ff4ea..97e7d2ab 100644 --- a/docs/pt-br/manage/configuration.md +++ b/docs/pt-br/manage/configuration.md @@ -22,7 +22,7 @@ As versões podem estar no seguinte formato: - `10.15.0` - uma versão real. Os plugins que suportam o download de binários farão o download de binários. - `ref:v1.0.2-a` ou `ref:39cb398vb39` - _tag/commit/branch_ para download pelo github e compilação um path costumizado e compi -- `path:/src/elixir` - um path para uma versão compilada e personalizada de uma ferramenta pronta para usar. Para uso por linguagens de desenvolvimento e outros. +- `path:~/src/elixir` - um path para uma versão compilada e personalizada de uma ferramenta pronta para usar. Para uso por linguagens de desenvolvimento e outros. - `system` - faz com que asdf passe para a versão da ferramenta no sistema que não é gerenciada por asdf . Várias versões podem ser definidas, separando-as com um espaço. Por exemplo, para usar Python 3.7.2, e também Python 2.7.15, use a linha abaixo em seu arquivo `.tool-versions`. diff --git a/docs/zh-hans/manage/configuration.md b/docs/zh-hans/manage/configuration.md index 102f08c8..2923c4dd 100644 --- a/docs/zh-hans/manage/configuration.md +++ b/docs/zh-hans/manage/configuration.md @@ -29,7 +29,7 @@ nodejs 10.15.0 - `10.15.0` - 实际的版本号。支持下载二进制文件的插件将会下载二进制文件。 - `ref:v1.0.2-a` 或者 `ref:39cb398vb39` - 指定标签/提交/分支从 github 下载并编译。 -- `path:/src/elixir` - 要使用的工具的自定义编译版本的路径。这种方式供语言开发者等使用。 +- `path:~/src/elixir` - 要使用的工具的自定义编译版本的路径。这种方式供语言开发者等使用。 - `system` - 此关键字会导致 asdf 传递系统上未由 asdf 管理的工具版本。 ::: tip 提示 diff --git a/lib/functions/versions.bash b/lib/functions/versions.bash index 07ef4342..1a748c07 100644 --- a/lib/functions/versions.bash +++ b/lib/functions/versions.bash @@ -64,7 +64,7 @@ version_command() { sed -i.bak -e "s|^$plugin_name .*$|$plugin_name ${resolved_versions[*]}|" "$file" rm -f "$file".bak else - # Add a trailing newline at the end of the file if missing and file present + # Add a trailing newline at the end of the file if missing [[ -f "$file" && -n "$(tail -c1 "$file")" ]] && printf '\n' >>"$file" # Add a new version line to the end of the file diff --git a/lib/utils.bash b/lib/utils.bash index d62f24ca..635fe6a4 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -263,7 +263,9 @@ find_install_path() { # And then use the binaries there local install_type="path" local version="path" - printf "%s\n" "${version_info[1]}" + + util_resolve_user_path "${version_info[1]}" + printf "%s\n" "${util_resolve_user_path_reply}" else local install_type="version" local version="${version_info[0]}" @@ -319,8 +321,15 @@ parse_asdf_version_file() { if [ -f "$file_path" ]; then local version version=$(strip_tool_version_comments "$file_path" | grep "^${plugin_name} " | sed -e "s/^${plugin_name} //") + if [ -n "$version" ]; then - printf "%s\n" "$version" + if [[ "$version" == path:* ]]; then + util_resolve_user_path "${version#path:}" + printf "%s\n" "path:${util_resolve_user_path_reply}" + else + printf "%s\n" "$version" + fi + return 0 fi fi @@ -837,3 +846,17 @@ remove_path_from_path() { local path=$2 substitute "$PATH" "$path" "" | sed -e "s|::|:|g" } + +# @description Strings that began with a ~ are always paths. In +# that case, then ensure ~ it handled like a shell +util_resolve_user_path() { + util_resolve_user_path_reply= + local path="$1" + + # shellcheck disable=SC2088 + if [ "${path::2}" = '~/' ]; then + util_resolve_user_path_reply="${HOME}/${path:2}" + else + util_resolve_user_path_reply="$path" + fi +} diff --git a/test/utils.bats b/test/utils.bats index da181b37..e750430b 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -133,6 +133,13 @@ teardown() { [ "$output" == "path:/some/dummy path" ] } +@test "parse_asdf_version_file should output path version with tilda" { + echo "dummy path:~/some/dummy path" >$PROJECT_DIR/.tool-versions + run parse_asdf_version_file $PROJECT_DIR/.tool-versions dummy + [ "$status" -eq 0 ] + [ "$output" == "path:$HOME/some/dummy path" ] +} + @test "find_versions 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 @@ -272,6 +279,14 @@ teardown() { [ "$output" = "path:/some/place with spaces" ] } +@test "get_preset_version_for should return path version with tilda" { + cd $PROJECT_DIR + echo "dummy path:~/some/place with spaces" >$PROJECT_DIR/.tool-versions + run get_preset_version_for "dummy" + [ "$status" -eq 0 ] + [ "$output" = "path:$HOME/some/place with spaces" ] +} + @test "get_executable_path for system version should return system path" { mkdir -p $ASDF_DIR/plugins/foo run get_executable_path "foo" "system" "ls"