From 06477bd7ed604ad7b1423835f0ae2827e2428123 Mon Sep 17 00:00:00 2001 From: Giles Westwood Date: Wed, 21 Aug 2024 11:34:30 +0100 Subject: [PATCH] bump asdf to 0.14.1 --- asdf/asdf.sh | 173 ++++++++++--- asdf/bin/asdf | 68 +++-- asdf/completions/_asdf | 4 +- asdf/completions/asdf.bash | 24 +- asdf/defaults | 8 +- asdf/help.txt | 35 +-- asdf/lib/commands/command-current.bash | 10 +- asdf/lib/commands/command-env.bash | 2 +- asdf/lib/commands/command-exec.bash | 4 +- .../command-export-shell-version.bash | 20 +- asdf/lib/commands/command-help.bash | 8 +- asdf/lib/commands/command-info.bash | 15 +- asdf/lib/commands/command-list.bash | 15 +- .../lib/commands/command-plugin-list-all.bash | 6 +- asdf/lib/commands/command-plugin-push.bash | 4 +- asdf/lib/commands/command-plugin-remove.bash | 8 +- asdf/lib/commands/command-plugin-test.bash | 55 ++-- asdf/lib/commands/command-update.bash | 6 +- asdf/lib/commands/command-where.bash | 6 +- asdf/lib/commands/command-which.bash | 6 +- asdf/lib/commands/reshim.bash | 49 ++-- asdf/lib/functions/installs.bash | 57 +++-- asdf/lib/functions/plugins.bash | 31 +-- asdf/lib/functions/versions.bash | 47 ++-- asdf/lib/utils.bash | 241 +++++++++++------- asdf/version.txt | 2 +- asdf_sync.sh | 15 ++ 27 files changed, 612 insertions(+), 307 deletions(-) mode change 100644 => 100755 asdf/completions/_asdf create mode 100755 asdf_sync.sh diff --git a/asdf/asdf.sh b/asdf/asdf.sh index 7a01f5e..0fd6799 100644 --- a/asdf/asdf.sh +++ b/asdf/asdf.sh @@ -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 +} diff --git a/asdf/bin/asdf b/asdf/bin/asdf index 0693133..439fd18 100755 --- a/asdf/bin/asdf +++ b/asdf/bin/asdf @@ -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" @@ -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 } @@ -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 ;; *) @@ -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 diff --git a/asdf/completions/_asdf b/asdf/completions/_asdf old mode 100644 new mode 100755 index ed67708..f657335 --- a/asdf/completions/_asdf +++ b/asdf/completions/_asdf @@ -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]} diff --git a/asdf/completions/asdf.bash b/asdf/completions/asdf.bash index d044882..f828e9d 100644 --- a/asdf/completions/asdf.bash +++ b/asdf/completions/asdf.bash @@ -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]} @@ -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")) ;; @@ -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) @@ -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")) ;; diff --git a/asdf/defaults b/asdf/defaults index 0b0964f..abd3428 100644 --- a/asdf/defaults +++ b/asdf/defaults @@ -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 diff --git a/asdf/help.txt b/asdf/help.txt index f2158e8..2435d15 100644 --- a/asdf/help.txt +++ b/asdf/help.txt @@ -14,6 +14,14 @@ asdf plugin update --all Update all plugins to latest commit on MANAGE PACKAGES +asdf current Display current version set or being + used for all packages +asdf current Display current version set or being + used for package +asdf global Set the package global version +asdf global latest[:] Set the package global version to the + latest provided version +asdf help [] Output documentation for plugin and tool asdf install Install all the package versions listed in the .tool-versions file asdf install Install one tool at the version @@ -23,22 +31,6 @@ asdf install latest[:] Install the latest stable version of a package, or with optional version, install the latest stable version that begins with the given string -asdf uninstall Remove a specific version of a package -asdf current Display current version set or being - used for all packages -asdf current Display current version set or being - used for package -asdf where [] Display install path for an installed - or current version -asdf which Display the path to an executable -asdf local Set the package local version -asdf local latest[:] Set the package local version to the - latest provided version -asdf global Set the package global version -asdf global latest[:] Set the package global version to the - latest provided version -asdf shell Set the package version to - `ASDF_${LANG}_VERSION` in the current shell asdf latest [] Show latest stable version of a package asdf latest --all Show latest stable version of all the packages and if they are installed @@ -46,7 +38,15 @@ asdf list [version] List installed versions of a package and optionally filter the versions asdf list all [] List all versions of a package and optionally filter the returned versions -asdf help [] Output documentation for plugin and tool +asdf local Set the package local version +asdf local latest[:] Set the package local version to the + latest provided version +asdf shell Set the package version to + `ASDF_${LANG}_VERSION` in the current shell +asdf uninstall Remove a specific version of a package +asdf where [] Display install path for an installed + or current version +asdf which Display the path to an executable UTILS @@ -54,6 +54,7 @@ asdf exec [args...] Executes the command shim for current ve asdf env [util] Runs util (default: `env`) inside the environment used for command shim execution. asdf info Print OS, Shell and ASDF debug information. +asdf version Print the currently installed version of ASDF asdf reshim Recreate shims for version of a package asdf shim-versions List the plugins and versions that provide a command diff --git a/asdf/lib/commands/command-current.bash b/asdf/lib/commands/command-current.bash index e15bf6e..f472462 100644 --- a/asdf/lib/commands/command-current.bash +++ b/asdf/lib/commands/command-current.bash @@ -10,7 +10,7 @@ plugin_current_command() { check_if_plugin_exists "$plugin_name" local search_path - search_path=$(pwd) + search_path=$PWD local version_and_path version_and_path=$(find_versions "$plugin_name" "$search_path") local full_version @@ -44,11 +44,11 @@ plugin_current_command() { # shellcheck disable=SC2059 current_command() { - local terminal_format="%-15s %-15s %-10s\\n" + local terminal_format="%-15s %-15s %-10s\n" local exit_status=0 local plugin - # printf "$terminal_format" "PLUGIN" "VERSION" "SET BY CONFIG" # disbale this until we release headings across the board + # printf "$terminal_format" "PLUGIN" "VERSION" "SET BY CONFIG" # disable this until we release headings across the board if [ $# -eq 0 ]; then # shellcheck disable=SC2119 for plugin in $(plugin_list_command); do @@ -75,8 +75,8 @@ check_for_deprecated_plugin() { local new_script="${plugin_path}/bin/list-legacy-filenames" if [ "$legacy_config" = "yes" ] && [ -f "$deprecated_script" ] && [ ! -f "$new_script" ]; then - printf "Heads up! It looks like your %s plugin is out of date. You can update it with:\\n\\n" "$plugin_name" - printf " asdf plugin-update %s\\n\\n" "$plugin_name" + printf "Heads up! It looks like your %s plugin is out of date. You can update it with:\n\n" "$plugin_name" + printf " asdf plugin-update %s\n\n" "$plugin_name" fi } diff --git a/asdf/lib/commands/command-env.bash b/asdf/lib/commands/command-env.bash index e694e7b..30cf7ec 100644 --- a/asdf/lib/commands/command-env.bash +++ b/asdf/lib/commands/command-env.bash @@ -6,7 +6,7 @@ shim_env_command() { local env_args=("${@:3}") if [ -z "$shim_name" ]; then - printf "usage: asdf env \\n" + printf "usage: asdf env \n" exit 1 fi diff --git a/asdf/lib/commands/command-exec.bash b/asdf/lib/commands/command-exec.bash index 427922c..cb56b36 100644 --- a/asdf/lib/commands/command-exec.bash +++ b/asdf/lib/commands/command-exec.bash @@ -6,7 +6,7 @@ shim_exec_command() { local shim_args=("${@:2}") if [ -z "$shim_name" ]; then - printf "usage: asdf exec \\n" + printf "usage: asdf exec \n" exit 1 fi @@ -16,7 +16,7 @@ shim_exec_command() { local executable_path="$3" if [ ! -x "$executable_path" ]; then - printf "No %s executable found for %s %s\\n" "$shim_name" "$plugin_name" "$version" >&2 + printf "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version" >&2 exit 2 fi diff --git a/asdf/lib/commands/command-export-shell-version.bash b/asdf/lib/commands/command-export-shell-version.bash index a398a00..92ace8a 100644 --- a/asdf/lib/commands/command-export-shell-version.bash +++ b/asdf/lib/commands/command-export-shell-version.bash @@ -8,8 +8,8 @@ shell_command() { shift if [ "$#" -lt "2" ]; then - printf "Usage: asdf shell {|--unset}\\n" >&2 - printf "false\\n" + printf "Usage: asdf shell {|--unset}\n" >&2 + printf "false\n" exit 1 fi @@ -23,7 +23,7 @@ shell_command() { if [ "$version" = "--unset" ]; then case "$asdf_shell" in fish) - printf "set -e %s\\n" "$version_env_var" + printf "set -e %s\n" "$version_env_var" ;; elvish) # Elvish doesn't have a `source` command, and eval is banned, so the @@ -31,8 +31,11 @@ shell_command() { # and pass to unset-env. printf "unset-env\n%s" "$version_env_var" ;; + pwsh) + printf '%s\n' "if (\$(Test-Path Env:$version_env_var) -eq 'True') { Remove-Item Env:$version_env_var }" + ;; *) - printf "unset %s\\n" "$version_env_var" + printf "unset %s\n" "$version_env_var" ;; esac exit 0 @@ -42,13 +45,13 @@ shell_command() { fi if ! (check_if_version_exists "$plugin" "$version"); then version_not_installed_text "$plugin" "$version" 1>&2 - printf "false\\n" + printf "false\n" exit 1 fi case "$asdf_shell" in fish) - printf "set -gx %s \"%s\"\\n" "$version_env_var" "$version" + printf "set -gx %s \"%s\"\n" "$version_env_var" "$version" ;; elvish) # Elvish doesn't have a `source` command, and eval is banned, so the @@ -56,8 +59,11 @@ shell_command() { # and pass to set-env. printf "set-env\n%s\n%s" "$version_env_var" "$version" ;; + pwsh) + printf '%s\n' "\$Env:$version_env_var = '$version'" + ;; *) - printf "export %s=\"%s\"\\n" "$version_env_var" "$version" + printf "export %s=\"%s\"\n" "$version_env_var" "$version" ;; esac } diff --git a/asdf/lib/commands/command-help.bash b/asdf/lib/commands/command-help.bash index eba6c99..77f1bb2 100644 --- a/asdf/lib/commands/command-help.bash +++ b/asdf/lib/commands/command-help.bash @@ -3,7 +3,7 @@ . "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" asdf_help() { - printf "version: %s\\n\\n" "$(asdf_version)" + printf "version: %s\n\n" "$(asdf_version)" cat "$(asdf_dir)/help.txt" } @@ -23,7 +23,7 @@ asdf_extension_cmds() { ext_cmd_path="$plugin_path/lib/commands" ext_cmds="$(find "$ext_cmd_path" -name "command*.bash" 2>/dev/null)" if [[ -n $ext_cmds ]]; then - printf "\\nPLUGIN %s\\n" "$plugin" + printf "\nPLUGIN %s\n" "$plugin" for ext_cmd in $ext_cmds; do ext_cmd_name="$(basename "$ext_cmd")" sed "s/-/ /g;s/.bash//;s/command-*/ asdf $plugin/;" <<<"$ext_cmd_name" @@ -78,11 +78,11 @@ help_command() { (print_plugin_help "$plugin_path") fi else - printf "No documentation for plugin %s\\n" "$plugin_name" >&2 + printf "No documentation for plugin %s\n" "$plugin_name" >&2 exit 1 fi else - printf "No plugin named %s\\n" "$plugin_name" >&2 + printf "No plugin named %s\n" "$plugin_name" >&2 exit 1 fi else diff --git a/asdf/lib/commands/command-info.bash b/asdf/lib/commands/command-info.bash index 2ab6ffb..d4e761c 100644 --- a/asdf/lib/commands/command-info.bash +++ b/asdf/lib/commands/command-info.bash @@ -3,11 +3,16 @@ . "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" info_command() { - printf "%s:\\n%s\\n\\n" "OS" "$(uname -a)" - printf "%s:\\n%s\\n\\n" "SHELL" "$($SHELL --version)" - printf "%s:\\n%s\\n\\n" "ASDF VERSION" "$(asdf_version)" - printf "%s:\\n%s\\n\\n" "ASDF ENVIRONMENT VARIABLES" "$(env | grep -E "ASDF_DIR|ASDF_DATA_DIR|ASDF_CONFIG_FILE|ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" - printf "%s:\\n%s\\n\\n" "ASDF INSTALLED PLUGINS" "$(plugin_list_command --urls --refs)" + printf "%s:\n%s\n\n" "OS" "$(uname -a)" + printf "%s:\n%s\n\n" "SHELL" "$("$SHELL" --version)" + printf "%s:\n%s\n\n" "BASH VERSION" "$BASH_VERSION" + printf "%s:\n%s\n\n" "ASDF VERSION" "$(asdf_version)" + printf '%s\n' 'ASDF INTERNAL VARIABLES:' + printf 'ASDF_DEFAULT_TOOL_VERSIONS_FILENAME=%s\n' "${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME}" + printf 'ASDF_DATA_DIR=%s\n' "${ASDF_DATA_DIR}" + printf 'ASDF_DIR=%s\n' "${ASDF_DIR}" + printf 'ASDF_CONFIG_FILE=%s\n\n' "${ASDF_CONFIG_FILE}" + printf "%s:\n%s\n\n" "ASDF INSTALLED PLUGINS" "$(plugin_list_command --urls --refs)" } info_command "$@" diff --git a/asdf/lib/commands/command-list.bash b/asdf/lib/commands/command-list.bash index 85d7d21..6cb4cd4 100644 --- a/asdf/lib/commands/command-list.bash +++ b/asdf/lib/commands/command-list.bash @@ -11,11 +11,11 @@ list_command() { if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then for plugin_path in "$plugins_path"/*/; do plugin_name=$(basename "$plugin_path") - printf "%s\\n" "$plugin_name" + printf "%s\n" "$plugin_name" display_installed_versions "$plugin_name" "$query" done else - printf "%s\\n" 'No plugins installed' + printf "%s\n" 'No plugins installed' fi else check_if_plugin_exists "$plugin_name" @@ -27,6 +27,9 @@ display_installed_versions() { local plugin_name=$1 local query=$2 local versions + local current_version + local flag + versions=$(list_installed_versions "$plugin_name") if [[ $query ]]; then @@ -39,8 +42,14 @@ display_installed_versions() { fi if [ -n "${versions}" ]; then + current_version=$(cut -d '|' -f 1 <<<"$(find_versions "$plugin_name" "$PWD")") + for version in $versions; do - printf " %s\\n" "$version" + flag=" " + if [[ "$version" == "$current_version" ]]; then + flag=" *" + fi + printf "%s%s\n" "$flag" "$version" done else display_error ' No versions installed' diff --git a/asdf/lib/commands/command-plugin-list-all.bash b/asdf/lib/commands/command-plugin-list-all.bash index 3c7c3f6..232b69c 100644 --- a/asdf/lib/commands/command-plugin-list-all.bash +++ b/asdf/lib/commands/command-plugin-list-all.bash @@ -1,7 +1,7 @@ # -*- sh -*- plugin_list_all_command() { - initialize_or_update_repository + initialize_or_update_plugin_repository local plugins_index_path plugins_index_path="$(asdf_data_dir)/repository/plugins" @@ -18,11 +18,11 @@ plugin_list_all_command() { [[ -d "${plugins_local_path}/${index_plugin_name}" ]] && installed_flag='*' - printf "%s\\t%s\\n" "$index_plugin_name" "$installed_flag$source_url" + printf "%s\t%s\n" "$index_plugin_name" "$installed_flag$source_url" done ) | awk '{ printf("%-28s", $1); sub(/^[^*]/, " &", $2); $1=""; print $0 }' else - printf "%s%s\\n" "error: index of plugins not found at " "$plugins_index_path" + printf "%s%s\n" "error: index of plugins not found at " "$plugins_index_path" fi } diff --git a/asdf/lib/commands/command-plugin-push.bash b/asdf/lib/commands/command-plugin-push.bash index ce81486..e09a222 100644 --- a/asdf/lib/commands/command-plugin-push.bash +++ b/asdf/lib/commands/command-plugin-push.bash @@ -4,14 +4,14 @@ plugin_push_command() { local plugin_name=$1 if [ "$plugin_name" = "--all" ]; then for dir in "$(asdf_data_dir)"/plugins/*/; do - printf "Pushing %s...\\n" "$(basename "$dir")" + printf "Pushing %s...\n" "$(basename "$dir")" (cd "$dir" && git push) done else local plugin_path plugin_path=$(get_plugin_path "$plugin_name") check_if_plugin_exists "$plugin_name" - printf "Pushing %s...\\n" "$plugin_name" + printf "Pushing %s...\n" "$plugin_name" (cd "$plugin_path" && git push) fi } diff --git a/asdf/lib/commands/command-plugin-remove.bash b/asdf/lib/commands/command-plugin-remove.bash index e4cee05..7b959bd 100644 --- a/asdf/lib/commands/command-plugin-remove.bash +++ b/asdf/lib/commands/command-plugin-remove.bash @@ -21,7 +21,13 @@ plugin_remove_command() { rm -rf "$(asdf_data_dir)/installs/${plugin_name}" rm -rf "$(asdf_data_dir)/downloads/${plugin_name}" - grep -l "asdf-plugin: ${plugin_name}" "$(asdf_data_dir)"/shims/* 2>/dev/null | xargs rm -f + for f in "$(asdf_data_dir)"/shims/*; do + if [ -f "$f" ]; then # nullglob may not be set + if grep -q "asdf-plugin: ${plugin_name}" "$f"; then + rm -f "$f" + fi + fi + done asdf_run_hook "post_asdf_plugin_remove" "$plugin_name" asdf_run_hook "post_asdf_plugin_remove_${plugin_name}" diff --git a/asdf/lib/commands/command-plugin-test.bash b/asdf/lib/commands/command-plugin-test.bash index e253fea..0cfa680 100644 --- a/asdf/lib/commands/command-plugin-test.bash +++ b/asdf/lib/commands/command-plugin-test.bash @@ -14,7 +14,20 @@ plugin_test_command() { local plugin_url="$2" shift 2 - local plugin_gitref="master" + local exit_code + local TEST_DIR + + fail_test() { + printf "FAILED: %s\n" "$1" + rm -rf "$TEST_DIR" + exit 1 + } + + if [ -z "$plugin_name" ] || [ -z "$plugin_url" ]; then + fail_test "please provide a plugin name and url" + fi + + local plugin_gitref local tool_version local interpret_args_literally local skip_next_arg @@ -45,21 +58,13 @@ plugin_test_command() { fi done - if [ "$#" -eq 1 ]; then - set -- "${SHELL:-sh}" -c "$1" + if [ -z "$plugin_gitref" ]; then + plugin_remote_default_branch=$(git ls-remote --symref "$plugin_url" HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }') + plugin_gitref=${3:-${plugin_remote_default_branch}} fi - local exit_code - local TEST_DIR - - fail_test() { - printf "FAILED: %s\\n" "$1" - rm -rf "$TEST_DIR" - exit 1 - } - - if [ -z "$plugin_name" ] || [ -z "$plugin_url" ]; then - fail_test "please provide a plugin name and url" + if [ "$#" -eq 1 ]; then + set -- "${SHELL:-sh}" -c "$1" fi TEST_DIR=$(mktemp -dt asdf.XXXX) @@ -79,7 +84,7 @@ plugin_test_command() { fi # shellcheck disable=SC2119 - if ! (plugin_list_command | grep "^$plugin_name$" >/dev/null); then + if ! (plugin_list_command | grep -q "^$plugin_name$"); then fail_test "$plugin_name was not properly installed" fi @@ -90,22 +95,22 @@ plugin_test_command() { local plugin_path plugin_path=$(get_plugin_path "$plugin_name") local list_all="$plugin_path/bin/list-all" - if grep api.github.com "$list_all" >/dev/null; then - if ! grep Authorization "$list_all" >/dev/null; then - printf "\\nLooks like %s/bin/list-all relies on GitHub releases\\n" "$plugin_name" - printf "but it does not properly sets an Authorization header to prevent\\n" - printf "GitHub API rate limiting.\\n\\n" - printf "See https://github.com/asdf-vm/asdf/blob/master/docs/creating-plugins.md#github-api-rate-limiting\\n" + if grep -q api.github.com "$list_all"; then + if ! grep -q Authorization "$list_all"; then + printf "\nLooks like %s/bin/list-all relies on GitHub releases\n" "$plugin_name" + printf "but it does not properly sets an Authorization header to prevent\n" + printf "GitHub API rate limiting.\n\n" + printf "See https://github.com/asdf-vm/asdf/blob/master/docs/creating-plugins.md#github-api-rate-limiting\n" fail_test "$plugin_name/bin/list-all does not set GitHub Authorization token" fi # test for most common token names we have on plugins. If both are empty show this warning if [ -z "$OAUTH_TOKEN" ] && [ -z "$GITHUB_API_TOKEN" ]; then - printf "%s/bin/list-all is using GitHub API, just be sure you provide an API Authorization token\\n" "$plugin_name" - printf "via your CI env GITHUB_API_TOKEN. This is the current rate_limit:\\n\\n" + printf "%s/bin/list-all is using GitHub API, just be sure you provide an API Authorization token\n" "$plugin_name" + printf "via your CI env GITHUB_API_TOKEN. This is the current rate_limit:\n\n" curl -s https://api.github.com/rate_limit - printf "\\n" + printf "\n" fi fi @@ -150,7 +155,7 @@ plugin_test_command() { "$@" exit_code=$? if [ $exit_code != 0 ]; then - fail_test "$* failed with exit code $?" + fail_test "$* failed with exit code $exit_code" fi fi diff --git a/asdf/lib/commands/command-update.bash b/asdf/lib/commands/command-update.bash index 824e4da..f991264 100644 --- a/asdf/lib/commands/command-update.bash +++ b/asdf/lib/commands/command-update.bash @@ -7,7 +7,7 @@ update_command() { cd "$(asdf_dir)" || exit 1 if [ -f asdf_updates_disabled ] || ! git rev-parse --is-inside-work-tree &>/dev/null; then - printf "Update command disabled. Please use the package manager that you used to install asdf to upgrade asdf.\\n" + printf "Update command disabled. Please use the package manager that you used to install asdf to upgrade asdf.\n" exit 42 else do_update "$update_to_head" @@ -23,7 +23,7 @@ do_update() { git fetch origin master git checkout master git reset --hard origin/master - printf "Updated asdf to latest on the master branch\\n" + printf "Updated asdf to latest on the master branch\n" else # Update to latest release git fetch origin --tags || exit 1 @@ -38,7 +38,7 @@ do_update() { # Update git checkout "$tag" || exit 1 - printf "Updated asdf to release %s\\n" "$tag" + printf "Updated asdf to release %s\n" "$tag" fi } diff --git a/asdf/lib/commands/command-where.bash b/asdf/lib/commands/command-where.bash index 2ed7f0b..d2b9d03 100644 --- a/asdf/lib/commands/command-where.bash +++ b/asdf/lib/commands/command-where.bash @@ -34,14 +34,14 @@ where_command() { install_path=$(get_install_path "$plugin_name" "$install_type" "$version") if [ -d "$install_path" ]; then - printf "%s\\n" "$install_path" + printf "%s\n" "$install_path" exit 0 else if [ "$version" = "system" ]; then - printf "System version is selected\\n" + printf "System version is selected\n" exit 1 else - printf "Version not installed\\n" + printf "Version not installed\n" exit 1 fi fi diff --git a/asdf/lib/commands/command-which.bash b/asdf/lib/commands/command-which.bash index 7704ffc..8696dbf 100644 --- a/asdf/lib/commands/command-which.bash +++ b/asdf/lib/commands/command-which.bash @@ -5,7 +5,7 @@ which_command() { shim_name=$(basename "$1") if [ -z "$shim_name" ]; then - printf "usage: asdf which \\n" + printf "usage: asdf which \n" exit 1 fi @@ -15,11 +15,11 @@ which_command() { local executable_path="$3" if [ ! -x "$executable_path" ]; then - printf "No %s executable found for %s %s\\n" "$shim_name" "$plugin_name" "$version" >&2 + printf "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version" >&2 exit 1 fi - printf "%s\\n" "$executable_path" + printf "%s\n" "$executable_path" exit 0 } diff --git a/asdf/lib/commands/reshim.bash b/asdf/lib/commands/reshim.bash index 2864bd2..2317973 100644 --- a/asdf/lib/commands/reshim.bash +++ b/asdf/lib/commands/reshim.bash @@ -11,14 +11,14 @@ remove_shim_for_version() { local count_installed count_installed=$(list_installed_versions "$plugin_name" | wc -l) - if ! grep -x "# asdf-plugin: $plugin_name $version" "$shim_path" >/dev/null 2>&1; then + if ! grep -x "# asdf-plugin: $plugin_name $version" "$shim_path" &>/dev/null; then return 0 fi sed -i.bak -e "/# asdf-plugin: $plugin_name $version"'$/d' "$shim_path" rm "$shim_path".bak - if ! grep "# asdf-plugin:" "$shim_path" >/dev/null || + if ! grep -q "# asdf-plugin:" "$shim_path" || [ "$count_installed" -eq 0 ]; then rm -f "$shim_path" fi @@ -88,37 +88,28 @@ write_shim_script() { local shim_path shim_path="$(asdf_data_dir)/shims/$executable_name" - if [ -f "$shim_path" ]; then - if ! grep -x "# asdf-plugin: ${plugin_name} ${version}" "$shim_path" >/dev/null; then - sed -i.bak -e "s/exec /# asdf-plugin: ${plugin_name} ${version}\\"$'\n''exec /' "$shim_path" - rm -f "$shim_path".bak - fi - else - cat <"$shim_path" -#!/usr/bin/env bash + local temp_dir + temp_dir=${TMPDIR:-/tmp} + + local temp_versions_path + temp_versions_path=$(mktemp "$temp_dir/asdf-command-reshim-write-shims.XXXXXX") + cat <"$temp_versions_path" # asdf-plugin: ${plugin_name} ${version} -exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf ' EOF - fi - - chmod +x "$shim_path" -} -generate_shim_for_executable() { - local plugin_name=$1 - local executable=$2 + if [ -f "$shim_path" ]; then + grep '^#\sasdf-plugin:\s' <"$shim_path" >>"$temp_versions_path" + fi - check_if_plugin_exists "$plugin_name" + cat <"$shim_path" +#!/usr/bin/env bash +$(sort -u <"$temp_versions_path") +exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf ' +EOF - local version - IFS=':' read -r -a version_info <<<"$full_version" - if [ "${version_info[0]}" = "ref" ]; then - version="${version_info[1]}" - else - version="${version_info[0]}" - fi + rm "$temp_versions_path" - write_shim_script "$plugin_name" "$version" "$executable" + chmod +x "$shim_path" } generate_shims_for_version() { @@ -151,10 +142,10 @@ remove_obsolete_shims() { # comm only takes to files, so we write this data to temp files so we can # pass it to comm. formatted_shims="$(mktemp "$temp_dir/asdf-command-reshim-formatted-shims.XXXXXX")" - printf "%s\\n" "$shims" >"$formatted_shims" + printf "%s\n" "$shims" >"$formatted_shims" formatted_exec_names="$(mktemp "$temp_dir/asdf-command-reshim-formatted-exec-names.XXXXXX")" - printf "%s\\n" "$exec_names" >"$formatted_exec_names" + printf "%s\n" "$exec_names" >"$formatted_exec_names" obsolete_shims=$(comm -23 "$formatted_shims" "$formatted_exec_names") rm -f "$formatted_exec_names" "$formatted_shims" diff --git a/asdf/lib/functions/installs.bash b/asdf/lib/functions/installs.bash index 6a1ab25..1b440d2 100644 --- a/asdf/lib/functions/installs.bash +++ b/asdf/lib/functions/installs.bash @@ -6,7 +6,7 @@ handle_failure() { handle_cancel() { local install_path="$1" - printf "\\nreceived sigint, cleaning up" + printf "\nreceived sigint, cleaning up" handle_failure "$install_path" } @@ -25,21 +25,33 @@ install_command() { } get_concurrency() { - if command -v nproc >/dev/null 2>&1; then - nproc - elif command -v sysctl >/dev/null 2>&1 && sysctl hw.ncpu >/dev/null 2>&1; then - sysctl -n hw.ncpu - elif [ -f /proc/cpuinfo ]; then - grep -c processor /proc/cpuinfo + local asdf_concurrency= + + if [ -n "$ASDF_CONCURRENCY" ]; then + asdf_concurrency="$ASDF_CONCURRENCY" else - printf "1\\n" + asdf_concurrency=$(get_asdf_config_value 'concurrency') + fi + + if [ "$asdf_concurrency" = 'auto' ]; then + if command -v nproc &>/dev/null; then + asdf_concurrency=$(nproc) + elif command -v sysctl &>/dev/null && sysctl hw.ncpu &>/dev/null; then + asdf_concurrency=$(sysctl -n hw.ncpu) + elif [ -f /proc/cpuinfo ]; then + asdf_concurrency=$(grep -c processor /proc/cpuinfo) + else + asdf_concurrency="1" + fi fi + + printf "%s\n" "$asdf_concurrency" } install_one_local_tool() { local plugin_name=$1 local search_path - search_path=$(pwd) + search_path=$PWD local plugin_versions @@ -50,13 +62,12 @@ install_one_local_tool() { if [ -n "$plugin_version_and_path" ]; then local plugin_version - some_tools_installed='yes' plugin_versions=$(cut -d '|' -f 1 <<<"$plugin_version_and_path") for plugin_version in $plugin_versions; do install_tool_version "$plugin_name" "$plugin_version" done else - printf "No versions specified for %s in config files or environment\\n" "$plugin_name" + printf "No versions specified for %s in config files or environment\n" "$plugin_name" exit 1 fi } @@ -66,7 +77,7 @@ install_local_tool_versions() { plugins_path=$(get_plugin_path) local search_path - search_path=$(pwd) + search_path=$PWD local some_tools_installed local some_plugin_not_installed @@ -86,7 +97,7 @@ install_local_tool_versions() { fi if [ -z "$plugins_installed" ]; then - printf "Install plugins first to be able to install tools\\n" + printf "Install plugins first to be able to install tools\n" exit 1 fi @@ -123,9 +134,9 @@ install_local_tool_versions() { fi if [ -z "$some_tools_installed" ]; then - printf "Either specify a tool & version in the command\\n" - printf "OR add .tool-versions file in this directory\\n" - printf "or in a parent directory\\n" + printf "Either specify a tool & version in the command\n" + printf "OR add .tool-versions file in this directory\n" + printf "or in a parent directory\n" exit 1 fi } @@ -181,7 +192,7 @@ install_tool_version() { trap 'handle_cancel $install_path' INT if [ -d "$install_path" ]; then - printf "%s %s is already installed\\n" "$plugin_name" "$full_version" + printf "%s %s is already installed\n" "$plugin_name" "$full_version" else if [ -f "${plugin_path}/bin/download" ]; then @@ -196,7 +207,7 @@ install_tool_version() { export ASDF_INSTALL_PATH=$install_path # shellcheck disable=SC2030 export ASDF_DOWNLOAD_PATH=$download_path - mkdir "$download_path" + mkdir -p "$download_path" asdf_run_hook "pre_asdf_download_${plugin_name}" "$full_version" "${plugin_path}"/bin/download ) @@ -223,9 +234,15 @@ install_tool_version() { local install_exit_code=$? if [ $install_exit_code -eq 0 ] && [ $download_exit_code -eq 0 ]; then - # Remove download directory if --keep-download flag or always_keep_download config setting are not set + # If the download directory should be kept, but isn't available, warn the user always_keep_download=$(get_asdf_config_value "always_keep_download") - if [ ! "$keep_download" = "true" ] && [ ! "$always_keep_download" = "yes" ] && [ -d "$download_path" ]; then + if [ "$keep_download" = "true" ] || [ "$always_keep_download" = "yes" ]; then + if [ ! -d "$download_path" ]; then + printf '%s\n' "asdf: Warn: You have configured asdf to preserve downloaded files (with always_keep_download=yes or --keep-download). But" >&2 + printf '%s\n' "asdf: Warn: the current plugin ($plugin_name) does not support that. Downloaded files will not be preserved." >&2 + fi + # Otherwise, remove the download directory if it exists + elif [ -d "$download_path" ]; then rm -r "$download_path" fi diff --git a/asdf/lib/functions/plugins.bash b/asdf/lib/functions/plugins.bash index f2f3b8e..6f7a943 100644 --- a/asdf/lib/functions/plugins.bash +++ b/asdf/lib/functions/plugins.bash @@ -28,23 +28,21 @@ plugin_list_command() { printf "%s" "$plugin_name" if [ -n "$show_repo" ]; then - printf "\\t%s" "$(git --git-dir "$plugin_path/.git" remote get-url origin 2>/dev/null)" + printf "\t%s" "$(get_plugin_remote_url "$plugin_name")" fi if [ -n "$show_ref" ]; then - local branch - local gitref - branch=$(git --git-dir "$plugin_path/.git" rev-parse --abbrev-ref HEAD 2>/dev/null) - gitref=$(git --git-dir "$plugin_path/.git" rev-parse --short HEAD 2>/dev/null) - printf "\\t%s\\t%s" "$branch" "$gitref" + printf "\t%s\t%s" \ + "$(get_plugin_remote_branch "$plugin_name")" \ + "$(get_plugin_remote_gitref "$plugin_name")" fi - printf "\\n" + printf "\n" done ) | awk '{ if (NF > 1) { printf("%-28s", $1) ; $1="" }; print $0}' else display_error 'No plugins installed' - exit 1 + exit 0 fi } @@ -56,16 +54,16 @@ plugin_add_command() { local plugin_name=$1 - local regex="^[[:alpha:][:digit:]_-]+$" + local regex="^[[:lower:][:digit:]_-]+$" if ! printf "%s" "$plugin_name" | grep -q -E "$regex"; then - display_error "$plugin_name is invalid. Name must match regex $regex" + display_error "$plugin_name is invalid. Name may only contain lowercase letters, numbers, '_', and '-'" exit 1 fi if [ -n "$2" ]; then local source_url=$2 else - initialize_or_update_repository + initialize_or_update_plugin_repository local source_url source_url=$(get_plugin_source_url "$plugin_name") fi @@ -78,11 +76,11 @@ plugin_add_command() { local plugin_path plugin_path=$(get_plugin_path "$plugin_name") - mkdir -p "$(asdf_data_dir)/plugins" + [ -d "$(asdf_data_dir)/plugins" ] || mkdir -p "$(asdf_data_dir)/plugins" if [ -d "$plugin_path" ]; then - display_error "Plugin named $plugin_name already added" - exit 2 + printf '%s\n' "Plugin named $plugin_name already added" + exit 0 else asdf_run_hook "pre_asdf_plugin_add" "$plugin_name" asdf_run_hook "pre_asdf_plugin_add_${plugin_name}" @@ -94,6 +92,7 @@ plugin_add_command() { if [ -f "${plugin_path}/bin/post-plugin-add" ]; then ( export ASDF_PLUGIN_SOURCE_URL=$source_url + # shellcheck disable=SC2030 export ASDF_PLUGIN_PATH=$plugin_path "${plugin_path}/bin/post-plugin-add" ) @@ -141,10 +140,11 @@ update_plugin() { local prev_ref= local post_ref= { + printf "Location of %s plugin: %s\n" "$plugin_name" "$plugin_path" asdf_run_hook "pre_asdf_plugin_update" "$plugin_name" asdf_run_hook "pre_asdf_plugin_update_${plugin_name}" - printf "Updating %s to %s\\n" "$plugin_name" "$gitref" + printf "Updating %s to %s\n" "$plugin_name" "$gitref" git "${common_git_options[@]}" fetch --prune --update-head-ok origin "$gitref:$gitref" prev_ref=$(git "${common_git_options[@]}" rev-parse --short HEAD) @@ -153,6 +153,7 @@ update_plugin() { if [ -f "${plugin_path}/bin/post-plugin-update" ]; then ( + # shellcheck disable=SC2031 export ASDF_PLUGIN_PATH=$plugin_path export ASDF_PLUGIN_PREV_REF=$prev_ref export ASDF_PLUGIN_POST_REF=$post_ref diff --git a/asdf/lib/functions/versions.bash b/asdf/lib/functions/versions.bash index 4e27ce8..7d1d941 100644 --- a/asdf/lib/functions/versions.bash +++ b/asdf/lib/functions/versions.bash @@ -4,9 +4,9 @@ version_command() { if [ "$#" -lt "3" ]; then if [ "$cmd" = "global" ]; then - printf "Usage: asdf global \\n" + printf "Usage: asdf global \n" else - printf "Usage: asdf local \\n" + printf "Usage: asdf local \n" fi exit 1 fi @@ -17,14 +17,14 @@ version_command() { local file_name local file - file_name="$(version_file_name)" + file_name="$(asdf_tool_versions_filename)" if [ "$cmd" = "global" ]; then file="$HOME/$file_name" elif [ "$cmd" = "local-tree" ]; then file=$(find_tool_versions) else # cmd = local - file="$(pwd)/$file_name" + file="$PWD/$file_name" fi if [ -L "$file" ]; then @@ -60,11 +60,22 @@ version_command() { resolved_versions+=("$version") done - if [ -f "$file" ] && grep "^$plugin_name " "$file" >/dev/null; then - sed -i.bak -e "s|^$plugin_name .*$|$plugin_name ${resolved_versions[*]}|" "$file" - rm -f "$file".bak + if [ -f "$file" ] && grep -q "^$plugin_name " "$file"; then + local temp_dir + temp_dir=${TMPDIR:-/tmp} + + local temp_tool_versions_file + temp_tool_versions_file=$(mktemp "$temp_dir/asdf-tool-versions-file.XXXXXX") + + cp -f "$file" "$temp_tool_versions_file" + sed -e "s|^$plugin_name .*$|$plugin_name ${resolved_versions[*]}|" "$temp_tool_versions_file" >"$file" + rm -f "$temp_tool_versions_file" else - printf "%s %s\\n" "$plugin_name" "${resolved_versions[*]}" >>"$file" + # Add a trailing newline at the end of the file if missing + [[ -f "$file" && -n "$(tail -c1 "$file")" ]] && printf '\n' >>"$file" + + # Add a new version line to the end of the file + printf "%s %s\n" "$plugin_name" "${resolved_versions[*]}" >>"$file" fi } @@ -88,9 +99,9 @@ list_all_command() { if [[ $return_code -ne 0 ]]; then # Printing all output to allow plugin to handle error formatting - printf "Plugin %s's list-all callback script failed with output:\\n" "${plugin_name}" >&2 - printf "%s\\n" "$(cat "$std_err_file")" >&2 - printf "%s\\n" "$(cat "$std_out_file")" >&2 + printf "Plugin %s's list-all callback script failed with output:\n" "${plugin_name}" >&2 + printf "%s\n" "$(cat "$std_err_file")" >&2 + printf "%s\n" "$(cat "$std_out_file")" >&2 rm "$std_out_file" "$std_err_file" exit 1 fi @@ -111,7 +122,7 @@ list_all_command() { IFS=' ' read -r -a versions_list <<<"$output" for version in "${versions_list[@]}"; do - printf "%s\\n" "${version}" + printf "%s\n" "${version}" done # Remove temp files if they still exist @@ -125,7 +136,7 @@ latest_command() { local query=$2 local plugin_path - if [ "$plugin_name" == "--all" ]; then + if [ "$plugin_name" = "--all" ]; then latest_all fi @@ -140,13 +151,13 @@ latest_command() { versions=$("${plugin_path}"/bin/latest-stable "$query") if [ -z "${versions}" ]; then # this branch requires this print to mimic the error from the list-all branch - printf "No compatible versions available (%s %s)\\n" "$plugin_name" "$query" >&2 + printf "No compatible versions available (%s %s)\n" "$plugin_name" "$query" >&2 exit 1 fi else # pattern from xxenv-latest (https://github.com/momo-lab/xxenv-latest) versions=$(list_all_command "$plugin_name" "$query" | - grep -ivE "(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)" | + grep -ivE "(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-milestone|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)" | sed 's/^[[:space:]]\+//' | tail -1) if [ -z "${versions}" ]; then @@ -154,7 +165,7 @@ latest_command() { fi fi - printf "%s\\n" "$versions" + printf "%s\n" "$versions" } latest_all() { @@ -194,10 +205,10 @@ latest_all() { if [ -n "$installed_versions" ] && printf '%s\n' "$installed_versions" | grep -q "^$version\$"; then installed_status="installed" fi - printf "%s\\t%s\\t%s\\n" "$plugin_name" "$version" "$installed_status" + printf "%s\t%s\t%s\n" "$plugin_name" "$version" "$installed_status" done else - printf "%s\\n" 'No plugins installed' + printf "%s\n" 'No plugins installed' fi exit 0 } diff --git a/asdf/lib/utils.bash b/asdf/lib/utils.bash index 1d3a72a..21978a9 100644 --- a/asdf/lib/utils.bash +++ b/asdf/lib/utils.bash @@ -5,35 +5,23 @@ GREP_OPTIONS="--color=never" # shellcheck disable=SC2034 GREP_COLORS= -ASDF_DIR=${ASDF_DIR:-''} -ASDF_DATA_DIR=${ASDF_DATA_DIR:-''} - asdf_version() { local version git_rev version="v$(cat "$(asdf_dir)/version.txt")" if [ -d "$(asdf_dir)/.git" ]; then git_rev="$(git --git-dir "$(asdf_dir)/.git" rev-parse --short HEAD)" - printf "%s-%s\\n" "$version" "$git_rev" + printf "%s-%s\n" "$version" "$git_rev" else - printf "%s\\n" "$version" + printf "%s\n" "$version" fi } -asdf_dir() { - if [ -z "$ASDF_DIR" ]; then - local current_script_path=${BASH_SOURCE[0]} - export ASDF_DIR - ASDF_DIR=$( - cd "$(dirname "$(dirname "$current_script_path")")" || exit - pwd - ) - fi - - printf "%s\\n" "$ASDF_DIR" +asdf_tool_versions_filename() { + printf '%s\n' "${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME:-.tool-versions}" } -asdf_repository_url() { - printf "https://github.com/asdf-vm/asdf-plugins.git\\n" +asdf_config_file() { + printf '%s\n' "${ASDF_CONFIG_FILE:-$HOME/.asdfrc}" } asdf_data_dir() { @@ -47,7 +35,23 @@ asdf_data_dir() { data_dir=$(asdf_dir) fi - printf "%s\\n" "$data_dir" + printf "%s\n" "$data_dir" +} + +asdf_dir() { + if [ -z "$ASDF_DIR" ]; then + local current_script_path=${BASH_SOURCE[0]} + printf '%s\n' "$( + cd -- "$(dirname "$(dirname "$current_script_path")")" || exit + printf '%s\n' "$PWD" + )" + else + printf '%s\n' "$ASDF_DIR" + fi +} + +asdf_plugin_repository_url() { + printf "https://github.com/asdf-vm/asdf-plugins.git\n" } get_install_path() { @@ -58,14 +62,14 @@ get_install_path() { local install_dir install_dir="$(asdf_data_dir)/installs" - mkdir -p "${install_dir}/${plugin}" + [ -d "${install_dir}/${plugin}" ] || mkdir -p "${install_dir}/${plugin}" if [ "$install_type" = "version" ]; then - printf "%s/%s/%s\\n" "$install_dir" "$plugin" "$version" + printf "%s/%s/%s\n" "$install_dir" "$plugin" "$version" elif [ "$install_type" = "path" ]; then - printf "%s\\n" "$version" + printf "%s\n" "$version" else - printf "%s/%s/%s-%s\\n" "$install_dir" "$plugin" "$install_type" "$version" + printf "%s/%s/%s-%s\n" "$install_dir" "$plugin" "$install_type" "$version" fi } @@ -77,14 +81,14 @@ get_download_path() { local download_dir download_dir="$(asdf_data_dir)/downloads" - mkdir -p "${download_dir}/${plugin}" + [ -d "${download_dir}/${plugin}" ] || mkdir -p "${download_dir}/${plugin}" if [ "$install_type" = "version" ]; then - printf "%s/%s/%s\\n" "$download_dir" "$plugin" "$version" + printf "%s/%s/%s\n" "$download_dir" "$plugin" "$version" elif [ "$install_type" = "path" ]; then return else - printf "%s/%s/%s-%s\\n" "$download_dir" "$plugin" "$install_type" "$version" + printf "%s/%s/%s-%s\n" "$download_dir" "$plugin" "$install_type" "$version" fi } @@ -137,19 +141,19 @@ version_not_installed_text() { local plugin_name=$1 local version=$2 - printf "version %s is not installed for %s\\n" "$version" "$plugin_name" + printf "version %s is not installed for %s\n" "$version" "$plugin_name" } get_plugin_path() { - if test -n "$1"; then - printf "%s\\n" "$(asdf_data_dir)/plugins/$1" + if [ -n "$1" ]; then + printf "%s\n" "$(asdf_data_dir)/plugins/$1" else - printf "%s\\n" "$(asdf_data_dir)/plugins" + printf "%s\n" "$(asdf_data_dir)/plugins" fi } display_error() { - printf "%s\\n" "$1" >&2 + printf "%s\n" "$1" >&2 } get_version_in_dir() { @@ -159,11 +163,11 @@ get_version_in_dir() { local asdf_version - file_name=$(version_file_name) + file_name=$(asdf_tool_versions_filename) asdf_version=$(parse_asdf_version_file "$search_path/$file_name" "$plugin_name") if [ -n "$asdf_version" ]; then - printf "%s\\n" "$asdf_version|$search_path/$file_name" + printf "%s\n" "$asdf_version|$search_path/$file_name" return 0 fi @@ -172,16 +176,12 @@ get_version_in_dir() { legacy_version=$(parse_legacy_version_file "$search_path/$filename" "$plugin_name") if [ -n "$legacy_version" ]; then - printf "%s\\n" "$legacy_version|$search_path/$filename" + printf "%s\n" "$legacy_version|$search_path/$filename" return 0 fi done } -version_file_name() { - printf "%s" "${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME:-.tool-versions}" -} - find_versions() { local plugin_name=$1 local search_path=$2 @@ -190,10 +190,10 @@ find_versions() { version=$(get_version_from_env "$plugin_name") if [ -n "$version" ]; then local upcase_name - upcase_name=$(printf "%s\\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') + upcase_name=$(printf "%s\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') local version_env_var="ASDF_${upcase_name}_VERSION" - printf "%s\\n" "$version|$version_env_var environment variable" + printf "%s\n" "$version|$version_env_var environment variable" return 0 fi @@ -206,13 +206,13 @@ find_versions() { local legacy_filenames="" if [ "$legacy_config" = "yes" ] && [ -f "$legacy_list_filenames_script" ]; then - legacy_filenames=$($legacy_list_filenames_script) + legacy_filenames=$("$legacy_list_filenames_script") fi while [ "$search_path" != "/" ]; do version=$(get_version_in_dir "$plugin_name" "$search_path" "$legacy_filenames") if [ -n "$version" ]; then - printf "%s\\n" "$version" + printf "%s\n" "$version" return 0 fi search_path=$(dirname "$search_path") @@ -223,7 +223,7 @@ find_versions() { if [ -f "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" ]; then versions=$(parse_asdf_version_file "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$plugin_name") if [ -n "$versions" ]; then - printf "%s\\n" "$versions|$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" + printf "%s\n" "$versions|$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" return 0 fi fi @@ -231,16 +231,16 @@ find_versions() { display_no_version_set() { local plugin_name=$1 - printf "No version is set for %s; please run \`asdf %s \`\\n" "$plugin_name" "$plugin_name" + printf "No version is set for %s; please run \`asdf %s \`\n" "$plugin_name" "$plugin_name" } get_version_from_env() { local plugin_name=$1 local upcase_name - upcase_name=$(printf "%s\\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') + upcase_name=$(printf "%s\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') local version_env_var="ASDF_${upcase_name}_VERSION" local version=${!version_env_var:-} - printf "%s\\n" "$version" + printf "%s\n" "$version" } find_install_path() { @@ -251,7 +251,7 @@ find_install_path() { IFS=':' read -a version_info <<<"$version" if [ "$version" = "system" ]; then - printf "\\n" + printf "\n" elif [ "${version_info[0]}" = "ref" ]; then local install_type="${version_info[0]}" local version="${version_info[1]}" @@ -263,7 +263,9 @@ find_install_path() { # And then use the binaries there local install_type="path" local version="path" - printf "%s\\n" "${version_info[1]}" + + util_resolve_user_path "${version_info[1]}" + printf "%s\n" "${util_resolve_user_path_reply}" else local install_type="version" local version="${version_info[0]}" @@ -281,12 +283,12 @@ get_custom_executable_path() { cmd=$(basename "$executable_path") local relative_path # shellcheck disable=SC2001 - relative_path=$(printf "%s\\n" "$executable_path" | sed -e "s|${install_path}/||") + relative_path=$(printf "%s\n" "$executable_path" | sed -e "s|${install_path}/||") relative_path="$("${plugin_path}/bin/exec-path" "$install_path" "$cmd" "$relative_path")" executable_path="$install_path/$relative_path" fi - printf "%s\\n" "$executable_path" + printf "%s\n" "$executable_path" } get_executable_path() { @@ -304,11 +306,11 @@ get_executable_path() { if [ $? -ne 0 ]; then return 1 fi - printf "%s\\n" "$cmd_path" + printf "%s\n" "$cmd_path" else local install_path install_path=$(find_install_path "$plugin_name" "$version") - printf "%s\\n" "${install_path}"/"${executable_path}" + printf "%s\n" "${install_path}"/"${executable_path}" fi } @@ -319,8 +321,15 @@ parse_asdf_version_file() { if [ -f "$file_path" ]; then local version version=$(strip_tool_version_comments "$file_path" | grep "^${plugin_name} " | sed -e "s/^${plugin_name} //") + if [ -n "$version" ]; then - printf "%s\\n" "$version" + if [[ "$version" == path:* ]]; then + util_resolve_user_path "${version#path:}" + printf "%s\n" "path:${util_resolve_user_path_reply}" + else + printf "%s\n" "$version" + fi + return 0 fi fi @@ -347,13 +356,13 @@ parse_legacy_version_file() { get_preset_version_for() { local plugin_name=$1 local search_path - search_path=$(pwd) + search_path=$PWD local version_and_path version_and_path=$(find_versions "$plugin_name" "$search_path") local version version=$(cut -d '|' -f 1 <<<"$version_and_path") - printf "%s\\n" "$version" + printf "%s\n" "$version" } get_asdf_config_value_from_file() { @@ -364,10 +373,12 @@ get_asdf_config_value_from_file() { return 1 fi + util_validate_no_carriage_returns "$config_path" + local result - result=$(grep -E "^\\s*$key\\s*=\\s*" "$config_path" | head | sed -e 's/^[^=]*= *//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + result=$(grep -E "^\s*$key\s*=\s*" "$config_path" | head | sed -e 's/^[^=]*= *//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') if [ -n "$result" ]; then - printf "%s\\n" "$result" + printf "%s\n" "$result" return 0 fi @@ -376,7 +387,8 @@ get_asdf_config_value_from_file() { get_asdf_config_value() { local key=$1 - local config_path=${ASDF_CONFIG_FILE:-"$HOME/.asdfrc"} + local config_path= + config_path=$(asdf_config_file) local default_config_path=${ASDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"} local local_config_path @@ -408,11 +420,17 @@ repository_needs_update() { [ "$sync_required" ] } -initialize_or_update_repository() { +initialize_or_update_plugin_repository() { local repository_url local repository_path - repository_url=$(asdf_repository_url) + disable_plugin_short_name_repo="$(get_asdf_config_value "disable_plugin_short_name_repository")" + if [ "yes" = "$disable_plugin_short_name_repo" ]; then + printf "Short-name plugin repository is disabled\n" >&2 + exit 1 + fi + + repository_url=$(asdf_plugin_repository_url) repository_path=$(asdf_data_dir)/repository if [ ! -d "$repository_path" ]; then @@ -420,10 +438,11 @@ initialize_or_update_repository() { git clone "$repository_url" "$repository_path" elif repository_needs_update; then printf "updating plugin repository..." - (cd "$repository_path" && git fetch && git reset --hard origin/master) + git -C "$repository_path" fetch + git -C "$repository_path" reset --hard origin/master fi - mkdir -p "$(asdf_data_dir)/tmp" + [ -d "$(asdf_data_dir)/tmp" ] || mkdir -p "$(asdf_data_dir)/tmp" touch "$(asdf_data_dir)/tmp/repo-updated" } @@ -439,16 +458,18 @@ get_plugin_source_url() { } find_tool_versions() { - find_file_upwards "$(version_file_name)" + find_file_upwards "$(asdf_tool_versions_filename)" } find_file_upwards() { local name="$1" local search_path - search_path=$(pwd) + search_path=$PWD while [ "$search_path" != "/" ]; do if [ -f "$search_path/$name" ]; then - printf "%s\\n" "${search_path}/$name" + util_validate_no_carriage_returns "$search_path/$name" + + printf "%s\n" "${search_path}/$name" return 0 fi search_path=$(dirname "$search_path") @@ -469,12 +490,12 @@ resolve_symlink() { # as relative case $resolved_path in /*) - printf "%s\\n" "$resolved_path" + printf "%s\n" "$resolved_path" ;; *) ( cd "$(dirname "$symlink")" || exit 1 - printf "%s\\n" "$PWD/$resolved_path" + printf "%s\n" "$PWD/$resolved_path" ) ;; esac @@ -502,7 +523,7 @@ list_plugin_bin_paths() { else local space_separated_list_of_bin_paths="bin" fi - printf "%s\\n" "$space_separated_list_of_bin_paths" + printf "%s\n" "$space_separated_list_of_bin_paths" } list_plugin_exec_paths() { @@ -525,7 +546,7 @@ list_plugin_exec_paths() { local plugin_shims_path plugin_shims_path=$(get_plugin_path "$plugin_name")/shims if [ -d "$plugin_shims_path" ]; then - printf "%s\\n" "$plugin_shims_path" + printf "%s\n" "$plugin_shims_path" fi space_separated_list_of_bin_paths="$(list_plugin_bin_paths "$plugin_name" "$version" "$install_type")" @@ -535,7 +556,7 @@ list_plugin_exec_paths() { install_path=$(get_install_path "$plugin_name" "$install_type" "$version") for bin_path in "${all_bin_paths[@]}"; do - printf "%s\\n" "$install_path/$bin_path" + printf "%s\n" "$install_path/$bin_path" done } @@ -597,7 +618,7 @@ plugin_executables() { for bin_path in "${all_bin_paths[@]}"; do for executable_file in "$bin_path"/*; do if is_executable "$executable_file"; then - printf "%s\\n" "$executable_file" + printf "%s\n" "$executable_file" fi done done @@ -625,7 +646,7 @@ shim_plugin_versions() { if [ -x "$shim_path" ]; then grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | uniq else - printf "asdf: unknown shim %s\\n" "$executable_name" + printf "asdf: unknown shim %s\n" "$executable_name" return 1 fi } @@ -638,7 +659,7 @@ shim_plugins() { if [ -x "$shim_path" ]; then grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | cut -d' ' -f 1 | uniq else - printf "asdf: unknown shim %s\\n" "$executable_name" + printf "asdf: unknown shim %s\n" "$executable_name" return 1 fi } @@ -647,7 +668,7 @@ strip_tool_version_comments() { local tool_version_path="$1" # Use sed to strip comments from the tool version file # Breakdown of sed command: - # This command represents 3 steps, seperated by a semi-colon (;), that run on each line. + # This command represents 3 steps, separated by a semi-colon (;), that run on each line. # 1. Delete line if it starts with any blankspace and a #. # 2. Find a # and delete it and everything after the #. # 3. Remove any whitespace from the end of the line. @@ -676,7 +697,7 @@ get_shim_versions() { preset_versions() { shim_name=$1 - shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c ". $(asdf_dir)/lib/utils.bash; printf \"%s %s\\n\" PLUGIN \$(get_preset_version_for PLUGIN)" + shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c ". $(asdf_dir)/lib/utils.bash; printf \"%s %s\n\" PLUGIN \$(get_preset_version_for PLUGIN)" } select_from_preset_version() { @@ -687,7 +708,7 @@ select_from_preset_version() { shim_versions=$(get_shim_versions "$shim_name") if [ -n "$shim_versions" ]; then preset_versions=$(preset_versions "$shim_name") - grep -F "$shim_versions" <<<"$preset_versions" | head -n 1 | xargs -IVERSION printf "%s\\n" VERSION + grep -F "$shim_versions" <<<"$preset_versions" | head -n 1 | xargs -IVERSION printf "%s\n" VERSION fi } @@ -700,7 +721,7 @@ select_version() { # These are separated by a space. e.g. python 3.7.2 2.7.15 # For each plugin/version pair, we check if it is present in the shim local search_path - search_path=$(pwd) + search_path=$PWD local shim_versions IFS=$'\n' read -rd '' -a shim_versions <<<"$(get_shim_versions "$shim_name")" @@ -722,10 +743,10 @@ select_version() { IFS=' ' read -r plugin_shim_name plugin_shim_version <<<"$plugin_and_version" if [[ "$plugin_name" == "$plugin_shim_name" ]]; then if [[ "$plugin_version" == "$plugin_shim_version" ]]; then - printf "%s\\n" "$plugin_name $plugin_version" + printf "%s\n" "$plugin_name $plugin_version" return elif [[ "$plugin_version" == "path:"* ]]; then - printf "%s\\n" "$plugin_name $plugin_version" + printf "%s\n" "$plugin_name $plugin_version" return fi fi @@ -740,7 +761,7 @@ with_shim_executable() { local shim_exec="${2}" if [ ! -f "$(asdf_data_dir)/shims/${shim_name}" ]; then - printf "%s %s %s\\n" "unknown command:" "${shim_name}." "Perhaps you have to reshim?" >&2 + printf "%s %s %s\n" "unknown command:" "${shim_name}." "Perhaps you have to reshim?" >&2 return 1 fi @@ -759,6 +780,8 @@ with_shim_executable() { IFS=' ' read -r plugin_name full_version <<<"$selected_version" plugin_path=$(get_plugin_path "$plugin_name") + # This function does get invoked, but shellcheck sees it as unused code + # shellcheck disable=SC2317 run_within_env() { local path path=$(remove_path_from_path "$PATH" "$(asdf_data_dir)/shims") @@ -797,15 +820,15 @@ with_shim_executable() { done if [ -n "${preset_plugin_versions[*]}" ]; then - printf "%s %s\\n" "No preset version installed for command" "$shim_name" - printf "%s\\n\\n" "Please install a version by running one of the following:" + printf "%s %s\n" "No preset version installed for command" "$shim_name" + printf "%s\n\n" "Please install a version by running one of the following:" for preset_plugin_version in "${preset_plugin_versions[@]}"; do - printf "%s %s\\n" "asdf install" "$preset_plugin_version" + printf "%s %s\n" "asdf install" "$preset_plugin_version" done - printf "\\n%s %s\\n" "or add one of the following versions in your config file at" "$closest_tool_version" + printf "\n%s %s\n" "or add one of the following versions in your config file at" "$closest_tool_version" else - printf "%s %s\\n" "No version is set for command" "$shim_name" - printf "%s %s\\n" "Consider adding one of the following versions in your config file at" "$closest_tool_version" + printf "%s %s\n" "No version is set for command" "$shim_name" + printf "%s %s\n" "Consider adding one of the following versions in your config file at" "$closest_tool_version" fi shim_plugin_versions "${shim_name}" ) >&2 @@ -814,7 +837,7 @@ with_shim_executable() { } substitute() { - # Use Bash substituion rather than sed as it will handle escaping of all + # Use Bash substitution rather than sed as it will handle escaping of all # strings for us. local input=$1 local find_str=$2 @@ -829,3 +852,47 @@ remove_path_from_path() { local path=$2 substitute "$PATH" "$path" "" | sed -e "s|::|:|g" } + +# @description Strings that began with a ~ are always paths. In +# that case, then ensure ~ it handled like a shell +util_resolve_user_path() { + util_resolve_user_path_reply= + local path="$1" + + # shellcheck disable=SC2088 + if [ "${path::2}" = '~/' ]; then + util_resolve_user_path_reply="${HOME}/${path:2}" + else + util_resolve_user_path_reply="$path" + fi +} + +# @description Check if a file contains carriage returns. If it does, print a warning. +util_validate_no_carriage_returns() { + local file_path="$1" + + if grep -qr $'\r' "$file_path"; then + printf '%s\n' "asdf: Warning: File $file_path contains carriage returns. Please remove them." >&2 + fi +} + +get_plugin_remote_url() { + local plugin_name="$1" + local plugin_path + plugin_path="$(get_plugin_path "$plugin_name")" + git --git-dir "$plugin_path/.git" remote get-url origin 2>/dev/null +} + +get_plugin_remote_branch() { + local plugin_name="$1" + local plugin_path + plugin_path="$(get_plugin_path "$plugin_name")" + git --git-dir "$plugin_path/.git" rev-parse --abbrev-ref HEAD 2>/dev/null +} + +get_plugin_remote_gitref() { + local plugin_name="$1" + local plugin_path + plugin_path="$(get_plugin_path "$plugin_name")" + git --git-dir "$plugin_path/.git" rev-parse --short HEAD 2>/dev/null +} diff --git a/asdf/version.txt b/asdf/version.txt index 5eef0f1..930e300 100644 --- a/asdf/version.txt +++ b/asdf/version.txt @@ -1 +1 @@ -0.10.2 +0.14.1 diff --git a/asdf_sync.sh b/asdf_sync.sh new file mode 100755 index 0000000..3009041 --- /dev/null +++ b/asdf_sync.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +version=${1:-v0.14.1} + +mkdir /tmp/asdf_bump + +asdf_clone_dir=/tmp/asdf_bump/asdf-$version +git clone https://github.com/asdf-vm/asdf.git --branch $version $asdf_clone_dir + + +rsync -avz --exclude 'asdf.fish' --exclude 'asdf.nu' --exclude 'asdf.ps1' \ + --exclude '*.md' --exclude 'test/*' --exclude 'scripts/*' --exclude 'docs/*' \ + $asdf_clone_dir/* asdf/ + +