pyenv/libexec/rbenv-version-file
Mislav Marohnić ca25259900 Allow explicit target directory argument to rbenv-version-file
Can be used for `.ruby-version` file lookup in the ancestry of a
specific directory. In this mode of operation, global version files
aren't taken into consideration, and the command fails unless a local
version file was found.
2015-12-23 15:19:54 +01:00

45 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# Usage: rbenv version-file [<dir>]
# Summary: Detect the file that sets the current rbenv version
set -e
[ -n "$RBENV_DEBUG" ] && set -x
target_dir="$1"
find_local_version_file() {
local root="$1"
while ! [[ "$root" =~ ^//[^/]*$ ]]; do
if [ -e "${root}/.ruby-version" ]; then
echo "${root}/.ruby-version"
return 0
elif [ -e "${root}/.rbenv-version" ]; then
echo "${root}/.rbenv-version"
return 0
fi
[ -n "$root" ] || break
root="${root%/*}"
done
return 1
}
find_global_version_file() {
local global_version_file="${RBENV_ROOT}/version"
if [ -e "$global_version_file" ]; then
echo "$global_version_file"
elif [ -e "${RBENV_ROOT}/global" ]; then
echo "${RBENV_ROOT}/global"
elif [ -e "${RBENV_ROOT}/default" ]; then
echo "${RBENV_ROOT}/default"
else
echo "$global_version_file"
fi
}
if [ -n "$target_dir" ]; then
find_local_version_file "$target_dir"
else
find_local_version_file "$RBENV_DIR" || {
[ "$RBENV_DIR" != "$PWD" ] && find_local_version_file "$PWD"
} || find_global_version_file
fi