Skip to content

Commit e51f778

Browse files
committed
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.
1 parent f1809ae commit e51f778

File tree

6 files changed

+110
-95
lines changed

6 files changed

+110
-95
lines changed

bin/asdf

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ source "$(dirname "$(dirname "$0")")/lib/utils.sh"
55

66
# shellcheck source=lib/commands/help.sh
77
source "$(dirname "$(dirname "$0")")/lib/commands/help.sh"
8+
# shellcheck source=lib/commands/shim-exec.sh
9+
source "$(dirname "$(dirname "$0")")/lib/commands/shim-exec.sh"
810
# shellcheck source=lib/commands/update.sh
911
source "$(dirname "$(dirname "$0")")/lib/commands/update.sh"
1012
# shellcheck source=lib/commands/install.sh
@@ -49,70 +51,73 @@ callback_args="${@:2}"
4951
# shellcheck disable=SC2086
5052
case $1 in
5153

52-
"--version")
54+
"--version")
5355
asdf_version $callback_args;;
5456

55-
"help")
57+
"exec")
58+
shim_exec_command $callback_args;;
59+
60+
"help")
5661
help_command $callback_args;;
5762

58-
"update")
63+
"update")
5964
update_command $callback_args;;
6065

61-
"install")
66+
"install")
6267
install_command $callback_args;;
6368

64-
"uninstall")
69+
"uninstall")
6570
uninstall_command $callback_args;;
6671

67-
"current")
72+
"current")
6873
current_command $callback_args;;
6974

70-
"where")
75+
"where")
7176
where_command $callback_args;;
7277

73-
"which")
78+
"which")
7479
which_command $callback_args;;
7580

76-
"local")
81+
"local")
7782
local_command $callback_args;;
7883

79-
"global")
84+
"global")
8085
global_command $callback_args;;
8186

82-
"list")
87+
"list")
8388
list_command $callback_args;;
8489

85-
"list-all")
90+
"list-all")
8691
list_all_command $callback_args;;
8792

88-
"reshim")
93+
"reshim")
8994
reshim_command $callback_args;;
9095

91-
"plugin-add")
96+
"plugin-add")
9297
plugin_add_command $callback_args;;
9398

94-
"plugin-list")
99+
"plugin-list")
95100
plugin_list_command $callback_args;;
96101

97-
"plugin-list-all")
102+
"plugin-list-all")
98103
plugin_list_all_command $callback_args;;
99104

100-
"plugin-update")
105+
"plugin-update")
101106
plugin_update_command $callback_args;;
102107

103-
"plugin-remove")
108+
"plugin-remove")
104109
plugin_remove_command $callback_args;;
105110

106-
# Undocumented commands for development
107-
"plugin-push")
111+
# Undocumented commands for development
112+
"plugin-push")
108113
plugin_push_command $callback_args;;
109114

110-
"plugin-test")
115+
"plugin-test")
111116
plugin_test_command $callback_args;;
112117

113-
"shim-versions")
118+
"shim-versions")
114119
shim_versions_command $callback_args;;
115-
*)
116-
help_command
117-
exit 1;;
120+
*)
121+
help_command
122+
exit 1;;
118123
esac

bin/private/asdf-tool-exec

Lines changed: 0 additions & 69 deletions
This file was deleted.

help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ MANAGE PACKAGES
2525

2626

2727
UTILS
28+
asdf exec <executable> [args..] Run an executable command shim
2829
asdf reshim <name> <version> Recreate shims for version of a package
2930
asdf shim-versions <executable> List on which plugins and versions is an executable provided.
3031
asdf update Update asdf to the latest stable release

lib/commands/reshim.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ write_shim_script() {
7373
cat <<EOF > "$shim_path"
7474
#!/usr/bin/env bash
7575
# asdf-plugin: ${plugin_name} ${version}
76-
exec $(asdf_dir)/bin/private/asdf-tool-exec "${executable_name}" "\$@"
76+
exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@"
7777
EOF
7878
fi
7979

lib/commands/shim-exec.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
tool_versions() {
3+
env | awk -F= '/^ASDF_[A-Z]+_VERSION/ {print $1" "$2}' | sed -e "s/^ASDF_//" | sed -e "s/_VERSION / /" | tr "[:upper:]_" "[:lower:]-"
4+
local asdf_versions_path
5+
asdf_versions_path="$(find_tool_versions)"
6+
[ -f "${asdf_versions_path}" ] && cat "${asdf_versions_path}"
7+
}
8+
9+
shim_versions() {
10+
shim_plugin_versions "${shim_name}"
11+
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | awk '{print$1" system"}'
12+
}
13+
14+
select_from_tool_versions() {
15+
grep -f <(shim_versions) <(tool_versions) | head -n 1 | xargs echo
16+
}
17+
18+
preset_versions() {
19+
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)"
20+
}
21+
22+
select_from_preset_version() {
23+
grep -f <(shim_versions) <(preset_versions) | head -n 1 | xargs echo
24+
}
25+
26+
shim_exec_command() {
27+
local shim_name="$1"
28+
29+
selected_version=$(select_from_tool_versions)
30+
31+
if [ -z "$selected_version" ]; then
32+
selected_version=$(select_from_preset_version)
33+
fi
34+
35+
36+
if [ ! -z "$selected_version" ]; then
37+
plugin_name=$(cut -d ' ' -f 1 <<< "$selected_version");
38+
version=$(cut -d ' ' -f 2- <<< "$selected_version");
39+
plugin_path=$(get_plugin_path "$plugin_name")
40+
41+
plugin_exec_env "$plugin_name" "$version"
42+
executable_path=$(command -v "$shim_name")
43+
44+
if [ -x "${plugin_path}/bin/exec-path" ]; then
45+
install_path=$(find_install_path "$plugin_name" "$version")
46+
executable_path=$(get_custom_executable_path "${plugin_path}" "${install_path}" "${executable_path}")
47+
fi
48+
49+
asdf_run_hook "pre_${plugin_name}_${shim_name}" "${@:2}"
50+
pre_status=$?
51+
if [ "$pre_status" -eq 0 ]; then
52+
"$executable_path" "${@:2}"
53+
exit_status=$?
54+
fi
55+
if [ "${exit_status:-${pre_status}}" -eq 0 ]; then
56+
asdf_run_hook "post_${plugin_name}_${shim_name}" "${@:2}"
57+
post_status=$?
58+
fi
59+
exit "${post_status:-${exit_status:-${pre_status}}}"
60+
fi
61+
62+
(
63+
echo "asdf: No version set for command ${shim_name}"
64+
echo "you might want to add one of the following in your .tool-versions file:"
65+
echo ""
66+
shim_plugin_versions "${shim_name}"
67+
) >&2
68+
exit 126
69+
}

test/shim_exec.bats

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ teardown() {
2121
clean_asdf_dir
2222
}
2323

24+
@test "asdf exec should pass all arguments to executable" {
25+
echo "dummy 1.0" > $PROJECT_DIR/.tool-versions
26+
run install_command
27+
28+
run $ASDF_DIR/bin/asdf exec dummy world hello
29+
[ "$output" == "This is Dummy 1.0! hello world" ]
30+
[ "$status" -eq 0 ]
31+
}
32+
2433
@test "shim exec should pass all arguments to executable" {
2534
echo "dummy 1.0" > $PROJECT_DIR/.tool-versions
2635
run install_command

0 commit comments

Comments
 (0)