Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: don't invoke asdf inside asdf commands (#1208)
* fix: don't invoke asdf inside asdf commands

Recursive calls have a number of disadvantages:

* Poorer performance since each invocation spawns and new process and re-executes all the code in bin/asdf
* Makes debugging more difficult
* More likely to introduce subtle bugs and the possibility for infinite loops
  • Loading branch information
Stratus3D committed Apr 25, 2022
1 parent 713bceb commit 27f7ef7
Show file tree
Hide file tree
Showing 18 changed files with 813 additions and 780 deletions.
5 changes: 4 additions & 1 deletion lib/commands/command-current.bash
@@ -1,4 +1,6 @@
# -*- sh -*-
# shellcheck source=lib/functions/plugins.bash
. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash"

# shellcheck disable=SC2059
plugin_current_command() {
Expand Down Expand Up @@ -48,7 +50,8 @@ current_command() {

# printf "$terminal_format" "PLUGIN" "VERSION" "SET BY CONFIG" # disbale this until we release headings across the board
if [ $# -eq 0 ]; then
for plugin in $(asdf plugin list); do
# shellcheck disable=SC2119
for plugin in $(plugin_list_command); do
plugin_current_command "$plugin" "$terminal_format"
done
else
Expand Down
4 changes: 3 additions & 1 deletion lib/commands/command-export-shell-version.bash
@@ -1,4 +1,6 @@
# -*- sh -*-
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"

# Output from this command must be executable shell code
shell_command() {
Expand Down Expand Up @@ -36,7 +38,7 @@ shell_command() {
exit 0
fi
if [ "$version" = "latest" ]; then
version=$(asdf latest "$plugin")
version=$(latest_command "$plugin")
fi
if ! (check_if_version_exists "$plugin" "$version"); then
version_not_installed_text "$plugin" "$version" 1>&2
Expand Down
4 changes: 3 additions & 1 deletion lib/commands/command-help.bash
@@ -1,4 +1,6 @@
# -*- sh -*-
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"

asdf_help() {
printf "version: %s\\n\\n" "$(asdf_version)"
Expand Down Expand Up @@ -53,7 +55,7 @@ help_command() {

if [ "${version_info[0]}" = "latest" ]; then
local version
version=$(asdf latest "$plugin_name" "${version_info[1]}")
version=$(latest_command "$plugin_name" "${version_info[1]}")
else
local version="${version_info[0]}"
fi
Expand Down
4 changes: 3 additions & 1 deletion lib/commands/command-info.bash
@@ -1,11 +1,13 @@
# -*- sh -*-
# shellcheck source=lib/functions/plugins.bash
. "$(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" "$(asdf plugin list --urls --refs)"
printf "%s:\\n%s\\n\\n" "ASDF INSTALLED PLUGINS" "$(plugin_list_command --urls --refs)"
}

info_command "$@"
246 changes: 6 additions & 240 deletions lib/commands/command-install.bash
@@ -1,243 +1,9 @@
# -*- sh -*-

handle_failure() {
local install_path="$1"
rm -rf "$install_path"
exit 1
}

handle_cancel() {
local install_path="$1"
printf "\\nreceived sigint, cleaning up"
handle_failure "$install_path"
}

install_command() {
local plugin_name=$1
local full_version=$2
local extra_args="${*:3}"

if [ "$plugin_name" = "" ] && [ "$full_version" = "" ]; then
install_local_tool_versions "$extra_args"
elif [[ $# -eq 1 ]]; then
install_one_local_tool "$plugin_name"
else
install_tool_version "$plugin_name" "$full_version" "$extra_args"
fi
}

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
else
printf "1\\n"
fi
}

install_one_local_tool() {
local plugin_name=$1
local search_path
search_path=$(pwd)

local plugin_versions

local plugin_version

local plugin_version_and_path
plugin_version_and_path="$(find_versions "$plugin_name" "$search_path")"

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"
exit 1
fi
}

install_local_tool_versions() {
local plugins_path
plugins_path=$(get_plugin_path)

local search_path
search_path=$(pwd)

local some_tools_installed
local some_plugin_not_installed

local tool_versions_path
tool_versions_path=$(find_tool_versions)

# Locate all the plugins installed in the system
local plugins_installed
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
local plugin_name
plugin_name=$(basename "$plugin_path")
plugins_installed="$plugins_installed $plugin_name"
done
plugins_installed=$(printf "%s" "$plugins_installed" | tr " " "\n")
fi

if [ -z "$plugins_installed" ]; then
printf "Install plugins first to be able to install tools\\n"
exit 1
fi

# Locate all the plugins defined in the versions file.
local tools_file
if [ -f "$tool_versions_path" ]; then
tools_file=$(strip_tool_version_comments "$tool_versions_path" | cut -d ' ' -f 1)
for plugin_name in $tools_file; do
if ! printf '%s\n' "${plugins_installed[@]}" | grep -q "^$plugin_name\$"; then
printf "%s plugin is not installed\n" "$plugin_name"
some_plugin_not_installed='yes'
fi
done
fi

if [ -n "$some_plugin_not_installed" ]; then
exit 1
fi

if [ -n "$plugins_installed" ]; then
for plugin_name in $plugins_installed; do
local plugin_version_and_path
plugin_version_and_path="$(find_versions "$plugin_name" "$search_path")"

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
fi
done
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"
exit 1
fi
}

install_tool_version() {
local plugin_name=$1
local full_version=$2
local flags=$3
local keep_download
local plugin_path

plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"

for flag in $flags; do
case "$flag" in
"--keep-download")
keep_download=true
shift
;;
*)
shift
;;
esac
done

if [ "$full_version" = "system" ]; then
return
fi

IFS=':' read -r -a version_info <<<"$full_version"
if [ "${version_info[0]}" = "ref" ]; then
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"

if [ "${version_info[0]}" = "latest" ]; then
local version
version=$(asdf latest "$plugin_name" "${version_info[1]}")
full_version=$version
else
local version="${version_info[0]}"
fi
fi

local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
local download_path
download_path=$(get_download_path "$plugin_name" "$install_type" "$version")
local concurrency
concurrency=$(get_concurrency)
trap 'handle_cancel $install_path' INT

if [ -d "$install_path" ]; then
printf "%s %s is already installed\\n" "$plugin_name" "$full_version"
else

if [ -f "${plugin_path}/bin/download" ]; then
# Not a legacy plugin
# Run the download script
(
# shellcheck disable=SC2030
export ASDF_INSTALL_TYPE=$install_type
# shellcheck disable=SC2030
export ASDF_INSTALL_VERSION=$version
# shellcheck disable=SC2030
export ASDF_INSTALL_PATH=$install_path
# shellcheck disable=SC2030
export ASDF_DOWNLOAD_PATH=$download_path
mkdir "$download_path"
asdf_run_hook "pre_asdf_download_${plugin_name}" "$full_version"
"${plugin_path}"/bin/download
)
fi

local download_exit_code=$?
if [ $download_exit_code -eq 0 ]; then
(
# shellcheck disable=SC2031
export ASDF_INSTALL_TYPE=$install_type
# shellcheck disable=SC2031
export ASDF_INSTALL_VERSION=$version
# shellcheck disable=SC2031
export ASDF_INSTALL_PATH=$install_path
# shellcheck disable=SC2031
export ASDF_DOWNLOAD_PATH=$download_path
# shellcheck disable=SC2031
export ASDF_CONCURRENCY=$concurrency
mkdir "$install_path"
asdf_run_hook "pre_asdf_install_${plugin_name}" "$full_version"
"${plugin_path}"/bin/install
)
fi

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
always_keep_download=$(get_asdf_config_value "always_keep_download")
if [ ! "$keep_download" = "true" ] && [ ! "$always_keep_download" = "yes" ] && [ -d "$download_path" ]; then
rm -r "$download_path"
fi

asdf reshim "$plugin_name" "$full_version"

asdf_run_hook "post_asdf_install_${plugin_name}" "$full_version"
else
handle_failure "$install_path"
fi
fi
}
# shellcheck source=lib/functions/versions.bash
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"
# shellcheck source=lib/commands/reshim.bash
. "$(dirname "$ASDF_CMD_FILE")/reshim.bash"
# shellcheck source=lib/functions/installs.bash
. "$(dirname "$(dirname "$0")")/lib/functions/installs.bash"

install_command "$@"

0 comments on commit 27f7ef7

Please sign in to comment.