fix: reshim did not rewrite executable path (#1311)

Co-authored-by: James Hegedus <jthegedus@hey.com>
Fixes https://github.com/asdf-vm/asdf/issues/1115
Fixes https://github.com/asdf-vm/asdf/issues/1231
Fixes https://github.com/asdf-vm/asdf/issues/1286
This commit is contained in:
Dylan Chong 2022-12-21 10:25:34 +13:00 committed by GitHub
parent 15faf93a0d
commit 5af7625769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 26 deletions

View File

@ -88,39 +88,30 @@ write_shim_script() {
local shim_path local shim_path
shim_path="$(asdf_data_dir)/shims/$executable_name" shim_path="$(asdf_data_dir)/shims/$executable_name"
local temp_dir
temp_dir=${TMPDIR:-/tmp}
local temp_versions_path
temp_versions_path=$(mktemp "$temp_dir/asdf-command-reshim-write-shims.XXXXXX")
cat <<EOF >"$temp_versions_path"
# asdf-plugin: ${plugin_name} ${version}
EOF
if [ -f "$shim_path" ]; then if [ -f "$shim_path" ]; then
if ! grep -x "# asdf-plugin: ${plugin_name} ${version}" "$shim_path" >/dev/null; then grep '^#\sasdf-plugin:\s' <"$shim_path" >>"$temp_versions_path"
sed -i.bak -e "s/exec /# asdf-plugin: ${plugin_name} ${version}\\"$'\n''exec /' "$shim_path"
rm -f "$shim_path".bak
fi fi
else
cat <<EOF >"$shim_path" cat <<EOF >"$shim_path"
#!/usr/bin/env bash #!/usr/bin/env bash
# asdf-plugin: ${plugin_name} ${version} $(sort -u <"$temp_versions_path")
exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf ' exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf '
EOF EOF
fi
rm "$temp_versions_path"
chmod +x "$shim_path" chmod +x "$shim_path"
} }
generate_shim_for_executable() {
local plugin_name=$1
local executable=$2
check_if_plugin_exists "$plugin_name"
local version
IFS=':' read -r -a version_info <<<"$full_version"
if [ "${version_info[0]}" = "ref" ]; then
version="${version_info[1]}"
else
version="${version_info[0]}"
fi
write_shim_script "$plugin_name" "$version" "$executable"
}
generate_shims_for_version() { generate_shims_for_version() {
local plugin_name=$1 local plugin_name=$1
local full_version=$2 local full_version=$2

View File

@ -140,3 +140,19 @@ EOM
run asdf reshim dummy 1.0 run asdf reshim dummy 1.0
[ "$output" == "RESHIM" ] [ "$output" == "RESHIM" ]
} }
# Fixes https://github.com/asdf-vm/asdf/issues/1115
# (Issue with executable_name changing after homebre updates)
@test "reshim should rewrite the shim file except the version list" {
run asdf install dummy 1.0
local dummy_shim
dummy_shim="$ASDF_DIR/shims/dummy"
sed -i.bak -e 's/exec /exec \/borked_path_due_to_homebrew_update/' "$dummy_shim"
run grep 'borked_path_due_to_homebrew_update' "$dummy_shim" # Sanity check
[ "$status" -eq 0 ]
run asdf reshim dummy "path:$ASDF_DIR/installs/dummy/path"
run grep -v 'borked_path_due_to_homebrew_update' "$dummy_shim"
[ "$status" -eq 0 ]
}