2023-03-20 22:06:57 -07:00
|
|
|
# shellcheck shell=sh
|
|
|
|
# shellcheck disable=SC1007
|
|
|
|
|
|
|
|
# This file is the entrypoint for all POSIX-compatible shells. If `ASDF_DIR` is
|
|
|
|
# not already set, this script is able to calculate it, but only if the shell is
|
|
|
|
# either Bash, Zsh, and Ksh. For other shells, `ASDF_DIR` must be manually set.
|
|
|
|
|
|
|
|
export ASDF_DIR="${ASDF_DIR:-}"
|
|
|
|
|
|
|
|
if [ -z "$ASDF_DIR" ]; then
|
2023-09-10 07:42:55 -07:00
|
|
|
if [ -n "${BASH_VERSION:-}" ]; then
|
2023-03-20 22:06:57 -07:00
|
|
|
# Use BASH_SOURCE[0] to obtain the relative path to this source'd file. Since it's
|
2023-05-28 06:06:23 -07:00
|
|
|
# a relative path, 'cd' to its dirname and use '$PWD' to obtain the fullpath.
|
2023-03-20 22:06:57 -07:00
|
|
|
# Use 'builtin cd' to ensure user-defined 'cd()' functions aren't called.
|
|
|
|
# Use variable '_asdf_old_dir' to avoid using subshells.
|
|
|
|
|
|
|
|
_asdf_old_dir=$PWD
|
|
|
|
# shellcheck disable=SC3028,SC3054
|
|
|
|
if ! CDPATH= builtin cd -- "${BASH_SOURCE[0]%/*}"; then
|
|
|
|
printf '%s\n' 'asdf: Error: Failed to cd' >&2
|
|
|
|
unset -v _asdf_old_dir
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
ASDF_DIR=$PWD
|
|
|
|
if ! CDPATH= builtin cd -- "$_asdf_old_dir"; then
|
|
|
|
printf '%s\n' 'asdf: Error: Failed to cd' >&2
|
|
|
|
unset -v _asdf_old_dir
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
unset -v _asdf_old_dir
|
2023-09-10 07:42:55 -07:00
|
|
|
elif [ -n "${ZSH_VERSION:-}" ]; then
|
2023-03-20 22:06:57 -07:00
|
|
|
# Use '%x' to expand to path of current file. It must be prefixed
|
|
|
|
# with '(%):-', so it expands in non-prompt-string contexts.
|
|
|
|
|
|
|
|
# shellcheck disable=SC2296
|
|
|
|
ASDF_DIR=${(%):-%x}
|
|
|
|
ASDF_DIR=${ASDF_DIR%/*}
|
2023-09-10 07:42:55 -07:00
|
|
|
elif [ -n "${KSH_VERSION:-}" ] && [ -z "$PATHSEP" ]; then
|
2023-03-20 22:06:57 -07:00
|
|
|
# Only the original KornShell (kornshell.com) has a '.sh.file' variable with the path
|
|
|
|
# of the current file. To prevent errors with other variations, such as the MirBSD
|
|
|
|
# Korn shell (mksh), test for 'PATHSEP' which is _not_ set on the original Korn Shell.
|
|
|
|
|
|
|
|
# shellcheck disable=SC2296
|
|
|
|
ASDF_DIR=${.sh.file}
|
|
|
|
ASDF_DIR=${ASDF_DIR%/*}
|
2021-11-02 15:47:43 -07:00
|
|
|
fi
|
2023-03-20 22:06:57 -07:00
|
|
|
fi
|
2015-06-16 17:44:27 -07:00
|
|
|
|
2023-03-20 22:06:57 -07:00
|
|
|
if [ -z "$ASDF_DIR" ]; then
|
2023-03-27 23:42:55 -07:00
|
|
|
printf "%s\n" "asdf: Error: Source directory could not be calculated. Please set \$ASDF_DIR manually before sourcing this file." >&2
|
2023-03-20 22:06:57 -07:00
|
|
|
return 1
|
2021-11-02 15:47:43 -07:00
|
|
|
fi
|
2023-03-20 22:06:57 -07:00
|
|
|
|
|
|
|
if [ ! -d "$ASDF_DIR" ]; then
|
|
|
|
printf "%s\n" "asdf: Error: Variable '\$ASDF_DIR' is not a directory: $ASDF_DIR" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-03-27 23:42:55 -07:00
|
|
|
_asdf_bin="$ASDF_DIR/bin"
|
2023-03-20 22:06:57 -07:00
|
|
|
_asdf_shims="${ASDF_DATA_DIR:-$HOME/.asdf}/shims"
|
|
|
|
|
2023-06-08 17:31:07 -07:00
|
|
|
_asdf_should_prepend=no
|
|
|
|
if [ -n "${ASDF_FORCE_PREPEND+x}" ]; then
|
|
|
|
_asdf_should_prepend=$ASDF_FORCE_PREPEND
|
|
|
|
else
|
|
|
|
# If ASDF_FORCE_PREPEND is not set, then prepend by default on macOS
|
|
|
|
# to workaround `path_helper`.
|
2023-09-10 07:42:55 -07:00
|
|
|
if [ -n "${BASH_VERSION:-}" ] || [ -n "${ZSH_VERSION:-}" ]; then
|
2023-06-08 17:31:07 -07:00
|
|
|
# shellcheck disable=SC3028
|
|
|
|
case $OSTYPE in
|
|
|
|
darwin*) _asdf_should_prepend=yes ;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
if ! _asdf_output=$(uname); then
|
|
|
|
printf "%s\n" "asdf: Error: Failed to execute 'uname'" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ "$_asdf_output" = 'Darwin' ]; then
|
|
|
|
_asdf_should_prepend=yes
|
|
|
|
fi
|
|
|
|
unset -v _asdf_output
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If prepending is enabled, remove any existing instances of asdf from PATH so
|
|
|
|
# the prepending done after is always at the frontmost part of the PATH.
|
|
|
|
if [ "$_asdf_should_prepend" = 'yes' ]; then
|
2023-09-10 07:42:55 -07:00
|
|
|
if [ -n "${BASH_VERSION:-}" ] || [ -n "${ZSH_VERSION:-}" ]; then
|
2023-06-08 17:31:07 -07:00
|
|
|
# shellcheck disable=SC3060
|
|
|
|
case ":$PATH:" in
|
|
|
|
*":${_asdf_bin}:"*) PATH=${PATH//$_asdf_bin:/} ;;
|
|
|
|
esac
|
|
|
|
# shellcheck disable=SC3060
|
|
|
|
case ":$PATH:" in
|
|
|
|
*":${_asdf_shims}:"*) PATH=${PATH//$_asdf_shims:/} ;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
_path=${PATH}:
|
|
|
|
_new_path=
|
|
|
|
while [ -n "$_path" ]; do
|
|
|
|
_part=${_path%%:*}
|
|
|
|
_path=${_path#*:}
|
|
|
|
|
|
|
|
if [ "$_part" = "$_asdf_bin" ] || [ "$_part" = "$_asdf_shims" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
_new_path="$_new_path${_new_path:+:}$_part"
|
|
|
|
done
|
|
|
|
PATH=$_new_path
|
|
|
|
unset -v _path _new_path _part
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
unset -v _asdf_should_prepend
|
|
|
|
|
2023-03-27 23:42:55 -07:00
|
|
|
case ":$PATH:" in
|
|
|
|
*":$_asdf_bin:"*) : ;;
|
|
|
|
*) PATH="$_asdf_bin:$PATH" ;;
|
|
|
|
esac
|
|
|
|
case ":$PATH:" in
|
|
|
|
*":$_asdf_shims:"*) : ;;
|
|
|
|
*) PATH="$_asdf_shims:$PATH" ;;
|
|
|
|
esac
|
2023-03-20 22:06:57 -07:00
|
|
|
|
|
|
|
unset -v _asdf_bin _asdf_shims
|
2015-12-05 03:14:37 -07:00
|
|
|
|
2023-03-31 20:22:24 -07:00
|
|
|
# The asdf function is a wrapper so we can export variables
|
|
|
|
asdf() {
|
|
|
|
case $1 in
|
|
|
|
"shell")
|
|
|
|
if ! shift; then
|
|
|
|
printf '%s\n' 'asdf: Error: Failed to shift' >&2
|
|
|
|
return 1
|
|
|
|
fi
|
2021-11-02 15:47:43 -07:00
|
|
|
|
2023-03-31 20:22:24 -07:00
|
|
|
# Invoke command that needs to export variables.
|
|
|
|
eval "$(asdf export-shell-version sh "$@")" # asdf_allow: eval
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# Forward other commands to asdf script.
|
|
|
|
command asdf "$@" # asdf_allow: ' asdf '
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|