asdf/lib/commands/install.sh

107 lines
2.6 KiB
Bash
Raw Normal View History

handle_failure() {
local install_path="$1"
rm -rf "$install_path"
exit 1
}
handle_cancel() {
local install_path="$1"
echo -e "\\nreceived sigint, cleaning up"
handle_failure "$install_path"
}
2015-05-17 01:49:28 -07:00
install_command() {
2015-05-21 22:28:18 -07:00
local plugin_name=$1
2015-05-17 00:46:56 -07:00
local full_version=$2
if [ "$plugin_name" = "" ] && [ "$full_version" = "" ]; then
install_local_tool_versions
elif [[ $# -eq 1 ]]; then
display_error "You must specify a name and a version to install"
exit 1
else
2017-09-04 10:04:56 -07:00
install_tool_version "$plugin_name" "$full_version"
fi
}
2016-06-28 20:56:33 -07:00
get_concurrency() {
2018-06-16 12:04:38 -07:00
if command -v nproc > /dev/null 2>&1; then
2017-09-04 10:04:56 -07:00
nproc
2018-06-16 12:04:38 -07:00
elif command -v sysctl > /dev/null 2>&1 && sysctl hw.ncpu > /dev/null 2>&1; then
2017-09-04 10:04:56 -07:00
sysctl -n hw.ncpu
2016-06-28 20:56:33 -07:00
elif [ -f /proc/cpuinfo ]; then
2017-09-04 10:04:56 -07:00
grep -c processor /proc/cpuinfo
2016-06-28 20:56:33 -07:00
else
echo "1"
fi
}
install_local_tool_versions() {
local asdf_versions_path
asdf_versions_path=$(find_tool_versions)
if [ -f "${asdf_versions_path}" ]; then
2017-09-04 10:04:56 -07:00
while read -r tool_line; do
IFS=' ' read -r -a tool_info <<< "$tool_line"
local tool_name
tool_name=$(echo "${tool_info[0]}" | xargs)
local tool_version
tool_version=$(echo "${tool_info[1]}" | xargs)
if ! [[ -z "$tool_name" || -z "$tool_version" ]]; then
2017-09-04 10:04:56 -07:00
install_tool_version "$tool_name" "$tool_version"
fi
done < "$asdf_versions_path"
else
echo "Either specify a tool & version in the command"
echo "OR add .tool-versions file in this directory"
echo "or in a parent directory"
exit 1
fi
}
install_tool_version() {
local plugin_name=$1
local full_version=$2
2017-09-04 10:04:56 -07:00
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"
2015-05-07 22:58:07 -07:00
2017-09-04 10:04:56 -07:00
IFS=':' read -r -a version_info <<< "$full_version"
2015-05-21 21:47:27 -07:00
if [ "${version_info[0]}" = "ref" ]; then
2015-05-17 00:46:56 -07:00
local install_type="${version_info[0]}"
local version="${version_info[1]}"
else
local install_type="version"
local version="${version_info[0]}"
fi
2015-05-07 22:58:07 -07:00
2017-09-04 10:04:56 -07:00
local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
local concurrency
concurrency=$(get_concurrency)
trap 'handle_cancel $install_path' INT
if [ -d "$install_path" ]; then
2015-05-21 22:28:18 -07:00
echo "$plugin_name $full_version is already installed"
2015-05-21 22:17:44 -07:00
else
(
export ASDF_INSTALL_TYPE=$install_type
export ASDF_INSTALL_VERSION=$version
export ASDF_INSTALL_PATH=$install_path
2016-06-28 20:56:33 -07:00
export ASDF_CONCURRENCY=$concurrency
mkdir "$install_path"
bash "${plugin_path}"/bin/install
)
local exit_code=$?
if [ $exit_code -eq 0 ]; then
2017-09-04 10:04:56 -07:00
reshim_command "$plugin_name" "$full_version"
else
2017-07-30 11:15:21 -07:00
handle_failure "$install_path"
fi
2015-05-21 22:17:44 -07:00
fi
2015-05-17 00:46:56 -07:00
}