From 5e89cc4979d75f1e8cf57632b848fb2d7d0b9f7e Mon Sep 17 00:00:00 2001 From: Tim Lucas Date: Fri, 6 Feb 2009 11:45:59 +1100 Subject: [PATCH] Added a sources option to Gem::Dependency to allow you to specify multiple gem sources --- railties/lib/initializer.rb | 24 ++++++++++++++++-------- railties/lib/rails/gem_dependency.rb | 6 +++--- railties/test/gem_dependency_test.rb | 17 +++++++++++++---- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index e3811dd..6d0d543 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -750,17 +750,25 @@ Run `rake gems:install` to install the missing gems. # You can add gems with the #gem method. attr_accessor :gems - # Adds a single Gem dependency to the rails application. By default, it will require - # the library with the same name as the gem. Use :lib to specify a different name. + # Adds a dependency to the gem with the given name. # - # # gem 'aws-s3', '>= 0.4.0' - # # require 'aws/s3' - # config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \ - # :source => "http://code.whytheluckystiff.net" + # Options: + # * :version: The version of the gem to depend on. + # * :lib: The path to be required or false to not require at all. Default is the name of the gem. + # * :sources: A URL or array of URLs to gem repositories where the gem and its dependencies can be found. + # * :source: Equivalent to the :sources option. # - # To require a library be installed, but not attempt to load it, pass :lib => false + # For example, the equivalent to the following standard gem declaration + # and require: # - # config.gem 'qrp', :version => '0.4.1', :lib => false + # gem 'aws-s3', '>= 0.4.0' + # require 'aws/s3' + # + # with the addition of the gem's source, would be: + # + # config.gem 'aws-s3', :version => '>= 0.4.0', + # :lib => 'aws/s3', + # :source => "http://code.whytheluckystiff.net" def gem(name, options = {}) @gems << Rails::GemDependency.new(name, options) end diff --git a/railties/lib/rails/gem_dependency.rb b/railties/lib/rails/gem_dependency.rb index 5a07841..703f21d 100644 --- a/railties/lib/rails/gem_dependency.rb +++ b/railties/lib/rails/gem_dependency.rb @@ -8,7 +8,7 @@ end module Rails class GemDependency - attr_accessor :lib, :source + attr_accessor :lib, :sources def self.unpacked_path @unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems') @@ -54,7 +54,7 @@ module Rails @dep = Gem::Dependency.new(name, req) @lib = options[:lib] - @source = options[:source] + @sources = [options[:source], options[:sources]].flatten.compact @loaded = @frozen = @load_paths_added = false end @@ -239,7 +239,7 @@ module Rails def install_command cmd = %w(install) << name cmd << "--version" << %("#{requirement.to_s}") if requirement - cmd << "--source" << @source if @source + @sources.each {|source| cmd << "--source" << source } cmd end diff --git a/railties/test/gem_dependency_test.rb b/railties/test/gem_dependency_test.rb index 9cb02fc..2ab4bce 100644 --- a/railties/test/gem_dependency_test.rb +++ b/railties/test/gem_dependency_test.rb @@ -9,10 +9,9 @@ Rails::VendorGemSourceIndex.silence_spec_warnings = true class GemDependencyTest < Test::Unit::TestCase def setup @gem = Rails::GemDependency.new "xhpricotx" - @gem_with_source = Rails::GemDependency.new "xhpricotx", :source => "http://code.whytheluckystiff.net" @gem_with_version = Rails::GemDependency.new "xhpricotx", :version => "= 0.6" @gem_with_lib = Rails::GemDependency.new "xaws-s3x", :lib => "aws/s3" - @gem_without_load = Rails::GemDependency.new "xhpricotx", :lib => false + @gem_without_load = Rails::GemDependency.new "xhpricotx", :lib => false end def test_configuration_adds_gem_dependency @@ -25,8 +24,18 @@ class GemDependencyTest < Test::Unit::TestCase assert_equal %w(install xhpricotx), @gem.install_command end - def test_gem_with_source_creates_install_command - assert_equal %w(install xhpricotx --source http://code.whytheluckystiff.net), @gem_with_source.install_command + def test_gem_with_source_with_single_value_creates_install_command + [:source, :sources].each do |source_option| + gem = Rails::GemDependency.new("xhpricotx", source_option => "http://code.whytheluckystiff.net") + assert_equal %w(install xhpricotx --source http://code.whytheluckystiff.net), gem.install_command + end + end + + def test_gem_with_source_with_array_value_creates_install_command + [:source, :sources].each do |source_option| + gem = Rails::GemDependency.new("xhpricotx", source_option => ["http://code.whytheluckystiff.net", "http://gems.rubyforge.org"]) + assert_equal %w(install xhpricotx --source http://code.whytheluckystiff.net --source http://gems.rubyforge.org), gem.install_command + end end def test_gem_with_version_creates_install_command -- 1.5.6