rbenv.plugin.zsh 1.69 KB
Newer Older
1
_homebrew-installed() {
2
3
4
  type brew &> /dev/null
}

5
_rbenv-from-homebrew-installed() {
6
7
8
  brew --prefix rbenv &> /dev/null
}

Peter Hoeg's avatar
Peter Hoeg committed
9
FOUND_RBENV=0
10
rbenvdirs=("$HOME/.rbenv" "/usr/local/rbenv" "/opt/rbenv")
11
if _homebrew-installed && _rbenv-from-homebrew-installed ; then
12
    rbenvdirs=($(brew --prefix rbenv) "${rbenvdirs[@]}")
13
14
15
    if [[ $RBENV_ROOT = '' ]]; then 
      RBENV_ROOT="$HOME/.rbenv"
    fi
16
17
18
fi

for rbenvdir in "${rbenvdirs[@]}" ; do
Peter Hoeg's avatar
Peter Hoeg committed
19
20
  if [ -d $rbenvdir/bin -a $FOUND_RBENV -eq 0 ] ; then
    FOUND_RBENV=1
21
22
23
24
    if [[ $RBENV_ROOT = '' ]]; then
      RBENV_ROOT=$rbenvdir
    fi
    export RBENV_ROOT
Peter Hoeg's avatar
Peter Hoeg committed
25
    export PATH=${rbenvdir}/bin:$PATH
26
    eval "$(rbenv init --no-rehash - zsh)"
Peter Hoeg's avatar
Peter Hoeg committed
27
28
29
30
31
32
33
34
35

    alias rubies="rbenv versions"
    alias gemsets="rbenv gemset list"

    function current_ruby() {
      echo "$(rbenv version-name)"
    }

    function current_gemset() {
36
      echo "$(rbenv gemset active 2&>/dev/null | sed -e ":a" -e '$ s/\n/+/gp;N;b a' | head -n1)"
Peter Hoeg's avatar
Peter Hoeg committed
37
38
39
40
    }

    function gems {
      local rbenv_path=$(rbenv prefix)
Jesse B. Hannah's avatar
Jesse B. Hannah committed
41
42
43
44
45
      gem list $@ | sed -E \
        -e "s/\([0-9a-z, \.]+( .+)?\)/$fg[blue]&$reset_color/g" \
        -e "s|$(echo $rbenv_path)|$fg[magenta]\$rbenv_path$reset_color|g" \
        -e "s/$current_ruby@global/$fg[yellow]&$reset_color/g" \
        -e "s/$current_ruby$current_gemset$/$fg[green]&$reset_color/g"
Peter Hoeg's avatar
Peter Hoeg committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    }

    function rbenv_prompt_info() {
      if [[ -n $(current_gemset) ]] ; then
        echo "$(current_ruby)@$(current_gemset)"
      else
        echo "$(current_ruby)"
      fi
    }
  fi
done
unset rbenvdir

if [ $FOUND_RBENV -eq 0 ] ; then
  alias rubies='ruby -v'
  function gemsets() { echo 'not supported' }
  function rbenv_prompt_info() { echo "system: $(ruby -v | cut -f-2 -d ' ')" }
fi