Skip to content

Commit c9dcae3

Browse files
committed
Add global attribute on rvm_gem which installs gem in global gemsets.
1 parent b707351 commit c9dcae3

5 files changed

Lines changed: 73 additions & 17 deletions

File tree

‎attributes/default.rb‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
# list of rubies that will be installed
3030
default[:rvm][:rubies] = [ select_ruby(rvm[:default_ruby]) ]
3131

32-
# hash of gemsets and their list of gems to be installed. If default_ruby
33-
# contains a gemset like ree-1.8.7@awesome then strip the gemset to get at the
34-
# global gemset for that ruby.
35-
default[:rvm][:gems] = {
36-
"#{select_ruby(rvm[:default_ruby])}@global" => [
37-
{ :name => "bundler" }
38-
]
39-
}
32+
# list of gems to be installed in global gemset of all rubies
33+
default[:rvm][:global_gems] = [
34+
{ :name => "bundler" }
35+
]
4036

37+
# hash of gemsets and their list of gems to be installed.
38+
default[:rvm][:gems] = Hash.new

‎libraries/helpers.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ def ruby_string_sane?(rubie)
3535
return true if rubie =~ /^[^-]+-[^-]+/ # must be xxx-vvv at least
3636
end
3737

38+
##
39+
# Returns a list of installed rvm rubies on the system
40+
#
41+
# @return [Array] the list of currently installed rvm rubies
42+
def installed_rubies
43+
RVM.list_strings
44+
end
45+
3846
##
3947
# Determines whether or not the given ruby is already installed
4048
#
@@ -43,8 +51,7 @@ def ruby_string_sane?(rubie)
4351
def ruby_installed?(rubie)
4452
return false unless ruby_string_sane?(rubie)
4553

46-
installed = RVM.list_strings
47-
! installed.select { |r| r.start_with?(rubie) }.empty?
54+
! installed_rubies.select { |r| r.start_with?(rubie) }.empty?
4855
end
4956

5057
##

‎providers/gem.rb‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,57 @@
2727
ruby_string.sub!(/default/, current_ruby_default)
2828
end
2929

30-
# ensure ruby is installed and gemset exists
31-
e = rvm_environment ruby_string do
32-
action :nothing
30+
if new_resource.global
31+
# add gem entry into global.gems
32+
update_global_gems_file :create
33+
34+
# install gem in all rubies in global gemset
35+
installed_rubies.each do |rubie|
36+
gem_package_wrapper :install, "#{rubie}@global"
37+
end
38+
else
39+
# ensure ruby is installed and gemset exists
40+
e = rvm_environment ruby_string do
41+
action :nothing
42+
end
43+
e.run_action(:create)
44+
45+
gem_package_wrapper :install
3346
end
34-
e.run_action(:create)
47+
end
3548

49+
##
50+
# Wraps the gem_package provider for rubygems
51+
#
52+
# @param [Symbol] action to be performed with gem_package provider
53+
# @param [optional, String, #to_s] the fully qualifed rvm string
54+
def gem_package_wrapper(exec_action, ruby_string=new_resource.ruby_string)
3655
g = gem_package new_resource.gem do
3756
gem_binary "rvm #{ruby_string} gem"
3857
source new_resource.source if new_resource.source
3958
options new_resource.options if new_resource.options
4059
version new_resource.version if new_resource.version
4160
action :nothing
4261
end
43-
g.run_action(:install)
62+
g.run_action(exec_action)
63+
end
64+
65+
##
66+
# Updates global.gems file to create or remove a gem entry
67+
#
68+
# @oaram [Symbol] action to :create or :remove the gem from the file
69+
def update_global_gems_file(exec_action)
70+
global_gems_file = "#{node[:rvm][:root_path]}/gemsets/global.gems"
71+
gem = new_resource.gem
72+
73+
if exec_action == :create
74+
e = execute "Add #{gem} to #{global_gems_file}" do
75+
user "root"
76+
group "rvm"
77+
command %{echo "#{gem}" >> "#{global_gems_file}"}
78+
not_if %{grep -q "^#{gem}" "#{global_gems_file}"}
79+
action :nothing
80+
end
81+
e.run_action(:run)
82+
end
4483
end

‎recipes/default.rb‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@
4646
rvm_default_ruby node[:rvm][:default_ruby]
4747
end
4848

49+
# installs global gems
50+
node[:rvm][:global_gems].each do |gem|
51+
rvm_gem gem[:name] do
52+
global true
53+
version gem[:version] if gem[:version]
54+
action gem[:action] if gem[:action]
55+
options gem[:options] if gem[:options]
56+
source gem[:source] if gem[:source]
57+
end
58+
end
59+
4960
# installs gems
5061
node[:rvm][:gems].each_pair do |rstring, gems|
5162
rvm_environment rstring
@@ -54,9 +65,9 @@
5465
rvm_gem gem[:name] do
5566
ruby_string rstring
5667
version gem[:version] if gem[:version]
57-
action gem[:action] if gem[:action]
68+
action gem[:action] if gem[:action]
5869
options gem[:options] if gem[:options]
59-
source gem[:source] if gem[:source]
70+
source gem[:source] if gem[:source]
6071
end
6172
end
6273
end

‎resources/gem.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
attribute :gem, :kind_of => String, :name_attribute => true
2525
attribute :ruby_string, :kind_of => String, :default => "default"
26+
attribute :global, :default => false
2627
attribute :source, :kind_of => String
2728
attribute :options, :kind_of => Hash
2829
attribute :version, :kind_of => String

0 commit comments

Comments
 (0)