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
|
2023-07-11 11:38:36 -04:00
|
|
|
#
|
|
|
|
# Detects and prints the location of a `.ruby-version` file that sets the
|
|
|
|
# version for the current working directory. If no file found, this prints
|
|
|
|
# the location of the global version file, even if that file does
|
|
|
|
# not exist.
|
|
|
|
|
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
|
2018-02-26 03:32:43 -05:00
|
|
|
if [ -s "${root}/.ruby-version" ]; then
|
2012-12-30 19:35:08 -05:00
|
|
|
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
|
|
|
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"
|
2015-12-29 08:44:32 -05:00
|
|
|
} || echo "${RBENV_ROOT}/version"
|
2011-08-18 15:32:33 -04:00
|
|
|
fi
|