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