Commit 36b2d13b authored by Sam Stephenson's avatar Sam Stephenson
Browse files

Allow plugins to run code before and after the installation process

parent 724a3f38
Loading
Loading
Loading
Loading
+41 −4
Original line number Diff line number Diff line
@@ -65,25 +65,62 @@ for option in "${OPTIONS[@]}"; do
  esac
done

unset VERSION_NAME
DEFINITION="${ARGUMENTS[0]}"
[ -n "$DEFINITION" ] || usage 1


# 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.
for script in $(rbenv-hooks install); do
  source "$script"
done

VERSION_NAME="${DEFINITION##*/}"

# Set VERSION_NAME from $DEFINITION, if it is not already set. Then
# compute the installation prefix.
[ -n "$VERSION_NAME" ] || VERSION_NAME="${DEFINITION##*/}"
PREFIX="${RBENV_ROOT}/versions/${VERSION_NAME}"

# If RBENV_BUILD_ROOT is set, then always pass keep options to ruby-build
# If RBENV_BUILD_ROOT is set, always pass keep options to ruby-build.
if [ -n "${RBENV_BUILD_ROOT}" ]; then
  export RUBY_BUILD_BUILD_PATH="${RBENV_BUILD_ROOT}/${VERSION_NAME}"
  KEEP="-k"
fi

# Set RUBY_BUILD_CACHE_PATH to $RBENV_ROOT/cache, if the directory
# exists and the variable is not already set.
if [ -z "${RUBY_BUILD_CACHE_PATH}" ] && [ -d "${RBENV_ROOT}/cache" ]; then
  export RUBY_BUILD_CACHE_PATH="${RBENV_ROOT}/cache"
fi

ruby-build $KEEP $VERBOSE "$DEFINITION" "$PREFIX"
rbenv rehash

# 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"