public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Added config.gem for specifying which gems are required by the 
application, 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
technoweenie (author)
Sat Mar 29 19:17:28 -0700 2008
commit  088ef182e3006294b8f0e9b185d272a777c4437a
tree    11b760af7a23bb5280f5aef5dc61350d0ea302f1
parent  81286f858770e0b95e15af37f19156b044ec6a95
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -1,5 +1,22 @@
0
 *SVN*
0
 
0
+* Added config.gem for specifying which gems are required by the application, as well as rake tasks for installing and freezing gems. [rick]
0
+
0
+ Rails::Initializer.run do |config|
0
+ config.gems "bj"
0
+ config.gems "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
0
+ config.gems "aws-s3", :lib => "aws/s3"
0
+ end
0
+
0
+ # List required gems.
0
+ rake gems
0
+
0
+ # Install all required gems:
0
+ rake gems:install
0
+
0
+ # Unpack specified gem to vendor/gems/gem_name-x.x.x
0
+ rake gems:unpack GEM=bj
0
+
0
 * Removed the default .htaccess configuration as there are so many good deployment options now (kept it as an example in README) [DHH]
0
 
0
 * config.time_zone accepts TZInfo::Timezone identifiers as well as Rails TimeZone identifiers [Geoff Buesing]
...
7
8
9
 
10
11
12
...
77
78
79
 
80
81
82
83
...
98
99
100
 
101
102
103
104
105
...
185
186
187
 
 
 
 
 
 
 
 
 
 
 
188
189
190
...
229
230
231
232
 
 
 
 
 
233
234
235
...
494
495
496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
497
498
499
...
529
530
531
 
532
533
534
...
711
712
713
 
 
 
 
714
715
716
...
7
8
9
10
11
12
13
...
78
79
80
81
82
83
84
85
...
100
101
102
103
104
 
105
106
107
...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
242
243
244
 
245
246
247
248
249
250
251
252
...
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
...
565
566
567
568
569
570
571
...
748
749
750
751
752
753
754
755
756
757
0
@@ -7,6 +7,7 @@
0
 require 'rails/version'
0
 require 'rails/plugin/locator'
0
 require 'rails/plugin/loader'
0
+require 'rails/gem_dependency'
0
 
0
 
0
 RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV)
0
@@ -77,6 +78,7 @@
0
       
0
       require_frameworks
0
       set_autoload_paths
0
+ add_gem_load_paths
0
       add_plugin_load_paths
0
       load_environment
0
 
0
0
@@ -98,8 +100,8 @@
0
 
0
       add_support_load_paths
0
 
0
+ load_gems
0
       load_plugins
0
-
0
       load_application_initializers
0
 
0
       # the framework is now fully initialized
0
@@ -185,6 +187,17 @@
0
       plugin_loader.add_plugin_load_paths
0
     end
0
 
0
+ def add_gem_load_paths
0
+ unless @configuration.gems.empty?
0
+ require "rubygems"
0
+ @configuration.gems.each &:add_load_paths
0
+ end
0
+ end
0
+
0
+ def load_gems
0
+ @configuration.gems.each &:load
0
+ end
0
+
0
     # Loads all plugins in <tt>config.plugin_paths</tt>. <tt>plugin_paths</tt>
0
     # defaults to <tt>vendor/plugins</tt> but may also be set to a list of
0
     # paths, such as
0
@@ -229,7 +242,11 @@
0
 
0
     def load_observers
0
       if configuration.frameworks.include?(:active_record)
0
- ActiveRecord::Base.instantiate_observers
0
+ if @configuration.gems.any? { |g| !g.loaded? }
0
+ puts "Unable to instantiate observers, some gems that this application depends on are missing. Run 'rake gems:install'"
0
+ else
0
+ ActiveRecord::Base.instantiate_observers
0
+ end
0
       end
0
     end
0
 
0
@@ -494,6 +511,25 @@
0
     # a sub class would have access to fine grained modification of the loading behavior. See
0
     # the implementation of Rails::Plugin::Loader for more details.
0
     attr_accessor :plugin_loader
0
+
0
+ # An array of gems that this rails application depends on. Rails will automatically load
0
+ # these gems during installation, and allow you to install any missing gems with:
0
+ #
0
+ # rake gems:install
0
+ #
0
+ # You can add with the #gem method.
0
+ attr_accessor :gems
0
+
0
+ # Adds a single Gem dependency to the rails application.
0
+ #
0
+ # # gem 'aws-s3', '>= 0.4.0'
0
+ # # require 'aws/s3'
0
+ # config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \
0
+ # :source => "http://code.whytheluckystiff.net"
0
+ #
0
+ def gem(name, options = {})
0
+ @gems << Rails::GemDependency.new(name, options)
0
+ end
0
     
0
     # Deprecated options:
0
     def breakpoint_server(_ = nil)
0
@@ -529,6 +565,7 @@
0
       self.plugin_locators = default_plugin_locators
0
       self.plugin_loader = default_plugin_loader
0
       self.database_configuration_file = default_database_configuration_file
0
+ self.gems = default_gems
0
 
0
       for framework in default_frameworks
0
         self.send("#{framework}=", Rails::OrderedOptions.new)
0
@@ -711,6 +748,10 @@
0
         else
0
           :memory_store
0
         end
0
+ end
0
+
0
+ def default_gems
0
+ []
0
       end
0
   end
0
 end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
0
@@ -1 +1,79 @@
0
+module Rails
0
+ class GemDependency
0
+ attr_accessor :name, :requirement, :version, :lib, :source
0
+
0
+ def self.unpacked_path
0
+ @unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
0
+ end
0
+
0
+ def initialize(name, options = {})
0
+ @name = name.to_s
0
+ if options[:version]
0
+ @requirement = Gem::Requirement.create(options[:version])
0
+ @version = @requirement.requirements.first.last
0
+ end
0
+ @lib = options[:lib]
0
+ @source = options[:source]
0
+ @loaded = false
0
+ @load_paths_added = false
0
+ @unpack_directory = nil
0
+ end
0
+
0
+ def add_load_paths
0
+ return if @loaded || @load_paths_added
0
+ unpacked_paths = Dir[File.join(self.class.unpacked_path, "#{@name}-#{@version || "*"}")]
0
+ if unpacked_paths.empty?
0
+ args = [@name]
0
+ args << @requirement.to_s if @requirement
0
+ gem *args
0
+ else
0
+ $LOAD_PATH << File.join(unpacked_paths.first, 'lib')
0
+ end
0
+ @load_paths_added = true
0
+ rescue Gem::LoadError
0
+ puts $!.to_s
0
+ end
0
+
0
+ def load
0
+ return if @load_paths_added == false
0
+ require(@lib || @name)
0
+ @loaded = true
0
+ rescue LoadError
0
+ puts $!.to_s
0
+ $!.backtrace.each { |b| puts b }
0
+ end
0
+
0
+ def loaded?
0
+ @loaded
0
+ end
0
+
0
+ def load_paths_added?
0
+ @load_paths_added
0
+ end
0
+
0
+ def install
0
+ Gem::GemRunner.new.run(install_command)
0
+ end
0
+
0
+ def unpack_to(directory)
0
+ FileUtils.mkdir_p directory
0
+ Dir.chdir directory do
0
+ Gem::GemRunner.new.run(unpack_command)
0
+ end
0
+ end
0
+
0
+ def install_command
0
+ cmd = %w(install) << @name
0
+ cmd << "--version" << "#{@requirement.to_s}" if @requirement
0
+ cmd << "--source" << @source if @source
0
+ cmd
0
+ end
0
+
0
+ def unpack_command
0
+ cmd = %w(unpack) << @name
0
+ cmd << "--version" << "#{@requirement.to_s}" if @requirement
0
+ cmd
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
@@ -1 +1,32 @@
0
+desc "List the gems that this rails application depends on"
0
+task :gems => :environment do
0
+ Rails.configuration.gems.each do |gem|
0
+ puts "[#{gem.loaded? ? '*' : ' '}] #{gem.name} #{gem.requirement.to_s}"
0
+ end
0
+end
0
+
0
+namespace :gems do
0
+ desc "Installs all required gems for this application."
0
+ task :install => :environment do
0
+ require 'rubygems'
0
+ require 'rubygems/gem_runner'
0
+ Rails.configuration.gems.each { |gem| gem.install unless gem.loaded? }
0
+ end
0
+
0
+ desc "Unpacks the specified gem into vendor/gems."
0
+ task :unpack do
0
+ raise "Specify name of gem in the config.gems array with GEM=" if ENV['GEM'].blank?
0
+ Rake::Task["environment"].invoke
0
+ require 'rubygems'
0
+ require 'rubygems/gem_runner'
0
+ unless Rails.configuration.gems.select do |gem|
0
+ if gem.loaded? && gem.name == ENV['GEM']
0
+ gem.unpack_to(File.join(RAILS_ROOT, 'vendor', 'gems'))
0
+ true
0
+ end
0
+ end.any?
0
+ puts "No gem named #{ENV['GEM'].inspect} found."
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
0
@@ -1 +1,63 @@
0
+require 'plugin_test_helper'
0
+
0
+uses_mocha "Plugin Tests" do
0
+ class GemDependencyTest < Test::Unit::TestCase
0
+ def setup
0
+ @gem = Rails::GemDependency.new "hpricot"
0
+ @gem_with_source = Rails::GemDependency.new "hpricot", :source => "http://code.whytheluckystiff.net"
0
+ @gem_with_version = Rails::GemDependency.new "hpricot", :version => "= 0.6"
0
+ @gem_with_lib = Rails::GemDependency.new "aws-s3", :lib => "aws/s3"
0
+ end
0
+
0
+ def test_configuration_adds_gem_dependency
0
+ config = Rails::Configuration.new
0
+ config.gem "aws-s3", :lib => "aws/s3", :version => "0.4.0"
0
+ assert_equal [["install", "aws-s3", "--version", "= 0.4.0"]], config.gems.collect(&:install_command)
0
+ end
0
+
0
+ def test_gem_creates_install_command
0
+ assert_equal %w(install hpricot), @gem.install_command
0
+ end
0
+
0
+ def test_gem_with_source_creates_install_command
0
+ assert_equal %w(install hpricot --source http://code.whytheluckystiff.net), @gem_with_source.install_command
0
+ end
0
+
0
+ def test_gem_with_version_creates_install_command
0
+ assert_equal ["install", "hpricot", "--version", "= 0.6"], @gem_with_version.install_command
0
+ end
0
+
0
+ def test_gem_creates_unpack_command
0
+ assert_equal %w(unpack hpricot), @gem.unpack_command
0
+ end
0
+
0
+ def test_gem_with_version_unpack_install_command
0
+ assert_equal ["unpack", "hpricot", "--version", "= 0.6"], @gem_with_version.unpack_command
0
+ end
0
+
0
+ def test_gem_adds_load_paths
0
+ @gem.expects(:gem).with(@gem.name)
0
+ @gem.add_load_paths
0
+ end
0
+
0
+ def test_gem_with_version_adds_load_paths
0
+ @gem_with_version.expects(:gem).with(@gem_with_version.name, @gem_with_version.requirement.to_s)
0
+ @gem_with_version.add_load_paths
0
+ end
0
+
0
+ def test_gem_loading
0
+ @gem.expects(:gem).with(@gem.name)
0
+ @gem.expects(:require).with(@gem.name)
0
+ @gem.add_load_paths
0
+ @gem.load
0
+ end
0
+
0
+ def test_gem_with_lib_loading
0
+ @gem_with_lib.expects(:gem).with(@gem_with_lib.name)
0
+ @gem_with_lib.expects(:require).with(@gem_with_lib.lib)
0
+ @gem_with_lib.add_load_paths
0
+ @gem_with_lib.load
0
+ end
0
+ end
0
+end

Comments

    No one has commented yet.