From 66ce809703d31a2a8841c325d8e4af262c6c58c1 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Mon, 10 Jun 2019 15:27:08 +0200 Subject: [PATCH] Implement automatic chruby --- bash/rc | 1 + shell/chruby | 29 +++++++++++++++++++++++++---- zsh/rc | 1 + 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/bash/rc b/bash/rc index 9369360..f8bd87b 100644 --- a/bash/rc +++ b/bash/rc @@ -28,6 +28,7 @@ precmd() { chpwd() { __git_ps1_gitdir _gopath + _auto-chruby } chpwd diff --git a/shell/chruby b/shell/chruby index 384899d..16642e0 100755 --- a/shell/chruby +++ b/shell/chruby @@ -1,7 +1,28 @@ if [[ -f /opt/arch/share/chruby/chruby.sh ]]; then source /opt/arch/share/chruby/chruby.sh - - if [[ -f $HOME/.ruby-version ]]; then - chruby $(cat $HOME/.ruby-version) - fi fi + +_ruby-version() { + local check_dir=$PWD + local next_check_dir=${check_dir%/*} + while [ "$next_check_dir" != "" ]; do + if [[ -f "$check_dir/.ruby-version" ]]; then + cat "$check_dir/.ruby-version" + return + fi + check_dir="$next_check_dir" + next_check_dir=${check_dir%/*} + done + if [[ -f $HOME/.ruby-version ]]; then + cat $HOME/.ruby-version + return + fi + false +} + +_auto-chruby() { + local ruby_version=$(_ruby-version) + if [[ -n "${ruby_version}" ]]; then + chruby "${ruby_version}" + fi +} diff --git a/zsh/rc b/zsh/rc index aa38622..3f97095 100644 --- a/zsh/rc +++ b/zsh/rc @@ -35,6 +35,7 @@ preexec() { chpwd() { __git_ps1_gitdir _gopath + _auto-chruby } chpwd