From 506bc3634f02f57af18a4fadd5a7ee6b10a9970c Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Thu, 18 Aug 2011 14:11:40 -0500 Subject: [PATCH] Extract rbenv-version-file{,-read,-write} --- libexec/rbenv-default | 19 +++++++------------ libexec/rbenv-local | 23 ++++++++++------------- libexec/rbenv-version-file | 15 +++++++++++++++ libexec/rbenv-version-file-read | 18 ++++++++++++++++++ libexec/rbenv-version-file-write | 15 +++++++++++++++ libexec/rbenv-version-name | 22 ++-------------------- libexec/rbenv-version-origin | 28 +++------------------------- 7 files changed, 70 insertions(+), 70 deletions(-) create mode 100755 libexec/rbenv-version-file create mode 100755 libexec/rbenv-version-file-read create mode 100755 libexec/rbenv-version-file-write diff --git a/libexec/rbenv-default b/libexec/rbenv-default index 56f1276e..d176e0cd 100755 --- a/libexec/rbenv-default +++ b/libexec/rbenv-default @@ -1,16 +1,11 @@ -#!/usr/bin/env bash -e +#!/usr/bin/env bash +set -e RBENV_VERSION="$1" -VERSION_FILE="${HOME}/.rbenv/default" -if [ -z "$RBENV_VERSION" ]; then - if [[ -e "$VERSION_FILE" ]]; then - cat "$VERSION_FILE" - else - echo "No default version configured - set with: rbenv default " - fi -else - # Make sure the specified version is installed - rbenv-prefix "$RBENV_VERSION" >/dev/null +RBENV_VERSION_FILE="${HOME}/.rbenv/default" - echo "$RBENV_VERSION" > "$VERSION_FILE" +if [ -n "$RBENV_VERSION" ]; then + rbenv-version-file-write "$RBENV_VERSION_FILE" "$RBENV_VERSION" +else + rbenv-version-file-read "$RBENV_VERSION_FILE" || echo system fi diff --git a/libexec/rbenv-local b/libexec/rbenv-local index cfc72067..010e93b3 100755 --- a/libexec/rbenv-local +++ b/libexec/rbenv-local @@ -1,17 +1,14 @@ -#!/usr/bin/env bash -e +#!/usr/bin/env bash +set -e RBENV_VERSION="$1" -VERSION_FILE=".rbenv-version" -if [ -z "$RBENV_VERSION" ]; then - if [[ -e "$VERSION_FILE" ]]; then - cat "$VERSION_FILE" - else - echo "No local version configured - set with: rbenv local " - fi +RBENV_VERSION_FILE=".rbenv-version" + +if [ -n "$RBENV_VERSION" ]; then + rbenv-version-file-write "$RBENV_VERSION_FILE" "$RBENV_VERSION" else - # Make sure the specified version is installed - rbenv-prefix "$RBENV_VERSION" >/dev/null - - echo "$RBENV_VERSION" > "$VERSION_FILE" + rbenv-version-file-read "$RBENV_VERSION_FILE" || + { echo "rbenv: no local version configured for this directory" + exit 1 + } >&2 fi - diff --git a/libexec/rbenv-version-file b/libexec/rbenv-version-file new file mode 100755 index 00000000..bb4da5b4 --- /dev/null +++ b/libexec/rbenv-version-file @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -e + +DEFAULT_PATH="${HOME}/.rbenv/default" + +root="$(pwd)" +while [ -n "$root" ]; do + if [ -e "${root}/.rbenv-version" ]; then + echo "${root}/.rbenv-version" + exit + fi + root="${root%/*}" +done + +echo "$DEFAULT_PATH" diff --git a/libexec/rbenv-version-file-read b/libexec/rbenv-version-file-read new file mode 100755 index 00000000..5bd293de --- /dev/null +++ b/libexec/rbenv-version-file-read @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -e + +VERSION_FILE="$1" + +if [ -e "$VERSION_FILE" ]; then + # Read and print the first non-whitespace word from the specified + # version file. + while read -a words; do + version="${words[0]}" + if [ -n "$version" ]; then + echo "$version" + break + fi + done < "$VERSION_FILE" +else + exit 1 +fi diff --git a/libexec/rbenv-version-file-write b/libexec/rbenv-version-file-write new file mode 100755 index 00000000..b42764b3 --- /dev/null +++ b/libexec/rbenv-version-file-write @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -e + +RBENV_VERSION_FILE="$1" +RBENV_VERSION="$2" + +if [ -z "$RBENV_VERSION" ] || [ -z "$RBENV_VERSION_FILE" ]; then + echo "usage: rbenv write-version-file filename version" >&2 +fi + +# Make sure the specified version is installed. +rbenv-prefix "$RBENV_VERSION" >/dev/null + +# Write the version out to disk. +echo "$RBENV_VERSION" > "$RBENV_VERSION_FILE" diff --git a/libexec/rbenv-version-name b/libexec/rbenv-version-name index 4faba8c7..a9c0d1ab 100755 --- a/libexec/rbenv-version-name +++ b/libexec/rbenv-version-name @@ -1,29 +1,11 @@ #!/usr/bin/env bash set -e -# Read the first non-whitespace word from the specified file. -read_version_file() { - local words version - while read -a words; do - version="${words[0]}" - if [ -n "$version" ]; then - echo "$version" - break - fi - done < "$1" -} - DEFAULT_PATH="${HOME}/.rbenv/default" if [ -z "$RBENV_VERSION" ]; then - RBENV_VERSION_FILE="$(rbenv-version-origin)" - - if [ -n "$RBENV_VERSION_FILE" ]; then - RBENV_VERSION="$(read_version_file "$RBENV_VERSION_FILE")" - else - echo system > "$DEFAULT_PATH" - RBENV_VERSION=system - fi + RBENV_VERSION_FILE="$(rbenv-version-file)" + RBENV_VERSION="$(rbenv-version-file-read "$RBENV_VERSION_FILE" || true)" fi if [ "$RBENV_VERSION" = "system" ]; then diff --git a/libexec/rbenv-version-origin b/libexec/rbenv-version-origin index e4e6d552..31653d49 100755 --- a/libexec/rbenv-version-origin +++ b/libexec/rbenv-version-origin @@ -1,30 +1,8 @@ #!/usr/bin/env bash set -e -find_version_file() { - local root="$(pwd)" - while [ -n "$root" ]; do - if [ -e "${root}/.rbenv-version" ]; then - echo "${root}/.rbenv-version" - return 0 - fi - root="${root%/*}" - done - return 1 -} - -DEFAULT_PATH="${HOME}/.rbenv/default" - -find_default_version_file() { - if [ -e "$DEFAULT_PATH" ]; then - echo "$DEFAULT_PATH" - return 0 - fi - return 1 -} - -if [ -z "$RBENV_VERSION" ]; then - find_version_file || find_default_version_file || true -else +if [ -n "$RBENV_VERSION" ]; then echo "RBENV_VERSION environment variable" +else + rbenv-version-file fi