-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
gemsets
executable file
·1027 lines (929 loc) · 27.2 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
source "$rvm_scripts_path/base"
source "$rvm_scripts_path/functions/build" # For gems with C extensions.
rvm_ruby_gem_home="${rvm_ruby_gem_home:-$GEM_HOME}"
if [[ ! -d "$rvm_ruby_gem_home" ]] && builtin command -v gem > /dev/null 2>&1
then
rvm_ruby_gem_home="$(gem env home)"
fi
usage()
{
cat -v "${rvm_help_path}/gemset"
}
gemset_list_all()
{
for rvm_ruby_string in $( rvm_project_rvmrc=0 rvm list strings )
do
(__rvm_use ; gemset_list)
done
unset rvm_ruby_string
}
gemset_list_strings()
{
typeset gem_string
for rvm_ruby_string in $( rvm_project_rvmrc=0 rvm list strings )
do
for gem_string in "${rvm_gems_path:-${rvm_path}/gems}/${rvm_ruby_string}" "${rvm_gems_path:-${rvm_path}/gems}/${rvm_ruby_string}${rvm_gemset_separator:-@}"*
do
printf "%b" "${gem_string##*/}\n"
done
done
unset rvm_ruby_string
}
gemset_update()
{
if [[ -z "$rvm_ruby_strings" ]]
then
rvm_log "Running gem update for all rubies and gemsets."
rvm_ruby_strings="$(
chpwd_functions="" builtin cd "${rvm_gems_path:-"$rvm_path/gems"}" ;
find . -maxdepth 1 -mindepth 1 -type d -print 2>/dev/null |
GREP_OPTIONS="" \grep -v '^\(doc\|cache\|@\|system\)' | \tr '\n' ','
)"
rvm_ruby_strings="${rvm_ruby_strings/%,}"
rvm_ruby_strings="${rvm_ruby_strings//.\/}"
else
rvm_log "Running gem update for the specified rubies."
fi
export rvm_ruby_strings
"$rvm_scripts_path/set" gem update
return $?
}
gemset_globalcache()
{
typeset gc_status globalcache_enabled directories directory_name \
full_directory_path directory_name
if [[ "$1" == "enabled" ]]
then
if __rvm_using_gemset_globalcache
then
gc_status="Enabled"
globalcache_enabled=0
else
gc_status="Disabled"
globalcache_enabled=1
fi
rvm_log "Gemset global cache is currently: $gc_status"
return $globalcache_enabled
elif [[ "$1" == "disable" ]]
then
rvm_log "Removing the global cache (note: this will empty the caches)"
__rvm_read_lines directories < <(
chpwd_functions="" builtin cd "${rvm_gems_path:-"$rvm_path/gems"}" ;
find . -maxdepth 1 -mindepth 1 -type d -print | cut -c 3-
)
for directory_name in "${directories[@]//.\/}"
do
current_cache_path="${rvm_gems_path:-"$rvm_path/gems"}/$directory_name/cache"
if [[ -L "$current_cache_path" \
&& "$(readlink "$current_cache_path")" == "$rvm_gems_cache_path" ]]
then
rvm_log "Reverting the gem cache for $directory_name to an empty directory."
rm -f "$current_cache_path" 2>/dev/null
mkdir -p "$current_cache_path" 2>/dev/null
fi
done
"$rvm_scripts_path/db" "$rvm_user_path/db" "use_gemset_globalcache" "delete"
elif [[ "$1" == "enable" ]]
then
rvm_log "Enabling global cache for gems."
mkdir -p "$rvm_gems_cache_path"
__rvm_read_lines directories < <(
chpwd_functions="" builtin cd "${rvm_gems_path:-"$rvm_path/gems"}" ;
find . -maxdepth 1 -mindepth 1 -type d -print | cut -c 3-
)
for directory_name in "${directories[@]//.\/}"
do
current_cache_path="${rvm_gems_path:-"$rvm_path/gems"}/$directory_name/cache"
if [[ -d "$current_cache_path" && ! -L "$current_cache_path" ]]
then
rvm_log "Moving the gem cache for $directory_name to the global cache."
mv "$current_cache_path/"*.gem "$rvm_gems_cache_path/" 2>/dev/null
case "${current_cache_path%\/}" in
*cache)
__rvm_rm_rf "$current_cache_path"
ln -fs "$rvm_gems_cache_path" "$current_cache_path"
;;
esac
fi
done
"$rvm_scripts_path/db" "$rvm_user_path/db" "use_gemset_globalcache" "true"
else
printf "%b" "
Usage:
rvm gemset globalcache {enable,disable,enabled}
Enable / Disable / Status the use of a global gem cachedir.
"
return 1
fi
}
gemset_name()
{
echo "${rvm_ruby_gem_home##*${rvm_gemset_separator:-"@"}}"
return $?
}
gemset_dir()
{
echo "$rvm_ruby_gem_home"
return $?
}
gemset_create()
{
typeset gem_home gemset gemsets prefix
[[ -z "$rvm_ruby_string" ]] || __rvm_select
prefix=$(echo $rvm_ruby_gem_home | sed 's/'${rvm_gemset_separator:-"@"}'.*$//')
for gemset in "$@"
do
if
[[ "$(__rvm_env_string)" == "system" ]]
then
rvm_error "Can not create gemset before using a ruby. Try 'rvm use <some ruby>'."
return 1
elif
[[ "$gemset" == *"${rvm_gemset_separator:-"@"}"* ]]
then
rvm_error "Can not create gemset '$gemset', it contains a \"${rvm_gemset_separator:-"@"}\"."
return 2
elif
[[ -z "$gemset" || "$gemset" == *"${rvm_gemset_separator:-"@"}" ]]
then
rvm_error "Can not create gemset '$gemset', Missing name. "
return 3
fi
gem_home="${prefix}${rvm_gemset_separator:-"@"}${gemset}"
[[ -d "$gem_home/bin" ]] || mkdir -p "$gem_home/bin"
: rvm_gems_cache_path:${rvm_gems_cache_path:=${rvm_gems_path:-"$rvm_path/gems"}/cache}
# When the globalcache is enabled, we need to ensure we setup the cache directory correctly.
if
__rvm_using_gemset_globalcache
then
if
[[ -d "$gem_home/cache" && ! -L "$gem_home/cache" ]]
then
mv "$gem_home/cache"/*.gem "$rvm_gems_cache_path/" 2>/dev/null
__rvm_rm_rf "$gem_home/cache"
fi
ln -fs "$rvm_gems_cache_path" "$gem_home/cache"
fi
if
(
rvm_ruby_string="$(__rvm_env_string)${rvm_gemset_separator:-"@"}${gemset}"
rvm_create_flag=1
rvm_verbose_flag=0
__rvm_select
)
then
rvm_log "gemset created $gemset\t=> $gem_home"
else
rvm_error "Can not create environment file for '$gemset', Could not use ruby. "
return 4
fi
done
}
__gemset_list_single()
{
typeset gemset current_gemset ruby_name
gemset="$1"
current_gemset="$2"
gemset="${gemset##*/}"
ruby_name="${gemset%%${rvm_gemset_separator:-@}*}"
gemset="${gemset#${ruby_name}}"
gemset="${gemset#${rvm_gemset_separator:-@}}"
[[ -n "${gemset}" ]] || gemset="(default)"
[[ -n "${current_gemset}" ]] || current_gemset="(default)"
if
[[ "${gemset}" == "${current_gemset}" ]]
then
if [[ "${_second_param}" == "strings" ]]
then echo "${gemset} (current)"
else echo "=> ${gemset}"
fi
else
if [[ "${_second_param}" == "strings" ]]
then echo "${gemset}"
else echo " ${gemset}"
fi
fi
}
gemset_list()
{
if [[ ${rvm_ruby_selected_flag:-0} -eq 0 ]]
then
__rvm_select
fi
[[ -d "${rvm_gems_path:-"$rvm_path/gems"}" ]] || {
rvm_error "${rvm_gems_path:-"$rvm_path/gems"} does not exist!"
return 1
}
[[ -n "${rvm_ruby_string:-""}" ]] || {
rvm_error "\$rvm_ruby_string is not set!"
return 1
}
typeset current_gemset IFS
current_gemset=$(__rvm_current_gemset)
IFS=""
[[ "$action" == "strings" ]] ||
rvm_log "\ngemsets for $rvm_ruby_string (found in ${rvm_gems_path:-"$rvm_path/gems"}/$rvm_ruby_string)"
for gemset in ${rvm_gems_path:-${rvm_path}/gems}/${rvm_ruby_string} ${rvm_gems_path:-${rvm_path}/gems}/${rvm_ruby_string}${rvm_gemset_separator:-@}*
do
__gemset_list_single "${gemset}" "${current_gemset}"
done
[[ "$action" == "strings" ]] || printf "%b" "\n"
}
gemset_delete_task()
{
typeset rvm_gemset_name ruby_at_gemset gemdir
rvm_gemset_name=$1
ruby_at_gemset=$2
gemdir=$3
__rvm_rm_rf "$gemdir" &&
if [[ -L "$gemdir/cache" ]]
then rm -f "$gemdir/cache"
fi &&
(
for item in $( $rvm_scripts_path/alias search_by_target ${ruby_at_gemset} )
do
$rvm_scripts_path/alias delete ${item}
done
find "${rvm_bin_path:=$rvm_path/bin}" \( -name \*${ruby_at_gemset} -or -lname \*${ruby_at_gemset}/\* \) -delete
) &&
rm -rf "${rvm_wrappers_path:="$rvm_path/wrappers"}/${ruby_at_gemset}"
}
gemset_delete()
{
if (( ${rvm_ruby_selected_flag:-0} == 0))
then __rvm_ruby_string
fi
[[ -n "$rvm_gemset_name" ]] ||
{
rvm_error "A gemset name must be specified in order to delete a gemset."
return 1
}
ruby_at_gemset="$rvm_ruby_string${rvm_gemset_separator:-"@"}$rvm_gemset_name"
gemdir="${rvm_gems_path:-$rvm_path/gems}/${ruby_at_gemset}"
[[ -d "$gemdir" ]] ||
{
rvm_warn "$gemdir did not previously exist. Ignoring."
return 0
}
(( ${rvm_force_flag:-0} )) ||
__rvm_ask_for "Are you SURE you wish to remove the entire gemset directory '$rvm_gemset_name' ($gemdir)?" yes ||
{
rvm_log "Not doing anything, phew... close call that one eh?"
return 2
}
__rvm_log_command "gemset.delete" "Removing gemset $rvm_gemset_name" \
gemset_delete_task "$rvm_gemset_name" "$ruby_at_gemset" "$gemdir"
}
gemset_empty()
{
typeset gemdir entry
if [[ -z "${rvm_ruby_gem_home:-""}" ]]
then __rvm_select
fi
if [[ -n "${rvm_gemset_name}" ]]
then
rvm_gemset_name="${rvm_gemset_name#default}"
ruby_at_gemset="$rvm_ruby_string${rvm_gemset_name:+${rvm_gemset_separator:-"@"}}${rvm_gemset_name}"
gemdir="${rvm_gems_path:-"$rvm_path/gems"}/${ruby_at_gemset}"
else
gemdir="${rvm_ruby_gem_home}"
fi
if
[[ ${rvm_force_flag:-0} -gt 0 ]]
then
for entry in "$gemdir"/bin/* "$gemdir"/doc/* "$gemdir"/gems/* "$gemdir"/specifications/*
do
__rvm_rm_rf "$entry"
done
elif
[[ -d "$gemdir" ]]
then
if
__rvm_ask_for "Are you SURE you wish to remove the installed gems for gemset '$(basename "$gemdir")' ($gemdir)?" yes
then
for entry in "$gemdir"/bin/* "$gemdir"/doc/* "$gemdir"/gems/* "$gemdir"/specifications/*
do
__rvm_rm_rf "$entry"
done
else
rvm_log "Not doing anything, phew... close call that one eh?"
return 2
fi
else
rvm_log "$gemdir did not previously exist. Ignoring."
fi
}
# Migrate gemsets from ruby X to ruby Y
gemset_copy()
{
typeset source_ruby destination_ruby source_path destination_path
# Clear the current environment so that it does not influence this operation.
unset rvm_gemset_name rvm_ruby_gem_home GEM_HOME GEM_PATH
source_ruby="${1:-}"
destination_ruby="${2:-}"
shift 2
if
[[ -z "$destination_ruby" || -z "$source_ruby" ]]
then
rvm_error "Source and destination must be specified: 'rvm gemset copy X Y'"
return 1
fi
# Verify the destination gemset exists before attempting to use it.
if
! (
rvm_ruby_string="$destination_ruby"
export rvm_create_flag=1
{ __rvm_ruby_string && __rvm_gemset_select; } 2> /dev/null
)
then
rvm_error "Destination gemset '$destination_ruby' does not yet exist."
return 1
fi
# TODO: Account for more possibilities:
# rvm gemset copy 1.9.2 @gemsetb # From 1.9.2 default to current ruby, 1.9.2 exists.
# rvm gemset copy @gemseta @gemsetb # Current ruby, gemseta exists.
# rvm gemset copy gemseta gemsetb # Currenty Ruby, gemseta exists.
# rvm gemset copy gemseta 1.8.7@gemsetb # Currenty Ruby@gemseta, current ruby@gemseta exists.
source_path=$(
rvm_ruby_string="$source_ruby"
{ __rvm_ruby_string && __rvm_gemset_select; } > /dev/null 2>&1
echo $rvm_ruby_gem_home
)
destination_path=$(
rvm_ruby_string="$destination_ruby"
{ __rvm_ruby_string && __rvm_gemset_select; } > /dev/null 2>&1
echo $rvm_ruby_gem_home
)
if
[[ -z "$source_path" || ! -d "$source_path" ]]
then
rvm_error "Unable to expand '$source_ruby' or directory does not exist."
return 1
elif
[[ -z "$destination_path" ]]
then
rvm_error "Unable to expand '$destination_ruby'."
return 1
fi
if
[[ -d "$source_path" ]]
then
rvm_log "Copying gemset from $source_ruby to $destination_ruby"
for dir in bin doc gems specifications cache bundle
do
mkdir -p "$destination_path/$dir"
if
[[ -d "$source_path/$dir" ]]
then
cp -Rf "$source_path/$dir" "$destination_path/"
elif
[[ -L "$source_path/$dir" ]]
then
cp "$source_path/$dir" "$destination_path/$dir"
else
rvm_warn "Unknown file type for: $source_path/$dir"
fi
done
rvm_log "Making gemset for $destination_ruby pristine."
__rvm_run_with_env "gemset.pristine" "$destination_ruby" "rvm gemset pristine"
else
rvm_error "Gems directory does not exist for $source_path ($source_path)"
return 1
fi
}
# Migrate gemsets from ruby X to ruby Y
gemset_rename()
{
typeset source_name destination_name source_path destination_path
source_name="${1:-}"
destination_name="${2:-""}"
shift 2
if
[[ -z "$destination_name" || -z "$source_name" ]]
then
rvm_error "Source and destination gemsets must be specified: 'rvm gemset rename X Y'"
return 1
fi
source_path="$(rvm_silence_logging=1 rvm_gemset_name=${source_name} __rvm_use "${rvm_ruby_string}@${source_name}" ; gem env gemdir)"
if
[[ -z "$source_path" || ! -d "$source_path" ]]
then
rvm_error "gemset '$source_name' does not exist."
return 2
fi
destination_path=${source_path/%$source_name/$destination_name}
if
[[ -d "$source_path" ]]
then
if
[[ ! -d "$destination_path" ]]
then
__rvm_log_command "gemset.rename" "Renaming gemset ${source_path##*/} => ${destination_path##*/}" mv "$source_path" "$destination_path"
__rvm_run_with_env "gemset.pristine" "${destination_path##*/}" "rvm gemset pristine" "Making gemset ${destination_path##*/} pristine."
else
rvm_error "Gemset $destination_name already exists!"
return 1
fi
else
rvm_error "Gems directory does not exist for $source_path ($source_path)"
return 1
fi
}
gemset_unpack()
{
typeset gems name directory version _platforms
directory="${1:-vendor/gems}"
if
[[ -n "$rvm_ruby_gem_home" ]]
then
export GEM_HOME="$rvm_ruby_gem_home"
export GEM_PATH="$rvm_ruby_gem_home:$rvm_ruby_global_gems_path"
fi
rvm_log "Unpacking current environments gemset to ${directory}\n"
unset -f gem
while read gem_name version _platforms
do
( command gem unpack "$gem_name" -v"$version" --target "$directory" )&
done < <(
GEM_PATH="$GEM_HOME" __rvm_list_gems
)
wait
rvm_log "Unpacking into ${directory} complete\n"
return 0
}
gemset_export()
{
typeset rvm_file_name gem_name version versions _platforms
rvm_file_name="${1:-}"
if
[[ -n "$rvm_ruby_gem_home" ]]
then
export GEM_HOME="$rvm_ruby_gem_home"
export GEM_PATH="$rvm_ruby_gem_home:$rvm_ruby_global_gems_path"
fi
if
[[ -n "$rvm_file_name" ]]
then
[[ "${rvm_file_name}" =~ Gemfile ]] || rvm_file_name="${rvm_file_name%.gems}.gems"
else
if [[ -n "$rvm_gemset_name" ]]
then rvm_file_name="$rvm_gemset_name.gems"
else rvm_file_name="default.gems"
fi
fi
rvm_log "Exporting current environments gemset to $rvm_file_name"
if [[ -f "$rvm_file_name" ]]
then rm -f "$rvm_file_name"
fi
if
[[ "${rvm_file_name}" =~ Gemfile ]]
then
printf "%b" "source :rubygems
#ruby=${GEM_HOME##*/}
"
else
printf "%b" "# $rvm_file_name generated gem export file. \
Note that any env variable settings will be missing. \
Append these after using a ';' field separator
"
fi > "$rvm_file_name"
if
(( ${rvm_latest_flag:-0} == 0 ))
then
while read gem_name version _platforms
do
if [[ "${rvm_file_name}" =~ Gemfile ]]
then echo "gem '$gem_name', '$version'"
else echo "$gem_name -v$version"
fi
done < <( GEM_PATH="$GEM_HOME" __rvm_list_gems )
else
while read gem_name versions
do
if [[ "${rvm_file_name}" =~ Gemfile ]]
then echo "gem '$gem_name'"
else echo "$gem_name"
fi
done < <( GEM_PATH="$GEM_HOME" gem list )
fi >> "$rvm_file_name"
}
gemset_import()
{
typeset rvm_file_name
typeset -a gem_file_names
unset -f gem
if
[[ -n "${rvm_ruby_gem_home:-""}" ]]
then
export GEM_HOME="$rvm_ruby_gem_home"
export GEM_PATH="$rvm_ruby_gem_home"
else
rvm_ruby_gem_home=${GEM_HOME:-$(gem env gemdir)}
fi
gem_file_names=(
"${1%.gems*}.gems"
"${rvm_gemset_name}.gems"
"default.gems"
"system.gems"
".gems"
)
__rvm_find_first_file rvm_file_name "${gem_file_names[@]}" ||
{
rvm_error "No *.gems file found."
return 1
}
[[ -d "$rvm_ruby_gem_home/specifications/" ]] || mkdir -p "$rvm_ruby_gem_home/specifications/"
[[ -d "$rvm_gems_cache_path" ]] || mkdir -p "$rvm_gems_cache_path" # Ensure the base cache dir is initialized.
if
[[ -s "$rvm_file_name" ]]
then
rvm_log "\nInstalling gems listed in $rvm_file_name file...\n"
typeset -a lines
__rvm_read_lines lines "${rvm_file_name}"
for line in "${lines[@]}"
do
# Parse the lines, throwing out comments and empty lines.
if [[ ! "${line}" =~ ^# && -n "${line// /}" ]]
then gem_install $line
fi
done
rvm_log "\nProcessing of $rvm_file_name is complete.\n"
else
rvm_error "${rvm_file_name} does not exist to import from."
return 1
fi
}
__rvm_parse_gems_args()
{
if [[ "$*" =~ ";" ]]
then gem_prefix="${*#*;}"
else gem_prefix=""
fi
gem="${*%%;*}"
if
__rvm_string_match "$gem" "*.gem$"
then
gem_name="$(basename "${gem/.gem/}" | awk -F'-' '{$NF=NULL;print}')"
gem_version="$(basename "${gem/.gem/}" | awk -F'-' '{print $NF}' )"
gem_postfix="$(basename "${gem/*.gem/}")"
else
gem_name="${gem/ */}"
case "$gem" in
*--version*)
gem_version=$(
echo "$gem" | sed -e 's#.*--version[=]*[ ]*##' | awk '{print $1}'
)
gem_postfix="$(
echo "$gem" |
sed -e "s#${gem_name/ /}##" -e "s#--version[=]*[ ]*${gem_version/ /}##"
)" #"
;;
*-v*)
gem_version=$(
echo "$gem" | sed -e 's#.*-v[=]*[ ]*##' | awk '{print $1}'
)
gem_postfix="$(
echo "$gem" |
sed -e "s#${gem_name/ /}##" -e "s#-v[=]*[ ]*${gem_version/ /}##"
)" #"
;;
*)
unset gem_version # no version
;;
esac
fi
if
[[ -s "$gem" ]]
then
gem_file_name="$gem"
elif
__rvm_string_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
gem_install()
{
typeset gem gem_prefix gem_name gem_version gem_file_name gem_postfix cache_file gem_file_name gem_string gem_action _command
result=0
# 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 (( ${rvm_force_flag:-0} == 0 )) &&
[[ -f "${rvm_ruby_gem_home}/specifications/$(basename "$gem_file_name")spec" ]]
then
gem=""
rvm_log "$gem_name $gem_version is already installed."
else
if [[ -s "$gem" ]]
then
cache_file="$gem"
elif [[ -s "$(__rvm_current_gemcache_dir)/${gem_file_name}" ]]
then
cache_file="$(__rvm_current_gemcache_dir)/${gem_file_name}"
else
true ${cache_file:=$( find "$(__rvm_current_gemcache_dir)/${gem_file_name}" -maxdepth 1 -mindepth 1 -type f -print 2> /dev/null | sort | head -n1)}
cache_file="${cache_file/.\/}"
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 (( ${rvm_force_flag:-0} == 0 )) &&
[[ -s "${rvm_ruby_gem_home}/specifications/$(basename $gem_file_name)spec" ]]
then
unset gem # already installed, not forcing reinstall.
rvm_log "$gem_name $gem_version exists, skipping (--force to re-install)"
else
if [[ -s "$(__rvm_current_gemcache_dir)/$(basename $gem_file_name)" ]]
then
mkdir -p "${rvm_tmp_path}/$$/"
mv "$(__rvm_current_gemcache_dir)/$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 [[ -n "$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 [[ -n "$rvm_ruby_gem_home" &&
"$rvm_ruby_gem_home" != "${rvm_gems_path:-"$rvm_path/gems"}" ]]
then
_command="GEM_HOME='$rvm_ruby_gem_home' GEM_PATH='$rvm_ruby_gem_home' $gem_prefix gem install --remote $* $rvm_gem_options $gem_postfix $vars"
else
_command="$gem_prefix gem install --ignore-dependencies --remote $* $rvm_gem_options -q $gem $gem_postfix $vars"
fi
unset -f gem
__rvm_run "gem.install" "$_command" "installing ${gem_name} ${gem_version}..."
result=$?
if (( result == 0 ))
then
rvm_log "$gem_name $gem_version installed."
else
rvm_log "$gem_name $gem_version failed to install ( output logged to: $rvm_log_path/$rvm_ruby_string/gem.install.log )"
fi
fi
return $result
}
# Output the user's current gem directory.
gemset_info()
{
if
(( ${rvm_user_flag:-0} == 1 ))
then
(__rvm_use system ; gem env | GREP_OPTIONS="" \grep "\- $HOME" | awk '{print $NF}')
elif
[[ ${rvm_system_flag:-0} == 1 ]]
then
(__rvm_use system ; gem env $action system)
elif
[[ -n "${rvm_ruby_string:-""}${rvm_gemset_name:+${rvm_gemset_separator:-"@"}}${rvm_gemset_name:-}" ]]
then
#TODO: why on ubuntu oneiric 32bit it's not enough to use gem env? why need to use again???
(
__rvm_use "${rvm_ruby_string:-""}${rvm_gemset_name:+${rvm_gemset_separator:-"@"}}${rvm_gemset_name:-}"
gem env $action
)
else
gem env $action
fi
return $?
}
gemset_prune()
{
typeset temporary_cache_path live_cache_path gemset_name version versions cached_gem_name cached_file_path
temporary_cache_path="$GEM_HOME/temporary-cache"
live_cache_path="$GEM_HOME/cache"
mkdir -p "$temporary_cache_path"
rvm_log "Moving active gems into temporary cache..."
while
read gem_name version _platforms
do
cached_gem_name="${gem_name}-${version}.gem"
cached_file_path="${live_cache_path}/${cached_gem_name}"
if
[[ -f "$cached_file_path" ]]
then
mv "$cached_file_path" "${temporary_cache_path}/${cached_gem_name}"
fi
done < <(
GEM_PATH="$GEM_HOME" __rvm_list_gems
)
rvm_log "Removing live cache and restoring temporary cache..."
# Switch the cache back.
__rvm_rm_rf "$live_cache_path"
mv "$temporary_cache_path" "$live_cache_path"
return 0
}
gemset_pristine()
{
if
(
unset -f gem
builtin command -v gem > /dev/null
)
then
typeset _gem _version _platforms
typeset -a _failed
rvm_log "Restoring gems to pristine condition..."
while
read _gem _version _platforms
do
printf "%b" "${_gem}-${_version} "
if ! gem pristine ${_gem} --version ${_version} >/dev/null
then _failed+=( "${_gem} --version ${_version}" )
fi
done < <(
GEM_PATH="$GEM_HOME" __rvm_list_gems
)
if
(( ${#_failed[@]} > 0 ))
then
rvm_error "\n'gem pristine ${_failed[*]}' failed, you need to fix this gems manually."
return 1
else
rvm_log "\nfinished."
fi
else
rvm_error "'gem' command not found in PATH."
return 1
fi
}
# Transform the list of gems one version per line
__rvm_list_gems()
{
gem list |
sed '/\*\*\*/ d ; /^$/ d; s/ (/,/; s/, /,/g; s/)//;' |
awk -F ',' '{for(i=2;i<=NF;i++) print $1" "$i }'
}
# Loads the default gemsets for the current interpreter and gemset.
gemset_initial()
{
typeset gemsets gemset _iterator paths
true ${rvm_gemsets_path:="$rvm_path/gemsets"}
rvm_log "Importing initial gemsets for $(__rvm_env_string)."
if [[ ! -d "$rvm_gemsets_path/${rvm_ruby_string//-//}/cache" ]]
then
mkdir -p "$rvm_gemsets_path/${rvm_ruby_string//-//}/cache" 2>/dev/null
fi
paths=($(__rvm_ruby_string_paths_under "$rvm_gemsets_path"))
echo "paths: ${paths[@]}"
for _iterator in "${paths[@]}"
do
if [[ -n "$rvm_gemset_name" ]]
then
if [[ -s "${rvm_gemset_name}.gems" ]]
then
( gemset_import "${rvm_gemset_name}.gems" )
fi
else
if [[ -s "${_iterator}/default.gems" ]]
then
( gemset_import "${_iterator}/default.gems" )
fi
if [[ -s "${_iterator}/global.gems" ]]
then
(
rvm_create_flag=1
rvm_ruby_gem_home="${rvm_ruby_gem_home//@*/}@global"
gemset_import "${_iterator}/global.gems"
)
fi
fi
done
rvm_log "Installation of gems for $(__rvm_env_string) is complete."
}
search()
{
typeset gemspec gemspecs gem_name option environment_id ruby_string name gem_version
gem_name="${1:-}"
option="${2:-}"
if [[ -z "${gem_name}" ]]
then
return 0
fi
true "${rvm_gems_path:="$rvm_path/gems"}"
__rvm_read_lines gemspecs < <(
find "${rvm_gems_path}" -mindepth 3 -iname "${gem_name}*.gemspec" -type f
)
if [[ "${option}" != "strings" ]]
then
printf "%-40s %-20s %-20s\n" "environment_id" "name" "version"
printf "%b" "================================================================================\n"
fi
for gemspec in "${gemspecs[@]}"
do
environment_id="${gemspec//${rvm_gems_path}\/}"
environment_id="${environment_id//\/*}"
ruby_string="${environment_id//@*}"
gemset_name="${environment_id//${ruby_string}}"
name=${gemspec//*\/}
name=${name/%.gemspec}
gem_version=${name//*-}
if [[ "${option}" != "strings" ]]
then
printf "%-40s %-20s %-20s\n" "${environment_id}" "${gem_name}" "${gem_version}"
else
printf "%b" "${environment_id}\n"
fi
done
}
action="$1"
(( $# == 0 )) || shift
export _second_param="${1:-}"
export rvm_gemset_name="${1:-}"
rvm_sticky_flag=1
gemset_actions_with_gem=(
gemdir gempath gemhome home path version export dump import
load pristine copy install initial prune rename update unpack
)
if
[[ " ${gemset_actions_with_gem[*]} " =~ " $action " ]] &&
! builtin command -v gem > /dev/null
then
rvm_error "'gem' was not found, cannot perform gem actions (Do you have an RVM ruby selected?)"
exit 1
fi
if [[ " $* " =~ " --force " ]]
then export rvm_force_flag=1
fi
if
[[ -z "$rvm_ruby_string" && "${GEM_HOME:-""}" =~ "${rvm_path}" ]]
then
rvm_ruby_string="${GEM_HOME##*/}"
rvm_ruby_string="${rvm_ruby_string%%@*}"
fi
if
[[ -z "$rvm_gemset_name" && "${GEM_HOME:-""}" =~ "${rvm_path}" ]]
then
rvm_gemset_name="${GEM_HOME##*/}"
rvm_gemset_name="${rvm_gemset_name#${rvm_gemset_name%%@*}}"
rvm_gemset_name="${rvm_gemset_name#@}"
fi
case "$action" in
import|load)
if
[[ -z "${rvm_ruby_strings:-""}" ]]
then
gemset_import "$@"
else
rubies=()
__rvm_custom_separated_array rubies , "$rvm_ruby_strings"
for rvm_ruby_string in "${rubies[@]}"
do
(
__rvm_become
gemset_import "$@"
)
done
fi
;;