asdf/lib/commands/reshim.sh

246 lines
7.2 KiB
Bash
Raw Normal View History

2015-05-24 02:32:38 -07:00
shim_command() {
local plugin_name=$1
2015-05-24 02:42:30 -07:00
local executable_path=$2
2017-09-04 10:04:56 -07:00
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"
2015-05-17 00:46:56 -07:00
ensure_shims_dir
2017-09-04 10:04:56 -07:00
generate_shim_for_executable "$plugin_name" "$executable_path"
2015-05-24 02:32:38 -07:00
}
2015-05-17 06:32:47 -07:00
2015-05-24 02:32:38 -07:00
reshim_command() {
local plugin_name=$1
local full_version=$2
2017-09-04 10:04:56 -07:00
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"
2015-05-24 02:32:38 -07:00
ensure_shims_dir
2015-05-17 06:32:47 -07:00
2015-05-24 02:32:38 -07:00
if [ "$full_version" != "" ]; then
# generate for the whole package version
2017-09-04 10:04:56 -07:00
generate_shims_for_version "$plugin_name" "$full_version"
2015-05-17 06:32:47 -07:00
else
# generate for all versions of the package
2017-09-04 10:04:56 -07:00
local plugin_installs_path
plugin_installs_path="$(asdf_dir)/installs/${plugin_name}"
for install in "${plugin_installs_path}"/*/; do
local full_version_name
full_version_name=$(basename "$install" | sed 's/ref\-/ref\:/')
generate_shims_for_version "$plugin_name" "$full_version_name"
remove_obsolete_shims "$plugin_name" "$full_version_name"
2015-05-17 00:46:56 -07:00
done
fi
}
2015-05-10 10:25:42 -07:00
2015-05-11 09:43:24 -07:00
ensure_shims_dir() {
# Create shims dir if doesn't exist
2017-09-04 10:04:56 -07:00
if [ ! -d "$(asdf_dir)/shims" ]; then
mkdir "$(asdf_dir)/shims"
2015-05-11 09:43:24 -07:00
fi
}
2015-05-10 10:25:42 -07:00
2015-05-11 09:43:24 -07:00
write_shim_script() {
2015-05-24 02:32:38 -07:00
local plugin_name=$1
local version=$2
local executable_path=$3
2017-09-04 10:04:56 -07:00
local executable_name
executable_name=$(basename "$executable_path")
local plugin_shims_path
plugin_shims_path=$(get_plugin_path "$plugin_name")/shims
local shim_path
shim_path="$(asdf_dir)/shims/$executable_name"
2015-05-11 09:43:24 -07:00
if [ -f "$plugin_shims_path/$executable_name" ]; then
cp "$plugin_shims_path/$executable_name" "$shim_path"
elif [ -f "$shim_path" ]; then
if ! grep "# asdf-plugin-version: $version" "$shim_path" > /dev/null; then
sed -i '' -e "s/\(asdf-plugin: $plugin_name\)/\1\\"$'\n'"# asdf-plugin-version: $version/" "$shim_path"
fi
else
cat <<EOF > "$shim_path"
#!/usr/bin/env bash
# asdf-plugin: ${plugin_name}
# asdf-plugin-version: ${version}
exec $(asdf_dir)/bin/private/asdf-exec ${plugin_name} ${executable_path} "\$@"
EOF
fi
2015-05-11 09:43:24 -07:00
chmod +x "$shim_path"
2015-05-11 09:43:24 -07:00
}
2015-05-17 06:32:47 -07:00
generate_shim_for_executable() {
2015-05-24 02:32:38 -07:00
local plugin_name=$1
2015-05-24 02:42:30 -07:00
local executable=$2
2017-09-04 10:04:56 -07:00
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
2015-05-17 06:32:47 -07:00
2017-09-04 10:04:56 -07:00
check_if_plugin_exists "$plugin_name"
2015-05-17 06:32:47 -07:00
2017-09-04 10:04:56 -07:00
IFS=':' read -r -a version_info <<< "$full_version"
2015-05-21 21:47:27 -07:00
if [ "${version_info[0]}" = "ref" ]; then
2015-05-17 06:32:47 -07:00
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"
local version="${version_info[0]}"
fi
write_shim_script "$plugin_name" "$version" "$executable"
}
list_plugin_bin_paths() {
local plugin_name=$1
local version=$2
local install_type=$3
2017-09-04 10:04:56 -07:00
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
if [ -f "${plugin_path}/bin/list-bin-paths" ]; then
2017-09-04 10:04:56 -07:00
local space_separated_list_of_bin_paths
space_separated_list_of_bin_paths=$(
export ASDF_INSTALL_TYPE=$install_type
export ASDF_INSTALL_VERSION=$version
export ASDF_INSTALL_PATH=$install_path
bash "${plugin_path}/bin/list-bin-paths"
)
else
local space_separated_list_of_bin_paths="bin"
fi
echo "$space_separated_list_of_bin_paths"
2015-05-17 06:32:47 -07:00
}
2015-05-11 09:43:24 -07:00
generate_shims_for_version() {
local plugin_name=$1
2015-05-11 09:43:24 -07:00
local full_version=$2
check_if_plugin_exists "$plugin_name"
2015-05-10 10:25:42 -07:00
2017-09-04 10:04:56 -07:00
IFS=':' read -r -a version_info <<< "$full_version"
2015-05-21 21:47:27 -07:00
if [ "${version_info[0]}" = "ref" ]; then
2015-05-11 09:43:24 -07:00
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"
local version="${version_info[0]}"
fi
space_separated_list_of_bin_paths="$(list_plugin_bin_paths "$plugin_name" "$version" "$install_type")"
2017-09-04 10:04:56 -07:00
IFS=' ' read -r -a all_bin_paths <<< "$space_separated_list_of_bin_paths"
2015-05-10 10:25:42 -07:00
2017-09-04 10:04:56 -07:00
local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
2015-05-17 06:32:47 -07:00
for bin_path in "${all_bin_paths[@]}"; do
for executable_file in $install_path/$bin_path/*; do
# because just $executable_file gives absolute path; We don't want version hardcoded in shim
2017-09-04 10:04:56 -07:00
local executable_path_relative_to_install_path
executable_path_relative_to_install_path="$bin_path"/$(basename "$executable_file")
if [ -x "$executable_file" ]; then
2017-09-04 10:04:56 -07:00
write_shim_script "$plugin_name" "$version" "$executable_path_relative_to_install_path"
fi
done
done
}
shim_still_exists() {
local shim_name=$1
local install_path=$2
local space_separated_list_of_bin_paths=$3
2017-09-04 10:04:56 -07:00
IFS=' ' read -r -a all_bin_paths <<< "$space_separated_list_of_bin_paths"
for bin_path in "${all_bin_paths[@]}"; do
if [ -x "$install_path/$bin_path/$shim_name" ]; then
return 0
fi
done
return 1
}
remove_obsolete_shims() {
local plugin_name=$1
local full_version=$2
2017-09-04 10:04:56 -07:00
local shims_path
shims_path="$(asdf_dir)/shims"
2017-09-04 10:04:56 -07:00
IFS=':' read -r -a version_info <<< "$full_version"
if [ "${version_info[0]}" = "ref" ]; then
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"
local version="${version_info[0]}"
fi
space_separated_list_of_bin_paths="$(list_plugin_bin_paths "$plugin_name" "$version" "$install_type")"
2017-09-04 10:04:56 -07:00
local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
for shim_path in "$shims_path"/*; do
2017-09-04 10:04:56 -07:00
local shim_name
shim_name="$(basename "$shim_path")"
if grep "# asdf-plugin: $plugin_name" "$shim_path" > /dev/null && \
grep "# asdf-plugin-version: $version" "$shim_path" > /dev/null && \
! shim_still_exists "$shim_name" "$install_path" "$space_separated_list_of_bin_paths"; then
remove_shim_for_version "$plugin_name" "$shim_name" "$version"
fi
done
}
remove_shim_for_version() {
local plugin_name=$1
local executable_name=$2
local version=$3
2017-09-04 10:04:56 -07:00
local plugin_shims_path
plugin_shims_path=$(get_plugin_path "$plugin_name")/shims
local shim_path
shim_path="$(asdf_dir)/shims/$executable_name"
local count_installed
count_installed=$(list_installed_versions "$plugin_name" | wc -l)
if ! grep "# asdf-plugin: $plugin_name" "$shim_path" > /dev/null 2>&1; then
return 0
fi
sed -i '' -e "/# asdf-plugin-version: $version/d" "$shim_path"
if [ ! -f "$plugin_shims_path/$executable_name" ] && \
! grep "# asdf-plugin-version" "$shim_path" > /dev/null || \
[ "$count_installed" -eq 0 ]; then
rm "$shim_path"
fi
}
remove_shims_for_version() {
local plugin_name=$1
local full_version=$2
check_if_plugin_exists "$plugin_name"
2017-09-04 10:04:56 -07:00
IFS=':' read -r -a version_info <<< "$full_version"
if [ "${version_info[0]}" = "ref" ]; then
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"
local version="${version_info[0]}"
fi
space_separated_list_of_bin_paths="$(list_plugin_bin_paths "$plugin_name" "$version" "$install_type")"
2017-09-04 10:04:56 -07:00
IFS=' ' read -r -a all_bin_paths <<< "$space_separated_list_of_bin_paths"
2015-05-11 09:43:24 -07:00
2015-05-17 06:32:47 -07:00
for bin_path in "${all_bin_paths[@]}"; do
for executable_file in $install_path/$bin_path/*; do
2017-09-04 10:04:56 -07:00
local executable_name
executable_name="$(basename "$executable_file")"
remove_shim_for_version "$plugin_name" "$executable_name" "$version"
2015-05-17 06:32:47 -07:00
done
2015-05-11 09:43:24 -07:00
done
}