2015-05-24 02:32:38 -07:00
|
|
|
reshim_command() {
|
|
|
|
local plugin_name=$1
|
|
|
|
local full_version=$2
|
2019-01-19 03:12:52 -07:00
|
|
|
|
|
|
|
if [ -z "$plugin_name" ]; then
|
|
|
|
local plugins_path
|
|
|
|
plugins_path=$(get_plugin_path)
|
|
|
|
|
|
|
|
if ls "$plugins_path" &> /dev/null; then
|
|
|
|
for plugin_path in "$plugins_path"/* ; do
|
|
|
|
plugin_name=$(basename "$plugin_path")
|
|
|
|
reshim_command "$plugin_name"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2017-09-04 10:04:56 -07:00
|
|
|
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"
|
2019-01-19 12:00:33 -07:00
|
|
|
asdf_run_hook "post_asdf_reshim_$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
|
2018-06-10 01:54:39 -07:00
|
|
|
plugin_installs_path="$(asdf_data_dir)/installs/${plugin_name}"
|
2017-09-04 10:04:56 -07:00
|
|
|
|
|
|
|
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"
|
2019-01-19 12:00:33 -07:00
|
|
|
asdf_run_hook "post_asdf_reshim_$plugin_name" "$full_version_name"
|
2015-05-17 00:46:56 -07:00
|
|
|
done
|
|
|
|
fi
|
2019-01-19 12:00:33 -07:00
|
|
|
|
2015-05-17 00:46:56 -07:00
|
|
|
}
|
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
|
2018-06-10 01:54:39 -07:00
|
|
|
if [ ! -d "$(asdf_data_dir)/shims" ]; then
|
|
|
|
mkdir "$(asdf_data_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
|
2017-07-26 13:00:55 -07:00
|
|
|
local version=$2
|
|
|
|
local executable_path=$3
|
2019-01-19 03:12:52 -07:00
|
|
|
|
|
|
|
if ! is_executable "$executable_path"; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2017-09-04 10:04:56 -07:00
|
|
|
local executable_name
|
|
|
|
executable_name=$(basename "$executable_path")
|
2019-01-19 03:12:52 -07:00
|
|
|
|
2017-09-04 10:04:56 -07:00
|
|
|
local shim_path
|
2018-06-10 01:54:39 -07:00
|
|
|
shim_path="$(asdf_data_dir)/shims/$executable_name"
|
2015-05-11 09:43:24 -07:00
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
if [ -f "$shim_path" ]; then
|
|
|
|
if ! grep "# 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"
|
2017-10-27 07:27:27 -07:00
|
|
|
rm "$shim_path".bak
|
2017-07-26 13:00:55 -07:00
|
|
|
fi
|
2015-06-06 12:00:45 -07:00
|
|
|
else
|
2017-07-26 13:00:55 -07:00
|
|
|
cat <<EOF > "$shim_path"
|
2016-12-10 11:17:50 -07:00
|
|
|
#!/usr/bin/env bash
|
2019-01-19 03:12:52 -07:00
|
|
|
# asdf-plugin: ${plugin_name} ${version}
|
|
|
|
exec $(asdf_dir)/bin/private/asdf-tool-exec "${executable_name}" "\$@"
|
2016-12-10 11:17:50 -07:00
|
|
|
EOF
|
2015-06-06 12:00:45 -07:00
|
|
|
fi
|
2015-05-11 09:43:24 -07:00
|
|
|
|
2017-07-26 13:00:55 -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
|
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
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
local version
|
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
|
2019-01-19 03:12:52 -07:00
|
|
|
version="${version_info[1]}"
|
2015-05-17 06:32:47 -07:00
|
|
|
else
|
2019-01-19 03:12:52 -07:00
|
|
|
version="${version_info[0]}"
|
2015-05-17 06:32:47 -07:00
|
|
|
fi
|
|
|
|
|
2017-07-26 13:00:55 -07:00
|
|
|
write_shim_script "$plugin_name" "$version" "$executable"
|
|
|
|
}
|
|
|
|
|
2015-05-11 09:43:24 -07:00
|
|
|
generate_shims_for_version() {
|
2015-05-26 23:46:17 -07:00
|
|
|
local plugin_name=$1
|
2015-05-11 09:43:24 -07:00
|
|
|
local full_version=$2
|
2019-01-19 03:12:52 -07:00
|
|
|
for executable_path in $(plugin_executables "$plugin_name" "$full_version"); do
|
|
|
|
write_shim_script "$plugin_name" "$full_version" "$executable_path"
|
2017-07-26 13:00:55 -07:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_obsolete_shims() {
|
|
|
|
local plugin_name=$1
|
|
|
|
local full_version=$2
|
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
local shims
|
|
|
|
shims=$(plugin_shims "$plugin_name" "$full_version" | xargs -IX basename X | sort)
|
2017-07-26 13:00:55 -07:00
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
local exec_names
|
|
|
|
exec_names=$(plugin_executables "$plugin_name" "$full_version" | xargs -IX basename X | sort)
|
2017-07-26 13:00:55 -07:00
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
local obsolete_shims
|
|
|
|
obsolete_shims=$(comm -23 <(echo "$shims") <(echo "$exec_names"))
|
2017-07-26 13:00:55 -07:00
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
for shim_name in $obsolete_shims; do
|
|
|
|
remove_shim_for_version "$plugin_name" "$version" "$shim_name"
|
2017-07-26 13:00:55 -07:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_shim_for_version() {
|
|
|
|
local plugin_name=$1
|
2019-01-19 03:12:52 -07:00
|
|
|
local version=$2
|
|
|
|
local shim_name
|
|
|
|
|
|
|
|
shim_name=$(basename "$3")
|
|
|
|
|
2017-09-04 10:04:56 -07:00
|
|
|
local shim_path
|
2019-01-19 03:12:52 -07:00
|
|
|
shim_path="$(asdf_data_dir)/shims/$shim_name"
|
|
|
|
|
2017-09-04 10:04:56 -07:00
|
|
|
local count_installed
|
|
|
|
count_installed=$(list_installed_versions "$plugin_name" | wc -l)
|
2017-07-26 13:00:55 -07:00
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
if ! grep "# asdf-plugin: $plugin_name $version" "$shim_path" > /dev/null 2>&1; then
|
2017-07-26 13:00:55 -07:00
|
|
|
return 0
|
2015-05-17 10:44:16 -07:00
|
|
|
fi
|
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
sed -i.bak -e "/# asdf-plugin: $plugin_name $version/d" "$shim_path"
|
2017-10-27 07:27:27 -07:00
|
|
|
rm "$shim_path".bak
|
2017-07-26 13:00:55 -07:00
|
|
|
|
2019-01-19 03:12:52 -07:00
|
|
|
if ! grep "# asdf-plugin:" "$shim_path" > /dev/null || \
|
2017-07-26 13:00:55 -07:00
|
|
|
[ "$count_installed" -eq 0 ]; then
|
|
|
|
rm "$shim_path"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_shims_for_version() {
|
|
|
|
local plugin_name=$1
|
|
|
|
local full_version=$2
|
2019-01-19 03:12:52 -07:00
|
|
|
for shim_path in $(plugin_shims "$plugin_name" "$full_version"); do
|
|
|
|
remove_shim_for_version "$plugin_name" "$version" "$shim_path"
|
2015-05-11 09:43:24 -07:00
|
|
|
done
|
|
|
|
}
|