2011-08-18 15:11:40 -04:00
|
|
|
#!/usr/bin/env bash
|
2015-12-23 09:19:54 -05:00
|
|
|
# Usage: rbenv version-file [<dir>]
|
2012-12-29 17:34:53 -05:00
|
|
|
# Summary: Detect the file that sets the current rbenv version
|
2011-08-18 15:11:40 -04:00
|
|
|
set -e
|
2011-09-12 11:11:59 -04:00
|
|
|
[ -n "$RBENV_DEBUG" ] && set -x
|
2011-08-18 15:11:40 -04:00
|
|
|
|
2015-12-23 09:19:54 -05:00
|
|
|
target_dir="$1"
|
|
|
|
|
2012-12-27 14:39:36 -05:00
|
|
|
find_local_version_file() {
|
|
|
|
local root="$1"
|
2015-12-23 09:19:54 -05:00
|
|
|
while ! [[ "$root" =~ ^//[^/]*$ ]]; do
|
2012-12-30 19:35:08 -05:00
|
|
|
if [ -e "${root}/.ruby-version" ]; then
|
|
|
|
echo "${root}/.ruby-version"
|
2015-12-23 09:19:54 -05:00
|
|
|
return 0
|
2012-12-27 14:39:36 -05:00
|
|
|
fi
|
2015-06-09 11:24:15 -04:00
|
|
|
[ -n "$root" ] || break
|
2012-12-27 14:39:36 -05:00
|
|
|
root="${root%/*}"
|
|
|
|
done
|
2015-12-23 09:19:54 -05:00
|
|
|
return 1
|
2012-12-27 14:39:36 -05:00
|
|
|
}
|
|
|
|
|
2015-12-23 09:19:54 -05:00
|
|
|
find_global_version_file() {
|
|
|
|
local global_version_file="${RBENV_ROOT}/version"
|
2011-08-18 15:11:40 -04:00
|
|
|
|
2015-12-23 09:19:54 -05:00
|
|
|
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
|
|
|
|
}
|
2011-08-18 15:32:33 -04:00
|
|
|
|
2015-12-23 09:19:54 -05:00
|
|
|
if [ -n "$target_dir" ]; then
|
|
|
|
find_local_version_file "$target_dir"
|
2011-08-18 15:32:33 -04:00
|
|
|
else
|
2015-12-23 09:19:54 -05:00
|
|
|
find_local_version_file "$RBENV_DIR" || {
|
|
|
|
[ "$RBENV_DIR" != "$PWD" ] && find_local_version_file "$PWD"
|
|
|
|
} || find_global_version_file
|
2011-08-18 15:32:33 -04:00
|
|
|
fi
|