forked from postmodern/chruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchruby.sh
More file actions
78 lines (64 loc) · 1.55 KB
/
chruby.sh
File metadata and controls
78 lines (64 loc) · 1.55 KB
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
typeset -a RUBIES
function chruby_reset()
{
[[ -z "$RUBY" ]] && return
export PATH=":$PATH:"
export PATH=${PATH//:$RUBY\/bin:/:}
if [[ -n "$GEM_HOME" ]] && [[ -n "$GEM_ROOT" ]]; then
export PATH=${PATH//:$GEM_HOME\/bin:/:}
export PATH=${PATH//:$GEM_ROOT\/bin:/:}
fi
export PATH=${PATH#:}
export PATH=${PATH%:}
unset RUBY RUBY_ENGINE RUBY_VERSION RUBYOPT GEM_ROOT GEM_HOME GEM_PATH
hash -r
}
function chruby_use()
{
[[ "$RUBY" == "$1" && "$RUBYOPT" == "$2" ]] && return
[[ -n "$RUBY" ]] && chruby_reset
export RUBY="$1"
export RUBYOPT="$2"
export PATH="$RUBY/bin:$PATH"
eval `ruby - <<EOF
require 'rubygems'
puts "export RUBY_ENGINE=#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'}"
puts "export RUBY_VERSION=#{RUBY_VERSION}"
puts "export GEM_ROOT=\"#{Gem.default_dir}\""
EOF`
if [[ ! $UID -eq 0 ]]; then
export GEM_HOME="$HOME/.gem/$RUBY_ENGINE/$RUBY_VERSION"
export GEM_PATH="$GEM_HOME:$GEM_ROOT"
export PATH="$GEM_HOME/bin:$GEM_ROOT/bin:$PATH"
fi
}
function chruby()
{
local ruby_path
case "$1" in
-h|--help)
echo "usage: chruby [RUBY|VERSION|system] [RUBY_OPTS]"
;;
"")
local star
for ruby_path in ${RUBIES[@]}; do
if [[ "$ruby_path" == "$RUBY" ]]; then star="*"
else star=" "
fi
echo " $star $(basename "$ruby_path")"
done
;;
system) chruby_reset ;;
*)
for ruby_path in ${RUBIES[@]}; do
if [[ `basename "$ruby_path"` == *$1* ]]; then
shift
chruby_use "$ruby_path" "$*"
return
fi
done
echo "Unknown Ruby: $1"
return 1
;;
esac
}