-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added config.gem for specifying which gems are required by the applic…
…ation, as well as rake tasks for installing and freezing gems. [rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9140 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
1 parent
81286f8
commit 088ef18
Showing
5 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module Rails | ||
class GemDependency | ||
attr_accessor :name, :requirement, :version, :lib, :source | ||
|
||
def self.unpacked_path | ||
@unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems') | ||
end | ||
|
||
def initialize(name, options = {}) | ||
@name = name.to_s | ||
if options[:version] | ||
@requirement = Gem::Requirement.create(options[:version]) | ||
@version = @requirement.requirements.first.last | ||
end | ||
@lib = options[:lib] | ||
@source = options[:source] | ||
@loaded = false | ||
@load_paths_added = false | ||
@unpack_directory = nil | ||
end | ||
|
||
def add_load_paths | ||
return if @loaded || @load_paths_added | ||
unpacked_paths = Dir[File.join(self.class.unpacked_path, "#{@name}-#{@version || "*"}")] | ||
if unpacked_paths.empty? | ||
args = [@name] | ||
args << @requirement.to_s if @requirement | ||
gem *args | ||
else | ||
$LOAD_PATH << File.join(unpacked_paths.first, 'lib') | ||
end | ||
@load_paths_added = true | ||
rescue Gem::LoadError | ||
puts $!.to_s | ||
end | ||
|
||
def load | ||
return if @load_paths_added == false | ||
require(@lib || @name) | ||
@loaded = true | ||
rescue LoadError | ||
puts $!.to_s | ||
$!.backtrace.each { |b| puts b } | ||
end | ||
|
||
def loaded? | ||
@loaded | ||
end | ||
|
||
def load_paths_added? | ||
@load_paths_added | ||
end | ||
|
||
def install | ||
Gem::GemRunner.new.run(install_command) | ||
end | ||
|
||
def unpack_to(directory) | ||
FileUtils.mkdir_p directory | ||
Dir.chdir directory do | ||
Gem::GemRunner.new.run(unpack_command) | ||
end | ||
end | ||
|
||
def install_command | ||
cmd = %w(install) << @name | ||
cmd << "--version" << "#{@requirement.to_s}" if @requirement | ||
cmd << "--source" << @source if @source | ||
cmd | ||
end | ||
|
||
def unpack_command | ||
cmd = %w(unpack) << @name | ||
cmd << "--version" << "#{@requirement.to_s}" if @requirement | ||
cmd | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
desc "List the gems that this rails application depends on" | ||
task :gems => :environment do | ||
Rails.configuration.gems.each do |gem| | ||
puts "[#{gem.loaded? ? '*' : ' '}] #{gem.name} #{gem.requirement.to_s}" | ||
end | ||
end | ||
|
||
namespace :gems do | ||
desc "Installs all required gems for this application." | ||
task :install => :environment do | ||
require 'rubygems' | ||
require 'rubygems/gem_runner' | ||
Rails.configuration.gems.each { |gem| gem.install unless gem.loaded? } | ||
end | ||
|
||
desc "Unpacks the specified gem into vendor/gems." | ||
task :unpack do | ||
raise "Specify name of gem in the config.gems array with GEM=" if ENV['GEM'].blank? | ||
Rake::Task["environment"].invoke | ||
require 'rubygems' | ||
require 'rubygems/gem_runner' | ||
unless Rails.configuration.gems.select do |gem| | ||
if gem.loaded? && gem.name == ENV['GEM'] | ||
gem.unpack_to(File.join(RAILS_ROOT, 'vendor', 'gems')) | ||
true | ||
end | ||
end.any? | ||
puts "No gem named #{ENV['GEM'].inspect} found." | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'plugin_test_helper' | ||
|
||
uses_mocha "Plugin Tests" do | ||
class GemDependencyTest < Test::Unit::TestCase | ||
def setup | ||
@gem = Rails::GemDependency.new "hpricot" | ||
@gem_with_source = Rails::GemDependency.new "hpricot", :source => "http://code.whytheluckystiff.net" | ||
@gem_with_version = Rails::GemDependency.new "hpricot", :version => "= 0.6" | ||
@gem_with_lib = Rails::GemDependency.new "aws-s3", :lib => "aws/s3" | ||
end | ||
|
||
def test_configuration_adds_gem_dependency | ||
config = Rails::Configuration.new | ||
config.gem "aws-s3", :lib => "aws/s3", :version => "0.4.0" | ||
assert_equal [["install", "aws-s3", "--version", "= 0.4.0"]], config.gems.collect(&:install_command) | ||
end | ||
|
||
def test_gem_creates_install_command | ||
assert_equal %w(install hpricot), @gem.install_command | ||
end | ||
|
||
def test_gem_with_source_creates_install_command | ||
assert_equal %w(install hpricot --source http://code.whytheluckystiff.net), @gem_with_source.install_command | ||
end | ||
|
||
def test_gem_with_version_creates_install_command | ||
assert_equal ["install", "hpricot", "--version", "= 0.6"], @gem_with_version.install_command | ||
end | ||
|
||
def test_gem_creates_unpack_command | ||
assert_equal %w(unpack hpricot), @gem.unpack_command | ||
end | ||
|
||
def test_gem_with_version_unpack_install_command | ||
assert_equal ["unpack", "hpricot", "--version", "= 0.6"], @gem_with_version.unpack_command | ||
end | ||
|
||
def test_gem_adds_load_paths | ||
@gem.expects(:gem).with(@gem.name) | ||
@gem.add_load_paths | ||
end | ||
|
||
def test_gem_with_version_adds_load_paths | ||
@gem_with_version.expects(:gem).with(@gem_with_version.name, @gem_with_version.requirement.to_s) | ||
@gem_with_version.add_load_paths | ||
end | ||
|
||
def test_gem_loading | ||
@gem.expects(:gem).with(@gem.name) | ||
@gem.expects(:require).with(@gem.name) | ||
@gem.add_load_paths | ||
@gem.load | ||
end | ||
|
||
def test_gem_with_lib_loading | ||
@gem_with_lib.expects(:gem).with(@gem_with_lib.name) | ||
@gem_with_lib.expects(:require).with(@gem_with_lib.lib) | ||
@gem_with_lib.add_load_paths | ||
@gem_with_lib.load | ||
end | ||
end | ||
end |