Skip to content

Commit

Permalink
Add global attribute on rvm_gem which installs gem in global gemsets.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnichol committed Feb 13, 2011
1 parent b707351 commit c9dcae3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
14 changes: 6 additions & 8 deletions attributes/default.rb
Expand Up @@ -29,12 +29,10 @@
# list of rubies that will be installed
default[:rvm][:rubies] = [ select_ruby(rvm[:default_ruby]) ]

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

# hash of gemsets and their list of gems to be installed.
default[:rvm][:gems] = Hash.new
11 changes: 9 additions & 2 deletions libraries/helpers.rb
Expand Up @@ -35,6 +35,14 @@ def ruby_string_sane?(rubie)
return true if rubie =~ /^[^-]+-[^-]+/ # must be xxx-vvv at least
end

##
# Returns a list of installed rvm rubies on the system
#
# @return [Array] the list of currently installed rvm rubies
def installed_rubies
RVM.list_strings
end

##
# Determines whether or not the given ruby is already installed
#
Expand All @@ -43,8 +51,7 @@ def ruby_string_sane?(rubie)
def ruby_installed?(rubie)
return false unless ruby_string_sane?(rubie)

installed = RVM.list_strings
! installed.select { |r| r.start_with?(rubie) }.empty?
! installed_rubies.select { |r| r.start_with?(rubie) }.empty?
end

##
Expand Down
49 changes: 44 additions & 5 deletions providers/gem.rb
Expand Up @@ -27,18 +27,57 @@
ruby_string.sub!(/default/, current_ruby_default)
end

# ensure ruby is installed and gemset exists
e = rvm_environment ruby_string do
action :nothing
if new_resource.global
# add gem entry into global.gems
update_global_gems_file :create

# install gem in all rubies in global gemset
installed_rubies.each do |rubie|
gem_package_wrapper :install, "#{rubie}@global"
end
else
# ensure ruby is installed and gemset exists
e = rvm_environment ruby_string do
action :nothing
end
e.run_action(:create)

gem_package_wrapper :install
end
e.run_action(:create)
end

##
# Wraps the gem_package provider for rubygems
#
# @param [Symbol] action to be performed with gem_package provider
# @param [optional, String, #to_s] the fully qualifed rvm string
def gem_package_wrapper(exec_action, ruby_string=new_resource.ruby_string)
g = gem_package new_resource.gem do
gem_binary "rvm #{ruby_string} gem"
source new_resource.source if new_resource.source
options new_resource.options if new_resource.options
version new_resource.version if new_resource.version
action :nothing
end
g.run_action(:install)
g.run_action(exec_action)
end

##
# Updates global.gems file to create or remove a gem entry
#
# @oaram [Symbol] action to :create or :remove the gem from the file
def update_global_gems_file(exec_action)
global_gems_file = "#{node[:rvm][:root_path]}/gemsets/global.gems"
gem = new_resource.gem

if exec_action == :create
e = execute "Add #{gem} to #{global_gems_file}" do
user "root"
group "rvm"
command %{echo "#{gem}" >> "#{global_gems_file}"}
not_if %{grep -q "^#{gem}" "#{global_gems_file}"}
action :nothing
end
e.run_action(:run)
end
end
15 changes: 13 additions & 2 deletions recipes/default.rb
Expand Up @@ -46,6 +46,17 @@
rvm_default_ruby node[:rvm][:default_ruby]
end

# installs global gems
node[:rvm][:global_gems].each do |gem|
rvm_gem gem[:name] do
global true
version gem[:version] if gem[:version]
action gem[:action] if gem[:action]
options gem[:options] if gem[:options]
source gem[:source] if gem[:source]
end
end

# installs gems
node[:rvm][:gems].each_pair do |rstring, gems|
rvm_environment rstring
Expand All @@ -54,9 +65,9 @@
rvm_gem gem[:name] do
ruby_string rstring
version gem[:version] if gem[:version]
action gem[:action] if gem[:action]
action gem[:action] if gem[:action]
options gem[:options] if gem[:options]
source gem[:source] if gem[:source]
source gem[:source] if gem[:source]
end
end
end
1 change: 1 addition & 0 deletions resources/gem.rb
Expand Up @@ -23,6 +23,7 @@

attribute :gem, :kind_of => String, :name_attribute => true
attribute :ruby_string, :kind_of => String, :default => "default"
attribute :global, :default => false
attribute :source, :kind_of => String
attribute :options, :kind_of => Hash
attribute :version, :kind_of => String
Expand Down

0 comments on commit c9dcae3

Please sign in to comment.