Add rbenv versions --skip-aliases option

Useful in combination with `--bare` to list just the unique version
numbers without the extra directory entries that are symlinks to other
version numbers in the same directory.
This commit is contained in:
Mislav Marohnić 2015-10-27 10:49:29 +01:00
parent 7026e529c7
commit e80886e9be
3 changed files with 79 additions and 6 deletions

View file

@ -81,10 +81,13 @@ remove_outdated_shims() {
# List basenames of executables for every Ruby version # List basenames of executables for every Ruby version
list_executable_names() { list_executable_names() {
local file local version file
for file in "$RBENV_ROOT"/versions/*/bin/*; do rbenv-versions --bare --skip-aliases | \
while read version; do
for file in "${RBENV_ROOT}/versions/${version}/bin/"*; do
echo "${file##*/}" echo "${file##*/}"
done done
done
} }
# The basename of each argument passed to `make_shims` will be # The basename of each argument passed to `make_shims` will be

View file

@ -1,13 +1,64 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Summary: List all Ruby versions available to rbenv # Summary: List all Ruby versions available to rbenv
# Usage: rbenv versions [--bare] # Usage: rbenv versions [--bare] [--skip-aliases]
# #
# Lists all Ruby versions found in `$RBENV_ROOT/versions/*'. # Lists all Ruby versions found in `$RBENV_ROOT/versions/*'.
set -e set -e
[ -n "$RBENV_DEBUG" ] && set -x [ -n "$RBENV_DEBUG" ] && set -x
if [ "$1" = "--bare" ]; then unset bare
unset skip_aliases
for arg; do
case "$arg" in
--bare ) bare=1 ;;
--skip-aliases ) skip_aliases=1 ;;
* )
rbenv-help --usage versions >&2
exit 1
;;
esac
done
versions_dir="${RBENV_ROOT}/versions"
if ! enable -f "${BASH_SOURCE%/*}"/../libexec/rbenv-realpath.dylib realpath 2>/dev/null; then
if [ -n "$RBENV_NATIVE_EXT" ]; then
echo "rbenv: failed to load \`realpath' builtin" >&2
exit 1
fi
READLINK=$(type -p greadlink readlink | head -1)
if [ -z "$READLINK" ]; then
echo "rbenv: cannot find readlink - are you missing GNU coreutils?" >&2
exit 1
fi
resolve_link() {
$READLINK "$1"
}
realpath() {
local cwd="$PWD"
local path="$1"
local name
while [ -n "$path" ]; do
name="${path##*/}"
[ "$name" = "$path" ] || cd "${path%/*}"
path="$(resolve_link "$name" || true)"
done
echo "${PWD}/$name"
cd "$cwd"
}
fi
if [ -d "$versions_dir" ]; then
versions_dir="$(realpath "$versions_dir")"
fi
if [ -n "$bare" ]; then
hit_prefix="" hit_prefix=""
miss_prefix="" miss_prefix=""
current_version="" current_version=""
@ -36,8 +87,12 @@ if [ -n "$include_system" ] && RBENV_VERSION=system rbenv-which ruby >/dev/null
fi fi
shopt -s nullglob shopt -s nullglob
for path in "${RBENV_ROOT}/versions/"*; do for path in "$versions_dir"/*; do
if [ -d "$path" ]; then if [ -d "$path" ]; then
if [ -n "$skip_aliases" ] && [ -L "$path" ]; then
target="$(realpath "$path")"
[ "${target%/*}" != "$versions_dir" ] || continue
fi
print_version "${path##*/}" print_version "${path##*/}"
fi fi
done done

View file

@ -139,3 +139,18 @@ OUT
1.8.7 1.8.7
OUT OUT
} }
@test "doesn't list symlink aliases when --skip-aliases" {
create_version "1.8.7"
ln -s "1.8.7" "${RBENV_ROOT}/versions/1.8"
mkdir moo
ln -s "${PWD}/moo" "${RBENV_ROOT}/versions/1.9"
run rbenv-versions --bare --skip-aliases
assert_success
assert_output <<OUT
1.8.7
1.9
OUT
}