/
utility
287 lines (215 loc) · 5.09 KB
/
utility
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
#!/usr/bin/env bash
#
# Functions RVM is built on
#
# match <value> <string|glob>
match()
{
case "$1" in
$2) return 0 ;;
*) return 1 ;;
esac
}
is_a_function() {
type $1 | head -1 | \grep "function" >/dev/null 2>&1
}
#
# RVM specific functions.
#
__rvm_warn_on_rubyopt()
{
if [[ -n "${RUBYOPT:-""}" ]]; then
rvm_warn \
"Please note: You have the RUBYOPT environment variable set and this \
may interfere with normal rvm operations. We sugges unsetting it."
return 1
else
return 0
fi
}
__rvm_strings()
{
local strings ruby_strings
ruby_strings=($(echo ${rvm_ruby_args:-$rvm_ruby_string}))
for rvm_ruby_string in "${ruby_strings[@]}" ; do
strings="$strings $(__rvm_select ; echo $rvm_ruby_string)"
done
echo $strings
return 0
}
# Return a list of directories under a given base path.
# Derived from rvm_ruby_string.
__rvm_ruby_string_paths_under()
{
local path part parts
path="${1%/}" # Strip off any trailing slash
parts=(${rvm_ruby_string//-/ }) # Strip white space.
echo "$path"
for part in "${parts[@]}" ; do
path="$path/$part"
echo "$path"
done
return 0
}
__rvm_quote_args()
{
local quoted_string=""
for quoted_argument in "$@"; do
if printf "%s" "$quoted_argument" | \grep -v "^[[:alnum:]]$" >/dev/null
then
quoted_string="$quoted_string '$(printf "%s" "$quoted_argument" \
| \sed "s/'/\'\\\'\'/g")'" #"-fix display quotes
else
quoted_string="$quoted_string $quoted_argument"
fi
done
echo "$quoted_string" | \sed -e 's/^ *//g' -e 's/ *$//g'
return 0
}
__rvm_quote_args_with_shift()
{
local shift_value="$1"; shift
while [[ "$shift_value" -gt 0 && $# -gt 0 ]]; do
shift
((shift_value--))
done
__rvm_quote_args "$@"
return 0
}
# Run a specified command and log it.
__rvm_run()
{
local name log temp_log_path command message
true ${rvm_debug_flag:=0} ${rvm_niceness:=0}
name="${1:-}"
command="${2:-}"
message="${3:-}"
if [[ -n "$message" ]]
then
rvm_log "$message"
fi
if (( rvm_debug_flag > 0 ))
then
rvm_debug "Executing: $command"
fi
if [[ -n "${rvm_ruby_string:-}" ]]
then
temp_log_path="${rvm_log_path}/$rvm_ruby_string"
else
temp_log_path="${rvm_log_path}"
fi
log="$temp_log_path/$name.log"
if [[ ! -d "${log%\/*}" ]]
then
\mkdir -p "${log%\/*}"
fi
if [[ ! -f "$log" ]]
then
\touch "$log" # for zsh :(
fi
# TODO: Allow an 'append_flag' setting?
printf "[$(date +'%Y-%m-%d %H:%M:%S')] $command\n" > "$log"
if (( rvm_niceness > 0 ))
then
command="nice -n $rvm_niceness $command"
fi
eval "$command" >> "$log" 2>&1
result=$?
if (( result > 0 ))
then
rvm_error "Error running '$command', please read $log"
fi
return ${result}
}
# Output the current ruby's rvm source path.
__rvm_source_dir()
{
if [[ ${rvm_ruby_selected_flag:-0} -eq 0 ]] ; then __rvm_select ; fi
if [[ -z "$rvm_ruby_src_path" ]] ; then
rvm_error "No source directory exists for the default implementation."
else
echo "$rvm_ruby_src_path"
fi
return 0
}
# Output an inspection of selected 'binary' scripts, based on CLI selection.
__rvm_inspect()
{
for binary in $rvm_ruby_args ; do
actual_file="$(unset -f gem ; command -v gem )"
rvm_log "$actual_file:"
if [[ ${rvm_shebang_flag:-0} -eq 1 ]] ; then
\head -n 1 < "$actual_file"
fi
if [[ ${rvm_env_flag:-0} -eq 1 ]] ; then
\awk '/ENV/' < "$actual_file"
fi
if [[ ${rvm_path_flag:-0} -eq 1 ]] ; then
\awk '/PATH/' < "$actual_file"
fi
if [[ ${rvm_head_flag:-0} -eq 1 ]] ; then
\head -n 5 < "$actual_file"
fi
if [[ ${rvm_tail_flag:-0} -eq 1 ]] ; then
\tail -n 5 < "$actual_file"
fi
if [[ ${rvm_all_flag:-0} -eq 1 ]] ; then
\cat $actual_file
fi
done
return 0
}
# Strip whitespace and normalize it all.
__rvm_strip()
{
\sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[[:space:]]\{1,\}/ /g'
return $?
}
__rvm_remove_from_path()
{
export PATH
PATH=":$PATH:"
PATH="${PATH//:$1:/:}"
PATH="${PATH//::/:}"
PATH="${PATH%:}"
PATH="${PATH#:}"
}
__rvm_add_to_path()
{
export PATH
if (( $# != 2 )) || [[ -z "$2" ]]
then
rvm_error "__rvm_add_to_path requires two parameters"
return 1
fi
__rvm_remove_from_path "$2"
case "$1" in
prepend) PATH="$2:$PATH" ;;
append) PATH="$PATH:$2" ;;
#*) anything else will just remove it from PATH - not adding back
esac
if [[ -n "${rvm_user_path_prefix:-}" ]]
then
__rvm_remove_from_path "${rvm_user_path_prefix}"
PATH="${rvm_user_path_prefix}:$PATH"
fi
builtin hash -r
}
__rvm_detect_xcode_version()
{
typeset version_file
version_file=/Developer/Applications/Xcode.app/Contents/version.plist
if [[ -f $version_file ]]
then
if [[ -x /usr/libexec/PlistBuddy ]]
then
/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" $version_file
else
awk -F"[<>]" '/CFBundleShortVersionString/{ getline ; print $3 }' < $version_file
fi
fi
}
__rvm_detect_xcode_version_is()
{
[[ "$(__rvm_detect_xcode_version)" == "$1" ]]
}