Skip to content

Commit

Permalink
bump asdf to 0.14.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesw committed Aug 21, 2024
1 parent 65e8e50 commit 06477bd
Show file tree
Hide file tree
Showing 27 changed files with 612 additions and 307 deletions.
173 changes: 140 additions & 33 deletions asdf/asdf.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,144 @@
# For Korn shells (ksh, mksh, etc.), capture $_ (the final parameter passed to
# the last command) straightaway, as it will contain the path to this script.
# For Bash, ${BASH_SOURCE[0]} will be used to obtain this script's path.
# For Zsh and others, $0 (the path to the shell or script) will be used.
_under="$_"
if [ -z "${ASDF_DIR:-}" ]; then
if [ -n "${BASH_SOURCE[0]}" ]; then
current_script_path="${BASH_SOURCE[0]}"
elif [[ "$_under" == *".sh" ]]; then
current_script_path="$_under"
# 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
if [ -n "${BASH_VERSION:-}" ]; then
# Use BASH_SOURCE[0] to obtain the relative path to this source'd file. Since it's
# a relative path, 'cd' to its dirname and use '$PWD' to obtain the fullpath.
# 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
elif [ -n "${ZSH_VERSION:-}" ]; then
# 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%/*}
elif [ -n "${KSH_VERSION:-}" ] && [ -z "$PATHSEP" ]; then
# 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%/*}
fi
fi

if [ -z "$ASDF_DIR" ]; then
printf "%s\n" "asdf: Error: Source directory could not be calculated. Please set \$ASDF_DIR manually before sourcing this file." >&2
return 1
fi

if [ ! -d "$ASDF_DIR" ]; then
printf "%s\n" "asdf: Error: Variable '\$ASDF_DIR' is not a directory: $ASDF_DIR" >&2
return 1
fi

_asdf_bin="$ASDF_DIR/bin"
_asdf_shims="${ASDF_DATA_DIR:-$HOME/.asdf}/shims"

_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`.
if [ -n "${BASH_VERSION:-}" ] || [ -n "${ZSH_VERSION:-}" ]; then
# shellcheck disable=SC3028
case $OSTYPE in
darwin*) _asdf_should_prepend=yes ;;
esac
else
current_script_path="$0"
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
if [ -n "${BASH_VERSION:-}" ] || [ -n "${ZSH_VERSION:-}" ]; then
# 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

ASDF_DIR="$(dirname "$current_script_path")"
_new_path="$_new_path${_new_path:+:}$_part"
done
PATH=$_new_path
unset -v _path _new_path _part
fi
fi
export ASDF_DIR
# shellcheck disable=SC2016
[ -d "$ASDF_DIR" ] || printf "%s\n" "$ASDF_DIR is not a directory"

# Add asdf to PATH
#
# if in $PATH, remove, regardless of if it is in the right place (at the front) or not.
# replace all occurrences - ${parameter//pattern/string}
ASDF_BIN="${ASDF_DIR}/bin"
ASDF_USER_SHIMS="${ASDF_DATA_DIR:-$HOME/.asdf}/shims"
[[ ":$PATH:" == *":${ASDF_BIN}:"* ]] && PATH="${PATH//$ASDF_BIN:/}"
[[ ":$PATH:" == *":${ASDF_USER_SHIMS}:"* ]] && PATH="${PATH//$ASDF_USER_SHIMS:/}"
# add to front of $PATH
PATH="${ASDF_BIN}:$PATH"
PATH="${ASDF_USER_SHIMS}:$PATH"

# shellcheck source=lib/asdf.sh
# Load the asdf wrapper function
. "${ASDF_DIR}/lib/asdf.sh"

unset _under current_script_path ASDF_BIN ASDF_USER_SHIMS
unset -v _asdf_should_prepend

case ":$PATH:" in
*":$_asdf_bin:"*) : ;;
*) PATH="$_asdf_bin:$PATH" ;;
esac
case ":$PATH:" in
*":$_asdf_shims:"*) : ;;
*) PATH="$_asdf_shims:$PATH" ;;
esac

unset -v _asdf_bin _asdf_shims

# 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

# 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
}
68 changes: 53 additions & 15 deletions asdf/bin/asdf
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env bash

set -o pipefail
if [[ "${ASDF_DEBUG}" == "1" ]]; then
set -x
fi

# shellcheck source=lib/utils.bash
. "$(dirname "$(dirname "$0")")/lib/utils.bash"

Expand All @@ -16,9 +21,9 @@ find_cmd() {
done

if [ -f "$cmd_dir/$cmd_name" ]; then
printf "%s %s\\n" "$cmd_dir/$cmd_name" "$((args_offset + 1))"
printf "%s %s\n" "$cmd_dir/$cmd_name" "$((args_offset + 1))"
elif [ -f "$cmd_dir/command.bash" ]; then
printf "%s %s\\n" "$cmd_dir/command.bash" 1
printf "%s %s\n" "$cmd_dir/command.bash" 1
fi
}

Expand All @@ -29,15 +34,15 @@ find_asdf_cmd() {
'exec' | 'current' | 'env' | 'global' | 'install' | 'latest' | 'local' | \
'reshim' | 'uninstall' | 'update' | 'where' | 'which' | \
'export-shell-version')
printf "%s %s\\n" "$asdf_cmd_dir/command-$1.bash" 2
printf "%s %s\n" "$asdf_cmd_dir/command-$1.bash" 2
;;

'' | '--help' | '-h' | 'help')
printf "%s %s\\n" "$asdf_cmd_dir/command-help.bash" 2
printf "%s %s\n" "$asdf_cmd_dir/command-help.bash" 2
;;

'--version' | 'version')
printf "%s %s\\n" "$asdf_cmd_dir/command-version.bash" 2
printf "%s %s\n" "$asdf_cmd_dir/command-version.bash" 2
;;

*)
Expand All @@ -48,37 +53,70 @@ find_asdf_cmd() {

find_plugin_cmd() {
local ASDF_CMD_FILE args_offset
if [ -d "$(get_plugin_path "$1")/bin" ]; then
IFS=' ' read -r ASDF_CMD_FILE args_offset <<<"$(find_cmd "$(get_plugin_path "$1")/lib/commands" "${@:2}")"
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"
fi
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"
fi
}

asdf_cmd() {
local ASDF_CMD_FILE args_offset

if [ "shell" == "$1" ]; then
if [ "shell" = "$1" ]; then
printf "Shell integration is not enabled. Please ensure you source asdf in your shell setup." >&2
exit 1
fi

IFS=' ' read -r ASDF_CMD_FILE args_offset <<<"$(find_asdf_cmd "$@")"
# Internal Variables
ASDF_DEFAULT_TOOL_VERSIONS_FILENAME=$(asdf_tool_versions_filename)
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

local result=
result="$(find_asdf_cmd "$@")"
ASDF_CMD_FILE=${result% *}
args_offset=${result##* }
if [ -z "$ASDF_CMD_FILE" ]; then
IFS=' ' read -r ASDF_CMD_FILE args_offset <<<"$(find_plugin_cmd "$@")"
result="$(find_plugin_cmd "$@")"
ASDF_CMD_FILE=${result% *}
args_offset=${result##* }
fi

if [ -x "$ASDF_CMD_FILE" ]; then
# 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

exec "$ASDF_CMD_FILE" "${@:${args_offset}}"
elif [ -f "$ASDF_CMD_FILE" ]; then
set -- "${@:${args_offset}}"
# shellcheck source=/dev/null
. "$ASDF_CMD_FILE"
else
local asdf_cmd_dir
asdf_cmd_dir="$(asdf_dir)/lib/commands"
printf "%s\\n" "Unknown command: \`asdf ${*}\`" >&2
printf "%s\n" "Unknown command: \`asdf ${*}\`" >&2
# shellcheck source=lib/commands/command-help.bash
. "$asdf_cmd_dir/command-help.bash" >&2
return 127
fi
Expand Down
4 changes: 3 additions & 1 deletion asdf/completions/_asdf
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ case "$subcmd" in
;;
(latest)
if (( CURRENT == 3 + IntermediateCount )); then
_asdf__installed_plugins
_alternative \
'all:all:(--all)' \
'asdf-available-plugins:Installed ASDF Plugins:_asdf__installed_plugins'
elif (( CURRENT == 4 + IntermediateCount )); then
local pkg="${words[3+IntermediateCount]}"
local query=${words[4+IntermediateCount]}
Expand Down
24 changes: 21 additions & 3 deletions asdf/completions/asdf.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
_asdf_list_shims() (
# this function runs in a subshell so shopt is scoped
shopt -s nullglob # globs that don't match should disappear
shopt -u failglob # globs that don't match shouldn't fail
for shim in "${ASDF_DATA_DIR:-$HOME/.asdf}"/shims/*; do
basename "$shim"
done
)

_asdf() {
local cur
cur=${COMP_WORDS[COMP_CWORD]}
Expand All @@ -19,7 +28,7 @@ _asdf() {
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$plugins --all" -- "$cur"))
;;
plugin-remove | current | list | list-all)
plugin-remove | current)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
;;
Expand All @@ -29,7 +38,7 @@ _asdf() {
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$available_plugins" -- "$cur"))
;;
install)
install | list | list-all | help)
if [[ "$plugins" == *"$prev"* ]]; then
local versions
versions=$(asdf list-all "$prev" 2>/dev/null)
Expand Down Expand Up @@ -67,8 +76,17 @@ _asdf() {
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
fi
;;
latest)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "--all" -- "$cur"))
;;
which | shim-versions)
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$(_asdf_list_shims)" -- "$cur"))
;;
plugin-list | plugin-list-all | info) ;;
*)
local cmds='current global help install list list-all local plugin-add plugin-list plugin-list-all plugin-remove plugin-update reshim shell uninstall update where which info'
local cmds='current global help install latest list list-all local plugin-add plugin-list plugin-list-all plugin-remove plugin-update reshim shim-versions shell uninstall update where which info'
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
;;
Expand Down
8 changes: 7 additions & 1 deletion asdf/defaults
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# enables the use of .ruby-version like files used by other version managers
# See the docs for explanations: https://asdf-vm.com/manage/configuration.html

legacy_version_file = no
use_release_candidates = no
always_keep_download = no
plugin_repository_last_check_duration = 60
disable_plugin_short_name_repository = no
concurrency = auto
Loading

0 comments on commit 06477bd

Please sign in to comment.