Skip to content

Commit 558709d

Browse files
committed
First add.
0 parents  commit 558709d

File tree

1 file changed

+251
-0
lines changed

1 file changed

+251
-0
lines changed

czruby.plugin.zsh

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
## Change Ruby
2+
3+
# I put the following in ~/.zshenv
4+
5+
# export RUBIES_DEFAULT="2.7.0"
6+
#
7+
# czruby_custom_init(){
8+
# for name in $HOME/Library/Frameworks/Ruby.framework/Versions/*; do
9+
# canon=$(greadlink -f "$name")
10+
# if [[ ${canon:t} != "Current" ]]; then
11+
# rubies=("$canon" $rubies)
12+
# fi
13+
# done
14+
# }
15+
16+
export -TU RUBIES rubies
17+
18+
19+
gem_calc_home(){
20+
local gh="$XDG_CACHE_HOME/Ruby/$RUBY_ENGINE/$RUBY_VERSION"
21+
mkdir -p "$gh"
22+
print -n "$gh"
23+
}
24+
25+
export -TU GEM_PATH gem_path
26+
27+
28+
export RUBY_CONFIG="$HOME/Library/ApplicationSupport/Ruby"
29+
export RUBY_STD_EXES=(bundle bundler erb gem irb racc racc2y rake rdoc ri ruby y2racc)
30+
31+
czruby_datadir="$XDG_DATA_HOME/czruby"
32+
33+
czruby_setup(){
34+
mkdir -p "$czruby_datadir"
35+
if whence -w czruby_custom_init 2>&1 > /dev/null; then
36+
czruby_custom_init
37+
fi
38+
# If system is not found in the rubies then add it
39+
if ! (( $rubies[(Ie)system] )); then
40+
rubies=(system $rubies)
41+
fi
42+
local ruby_root ruby_ver ruby_eng key
43+
for ruby_root in $rubies; do
44+
key="${ruby_root:t}"
45+
if [[ $key == system ]]; then
46+
ruby_root="/usr"
47+
ruby_eng="ruby"
48+
ruby_ver="2.3.7" # TODO remove hardcoding
49+
elif [[ $key =~ "-" ]]; then
50+
ruby_eng=${key:-ruby}
51+
splits=(${(s[-])ruby_eng});
52+
ruby_eng=$splits[1]
53+
ruby_ver=$splits[2]
54+
else
55+
ruby_eng="ruby"
56+
ruby_ver="$key"
57+
fi
58+
59+
if [[ ! -e "$czruby_datadir/$key" ]]; then
60+
cat << EOF > "$czruby_datadir/$key"
61+
export RUBY_ENGINE="$ruby_eng"
62+
export RUBY_ROOT="$ruby_root"
63+
export RUBY_VERSION="$ruby_ver"
64+
export GEM_HOME=\$(gem_calc_home)
65+
gem_path=(\$GEM_HOME "$ruby_root/lib/$ruby_eng/gems/$ruby_ver" \$gem_path)
66+
local bin
67+
for place in \${(Oa)gem_path}; do
68+
bin="\$place/bin"
69+
path=("\$bin" \$path)
70+
done
71+
unset bin
72+
EOF
73+
fi
74+
done
75+
unset ruby_root ruby_ver ruby_eng key
76+
czruby_set_default "$RUBIES_DEFAULT"
77+
}
78+
79+
80+
czruby_set_default(){
81+
local choice="${1:-system}"
82+
[[ -z $choice ]] && return 1
83+
if [[ ! $rubies(Ie)$choice ]]; then
84+
printf "That ruby is not available"
85+
czruby
86+
return 1
87+
fi
88+
local old_default=$RUBIES_DEFAULT
89+
RUBIES_DEFAULT="$choice"
90+
[[ -h $czruby_datadir/default ]] && rm $czruby_datadir/default
91+
ln -s "$czruby_datadir/${RUBIES_DEFAULT}" "$czruby_datadir/default"
92+
# Check if current ruby was the default
93+
# if so, change to new choice
94+
# At worst, either the user has to do this themselves
95+
# or set it back.
96+
if [[ "$RUBY_ENGINE-$RUBY_VERSION" == "$choice" ]] ||
97+
[[ "$RUBY_VERSION" == "$choice" && "$RUBY_ENGINE" == "ruby" ]]; then
98+
czruby "$choice"
99+
fi
100+
}
101+
102+
103+
czruby_reset(){
104+
local ver=${1:-default}
105+
local ruby_root=${2:-$RUBY_ROOT}
106+
local excludes=()
107+
for place in $gem_path; do
108+
bin="$place/bin"
109+
excludes=("$bin" $excludes)
110+
done
111+
if [[ $#gem_path > 0 ]]; then
112+
path=(${path:|excludes})
113+
fi
114+
gem_path=()
115+
unset excludes
116+
source "$czruby_datadir/$ver"
117+
czruby_aliases "$ruby_root"
118+
}
119+
120+
121+
czruby_aliases () {
122+
local oldifs="$IFS"
123+
local ruby_root="$1"
124+
for exe in $RUBY_STD_EXES; do
125+
IFS=$': '
126+
splits=($(whence -w ruby))
127+
if [[ $? == 0 && $splits[2] == "alias" ]]; then
128+
unalias "$exe" 2>/dev/null
129+
fi
130+
if [[ -f "$ruby_root/bin/$exe" ]]; then
131+
alias "$exe"="'$ruby_root/bin/$exe'"
132+
fi
133+
done
134+
IFS="$oldifs"
135+
}
136+
137+
138+
czruby_source(){
139+
# source the file
140+
if [[ -r "$1" ]]; then
141+
source "$1"
142+
else
143+
czruby_setup
144+
if [[ -r "$1" ]]; then
145+
source "$1"
146+
else
147+
# blow up
148+
echo "No source file found at $1"
149+
exit 1
150+
fi
151+
fi
152+
}
153+
154+
155+
czruby_use(){
156+
local dir ruby ruby_root ruby_ver ruby_eng
157+
local matches=()
158+
for ruby_root in $rubies; do
159+
if [[ ${ruby_root:t} =~ "-" ]]; then
160+
ruby_eng=${${ruby_root:t}:-ruby}
161+
splits=(${(s[-])ruby_eng});
162+
ruby_eng=$splits[1]
163+
ruby_ver=$splits[2]
164+
else
165+
ruby_eng="ruby"
166+
ruby_ver="${ruby_root:t}"
167+
fi
168+
if [[ "${ruby_root:t}" == "$1" ]]; then
169+
matches=("$ruby_root" $matches) && break
170+
elif [[ "${ruby_eng}" == "$1" ]]; then
171+
matches=("$ruby_root" $matches)
172+
fi
173+
done
174+
if [[ $#matches == 0 ]]; then
175+
printf "czruby: unknown Ruby: %s\n" $1 >&2
176+
return 1
177+
elif [[ $#matches -ge 2 ]]; then
178+
printf "Too many matches, be more specific\n"
179+
for match in $matches; do
180+
printf '%s %s\n' "${match:t}" "${match}";
181+
done
182+
return 1
183+
fi
184+
czruby_reset "${matches[1]:t}" "$ruby_root"
185+
unset dir ruby splits ruby_root ruby_ver ruby_eng oldifs matches
186+
}
187+
188+
189+
czruby () {
190+
case "$1" in
191+
-h|--help)
192+
printf "usage: czruby [RUBY|VERSION|system | --set-default RUBY] [RUBYOPT...]\n"
193+
;;
194+
# -V|--version)
195+
# echo "czruby: $CZRUBY_VERSION"
196+
# ;;
197+
"") # show available rubies
198+
local marked ruby_root ruby_ver ruby_eng key
199+
local rmarker dmarker smarker
200+
local stopper="%{$reset_color%}"
201+
local default=system
202+
if [[ -r "$czruby_datadir/default" ]]; then
203+
default="$(greadlink -f $czruby_datadir/default)"
204+
default=${default:t}
205+
fi
206+
typeset -A lines
207+
print -Pn -f '%6s| %-11s| %-10s| %4s\n' -- " " engine version root
208+
print -Pn -f '=%.0s' -- {1..40}
209+
print -n '\n'
210+
for ruby_root in $rubies; do
211+
if [[ $ruby_root == "system" ]]; then
212+
ruby_eng="ruby"
213+
ruby_ver="2.3.7"
214+
ruby_root="/usr"
215+
key="system"
216+
else
217+
key="${ruby_root:t}"
218+
if [[ ${ruby_root:t} =~ "-" ]]; then
219+
ruby_eng=${${ruby_root:t}:-ruby}
220+
splits=(${(s[-])ruby_eng});
221+
ruby_eng=$splits[1]
222+
ruby_ver=$splits[2]
223+
else
224+
ruby_eng="ruby"
225+
ruby_ver="${ruby_root:t}"
226+
fi
227+
fi
228+
[[ $RUBY_ROOT == $ruby_root ]] && rmarker="$FG[002]*$stopper " && marked="true" || rmarker=" "
229+
[[ $default == $key ]] && dmarker="$FG[199]*$stopper " || dmarker=" "
230+
[[ $key == "system" ]] && smarker="$FG[247]*$stopper " || smarker=" "
231+
232+
print -Pn -f '%2b%2b%2b %-12s %-11s %s%s\n' -- $rmarker $dmarker $smarker ${ruby_eng} $ruby_ver ${ruby_root} $stopper
233+
unset rmarker dmarker smarker
234+
done
235+
print -Pn -f '\n%b %b %b' "$FG[002]current$stopper" "$FG[199]default$stopper" "$FG[247]system$stopper\n"
236+
unset marked rmarker ruby_root ruby_eng ruby_ver
237+
;;
238+
--set-default) shift;
239+
czruby_set_default $1
240+
;;
241+
system) czruby_reset system ;;
242+
default) czruby_reset ;;
243+
*) czruby_use $1 ;;
244+
esac
245+
}
246+
247+
if [[ -z "$RUBIES_DEFAULT" ]]; then
248+
export RUBIES_DEFAULT="system"
249+
fi
250+
czruby_setup
251+
czruby "$RUBIES_DEFAULT"

0 commit comments

Comments
 (0)