/
ry
executable file
·355 lines (293 loc) · 7.55 KB
/
ry
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env bash
# Library version
VERSION="0.3.2"
[[ -z "$RY_PREFIX" ]] && RY_PREFIX="$HOME/.local"
RY_LIB="${RY_PREFIX}/lib/ry"
#
# Exit with the given <msg ...>
#
abort() {
printf "\033[31mError: $@\033[0m\n" >&2 && exit 1
}
log() {
printf "\033[90mry:\033[0m $@\n"
}
#
# Output ry version.
#
ry::version() {
echo $VERSION
}
#
# check whether a command is defined
#
exists?() {
type "$@" &>/dev/null
}
# indir <dir> <cmd...>
# perform <cmd> in the directory <dir>
#
indir() {
local dir="$1"; shift
local olddir="$PWD"
cd "$dir" && "$@"
local status=$?
cd "$olddir"
return $status
}
# setup
[[ -d $RY_LIB/rubies ]] || mkdir -p $RY_LIB/rubies
if [[ ! -d $RY_LIB/rubies ]]; then
abort "Failed to create rubies directory ($RY_LIB/rubies), do you have permissions to do this?"
fi
# curl / wget support
# curl support
if exists? curl; then
get() { curl -# -L "$@" ;}
# wget support (Added --no-check-certificate for Github downloads)
elif exists? wget; then
get() { wget --no-check-certificate -q -O- "$@" ;}
else
abort "curl or wget required"
fi
# alias
ry::help() { ry usage ;}
#
# Output the current ruby
#
ry::current() {
basename "$(readlink "$RY_LIB/current")"
}
#
# eval "$(ry setup [<name>])"
# a shortcut to load the correct paths and
# activate the completion
#
ry::setup() {
cat <<sh
export PATH="$(ry fullpath "$@")";
if [ -n "\$BASH_VERSION" ]; then . "$RY_PREFIX/lib/ry.bash_completion"; fi
sh
}
# Display current ruby name
# and others installed.
ry::ls() {
for dir in $RY_LIB/rubies/*; do
echo "${dir##*/}"
done
}
# Porcelain version of ry::ls
ry::rubies() {
local active="$(ry current)"
ry ls | while read name; do
if [[ "$name" == "$active" ]]; then
printf " \033[32mο\033[0m $name \033[90m$config\033[0m\n"
else
printf " $name\n"
fi
done
}
#
# ry install <tarball> <name> [config ...]
# Installs the ruby from <tarball> under the given name.
#
# ry install <ruby-name> [<name>]
# If ruby-build is present, use it to install <ruby-name>.
#
ry::install() {
local url="$1"; shift
local name="$1"; shift
# if you've got ruby-build, and you've named one of its recipes,
# use that instead :)
[[ -z "$url" ]] && abort "no URL given. usage: ry install <url> <name>"
[[ -z "$name" ]] && name="$url"
local dir="$RY_LIB/rubies/$name"
if exists? ruby-build \
&& ruby-build --definitions | grep -qx "$url"; then
ruby-build "$url" "$dir"
else
local logpath="/tmp/ry.log"
# create build directory
local builddir="$RY_LIB/rubies/$name/src"
mkdir -p $builddir
echo -n "fetching <$url> ..."
# fetch and unpack
cd $builddir \
&& get "$url" | tar xz --strip-components=1 > $logpath 2>&1
echo "done."
# see if things are alright
if test $? -gt 0; then
echo "\033[31mError: installation failed\033[0m"
echo " ry failed to fetch the tarball,"
echo " or tar failed. Try a different"
echo " version or view $logpath for"
echo " error details."
exit 1
fi
ry build "$name" "$@" || abort "build failed."
fi
ry use "$name"
}
#
# ry build <name> <args...>
# build and install the given ruby from source.
# remaining arguments will be passed to ./configure,
# if it's present.
#
ry::build() {
local name="$1"; shift
local config_args="$@"
assert_installed "$name"
local install_prefix="$RY_LIB/rubies/$name"
local builddir="$install_prefix/src"
pushd "$builddir" > /dev/null
if [[ -x "./configure" ]]; then
log "running configure script."
"./configure --prefix=\"$install_prefix\" $config_args"
./configure --prefix="$install_prefix" "$config_args"
else
log "no ./configure script found, skipping..."
fi
if [[ -f ./Makefile ]]; then
log "Makefile found, building with PREFIX=\"$install_prefix\" make install"
exists? make || abort "make is required to build this ruby."
PREFIX="$install_prefix" make install
elif [[ -x ./installer ]]; then
log "./installer script found, running ./install --auto=\"$install_prefix\""
./installer --auto="$install_prefix"
elif [[ -f ./Rakefile ]]; then
log "Rakefile found, building with PREFIX=\"$install_prefix\" rake install"
exists? rake || abort "rake is required to build this ruby."
PREFIX="$install_prefix" rake install
else
abort "couldn't figure out how to build this ruby."
fi
popd >/dev/null
}
# assert_installed <name>
# abort unless the given ruby is installed
assert_installed() {
if [[ -z "$1" ]]; then
abort "ruby version missing"
elif [[ ! -d "$RY_LIB/rubies/$1" ]]; then
abort "no such ruby: $1"
fi
}
#
# ry use <name>
# ry <name>
# Use the given ruby
#
ry::use() {
local name="$1"
assert_installed "$name"
rm -f "$RY_LIB/current"
indir "$RY_LIB" ln -s "rubies/$name" current
ry current
}
#
# ry binpath <name>
# Print the path to the bin directory for the given ruby.
# You can add this to your path to activate it for a particular
# subshell or process.
#
ry::binpath() {
local name="$1"; shift
assert_installed "$name"
echo "$RY_LIB/rubies/$name/bin"
}
#
# ry fullpath
# ry fullpath <name>
#
# Print a modified version of $PATH that points to the given ruby, or
# to the `current` symlink if no name is given
#
ry::fullpath() {
local name="$1"; shift
if [[ -n "$name" ]]; then
ry binpath "$name"
else
echo "$RY_LIB/current/bin"
fi | tr -d "\n"
tr : "\n" <<<"$PATH" | fgrep -v "$RY_LIB" | while read line; do
echo -n ":$line"
done
}
#
# ry exec <name>[,<name2>[,...]] <command...>
# execute the given command in the context of the given rub{y,ies}
#
ry::exec() {
local names="$1"; shift
names="$(tr , "\n" <<<"$names")"
for name in $names; do
PATH="$(ry fullpath "$name")" "$@"
done
}
#
# Remove <name ...>
#
ry::remove() {
[[ $# == 0 ]] && abort "name(s) required"
while [[ $# != 0 ]]; do
local name="$1"
rm -rf "$RY_LIB/rubies/$name"
shift
done
}
ry::rm() { ry::remove "$@" ;}
#
# Output usage information.
#
ry::usage() {
cat <<-usage
Usage: ry [COMMAND] [args]
Commands:
ry Output the installed rubies
ry ls
ry rubies Output the installed rubies,
and highlight the current one
ry <name> Use the ruby given by <name>
ry use <name>
ry install <tarball> <name>
Download and compile <tarball>,
and install as <name>.
ry install <ruby-name> [<name>]
Requires \`ruby-build\`. Install the ruby-build
recipe named by <ruby-name>, optionally under
the alias <name>.
ry remove <name> Remove the given rubies
ry rm <name>
ry exec <name>[,<name>[,...]] <command...>
Execute <command> in the context of each
comma-separated ruby.
ry binpath <name> Print the bin directory for the given ruby
ry fullpath [<name>] Print a modified version of \$PATH that exclusively
includes the given ruby's path.
If no name is given, uses the \`current\` symlink
Options:
-V, --version Output current version of ry
-h, --help Display help information
usage
}
# Main function (delegates to ry::*)
ry() {
if [[ $# == 0 ]]; then
ry ls
else
case "$1" in
-V|--version) ry version ;;
-h|--help|--usage|-?) ry usage ;;
*)
if exists? "ry::$1"; then
local command="$1"; shift
"ry::$command" "$@"
else
ry::use "$@"
fi
;;
esac
fi
}
ry "$@"