Move bin/private/asdf-tool-exec to be asdf exec command

This way the code for executing shims can also be invoked via
`asdf exec <tool> [args..]` and can be linted.
This commit is contained in:
Victor Hugo Borja 2019-01-19 20:00:34 -06:00
parent f1809ae290
commit e51f778b61
6 changed files with 110 additions and 95 deletions

View File

@ -5,6 +5,8 @@ source "$(dirname "$(dirname "$0")")/lib/utils.sh"
# shellcheck source=lib/commands/help.sh
source "$(dirname "$(dirname "$0")")/lib/commands/help.sh"
# shellcheck source=lib/commands/shim-exec.sh
source "$(dirname "$(dirname "$0")")/lib/commands/shim-exec.sh"
# shellcheck source=lib/commands/update.sh
source "$(dirname "$(dirname "$0")")/lib/commands/update.sh"
# shellcheck source=lib/commands/install.sh
@ -49,70 +51,73 @@ callback_args="${@:2}"
# shellcheck disable=SC2086
case $1 in
"--version")
"--version")
asdf_version $callback_args;;
"help")
"exec")
shim_exec_command $callback_args;;
"help")
help_command $callback_args;;
"update")
"update")
update_command $callback_args;;
"install")
"install")
install_command $callback_args;;
"uninstall")
"uninstall")
uninstall_command $callback_args;;
"current")
"current")
current_command $callback_args;;
"where")
"where")
where_command $callback_args;;
"which")
"which")
which_command $callback_args;;
"local")
"local")
local_command $callback_args;;
"global")
"global")
global_command $callback_args;;
"list")
"list")
list_command $callback_args;;
"list-all")
"list-all")
list_all_command $callback_args;;
"reshim")
"reshim")
reshim_command $callback_args;;
"plugin-add")
"plugin-add")
plugin_add_command $callback_args;;
"plugin-list")
"plugin-list")
plugin_list_command $callback_args;;
"plugin-list-all")
"plugin-list-all")
plugin_list_all_command $callback_args;;
"plugin-update")
"plugin-update")
plugin_update_command $callback_args;;
"plugin-remove")
"plugin-remove")
plugin_remove_command $callback_args;;
# Undocumented commands for development
"plugin-push")
# Undocumented commands for development
"plugin-push")
plugin_push_command $callback_args;;
"plugin-test")
"plugin-test")
plugin_test_command $callback_args;;
"shim-versions")
"shim-versions")
shim_versions_command $callback_args;;
*)
*)
help_command
exit 1;;
esac

View File

@ -1,69 +0,0 @@
#!/usr/bin/env bash
utils_path="$(dirname "$(dirname "$(dirname "$0")")")/lib/utils.sh"
# shellcheck source=lib/utils.sh
source $utils_path
shim_name="$1"
tool_versions() {
env | awk -F= '/^ASDF_[A-Z]+_VERSION/ {print $1" "$2}' | sed -e "s/^ASDF_//" | sed -e "s/_VERSION / /" | tr "[:upper:]_" "[:lower:]-"
local asdf_versions_path=$(find_tool_versions)
[ -f "${asdf_versions_path}" ] && cat "${asdf_versions_path}"
}
shim_versions() {
shim_plugin_versions "${shim_name}"
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | awk '{print$1" system"}'
}
select_from_tool_versions() {
grep -f <(shim_versions) <(tool_versions) | head | xargs
}
preset_versions() {
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c "source $utils_path; echo PLUGIN \$(get_preset_version_for PLUGIN)"
}
select_from_preset_version() {
grep -f <(shim_versions) <(preset_versions) | head | xargs
}
selected_version=$(select_from_tool_versions)
if [ -z "$selected_version" ]; then
selected_version=$(select_from_preset_version)
fi
if [ ! -z "$selected_version" ]; then
plugin_name=$(cut -d ' ' -f 1 <<< "$selected_version");
version=$(cut -d ' ' -f 2- <<< "$selected_version");
plugin_path=$(get_plugin_path "$plugin_name")
plugin_exec_env $plugin_name $version
executable_path=$(command -v "$shim_name")
if [ -x "${plugin_path}/bin/exec-path" ]; then
install_path=$(find_install_path "$plugin_name" "$version")
executable_path=$(get_custom_executable_path "${plugin_path}" "${install_path}" "${executable_path}")
fi
asdf_run_hook "pre_${plugin_name}_${shim_name}" "${@:2}"
pre_status=$?
if [ "$pre_status" -eq 0 ]; then
"$executable_path" "${@:2}"
exit_status=$?
fi
if [ "${exit_status:-${pre_status}}" -eq 0 ]; then
asdf_run_hook "post_${plugin_name}_${shim_name}" "${@:2}"
post_status=$?
fi
exit "${post_status:-${exit_status:-${pre_status}}}"
fi
(
echo "asdf: No version set for command ${shim_name}"
echo "you might want to add one of the following in your .tool-versions file:"
echo ""
shim_plugin_versions "${shim_name}"
) >&2
exit 126

View File

@ -25,6 +25,7 @@ MANAGE PACKAGES
UTILS
asdf exec <executable> [args..] Run an executable command shim
asdf reshim <name> <version> Recreate shims for version of a package
asdf shim-versions <executable> List on which plugins and versions is an executable provided.
asdf update Update asdf to the latest stable release

View File

@ -73,7 +73,7 @@ write_shim_script() {
cat <<EOF > "$shim_path"
#!/usr/bin/env bash
# asdf-plugin: ${plugin_name} ${version}
exec $(asdf_dir)/bin/private/asdf-tool-exec "${executable_name}" "\$@"
exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@"
EOF
fi

69
lib/commands/shim-exec.sh Normal file
View File

@ -0,0 +1,69 @@
tool_versions() {
env | awk -F= '/^ASDF_[A-Z]+_VERSION/ {print $1" "$2}' | sed -e "s/^ASDF_//" | sed -e "s/_VERSION / /" | tr "[:upper:]_" "[:lower:]-"
local asdf_versions_path
asdf_versions_path="$(find_tool_versions)"
[ -f "${asdf_versions_path}" ] && cat "${asdf_versions_path}"
}
shim_versions() {
shim_plugin_versions "${shim_name}"
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | awk '{print$1" system"}'
}
select_from_tool_versions() {
grep -f <(shim_versions) <(tool_versions) | head -n 1 | xargs echo
}
preset_versions() {
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c "source $(asdf_dir)/lib/utils.sh; echo PLUGIN \$(get_preset_version_for PLUGIN)"
}
select_from_preset_version() {
grep -f <(shim_versions) <(preset_versions) | head -n 1 | xargs echo
}
shim_exec_command() {
local shim_name="$1"
selected_version=$(select_from_tool_versions)
if [ -z "$selected_version" ]; then
selected_version=$(select_from_preset_version)
fi
if [ ! -z "$selected_version" ]; then
plugin_name=$(cut -d ' ' -f 1 <<< "$selected_version");
version=$(cut -d ' ' -f 2- <<< "$selected_version");
plugin_path=$(get_plugin_path "$plugin_name")
plugin_exec_env "$plugin_name" "$version"
executable_path=$(command -v "$shim_name")
if [ -x "${plugin_path}/bin/exec-path" ]; then
install_path=$(find_install_path "$plugin_name" "$version")
executable_path=$(get_custom_executable_path "${plugin_path}" "${install_path}" "${executable_path}")
fi
asdf_run_hook "pre_${plugin_name}_${shim_name}" "${@:2}"
pre_status=$?
if [ "$pre_status" -eq 0 ]; then
"$executable_path" "${@:2}"
exit_status=$?
fi
if [ "${exit_status:-${pre_status}}" -eq 0 ]; then
asdf_run_hook "post_${plugin_name}_${shim_name}" "${@:2}"
post_status=$?
fi
exit "${post_status:-${exit_status:-${pre_status}}}"
fi
(
echo "asdf: No version set for command ${shim_name}"
echo "you might want to add one of the following in your .tool-versions file:"
echo ""
shim_plugin_versions "${shim_name}"
) >&2
exit 126
}

View File

@ -21,6 +21,15 @@ teardown() {
clean_asdf_dir
}
@test "asdf exec should pass all arguments to executable" {
echo "dummy 1.0" > $PROJECT_DIR/.tool-versions
run install_command
run $ASDF_DIR/bin/asdf exec dummy world hello
[ "$output" == "This is Dummy 1.0! hello world" ]
[ "$status" -eq 0 ]
}
@test "shim exec should pass all arguments to executable" {
echo "dummy 1.0" > $PROJECT_DIR/.tool-versions
run install_command