Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,48 @@ get_absolute_path() {
)
}

gem_command() {
"${ASDF_INSTALL_PATH}/bin/gem" "$@"
}

# Before Ruby 2.6 Bundler was not installed by default
ensure_bundler_installed() {
echo -n "Checking: Bundler is ... "

if gem_command list -i bundler >/dev/null 2>&1; then
echo "INSTALLED"
return 0
else
echo "MISSING"
fi

if semantic_version_lt "$ASDF_INSTALL_VERSION" "1.9"; then
echo "Skipping installation of Bundler because requested Ruby version is prior to 1.9"
return 0
fi

# Version 1.17.3 is compatible with all Ruby versions 1.9 and above.
# This will ensure that some version of Bundler is installed and shimmed.
install_ruby_gem bundler --version 1.17.3
}

install_ruby_gem() {
local gem_name=$1
local args=("${@:2}")

# shellcheck disable=SC2145
echo -n "Running: gem install $gem_name ${args[@]:-} ... "

if output=$(gem_command install "$gem_name" "${args[@]+"${args[@]}"}" 2>&1); then
echo -e "SUCCESS"
else
echo -e "FAIL: $output"
fi
}

install_default_gems() {
local args=()
local default_gems="${ASDF_GEM_DEFAULT_PACKAGES_FILE:=$HOME/.default-gems}"
local gem="${ASDF_INSTALL_PATH}/bin/gem"

if [ ! -f "$default_gems" ]; then
return
Expand Down Expand Up @@ -114,15 +152,9 @@ install_default_gems() {
args=()
fi

# shellcheck disable=SC2145
echo -n "Running: gem install $gem_name ${args[@]:-} ... "
install_ruby_gem "$gem_name" "${args[@]}"

if output=$("$gem" install "$gem_name" "${args[@]+"${args[@]}"}" 2>&1); then
echo -e "SUCCESS"
else
echo -e "FAIL: $output"
fi
# echo here adds trailing newline, which is necessary if file lacks one,
# echo (inside install_ruby_gem) adds trailing newline, which is necessary if file lacks one,
# empty lines are already skipped some multiple trailing newlines are not a problem.
done < <(
cat "$default_gems"
Expand All @@ -131,4 +163,5 @@ install_default_gems() {
}

install_ruby "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
ensure_bundler_installed
install_default_gems
32 changes: 32 additions & 0 deletions lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,35 @@ ruby_build_source_dir() {
ruby_build_path() {
echo "$(ruby_build_dir)/bin/ruby-build"
}

# Compare semantic version numbers
# Success if first argument is lower than second argument
semantic_version_lt() {
local IFS=.
local -a v1 v2
local i

read -ra v1 <<< "$1"
read -ra v2 <<< "$2"

# Pad shorter version with zeros
for ((i=${#v1[@]}; i<${#v2[@]}; i++)); do
v1[i]=0
done
for ((i=${#v2[@]}; i<${#v1[@]}; i++)); do
v2[i]=0
done

# Compare each numeric part
for ((i=0; i<${#v1[@]}; i++)); do
# Force base-10 interpretation so values like 08 are not treated as invalid octal numbers
if ((10#${v1[i]} < 10#${v2[i]})); then
return 0
fi
if ((10#${v1[i]} > 10#${v2[i]})); then
return 1
fi
done

return 1
}