Skip to content

Commit

Permalink
fix: reshim did not rewrite executable path (asdf-vm#1311)
Browse files Browse the repository at this point in the history
Co-authored-by: James Hegedus <jthegedus@hey.com>
Fixes asdf-vm#1115
Fixes asdf-vm#1231
Fixes asdf-vm#1286
  • Loading branch information
dylan-chong authored and hyperupcall committed Dec 21, 2022
1 parent 48a63c9 commit d348ccb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
41 changes: 16 additions & 25 deletions lib/commands/reshim.bash
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,28 @@ write_shim_script() {
local shim_path
shim_path="$(asdf_data_dir)/shims/$executable_name"

if [ -f "$shim_path" ]; then
if ! grep -x "# asdf-plugin: ${plugin_name} ${version}" "$shim_path" >/dev/null; then
sed -i.bak -e "s/exec /# asdf-plugin: ${plugin_name} ${version}\\"$'\n''exec /' "$shim_path"
rm -f "$shim_path".bak
fi
else
cat <<EOF >"$shim_path"
#!/usr/bin/env bash
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}
exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf '
EOF
fi

chmod +x "$shim_path"
}

generate_shim_for_executable() {
local plugin_name=$1
local executable=$2
if [ -f "$shim_path" ]; then
grep '^#\sasdf-plugin:\s' <"$shim_path" >>"$temp_versions_path"
fi

check_if_plugin_exists "$plugin_name"
cat <<EOF >"$shim_path"
#!/usr/bin/env bash
$(sort -u <"$temp_versions_path")
exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf '
EOF

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
rm "$temp_versions_path"

write_shim_script "$plugin_name" "$version" "$executable"
chmod +x "$shim_path"
}

generate_shims_for_version() {
Expand Down
16 changes: 16 additions & 0 deletions test/reshim_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,19 @@ EOM
run asdf reshim dummy 1.0
[ "$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 ]
}

0 comments on commit d348ccb

Please sign in to comment.