2011-08-12 05:33:45 -04:00
|
|
|
#!/usr/bin/env bash
|
2012-12-29 23:05:04 -05:00
|
|
|
# Summary: Rehash rbenv shims (run this after installing executables)
|
2012-12-29 13:06:20 -05:00
|
|
|
|
2011-08-12 05:33:45 -04:00
|
|
|
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 ||
|
2013-03-07 00:13:50 -05:00
|
|
|
{ if [ -w "$SHIM_PATH" ]; then
|
|
|
|
echo "rbenv: cannot rehash: $PROTOTYPE_SHIM_PATH exists"
|
|
|
|
else
|
|
|
|
echo "rbenv: cannot rehash: $SHIM_PATH isn't writable"
|
|
|
|
fi
|
2011-08-15 02:16:13 -04:00
|
|
|
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
|
2013-01-03 11:06:12 -05:00
|
|
|
# hard-linked for every executable 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
|
2012-12-27 14:41:55 -05:00
|
|
|
[ -n "\$RBENV_DEBUG" ] && set -x
|
|
|
|
|
|
|
|
program="\${0##*/}"
|
|
|
|
if [ "\$program" = "ruby" ]; then
|
|
|
|
for arg; do
|
|
|
|
case "\$arg" in
|
|
|
|
-e* | -- ) break ;;
|
|
|
|
*/* )
|
|
|
|
if [ -f "\$arg" ]; then
|
|
|
|
export RBENV_DIR="\${arg%/*}"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2011-09-12 12:05:45 -04:00
|
|
|
export RBENV_ROOT="$RBENV_ROOT"
|
2013-11-03 05:18:28 -05:00
|
|
|
exec "$(command -v rbenv)" exec "\$program" "\$@"
|
2011-08-03 23:57:27 -04:00
|
|
|
SH
|
2011-08-15 02:16:13 -04:00
|
|
|
chmod +x "$PROTOTYPE_SHIM_PATH"
|
2011-08-03 23:57:27 -04:00
|
|
|
}
|
|
|
|
|
2012-12-27 14:42:25 -05:00
|
|
|
# If the contents of the prototype shim file differ from the contents
|
|
|
|
# of the first shim in the shims directory, assume rbenv has been
|
|
|
|
# upgraded and the existing shims need to be removed.
|
|
|
|
remove_outdated_shims() {
|
2014-10-14 21:39:04 -04:00
|
|
|
local shim
|
|
|
|
for shim in "$SHIM_PATH"/*; do
|
2012-12-27 14:42:25 -05:00
|
|
|
if ! diff "$PROTOTYPE_SHIM_PATH" "$shim" >/dev/null 2>&1; then
|
2014-10-14 21:39:04 -04:00
|
|
|
rm -f "$SHIM_PATH"/*
|
2012-12-27 14:42:25 -05:00
|
|
|
fi
|
|
|
|
break
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-12-24 15:16:37 -05:00
|
|
|
# The basename of each argument passed to `make_shims` will be
|
|
|
|
# registered for installation as a shim. In this way, plugins may call
|
|
|
|
# `make_shims` with a glob to register many shims at once.
|
2011-08-03 13:17:28 -04:00
|
|
|
make_shims() {
|
2014-10-14 21:39:04 -04:00
|
|
|
local file shim
|
|
|
|
for file; do
|
|
|
|
shim="${file##*/}"
|
2011-12-24 15:16:37 -05:00
|
|
|
register_shim "$shim"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2012-12-27 18:08:54 -05:00
|
|
|
# Create an empty array for the list of registered shims and an empty
|
|
|
|
# string to use as a search index.
|
2011-12-24 15:16:37 -05:00
|
|
|
registered_shims=()
|
2012-12-27 18:08:54 -05:00
|
|
|
registered_shims_index=""
|
2011-12-24 15:16:37 -05:00
|
|
|
|
|
|
|
# We will keep track of shims registered for installation with the
|
2013-01-04 17:26:01 -05:00
|
|
|
# global `registered_shims` array and with a global search index
|
2012-12-27 18:08:54 -05:00
|
|
|
# string. The array will let us iterate over all registered shims. The
|
|
|
|
# index string will let us quickly check whether a shim with the given
|
|
|
|
# name has been registered or not.
|
2011-12-24 15:16:37 -05:00
|
|
|
register_shim() {
|
|
|
|
local shim="$@"
|
2012-12-27 18:08:54 -05:00
|
|
|
registered_shims["${#registered_shims[@]}"]="$shim"
|
|
|
|
registered_shims_index="$registered_shims_index/$shim/"
|
2011-12-24 15:16:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# To install all the registered shims, we iterate over the
|
|
|
|
# `registered_shims` array and create a link if one does not already
|
|
|
|
# exist.
|
|
|
|
install_registered_shims() {
|
2014-10-14 21:39:04 -04:00
|
|
|
local shim file
|
2011-12-24 15:16:37 -05:00
|
|
|
for shim in "${registered_shims[@]}"; do
|
2014-10-14 21:39:04 -04:00
|
|
|
file="${SHIM_PATH}/${shim}"
|
|
|
|
[ -e "$file" ] || ln -f "$PROTOTYPE_SHIM_PATH" "$file"
|
2011-08-03 13:17:28 -04:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-12-24 15:16:37 -05:00
|
|
|
# Once the registered shims have been installed, we make a second pass
|
|
|
|
# over the contents of the shims directory. Any file that is present
|
|
|
|
# in the directory but has not been registered as a shim should be
|
|
|
|
# removed.
|
|
|
|
remove_stale_shims() {
|
2012-12-27 18:08:54 -05:00
|
|
|
local shim
|
2014-10-14 21:39:04 -04:00
|
|
|
for shim in "$SHIM_PATH"/*; do
|
|
|
|
if [[ "$registered_shims_index" != *"/${shim##*/}/"* ]]; then
|
2012-12-27 18:08:54 -05:00
|
|
|
rm -f "$shim"
|
2011-12-26 21:12:16 -05:00
|
|
|
fi
|
2011-12-24 15:16:37 -05:00
|
|
|
done
|
|
|
|
}
|
2011-09-19 10:45:12 -04:00
|
|
|
|
2012-12-27 14:42:25 -05:00
|
|
|
shopt -s nullglob
|
2011-08-03 13:17:28 -04:00
|
|
|
|
2013-01-03 11:06:12 -05:00
|
|
|
# Create the prototype shim, then register shims for all known
|
|
|
|
# executables.
|
2011-08-03 23:57:27 -04:00
|
|
|
create_prototype_shim
|
2012-12-27 14:42:25 -05:00
|
|
|
remove_outdated_shims
|
2011-08-03 13:17:28 -04:00
|
|
|
make_shims ../versions/*/bin/*
|
|
|
|
|
2011-09-19 10:45:12 -04:00
|
|
|
|
2011-12-24 15:16:37 -05:00
|
|
|
# Allow plugins to register shims.
|
2013-04-15 12:10:56 -04:00
|
|
|
OLDIFS="$IFS"
|
2013-02-24 08:52:50 -05:00
|
|
|
IFS=$'\n' scripts=(`rbenv-hooks rehash`)
|
2013-04-15 12:10:56 -04:00
|
|
|
IFS="$OLDIFS"
|
|
|
|
|
2013-02-24 08:52:50 -05:00
|
|
|
for script in "${scripts[@]}"; do
|
2011-09-21 14:05:08 -04:00
|
|
|
source "$script"
|
2011-08-01 16:50:26 -04:00
|
|
|
done
|
2011-12-24 15:16:37 -05:00
|
|
|
|
|
|
|
install_registered_shims
|
|
|
|
remove_stale_shims
|