/
gemsets
executable file
·358 lines (315 loc) · 13.8 KB
/
gemsets
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
356
357
358
#!/usr/bin/env bash
source $rvm_scripts_path/utility
source $rvm_scripts_path/selector
if [[ ! -z "$rvm_trace_flag" ]] ; then set -x ; export rvm_trace_flag ; fi
rvm_ruby_gem_home="${rvm_ruby_gem_home:-$GEM_HOME}"
if [[ ! -d "$rvm_ruby_gem_home" ]] && command -v gem > /dev/null 2>&1; then rvm_ruby_gem_home="$(gem env home)" ; fi
trap "if [[ -d $rvm_tmp_path/ ]] && [[ -s $rvm_tmp_path/$$ ]] ; then rm -f $rvm_tmp_path/$$ > /dev/null 2>&1 ; fi ; exit" 0 1 2 3 15
__rvm_gemset_name() {
__rvm_gemset_dir | awk -F${rvm_gemset_separator} '{print $2}'
}
__rvm_gemset_dir() {
echo "$rvm_ruby_gem_home"
}
__rvm_gemset_create() {
rvm_ruby_gem_prefix=$(echo $rvm_ruby_gem_home | sed 's/'${rvm_gemset_separator}'.*$//')
for gemset in $(echo $gems_args) ; do
gem_home="${rvm_ruby_gem_prefix}${rvm_gemset_separator}${gems_args/ /}"
mkdir -p $gem_home
$rvm_scripts_path/log "info" "Gemset '$gemset' created."
done ; unset gem_home
}
__rvm_gemset_list() {
if [[ -z "$rvm_ruby_selected_flag" ]] ; then __rvm_select ; fi
$rvm_scripts_path/log "info" "gemsets : for $rvm_ruby_string (found in $rvm_gems_path/)"
if [[ ! -z "$rvm_gems_path" ]] ; then
if [[ ! -z $rvm_ruby_string ]] ; then
\ls ${rvm_gems_path}/ | awk -F"${rvm_gemset_separator}" "/${rvm_ruby_string}${rvm_gemset_separator}/{print \$2}" 2>/dev/null
else
$rvm_scripts_path/log "error" "\$rvm_ruby_string is not set!"
fi
else
$rvm_scripts_path/log "error" "\$rvm_gems_path is not set!"
fi
}
__rvm_gemset_delete() {
if [[ -z "$rvm_ruby_selected_flag" ]] ; then __rvm_select ; fi
if [[ ! -z "$rvm_gemset_name" ]] ; then
gemdir="$rvm_gems_path/$rvm_ruby_string${rvm_gemset_separator}$rvm_gemset_name"
if [[ -d "$gemdir" ]] && [[ "$gemdir" != '/' ]] && [[ ! -z "$rvm_force_flag" ]] ; then
rm -rf "$gemdir"
elif [[ -d "$gemdir" ]] ; then
$rvm_scripts_path/log "warn" "Are you SURE you wish to remove the entire gemset directory '$rvm_gemset_name' ($gemdir)?"
echo -n "(anything other than 'yes' will cancel) > "
read response
if [[ "yes" = "$response" ]] ; then
rm -f $gemdir/cache 2>/dev/null
rm -rf $gemdir
else
$rvm_scripts_path/log "info" "Not doing anything, phew... close call that one eh?"
fi
else
$rvm_scripts_path/log "info" "$gemdir already does not exist."
fi ; unset gemdir
else
$rvm_scripts_path/log "error" "A gemset name must be specified in order to delete a gems."
fi
}
__rvm_gemset_empty() {
if [[ -z "$rvm_ruby_gem_home" ]] ; then __rvm_select ; fi
gemdir="$rvm_ruby_gem_home"
if [[ -d "$gemdir" ]] && [[ "$gemdir" != '/' ]] && [[ ! -z "$rvm_force_flag" ]] ; then
builtin cd $gemdir && rm -rf ./bin/* ./doc/* ./gems/* ./specifications/*
elif [[ -d "$gemdir" ]] ; then
$rvm_scripts_path/log "warn" "Are you SURE you wish to remove the installed gemset for gemset '$(basename $gemdir)' ($gemdir)?"
echo -n "(anything other than 'yes' will cancel) > "
read response
if [[ "yes" = "$response" ]] ; then
builtin cd $gemdir && rm -rf ./bin/* ./doc/* ./gems/* ./specifications/*
else
$rvm_scripts_path/log "info" "Not doing anything, phew... close call that one eh?"
fi
else
$rvm_scripts_path/log "info" "$gemdir already does not exist."
fi ; unset gemdir
}
# Migrate gemsets from ruby X to ruby Y
__rvm_gemset_copy() {
source_ruby="$(echo $gems_args | awk '{print $1}')"
destination_ruby="$(echo $gems_args | awk '{print $2}')"
if [[ -z "$source_ruby" ]] ; then
$rvm_scripts_path/log "error" "Source and destination must be specified: 'rvm gemset copy X Y'"
fi
if [[ -z "$destination_ruby" ]] ; then
$rvm_scripts_path/log "error" "Source and destination must be specified: 'rvm gemset copy X Y'"
fi
source_path="$(rvm $source_ruby gem env gemdir | tail -n 1)"
destination_path="$(rvm $destination_ruby gem env gemdir | tail -n 1)"
if [[ -d "$source_path" ]] ; then
if [[ ! -d "$destination_path" ]] ; then mkdir -p $destination_path ; fi
$rvm_scripts_path/log "info" "Copying gemset from $source_ruby to $destination_ruby"
for dir in bin doc gems specifications ; do
mkdir -p "$destination_path/$dir"
cp -Rf "$source_path/$dir" "$destination_path/"
done
$rvm_scripts_path/log "info" "Making gemset for $destination_ruby pristine."
$(rvm $destination_ruby ; gem pristine --all)
else
$rvm_scripts_path/log "error" "Gems directory does not exist for $source_path ($source_path)"
return 1
fi
unset source_ruby destination_ruby source_path destination_path
}
__rvm_gemset_export() {
rvm_file_name="${rvm_file_name:-$gems_args}"
if [[ ! -z "$rvm_ruby_gem_home" ]] ; then
export GEM_HOME="$rvm_ruby_gem_home"
export GEM_PATH="$rvm_ruby_gem_home/bin:$rvm_gems_path/$rvm_ruby_string${rvm_gemset_separator}global/bin"
export BUNDLE_PATH="$rvm_ruby_gem_home"
fi
if [[ -z "$rvm_file_name" ]] ; then
if [[ ! -z "$rvm_gemset_name" ]] ; then
rvm_file_name="$rvm_gemset_name.gems"
else
rvm_file_name="default.gems"
fi
fi
$rvm_scripts_path/log "info" "Exporting current environments gemset to $rvm_file_name"
touch $rvm_file_name
echo "# $rvm_file_name generated gem export file. Note that any env variable settings will be missing. Append these after using a ';' field separator" > $rvm_file_name
for gem in $(gem list | sed 's#[\(|\)]##g' | sed 's#, #,#g' | tr ' ' ';') ; do
name="$(echo $gem | awk -F';' '{print $1}')"
if [[ -z "$rvm_latest_flag" ]] ; then
versions="$(echo $gem | awk -F';' '{print $2}' | sed 's#,# #g')"
for version in $versions ; do
echo "$name -v$version" >> $rvm_file_name
done ; unset version versions
else
echo "$name" >> $rvm_file_name
fi ; unset name
done ; unset file_name
}
__rvm_gemset_import() {
if [[ ! -z "$rvm_ruby_gem_home" ]] ; then
export GEM_HOME="$rvm_ruby_gem_home"
export GEM_PATH="$rvm_ruby_gem_home/bin:$rvm_gems_path/$rvm_ruby_string${rvm_gemset_separator}global/bin"
export BUNDLE_PATH="$rvm_ruby_gem_home"
fi
if [[ ! -z "$(echo "$gems_args" | awk '/\.gem.*$/')" ]] ; then
rvm_gemset_name="$(echo "$gems_args" | sed 's#\.gem.*##g' | sed 's# ##g')"
else
rvm_gemset_name="$(echo "$gems_args" | sed 's# ##g')"
fi
if [[ -s "$rvm_gemset_name.gems" ]] ; then
rvm_file_name="$rvm_gemset_name.gems"
elif [[ -s "default.gems" ]] ; then
rvm_file_name="default.gems"
elif [[ -s "system.gems" ]] ; then
rvm_file_name="system.gems"
elif [[ -s ".gems" ]] ; then
rvm_file_name=".gems"
else
$rvm_scripts_path/log "error" "No *.gems file found."
return 1
fi
mkdir -p "$rvm_gems_path/cache" # Ensure the base cache dir is initialized.
if [[ -s "$rvm_file_name" ]] ; then
echo "Importing $rvm_file_name file..."
rvm_ruby_gem_list=$(\ls $rvm_ruby_gem_home/specifications/ 2> /dev/null | sed 's#.gems.*$##' 2> /dev/null)
while read -r line
do # Keep this on 2nd line :(
if [[ ! -z "$(echo $line | sed 's/\s//g')" ]] ; then
gems_args="$line" ; __rvm_gem_install
fi
done < <(awk '/^[^#]+/{print}' "${rvm_file_name}")
else
$rvm_scripts_path/log "error" "${rvm_file_name} does not exist to import from."
fi
}
__rvm_parse_gems_args() {
gem="$(echo $gems_args | awk -F';' '{print $1}')"
gem_prefix="$(echo $gems_args | awk -F';' '{print $2}')"
if $rvm_scripts_path/match "$gem" "\.gem$" ; then
gem_name="$(basename $gem | sed 's#\.gem##' | awk -F'-' '{$NF=NULL;print}')"
gem_version="$(basename $gem | sed 's#\.gem##' | awk -F'-' '{print $NF}' )"
gem_postfix="$(basename $gem | sed "s#.*\.gem##")"
else
gem_name="$(echo $gem | awk '{print $1}')"
if $rvm_scripts_path/match "$gem" "--version" ; then
gem_version="$(echo $gem | sed 's#.*--version[=]*[ ]*##' | awk '{print $1}')"
gem_postfix="$(echo $gem | sed "s#${gem_name/ /}##" | sed "s#--version[=]*[ ]*${gem_version/ /}##")"
elif $rvm_scripts_path/match "$gem" "-v" ; then
gem_version="$(echo $gem | sed 's#.*-v[=]*[ ]*##' | awk '{print $1}')"
gem_postfix="$(echo $gem | sed "s#${gem_name/ /}##" | sed "s#-v[=]*[ ]*${gem_version/ /}##")"
else
unset gem_version # no version
fi
fi
if [[ -s "$gem" ]] ; then
gem_file_name="$gem"
elif $rvm_scripts_path/match "$gem" ".gem$" ; then
gem_file_name="$gem"
elif [[ -z "${gem_version/ /}" ]] ; then
gem_file_name="${gem_name/ /}*.gem"
else # version
gem_file_name="${gem_name/ /}-${gem_version/ /}.gem"
fi
}
# Install a gem
__rvm_gem_install() {
# First we parse the gem args to pick apart the pieces.
__rvm_parse_gems_args
# Now we determine if a .gem cache file is already installed
if [[ -z "$rvm_force_flag" ]] && [[ -f "${rvm_ruby_gem_home}/specifications/$(basename $gem_file_name)spec" ]] ; then
unset gem
$rvm_scripts_path/log "info" "$gem_name $gem_version exists, skipping (--force to re-install)"
else
if [[ -s "$gem" ]] ; then
cache_file="$gem"
elif [[ -s "$rvm_ruby_gem_home/cache/${gem_file_name}" ]] ; then
cache_file="$rvm_ruby_gem_home/cache/${gem_file_name}"
else
cache_file="${cache_file:-$(\ls ${rvm_ruby_gem_home}/cache/${gem_file_name} 2> /dev/null | sort | head -n1)}"
fi
if [[ ! -s "$cache_file" ]] ; then
if [[ -s "$gem_file_name" ]] ; then
gem="$gem_file_name"
elif [[ -z "${gem_version/ /}" ]] ; then
gem="${gem_name/ /}"
else
gem="${gem_name/ /} -v $gem_version"
fi
else # cached
gem_file_name="$(basename "$cache_file")"
gem_string="$(echo "$gem_file_name" | sed 's#\.gem$##')"
if [[ -z "$rvm_force_flag" ]] && [[ -s "${rvm_ruby_gem_home}/specifications/$(basename $gem_file_name)spec" ]] ; then
unset gem # already installed, not forcing reinstall.
$rvm_scripts_path/log "info" "$gem_name $gem_version exists, skipping (--force to re-install)"
else
if [[ -s "$rvm_ruby_gem_home/cache/$(basename $gem_file_name)" ]] ; then
mkdir -p "$rvm_tmp_path/$$/"
mv "$rvm_ruby_gem_home/cache/$gem_file_name" "$rvm_tmp_path/$$/$gem_file_name"
gem="$rvm_tmp_path/$$/$gem_file_name -f -l"
else
gem="$cache_file"
fi
fi
fi
fi
# If $gem is still set, go forward with the install.
if [[ ! -z "$gem" ]] ; then
# TODO: Set vars if fourth field is non-empty (means that there are conditional statements to execute in the gem install line.
if [[ "rvm_make_flags_flag" -eq 1 ]] ; then __rvm_make_flags ; fi
if [[ ! -z "$rvm_ruby_gem_home" ]] && [[ "$rvm_ruby_gem_home" != "$rvm_gems_path" ]] ; then
#command="GEM_HOME='$rvm_ruby_gem_home' GEM_PATH='$rvm_ruby_gem_home/bin:$rvm_gems_path/${rvm_ruby_string}${rvm_gemset_separator}global/bin' BUNDLE_PATH='${rvm_ruby_gem_home}' ${gem_prefix} gem install --ignore-dependencies $gems_args $rvm_gem_options $gem_postfix $vars"
command="GEM_HOME='$rvm_ruby_gem_home' GEM_PATH='$rvm_ruby_gem_home/bin:$rvm_gems_path/${rvm_ruby_string}${rvm_gemset_separator}global/bin' BUNDLE_PATH='${rvm_ruby_gem_home}' ${gem_prefix} gem install $gems_args $rvm_gem_options $gem_postfix $vars"
else
#--ignore-dependencies
command="$gem_prefix gem install --ignore-dependencies $gems_args $rvm_gem_options -q $gem $gem_postfix $vars"
fi
__rvm_run "gem.install" "$command" "installing ${gem_name} ${gem_version}..."
result=$?
if [[ $result -eq 0 ]] ; then
$rvm_scripts_path/log "info" "$gem_name $gem_version installed, output logged to:\n$rvm_path/log/$rvm_ruby_string/gem.install.log"
else
$rvm_scripts_path/log "error" "$gem_name $gem_version failed to install, output logged to:\n$rvm_path/log/$rvm_ruby_string/gem.install.error.log"
fi
fi ; unset gem gem_prefix gem_name gem_version gem_file_name gem_postfix cache_file gem_file_name gem_string gem_action
return $result
}
# Output the user's current gem directory.
__rvm_gemset_info() {
if [[ "$rvm_user_flag" -eq 1 ]] ; then
echo $(rvm system ; gem env | grep "\- $HOME" | awk '{print $NF}')
elif [[ "$rvm_system_flag" -eq 1 ]] ; then
echo $(rvm system ; gem env $action system)
elif [[ ! -z "$rvm_ruby_string" ]] ; then
echo $(rvm "$rvm_ruby_string" ; gem env $action)
elif [[ ! -z "$GEM_HOME" ]] ; then
echo "$GEM_HOME"
else
gem env $action
fi
}
__rvm_gem_pristine() {
gem pristine --all
}
unset GEM_PATH
if ! command -v gem > /dev/null ; then
$rvm_scripts_path/log "error" "'gem' was not found, cannot perform gem actions (Do you have an RVM ruby selected?)"
exit 1
fi
action="$(echo $* | awk '{print $1}')"
gems_args=$(echo "$*" | awk '{$1="" ; print}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
export rvm_gemset_name
if [[ "import" = "$action" ]] || [[ "load" = "$action" ]] ; then
__rvm_gemset_import
elif [[ "export" = "$action" ]] || [[ "dump" = "$action" ]] ; then
__rvm_gemset_export
elif [[ "create" = "$action" ]] ; then
__rvm_gemset_create
elif [[ "copy" = "$action" ]] ; then
__rvm_gemset_copy
elif [[ "empty" = "$action" ]] ; then
__rvm_gemset_empty
elif [[ "delete" = "$action" ]] ; then
__rvm_gemset_delete
elif [[ "name" = "$action" ]] || [[ "string" = "$action" ]]; then
__rvm_gemset_name
elif [[ "dir" = "$action" ]] ; then
__rvm_gemset_dir
elif [[ "list" = "$action" ]] ; then
__rvm_gemset_list
elif [[ "gemdir" = "$action" ]] || [[ "gempath" = "$action" ]] || [[ "gemhome" = "$action" ]] || [[ "home" = "$action" ]] || [[ "path" = "$action" ]] || [[ "version" = "$action" ]] ; then
__rvm_gemset_info
elif [[ "install" = "$action" ]] ; then
__rvm_gem_install $*
elif [[ "pristine" = "$action" ]] ; then
__rvm_gem_pristine $*
elif [[ "clear" = "$action" ]] ; then
$rvm_scripts_path/log "info" "gemset cleared."
exit 0
else
$rvm_scripts_path/log "error" "Unrecognized gemset action '$action'.\n\nValid gems actions are: {import,export,create,copy,empty,delete,name,dir,list,gemdir,install,pristine,clear}"
fi
exit $?