Simplify the shims registration implementation in rbenv-rehash

It doesn't need to be a bash array and we don't need a separate index of
shims registered. Simply keep everything in a space-separated string and
use that as an index as well.

This assumes that executable names *never* have spaces in them.
This commit is contained in:
Mislav Marohnić 2014-10-15 05:12:28 +02:00
parent 89d4e8a0e0
commit 1381c2ca79
2 changed files with 26 additions and 18 deletions

View file

@ -98,28 +98,17 @@ make_shims() {
done done
} }
# Create an empty array for the list of registered shims and an empty registered_shims=" "
# string to use as a search index.
registered_shims=()
registered_shims_index=""
# We will keep track of shims registered for installation with the # Registers the name of a shim to be generated.
# global `registered_shims` array and with a global search index
# 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.
register_shim() { register_shim() {
local shim="$@" registered_shims="${registered_shims}${1} "
registered_shims["${#registered_shims[@]}"]="$shim"
registered_shims_index="$registered_shims_index/$shim/"
} }
# To install all the registered shims, we iterate over the # Install all the shims registered via `make_shims` or `register_shim` directly.
# `registered_shims` array and create a link if one does not already
# exist.
install_registered_shims() { install_registered_shims() {
local shim file local shim file
for shim in "${registered_shims[@]}"; do for shim in $registered_shims; do
file="${SHIM_PATH}/${shim}" file="${SHIM_PATH}/${shim}"
[ -e "$file" ] || ln -f "$PROTOTYPE_SHIM_PATH" "$file" [ -e "$file" ] || ln -f "$PROTOTYPE_SHIM_PATH" "$file"
done done
@ -132,7 +121,7 @@ install_registered_shims() {
remove_stale_shims() { remove_stale_shims() {
local shim local shim
for shim in "$SHIM_PATH"/*; do for shim in "$SHIM_PATH"/*; do
if [[ "$registered_shims_index" != *"/${shim##*/}/"* ]]; then if [[ "$registered_shims" != *" ${shim##*/} "* ]]; then
rm -f "$shim" rm -f "$shim"
fi fi
done done

View file

@ -53,7 +53,7 @@ ruby
OUT OUT
} }
@test "removes stale shims" { @test "removes outdated shims" {
mkdir -p "${RBENV_ROOT}/shims" mkdir -p "${RBENV_ROOT}/shims"
touch "${RBENV_ROOT}/shims/oldshim1" touch "${RBENV_ROOT}/shims/oldshim1"
chmod +x "${RBENV_ROOT}/shims/oldshim1" chmod +x "${RBENV_ROOT}/shims/oldshim1"
@ -67,6 +67,25 @@ OUT
assert [ ! -e "${RBENV_ROOT}/shims/oldshim1" ] assert [ ! -e "${RBENV_ROOT}/shims/oldshim1" ]
} }
@test "do exact matches when removing stale shims" {
create_executable "2.0" "unicorn_rails"
create_executable "2.0" "rspec-core"
rbenv-rehash
cp "$RBENV_ROOT"/shims/{rspec-core,rspec}
cp "$RBENV_ROOT"/shims/{rspec-core,rails}
cp "$RBENV_ROOT"/shims/{rspec-core,uni}
chmod +x "$RBENV_ROOT"/shims/{rspec,rails,uni}
run rbenv-rehash
assert_success ""
assert [ ! -e "${RBENV_ROOT}/shims/rails" ]
assert [ ! -e "${RBENV_ROOT}/shims/rake" ]
assert [ ! -e "${RBENV_ROOT}/shims/uni" ]
}
@test "binary install locations containing spaces" { @test "binary install locations containing spaces" {
create_executable "dirname1 p247" "ruby" create_executable "dirname1 p247" "ruby"
create_executable "dirname2 preview1" "rspec" create_executable "dirname2 preview1" "rspec"