2011-08-12 05:33:45 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
2011-09-12 11:11:59 -04:00
|
|
|
[ -n "$RBENV_DEBUG" ] && set -x
|
2011-08-01 16:50:26 -04:00
|
|
|
|
2011-09-11 12:58:57 -04:00
|
|
|
SHIM_PATH="${RBENV_ROOT}/shims"
|
2011-08-15 02:16:13 -04:00
|
|
|
PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.rbenv-shim"
|
|
|
|
|
|
|
|
# Create the shims directory if it doesn't already exist.
|
|
|
|
mkdir -p "$SHIM_PATH"
|
|
|
|
|
|
|
|
# Ensure only one instance of rbenv-rehash is running at a time by
|
|
|
|
# setting the shell's `noclobber` option and attempting to write to
|
|
|
|
# the prototype shim file. If the file already exists, print a warning
|
|
|
|
# to stderr and exit with a non-zero status.
|
|
|
|
set -o noclobber
|
|
|
|
{ echo > "$PROTOTYPE_SHIM_PATH"
|
|
|
|
} 2>/dev/null ||
|
|
|
|
{ echo "rbenv: cannot rehash: $PROTOTYPE_SHIM_PATH exists"
|
|
|
|
exit 1
|
|
|
|
} >&2
|
|
|
|
set +o noclobber
|
|
|
|
|
|
|
|
# If we were able to obtain a lock, register a trap to clean up the
|
|
|
|
# prototype shim when the process exits.
|
2011-11-14 15:45:43 -05:00
|
|
|
trap remove_prototype_shim EXIT
|
2011-08-15 02:16:13 -04:00
|
|
|
|
|
|
|
remove_prototype_shim() {
|
|
|
|
rm -f "$PROTOTYPE_SHIM_PATH"
|
|
|
|
}
|
|
|
|
|
|
|
|
# The prototype shim file is a script that re-execs itself, passing
|
|
|
|
# its filename and any arguments to `rbenv exec`. This file is
|
|
|
|
# hard-linked for every binary and then removed. The linking technique
|
|
|
|
# is fast, uses less disk space than unique files, and also serves as
|
|
|
|
# a locking mechanism.
|
2011-08-03 23:57:27 -04:00
|
|
|
create_prototype_shim() {
|
2011-08-15 02:16:13 -04:00
|
|
|
cat > "$PROTOTYPE_SHIM_PATH" <<SH
|
2011-08-12 05:33:45 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
2011-09-12 12:05:45 -04:00
|
|
|
export RBENV_ROOT="$RBENV_ROOT"
|
2011-08-03 23:57:27 -04:00
|
|
|
exec rbenv exec "\${0##*/}" "\$@"
|
|
|
|
SH
|
2011-08-15 02:16:13 -04:00
|
|
|
chmod +x "$PROTOTYPE_SHIM_PATH"
|
2011-08-03 23:57:27 -04:00
|
|
|
}
|
|
|
|
|
2011-08-15 02:16:13 -04:00
|
|
|
# Make shims by iterating over every filename argument and creating a
|
|
|
|
# hard link from the prototype shim to a file of the same name in the
|
|
|
|
# shims directory, if one does not already exist.
|
2011-08-03 13:17:28 -04:00
|
|
|
make_shims() {
|
|
|
|
local glob="$@"
|
2011-08-01 16:50:26 -04:00
|
|
|
|
2011-08-03 13:17:28 -04:00
|
|
|
for file in $glob; do
|
|
|
|
local shim="${file##*/}"
|
2011-08-15 02:16:13 -04:00
|
|
|
[ -e "$shim" ] || ln -f "$PROTOTYPE_SHIM_PATH" "$shim"
|
2011-08-03 13:17:28 -04:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-09-19 10:45:12 -04:00
|
|
|
# Save the working directory.
|
|
|
|
CUR_PATH=$PWD
|
|
|
|
|
2011-08-15 02:16:13 -04:00
|
|
|
# Empty out the shims directory and make it the working directory.
|
|
|
|
rm -f "$SHIM_PATH"/*
|
|
|
|
cd "$SHIM_PATH"
|
2011-08-03 13:17:28 -04:00
|
|
|
|
2011-08-15 02:16:13 -04:00
|
|
|
# Create the prototype shim, then make shims for all known binaries.
|
2011-08-03 23:57:27 -04:00
|
|
|
create_prototype_shim
|
2011-08-17 18:53:37 -04:00
|
|
|
shopt -s nullglob
|
2011-08-03 13:17:28 -04:00
|
|
|
make_shims ../versions/*/bin/*
|
|
|
|
|
2011-09-19 10:45:12 -04:00
|
|
|
# Restore the previous working directory.
|
|
|
|
cd "$CUR_PATH"
|
|
|
|
|
2011-09-23 11:43:06 -04:00
|
|
|
for script in $(rbenv-hooks rehash); do
|
2011-09-21 14:05:08 -04:00
|
|
|
source "$script"
|
2011-08-01 16:50:26 -04:00
|
|
|
done
|