This repository has been archived by the owner on Mar 4, 2018. It is now read-only.
/
rbfu
executable file
·159 lines (133 loc) · 4.15 KB
/
rbfu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
# Useful methods. \o/
#
set_variable () { export $1="$2"; }
unset_variable () { unset $1; }
path_append () { path_remove $1; set_variable PATH "$PATH:$1"; }
path_prepend () { path_remove $1; set_variable PATH "$1:$PATH"; }
path_remove () { set_variable PATH `echo $PATH | awk -v RS=: -v ORS=: '$0 != "'$1'"' | sed 's/:$//'`; }
command_exists () { type "$1" &> /dev/null; }
warn () { echo -e "$1" >&2; }
info () { warn "$1"; }
deactivate_previous_ruby_version () {
if [ $RBFU_RUBY_VERSION ]; then
path_remove "$RUBIES_ROOT/$RBFU_RUBY_VERSION/bin"
path_remove "$RUBIES_ROOT/$RBFU_RUBY_VERSION/lib/ruby/gems/bin"
unset_variable GEM_PATH
unset_variable GEM_HOME
unset_variable RBFU_RUBY_VERSION
fi
}
_rbfu_activate () {
local VERSION COMMAND MSG SOURCE
local RBFU_ROOT="$HOME/.rbfu"
local RUBIES_ROOT="$RBFU_ROOT/rubies"
# Run-time configuration
#
if [[ $1 =~ ^@ ]]; then
VERSION=$1; shift
VERSION=${VERSION:1}
fi
COMMAND="$@"
SOURCE="from command line"
# fall back to $PWD/.rbfu-version and $HOME/.rbfu-version if no version is given
read_version_from_file () { if [ -f "$1" ]; then SOURCE="from $1"; VERSION=`head -n 1 $1`; fi; }
[ -n "$VERSION" ] || read_version_from_file "$PWD/.rbfu-version"
[ -n "$VERSION" ] || read_version_from_file "$PWD/.ruby-version"
[ -n "$VERSION" ] || read_version_from_file "$HOME/.rbfu-version"
[ -n "$VERSION" ] || read_version_from_file "$HOME/.ruby-version"
# sanity checks
[ ! "$VERSION" ] && warn "Please specify the Ruby version to activate." && return
if [[ "$VERSION" == "system" ]]; then
deactivate_previous_ruby_version
MSG="Activated system Ruby."
else
# additional sanity checks
RUBY_ROOT="$RUBIES_ROOT/$VERSION"
if [ ! -e $RUBY_ROOT ]; then
warn "Ruby $VERSION was requested, but is not installed. ($SOURCE)"
warn "If you have ruby-build installed, you can install the requested version like this:"
warn "\nruby-build $VERSION \$HOME/.rbfu/rubies/$VERSION"
return 1
fi
deactivate_previous_ruby_version
# activate requested version
path_prepend "$RUBY_ROOT/lib/ruby/gems/bin"
path_prepend "$RUBY_ROOT/bin"
set_variable "RBFU_RUBY_VERSION" "$VERSION"
set_variable "GEM_PATH" "$RUBY_ROOT/lib/ruby/gems"
set_variable "GEM_HOME" "$RUBY_ROOT/lib/ruby/gems"
MSG="Activated Ruby $RBFU_RUBY_VERSION."
fi
info "$MSG ($SOURCE)"
# execute command, if given
[ -n "$COMMAND" ] && eval $COMMAND
}
# check for our favorite command line options!
#
if [ "$1" = "--help" ]; then
cat <<EOD
rbfu [@<version>] [command]
EOD
exit 0
# initialization code (run from shell startup script)
#
elif [ "$1" = "--init" ]; then
cat <<EOD
alias rbfu-env="source rbfu"
# enable tab completion for bash and zsh
if command -v complete >/dev/null 2>&1; then
_rbfu_complete_version() {
if [ -d "$HOME/.rbfu/rubies" ]; then
cur="\${COMP_WORDS[COMP_CWORD]}"
opts=\$(\\ls -1 $HOME/.rbfu/rubies | sed -e "s/^/@/")
if [[ \$COMP_CWORD == 1 ]]; then
COMPREPLY=( \$(compgen -W "\${opts} @system" -- \${cur}) )
return 0
fi
fi
}
complete -o nospace -F _rbfu_complete_version rbfu
complete -o nospace -F _rbfu_complete_version rbfu-env
elif command -v compdef >/dev/null 2>&1; then
_rbfu() {
local curcontext="\$curcontext" state line _rubies ret=1
_arguments -w -S '(-)--help[show usage and exit]: :->noargs' '1: :->versions' '*::: :->args' && ret=0
case \$state in
versions)
_rubies=( \$(\\ls -1 "$HOME/.rbfu/rubies" 2>/dev/null | sed -e "s/^/@/") "@system" )
_values 'rubies' \$_rubies && ret=0
;;
args)
_normal && ret=0
;;
esac
return \$ret
}
compdef _rbfu rbfu rbfu-env
fi
EOD
# optional automatic mode
#
if [ "$2" = "--auto" ]; then
cat <<EOD
function _rbfu_auto() {
([ -f "\$PWD/.rbfu-version" ] || [ -f "\$PWD/.ruby-version" ]) && source rbfu true
return 0
}
if [ -n "\$ZSH_VERSION" ]; then
chpwd_functions=(\${chpwd_functions[@]} "_rbfu_auto")
else
function cd() {
builtin cd "\$@" && _rbfu_auto
}
fi
_rbfu_auto
EOD
fi
exit 0
# normal operation
#
else
_rbfu_activate $@
fi