2011-09-13 03:28:24 +02:00
|
|
|
#!/usr/bin/env bash
|
2012-12-30 22:10:23 +01:00
|
|
|
#
|
2012-12-30 22:25:52 +01:00
|
|
|
# Summary: Install a Ruby version using the ruby-build plugin
|
|
|
|
#
|
2013-01-30 00:22:56 +01:00
|
|
|
# Usage: rbenv install [-f|--force] [-k|--keep] [-v|--verbose] <version>
|
|
|
|
# rbenv install [-f|--force] [-k|--keep] [-v|--verbose] <definition-file>
|
2012-12-30 22:10:23 +01:00
|
|
|
# rbenv install -l|--list
|
|
|
|
#
|
|
|
|
# -l/--list List all available versions
|
2013-01-30 00:22:56 +01:00
|
|
|
# -f/--force Install even if the version appears to be installed already
|
2012-12-30 22:10:23 +01:00
|
|
|
# -k/--keep Keep source tree in $RBENV_BUILD_ROOT after installation
|
|
|
|
# (defaults to $RBENV_ROOT/sources)
|
|
|
|
# -v/--verbose Verbose mode: print compilation status to stdout
|
|
|
|
#
|
2012-12-30 22:29:52 +01:00
|
|
|
# For detailed information on installing Ruby versions with
|
|
|
|
# ruby-build, including a list of environment variables for adjusting
|
|
|
|
# compilation, see: https://github.com/sstephenson/ruby-build#usage
|
|
|
|
#
|
2011-09-13 03:28:24 +02:00
|
|
|
set -e
|
|
|
|
[ -n "$RBENV_DEBUG" ] && set -x
|
|
|
|
|
2011-09-13 20:16:03 +02:00
|
|
|
# Provide rbenv completions
|
|
|
|
if [ "$1" = "--complete" ]; then
|
|
|
|
exec ruby-build --definitions
|
|
|
|
fi
|
|
|
|
|
2011-09-13 03:28:24 +02:00
|
|
|
if [ -z "$RBENV_ROOT" ]; then
|
|
|
|
RBENV_ROOT="${HOME}/.rbenv"
|
|
|
|
fi
|
|
|
|
|
2012-08-15 21:02:23 +02:00
|
|
|
# Load shared library functions
|
2012-08-15 21:43:44 +02:00
|
|
|
eval "$(ruby-build --lib)"
|
2012-08-15 21:02:23 +02:00
|
|
|
|
|
|
|
usage() {
|
2012-12-30 22:10:23 +01:00
|
|
|
# We can remove the sed fallback once rbenv 0.4.0 is widely available.
|
2012-12-30 22:25:52 +01:00
|
|
|
rbenv-help install 2>/dev/null || sed -ne '/^#/!q;s/.//;s/.//;1,4d;p' < "$0"
|
2012-08-15 21:02:23 +02:00
|
|
|
[ -z "$1" ] || exit "$1"
|
|
|
|
}
|
|
|
|
|
2013-01-30 00:22:56 +01:00
|
|
|
unset FORCE
|
2012-08-15 21:02:23 +02:00
|
|
|
unset KEEP
|
|
|
|
unset VERBOSE
|
|
|
|
|
|
|
|
parse_options "$@"
|
|
|
|
for option in "${OPTIONS[@]}"; do
|
|
|
|
case "$option" in
|
|
|
|
"h" | "help" )
|
|
|
|
usage 0
|
|
|
|
;;
|
|
|
|
"l" | "list" )
|
|
|
|
echo "Available versions:"
|
|
|
|
ruby-build --definitions | sed 's/^/ /'
|
|
|
|
exit
|
|
|
|
;;
|
2013-01-30 00:22:56 +01:00
|
|
|
"f" | "force" )
|
|
|
|
FORCE=true
|
|
|
|
;;
|
2012-08-15 21:02:23 +02:00
|
|
|
"k" | "keep" )
|
|
|
|
[ -n "${RBENV_BUILD_ROOT}" ] || RBENV_BUILD_ROOT="${RBENV_ROOT}/sources"
|
|
|
|
;;
|
|
|
|
"v" | "verbose" )
|
|
|
|
VERBOSE="-v"
|
|
|
|
;;
|
|
|
|
"version" )
|
|
|
|
exec ruby-build --version
|
|
|
|
;;
|
|
|
|
* )
|
|
|
|
usage 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2013-01-29 22:07:06 +01:00
|
|
|
unset VERSION_NAME
|
2013-01-30 00:01:13 +01:00
|
|
|
|
|
|
|
# The first argument contains the definition to install. If the
|
|
|
|
# argument is missing, try to install whatever local app-specific
|
|
|
|
# version is specified by rbenv. Show usage instructions if a local
|
|
|
|
# version is not specified.
|
2012-08-15 21:02:23 +02:00
|
|
|
DEFINITION="${ARGUMENTS[0]}"
|
2013-01-30 00:01:13 +01:00
|
|
|
[ -n "$DEFINITION" ] || DEFINITION="$(rbenv local 2>/dev/null || true)"
|
2012-08-15 21:02:23 +02:00
|
|
|
[ -n "$DEFINITION" ] || usage 1
|
2011-09-13 03:38:48 +02:00
|
|
|
|
2013-01-29 22:07:06 +01:00
|
|
|
|
|
|
|
# Define `before_install` and `after_install` functions that allow
|
|
|
|
# plugin hooks to register a string of code for execution before or
|
|
|
|
# after the installation process.
|
|
|
|
declare -a before_hooks after_hooks
|
|
|
|
|
|
|
|
before_install() {
|
|
|
|
local hook="$1"
|
|
|
|
before_hooks["${#before_hooks[@]}"]="$hook"
|
|
|
|
}
|
|
|
|
|
|
|
|
after_install() {
|
|
|
|
local hook="$1"
|
|
|
|
after_hooks["${#after_hooks[@]}"]="$hook"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Load plugin hooks.
|
2012-08-16 04:49:13 +02:00
|
|
|
for script in $(rbenv-hooks install); do
|
|
|
|
source "$script"
|
|
|
|
done
|
|
|
|
|
2013-01-29 22:07:06 +01:00
|
|
|
|
|
|
|
# Set VERSION_NAME from $DEFINITION, if it is not already set. Then
|
|
|
|
# compute the installation prefix.
|
|
|
|
[ -n "$VERSION_NAME" ] || VERSION_NAME="${DEFINITION##*/}"
|
2011-09-13 03:38:48 +02:00
|
|
|
PREFIX="${RBENV_ROOT}/versions/${VERSION_NAME}"
|
|
|
|
|
2013-01-30 00:22:56 +01:00
|
|
|
# If the installation prefix exists, prompt for confirmation unless
|
|
|
|
# the --force option was specified.
|
|
|
|
if [ -z "$FORCE" ] && [ -d "${PREFIX}/bin" ]; then
|
|
|
|
echo "rbenv: $PREFIX already exists" >&2
|
|
|
|
read -p "continue with installation? (y/N) "
|
|
|
|
|
|
|
|
case "$REPLY" in
|
|
|
|
y* | Y* ) ;;
|
|
|
|
* ) exit 1 ;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
2013-01-29 22:07:06 +01:00
|
|
|
# If RBENV_BUILD_ROOT is set, always pass keep options to ruby-build.
|
2012-04-28 23:26:52 +02:00
|
|
|
if [ -n "${RBENV_BUILD_ROOT}" ]; then
|
|
|
|
export RUBY_BUILD_BUILD_PATH="${RBENV_BUILD_ROOT}/${VERSION_NAME}"
|
2012-08-15 21:02:23 +02:00
|
|
|
KEEP="-k"
|
2012-04-28 23:26:52 +02:00
|
|
|
fi
|
|
|
|
|
2013-01-29 22:07:06 +01:00
|
|
|
# Set RUBY_BUILD_CACHE_PATH to $RBENV_ROOT/cache, if the directory
|
|
|
|
# exists and the variable is not already set.
|
2012-11-14 00:34:08 +01:00
|
|
|
if [ -z "${RUBY_BUILD_CACHE_PATH}" ] && [ -d "${RBENV_ROOT}/cache" ]; then
|
|
|
|
export RUBY_BUILD_CACHE_PATH="${RBENV_ROOT}/cache"
|
Simple, optional tarball cache support
Rationale:
Both in development and in production, some usage patterns of ruby-build
are slowed down by the download phase. In scenarios such as
troubleshooting failing builds or with provisioning situations (chef,
vagrant...) the repeated download is unnerving, bandwidth wasting and
simply against etiquette towards tarball hosters.
It also happens that some source sites happen to be down and in such
cases it is helpful to be able to sideload sources to rbenv.
Behavior:
By default nothing changes.
If the variable CACHE_PATH is set, then ruby-build will use that
directory to store a successful download, and will check before
downloading if the tarball is already there, in which case downloading
is skipped.
The file is first downloaded as before in the tmp subdirectory and only
moved afterwards, thus ensuring consistency.
There is no default cache path and the optional variable is to be set by
hand, ensuring people know what they're doing when using ruby-build.
Additionnally, rbenv-install will helpfully set CACHE_PATH if and only
if a RBENV_ROOT/cache directory exists. Again, the directory has to be
created manually.
The CACHE_PATH variable internally ends with a slash to mutualize
non-cached cases. Still, consistency is ensured whether or not a slash
is provided externally.
Notes:
I'm not quite sure CACHE_PATH is a good name, maybe
RUBY_BUILD_CACHE_PATH is better and less conflicting.
2012-11-07 16:43:15 +01:00
|
|
|
fi
|
|
|
|
|
2013-01-29 22:07:06 +01:00
|
|
|
|
|
|
|
# Execute `before_install` hooks.
|
|
|
|
for hook in "${before_hooks[@]}"; do eval "$hook"; done
|
|
|
|
|
|
|
|
# Invoke `ruby-build` and record the exit status in $STATUS. Run
|
|
|
|
# `rbenv rehash` after a successful installation.
|
|
|
|
STATUS=0
|
|
|
|
ruby-build $KEEP $VERBOSE "$DEFINITION" "$PREFIX" || STATUS="$?"
|
|
|
|
|
|
|
|
# Execute `after_install` hooks.
|
|
|
|
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
|
|
|
|
|
|
|
# Run `rbenv-rehash` after a successful installation.
|
|
|
|
[ "$STATUS" != "0" ] || rbenv rehash
|
|
|
|
|
|
|
|
exit "$STATUS"
|