From 9c1e572e0caf79e2d0f6333424e7a264f486f549 Mon Sep 17 00:00:00 2001 From: Jordi Bunster Date: Fri, 6 Mar 2009 10:02:16 -0500 Subject: [PATCH] Add :author to Rails::GemDependency to use GitHub gems with ease --- railties/environments/environment.rb | 1 + railties/lib/initializer.rb | 10 ++++++++++ railties/lib/rails/gem_dependency.rb | 13 ++++++++++--- railties/test/gem_dependency_test.rb | 26 +++++++++++++++++++++++++- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/railties/environments/environment.rb b/railties/environments/environment.rb index 4a2df36..d31b572 100644 --- a/railties/environments/environment.rb +++ b/railties/environments/environment.rb @@ -19,6 +19,7 @@ Rails::Initializer.run do |config| # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net" # config.gem "sqlite3-ruby", :lib => "sqlite3" # config.gem "aws-s3", :lib => "aws/s3" + # config.gem "will_paginate", :author => "mislav" # (use GitHub defaults for :lib and :source) # Only load the plugins named here, in the order given (default is alphabetical). # :all can be used as a placeholder for all plugins not explicitly named diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index edea4e5..823cd78 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -781,6 +781,16 @@ Run `rake gems:install` to install the missing gems. # config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \ # :source => "http://code.whytheluckystiff.net" # + # You can also pass an :author to use GitHub gems, like so: + # + # config.gem 'will_paginate', :version => '2.3.5', :author => 'mislav' + # + # Passing the :author parameter changes the default :source to be + # 'http://gems.github.com/', and changes the name of the gem to + # username-gem (here 'mislav-will_paginate'), as it is enforced in the + # GitHub gem repository. Note that it also provides an appropriate + # default to :lib, in this case, 'will_paginate'. + # # To require a library be installed, but not attempt to load it, pass :lib => false # # config.gem 'qrp', :version => '0.4.1', :lib => false diff --git a/railties/lib/rails/gem_dependency.rb b/railties/lib/rails/gem_dependency.rb index 2dd6590..642d2fb 100644 --- a/railties/lib/rails/gem_dependency.rb +++ b/railties/lib/rails/gem_dependency.rb @@ -52,10 +52,17 @@ module Rails req = Gem::Requirement.default end + if options[:author] # GitHub author given, assume GitHub defaults: + @source = options[:source] || 'http://gems.github.com/' + @lib = options[:lib].nil? ? name : options[:lib] # ||= catches false too + name = "#{options[:author]}-#{name}" + else + @source = options[:source] + @lib = options[:lib] + end + @dep = Gem::Dependency.new(name, req) - @lib = options[:lib] - @source = options[:source] - @loaded = @frozen = @load_paths_added = false + @loaded = @frozen = @load_paths_added = false end def add_load_paths diff --git a/railties/test/gem_dependency_test.rb b/railties/test/gem_dependency_test.rb index 8b761c4..72d6267 100644 --- a/railties/test/gem_dependency_test.rb +++ b/railties/test/gem_dependency_test.rb @@ -12,7 +12,11 @@ class GemDependencyTest < Test::Unit::TestCase @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 + + @gem_with_author = Rails::GemDependency.new "xwill_paginatex", :author => "mislav" + @gem_with_author_and_lib = Rails::GemDependency.new "xwill_paginatex", :author => "mislav", :lib => "user given lib" + @gem_with_author_and_source = Rails::GemDependency.new "xwill_paginatex", :author => "mislav", :source => "user given source" end def test_configuration_adds_gem_dependency @@ -37,6 +41,26 @@ class GemDependencyTest < Test::Unit::TestCase assert_equal %w(unpack xhpricotx), @gem.unpack_command end + def test_gem_with_author_changes_name + assert_equal 'mislav-xwill_paginatex', @gem_with_author.name + end + + def test_gem_with_author_provides_sane_lib_default + assert_equal 'xwill_paginatex', @gem_with_author.lib + end + + def test_gem_with_author_provides_github_source_default + assert_equal 'http://gems.github.com/', @gem_with_author.source + end + + def test_gem_with_author_does_not_clobber_user_provided_lib + assert_equal 'user given lib', @gem_with_author_and_lib.lib + end + + def test_gem_with_author_does_not_clobber_user_provided_source + assert_equal 'user given source', @gem_with_author_and_source.source + end + def test_gem_with_version_unpack_install_command # stub out specification method, or else test will fail if hpricot 0.6 isn't installed mock_spec = mock() -- 1.6.2