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
|
2023-07-11 17:38:36 +02: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 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
|
2018-02-26 09:32:43 +01:00
|
|
|
if [ -s "${root}/.ruby-version" ]; then
|
2012-12-31 01:35:08 +01:00
|
|
|
echo "${root}/.ruby-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
|
|
|
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"
|
2015-12-29 14:44:32 +01:00
|
|
|
} || echo "${RBENV_ROOT}/version"
|
2011-08-18 21:32:33 +02:00
|
|
|
fi
|