2015-05-18 22:14:08 -07:00
|
|
|
#!/usr/bin/env bash
|
2024-01-10 06:10:30 -07:00
|
|
|
|
2023-09-17 04:52:39 -07:00
|
|
|
set -o pipefail
|
2024-01-10 06:10:30 -07:00
|
|
|
if [[ "${ASDF_DEBUG}" == "1" ]]; then
|
|
|
|
set -x
|
|
|
|
fi
|
2014-09-29 16:53:59 -07:00
|
|
|
|
2020-03-02 10:44:31 -07:00
|
|
|
# shellcheck source=lib/utils.bash
|
2021-05-21 09:03:06 -07:00
|
|
|
. "$(dirname "$(dirname "$0")")/lib/utils.bash"
|
2017-08-31 20:21:09 -07:00
|
|
|
|
2019-11-27 01:24:29 -07:00
|
|
|
find_cmd() {
|
|
|
|
local cmd_dir="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
local cmd_name
|
|
|
|
local args_offset="$#"
|
2020-09-21 15:27:52 -07:00
|
|
|
cmd_name="command-$(tr ' ' '-' <<<"${@:1:${args_offset}}").bash"
|
2019-11-27 01:24:29 -07:00
|
|
|
while [ ! -f "$cmd_dir/$cmd_name" ] && [ "$args_offset" -gt 0 ]; do
|
|
|
|
args_offset=$((args_offset - 1))
|
2020-09-21 15:27:52 -07:00
|
|
|
cmd_name="command-$(tr ' ' '-' <<<"${@:1:${args_offset}}").bash"
|
2019-01-20 01:13:20 -07:00
|
|
|
done
|
2017-08-31 20:21:09 -07:00
|
|
|
|
2019-11-27 01:24:29 -07:00
|
|
|
if [ -f "$cmd_dir/$cmd_name" ]; then
|
2023-03-26 15:31:06 -07:00
|
|
|
printf "%s %s\n" "$cmd_dir/$cmd_name" "$((args_offset + 1))"
|
2020-03-02 11:13:20 -07:00
|
|
|
elif [ -f "$cmd_dir/command.bash" ]; then
|
2023-03-26 15:31:06 -07:00
|
|
|
printf "%s %s\n" "$cmd_dir/command.bash" 1
|
2019-11-27 01:24:29 -07:00
|
|
|
fi
|
2019-11-25 11:21:37 -07:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:24:29 -07:00
|
|
|
find_asdf_cmd() {
|
|
|
|
local asdf_cmd_dir
|
|
|
|
asdf_cmd_dir="$(asdf_dir)/lib/commands"
|
|
|
|
case "$1" in
|
2020-06-29 16:16:35 -07:00
|
|
|
'exec' | 'current' | 'env' | 'global' | 'install' | 'latest' | 'local' | \
|
|
|
|
'reshim' | 'uninstall' | 'update' | 'where' | 'which' | \
|
|
|
|
'export-shell-version')
|
2023-03-26 15:31:06 -07:00
|
|
|
printf "%s %s\n" "$asdf_cmd_dir/command-$1.bash" 2
|
2020-06-29 16:16:35 -07:00
|
|
|
;;
|
|
|
|
|
|
|
|
'' | '--help' | '-h' | 'help')
|
2023-03-26 15:31:06 -07:00
|
|
|
printf "%s %s\n" "$asdf_cmd_dir/command-help.bash" 2
|
2020-06-29 16:16:35 -07:00
|
|
|
;;
|
|
|
|
|
|
|
|
'--version' | 'version')
|
2023-03-26 15:31:06 -07:00
|
|
|
printf "%s %s\n" "$asdf_cmd_dir/command-version.bash" 2
|
2020-06-29 16:16:35 -07:00
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
find_cmd "$asdf_cmd_dir" "$@"
|
|
|
|
;;
|
2019-11-27 01:24:29 -07:00
|
|
|
esac
|
|
|
|
}
|
2019-11-25 11:21:37 -07:00
|
|
|
|
2019-11-27 01:24:29 -07:00
|
|
|
find_plugin_cmd() {
|
|
|
|
local ASDF_CMD_FILE args_offset
|
2023-09-17 04:50:47 -07:00
|
|
|
local result=
|
|
|
|
result="$(find_cmd "$(get_plugin_path "$1")/lib/commands" "${@:2}")"
|
|
|
|
ASDF_CMD_FILE=${result% *}
|
|
|
|
args_offset=${result##* }
|
|
|
|
if [ -n "$ASDF_CMD_FILE" ]; then
|
|
|
|
args_offset=$((args_offset + 1)) # since the first argument is the plugin name
|
|
|
|
printf "%s %s\n" "$ASDF_CMD_FILE" "$args_offset"
|
2019-11-27 01:24:29 -07:00
|
|
|
fi
|
|
|
|
}
|
2019-11-25 01:24:46 -07:00
|
|
|
|
2019-11-27 01:24:29 -07:00
|
|
|
asdf_cmd() {
|
|
|
|
local ASDF_CMD_FILE args_offset
|
|
|
|
|
2023-03-26 15:31:06 -07:00
|
|
|
if [ "shell" = "$1" ]; then
|
2020-09-21 15:27:52 -07:00
|
|
|
printf "Shell integration is not enabled. Please ensure you source asdf in your shell setup." >&2
|
2019-11-27 01:24:29 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-03-26 17:24:16 -07:00
|
|
|
# Internal Variables
|
2023-04-16 21:47:09 -07:00
|
|
|
ASDF_DEFAULT_TOOL_VERSIONS_FILENAME=$(asdf_tool_versions_filename)
|
2023-03-26 17:24:16 -07:00
|
|
|
export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
|
|
|
|
|
|
|
|
ASDF_CONFIG_FILE=$(asdf_config_file)
|
|
|
|
export ASDF_CONFIG_FILE
|
|
|
|
|
|
|
|
ASDF_DATA_DIR=$(asdf_data_dir)
|
|
|
|
export ASDF_DATA_DIR
|
|
|
|
|
|
|
|
ASDF_DIR=$(asdf_dir)
|
|
|
|
export ASDF_DIR
|
|
|
|
|
2023-03-24 05:37:23 -07:00
|
|
|
local result=
|
|
|
|
result="$(find_asdf_cmd "$@")"
|
|
|
|
ASDF_CMD_FILE=${result% *}
|
|
|
|
args_offset=${result##* }
|
2019-11-27 01:24:29 -07:00
|
|
|
if [ -z "$ASDF_CMD_FILE" ]; then
|
2023-03-24 05:37:23 -07:00
|
|
|
result="$(find_plugin_cmd "$@")"
|
|
|
|
ASDF_CMD_FILE=${result% *}
|
|
|
|
args_offset=${result##* }
|
2019-11-27 01:24:29 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -x "$ASDF_CMD_FILE" ]; then
|
2023-09-10 07:36:39 -07:00
|
|
|
# When '$ASDF_CMD_FILE' is an executable, we are executing a command directly from a plugin.
|
|
|
|
# Example: https://github.com/asdf-community/asdf-nim/blob/397c14a7f04ad5b91963814afc2e9cc92366e1c5/lib/commands/command-install-deps.bash
|
|
|
|
# In those cases, the path to that command is always an absolute path. However, this codepath can also be activated if a user accidentally
|
|
|
|
# marks files in ./lib/commands/* as executable. This code detects when that happens and prints a useful warning message.
|
|
|
|
if [[ "$ASDF_CMD_FILE" == ./lib/commands/* ]]; then
|
|
|
|
printf '%s\n' "----------"
|
|
|
|
printf '%s\n' "asdf: Warning: You are executing an asdf command from \$ASDF_DIR, but we detected that some files have been"
|
|
|
|
printf '%s\n' " erroneously marked as executable. All files under '$ASDF_DIR/lib/commands' must NOT be marked"
|
|
|
|
printf '%s\n' " as executable. Otherwise, asdf will not be able to source its core files"
|
|
|
|
printf '%s\n' "----------"
|
|
|
|
fi >&2
|
|
|
|
|
2019-11-27 01:24:29 -07:00
|
|
|
exec "$ASDF_CMD_FILE" "${@:${args_offset}}"
|
|
|
|
elif [ -f "$ASDF_CMD_FILE" ]; then
|
|
|
|
set -- "${@:${args_offset}}"
|
2023-06-04 19:48:30 -07:00
|
|
|
# shellcheck source=/dev/null
|
2021-05-21 09:03:06 -07:00
|
|
|
. "$ASDF_CMD_FILE"
|
2019-11-27 01:24:29 -07:00
|
|
|
else
|
|
|
|
local asdf_cmd_dir
|
|
|
|
asdf_cmd_dir="$(asdf_dir)/lib/commands"
|
2023-03-26 15:31:06 -07:00
|
|
|
printf "%s\n" "Unknown command: \`asdf ${*}\`" >&2
|
2023-06-04 19:48:30 -07:00
|
|
|
# shellcheck source=lib/commands/command-help.bash
|
2021-05-21 09:03:06 -07:00
|
|
|
. "$asdf_cmd_dir/command-help.bash" >&2
|
2019-11-27 01:24:29 -07:00
|
|
|
return 127
|
|
|
|
fi
|
|
|
|
}
|
2019-11-25 01:24:46 -07:00
|
|
|
|
2019-11-27 01:24:29 -07:00
|
|
|
asdf_cmd "$@"
|