From 2370e87ae0babf0fb4b21254c8120f7a93189d40 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 30 Sep 2009 12:05:34 -0700 Subject: Remove all calls to Rails::Initializer from boot.rb This is starting a refactor of the rails initialization process. The boot.rb file will not remain the same. --- .../generators/rails/app/templates/config/boot.rb | 47 +++++++++++++++++++--- .../rails/app/templates/config/environment.rb | 4 +- railties/lib/rails/initializer.rb | 23 ++--------- railties/test/boot_test.rb | 9 +---- railties/test/initializer/boot_test.rb | 16 ++++++++ railties/test/isolation/abstract_unit.rb | 20 ++++----- 6 files changed, 70 insertions(+), 49 deletions(-) create mode 100644 railties/test/initializer/boot_test.rb (limited to 'railties') diff --git a/railties/lib/rails/generators/rails/app/templates/config/boot.rb b/railties/lib/rails/generators/rails/app/templates/config/boot.rb index d2652af9b0..52086fbc7d 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/boot.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/boot.rb @@ -4,6 +4,11 @@ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT) module Rails + # Mark the version of Rails that generated the boot.rb file. This is + # a temporary solution and will most likely be removed as Rails 3.0 + # comes closer. + BOOTSTRAP_VERSION = "3.0" + class << self def boot! unless booted? @@ -36,20 +41,50 @@ module Rails class Boot def run load_initializer - Rails::Initializer.run(:set_load_path) + set_load_paths + end + + def set_load_paths + %w( + railties + railties/lib + activesupport/lib + actionpack/lib + activerecord/lib + actionmailer/lib + activeresource/lib + actionwebservice/lib + ).reverse_each do |path| + path = "#{framework_root_path}/#{path}" + $LOAD_PATH.unshift(path) if File.directory?(path) + $LOAD_PATH.uniq! + end + end + + def framework_root_path + defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{RAILS_ROOT}/vendor/rails" end end class VendorBoot < Boot def load_initializer - # activesupport/lib - %w(railties/lib).each do |path| - $:.unshift("#{RAILS_ROOT}/vendor/rails/#{path}") - end + $:.unshift("#{framework_root_path}/railties/lib") require "rails" - Rails::Initializer.run(:install_gem_spec_stubs) + install_gem_spec_stubs Rails::GemDependency.add_frozen_gem_path end + + def install_gem_spec_stubs + begin; require "rubygems"; rescue LoadError; return; end + + %w(rails activesupport activerecord actionpack actionmailer activeresource).each do |stub| + Gem.loaded_specs[stub] ||= Gem::Specification.new do |s| + s.name = stub + s.version = Rails::VERSION::STRING + s.loaded_from = "" + end + end + end end class GemBoot < Boot diff --git a/railties/lib/rails/generators/rails/app/templates/config/environment.rb b/railties/lib/rails/generators/rails/app/templates/config/environment.rb index 2f2d1162c0..adb3a3060a 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environment.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/environment.rb @@ -6,9 +6,7 @@ # Bootstrap the Rails environment, frameworks, and default configuration require File.join(File.dirname(__FILE__), 'boot') -module <%= app_name.camelize %>; end - -<%= app_name.camelize %>::Application = Rails::Initializer.run do |config| +Rails::Initializer.run do |config| # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. diff --git a/railties/lib/rails/initializer.rb b/railties/lib/rails/initializer.rb index f75bd917bf..2d63ac4d39 100644 --- a/railties/lib/rails/initializer.rb +++ b/railties/lib/rails/initializer.rb @@ -123,33 +123,16 @@ module Rails require 'rails/ruby_version_check' end - # If Rails is vendored and RubyGems is available, install stub GemSpecs - # for Rails, Active Support, Active Record, Action Pack, Action Mailer, and - # Active Resource. This allows Gem plugins to depend on Rails even when - # the Gem version of Rails shouldn't be loaded. - Initializer.default.add :install_gem_spec_stubs do - unless Rails.respond_to?(:vendor_rails?) + # Bail if boot.rb is outdated + Initializer.default.add :freak_out_if_boot_rb_is_outdated do + unless defined?(Rails::BOOTSTRAP_VERSION) abort %{Your config/boot.rb is outdated: Run "rake rails:update".} end - - if Rails.vendor_rails? - begin; require "rubygems"; rescue LoadError; return; end - - %w(rails activesupport activerecord actionpack actionmailer activeresource).each do |stub| - Gem.loaded_specs[stub] ||= Gem::Specification.new do |s| - s.name = stub - s.version = Rails::VERSION::STRING - s.loaded_from = "" - end - end - end end # Set the $LOAD_PATH based on the value of # Configuration#load_paths. Duplicates are removed. Initializer.default.add :set_load_path do - # TODO: Think about unifying this with the general Rails paths - configuration.framework_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) } configuration.paths.add_to_load_path $LOAD_PATH.uniq! end diff --git a/railties/test/boot_test.rb b/railties/test/boot_test.rb index f8f6d8f225..1280d27ffe 100644 --- a/railties/test/boot_test.rb +++ b/railties/test/boot_test.rb @@ -48,13 +48,6 @@ class BootTest < Test::Unit::TestCase Rails::GemBoot.any_instance.expects(:run).returns('result') assert_equal 'result', Rails.boot! end - - def test_run_loads_initializer_and_sets_load_path - boot = Rails::Boot.new - boot.expects(:load_initializer) - Rails::Initializer.expects(:run).with(:set_load_path) - boot.run - end end class VendorBootTest < Test::Unit::TestCase @@ -63,7 +56,7 @@ class VendorBootTest < Test::Unit::TestCase def test_load_initializer_requires_from_vendor_rails boot = VendorBoot.new boot.expects(:require).with("rails") - Rails::Initializer.expects(:run).with(:install_gem_spec_stubs) + boot.expects(:install_gem_spec_stubs) Rails::GemDependency.expects(:add_frozen_gem_path) boot.load_initializer end diff --git a/railties/test/initializer/boot_test.rb b/railties/test/initializer/boot_test.rb new file mode 100644 index 0000000000..5ee3c45b21 --- /dev/null +++ b/railties/test/initializer/boot_test.rb @@ -0,0 +1,16 @@ +require "isolation/abstract_unit" + +module BootTests + class GemBooting < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + # build_app + # boot_rails + end + + test "booting rails sets the load paths correctly" do + # This test is pending reworking the boot process + end + end +end \ No newline at end of file diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index bc1b43acaa..869e8429cf 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -97,20 +97,16 @@ module TestHelpers end def boot_rails - # return if defined?(RAILS) - # TODO: Get this working with boot.rb - $:.unshift "#{RAILS_FRAMEWORK_ROOT}/railties/lib" - Object.class_eval <<-RUBY - RAILS_ROOT = "#{app_path}" - module ::Rails - def self.vendor_rails? - true - end + # TMP mega hax to prevent boot.rb from actually booting + Object.class_eval <<-RUBY, __FILE__, __LINE__+1 + module Rails + Initializer = 'lol' + require "#{app_path}/config/boot" + remove_const(:Initializer) + booter = VendorBoot.new + booter.run end RUBY - require "rails" - Rails::Initializer.run(:install_gem_spec_stubs) - Rails::GemDependency.add_frozen_gem_path end end end -- cgit v1.2.3