aboutsummaryrefslogtreecommitdiffstats
path: root/railties/environments
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-10-23 05:36:52 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-10-23 05:36:52 +0000
commit2559feb53923cd4ca1615b3c6c0c3a12ef7186d7 (patch)
tree65bb5397d08b7939bd42d9ab384bec449c113fe0 /railties/environments
parent24077a1ffc3d7e944ba0e78df9a387523fb4f419 (diff)
downloadrails-2559feb53923cd4ca1615b3c6c0c3a12ef7186d7.tar.gz
rails-2559feb53923cd4ca1615b3c6c0c3a12ef7186d7.tar.bz2
rails-2559feb53923cd4ca1615b3c6c0c3a12ef7186d7.zip
Refactor and test boot.rb. Include tests from and closes #9834.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7998 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/environments')
-rw-r--r--railties/environments/boot.rb110
1 files changed, 84 insertions, 26 deletions
diff --git a/railties/environments/boot.rb b/railties/environments/boot.rb
index 0f034f07bf..3dec666c3a 100644
--- a/railties/environments/boot.rb
+++ b/railties/environments/boot.rb
@@ -1,39 +1,97 @@
-# Don't change this file. Configuration is done in config/environment.rb and config/environments/*.rb
+# Don't change this file!
+# Configure your app in config/environment.rb and config/environments/*.rb
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
-unless defined?(Rails::Initializer)
- if File.directory?("#{RAILS_ROOT}/vendor/rails")
- require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
- else
- require 'rubygems'
+module Rails
+ class << self
+ def boot!
+ pick_boot.run unless booted?
+ end
+
+ def booted?
+ defined? Rails::Initializer
+ end
+
+ def pick_boot
+ (vendor_rails? ? VendorBoot : GemBoot).new
+ end
+
+ def vendor_rails?
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
+ end
+ end
+
+ class Boot
+ def run
+ load_initializer
+ Rails::Initializer.run(:set_load_path)
+ end
+ end
+
+ class VendorBoot < Boot
+ def load_initializer
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
+ end
+ end
+
+ class GemBoot < Boot
+ def load_initializer
+ self.class.load_rubygems
+ load_rails_gem
+ require 'initializer'
+ end
- rails_gem_version =
- if defined? RAILS_GEM_VERSION
- RAILS_GEM_VERSION
+ def load_rails_gem
+ if version = self.class.gem_version
+ gem 'rails', "=#{version}"
else
- File.read("#{File.dirname(__FILE__)}/environment.rb") =~ /^[^#]*RAILS_GEM_VERSION\s+=\s+'([\d.]+)'/
- $1
+ gem 'rails'
+ end
+ rescue Gem::LoadError => load_error
+ STDERR.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
+ exit 1
+ end
+
+ class << self
+ def rubygems_version
+ Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
+ end
+
+ def gem_version
+ if defined? RAILS_GEM_VERSION
+ RAILS_GEM_VERSION
+ elsif ENV.include?('RAILS_GEM_VERSION')
+ ENV['RAILS_GEM_VERSION']
+ else
+ parse_gem_version(read_environment_rb)
+ end
end
- if rails_gem_version
- rails_gem = Gem.cache.search('rails', "=#{rails_gem_version}.0").sort_by { |g| g.version.version }.last
+ def load_rubygems
+ require 'rubygems'
- if rails_gem
- gem "rails", "=#{rails_gem.version.version}"
- require rails_gem.full_gem_path + '/lib/initializer'
- else
- STDERR.puts %(Cannot find gem for Rails =#{rails_gem_version}.0:
- Install the missing gem with 'gem install -v=#{rails_gem_version} rails', or
- change environment.rb to define RAILS_GEM_VERSION with your desired version.
- )
+ unless rubygems_version >= '0.9.4'
+ STDERR.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
+ exit 1
+ end
+
+ rescue LoadError
+ STDERR.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
- else
- gem "rails"
- require 'initializer'
+
+ def parse_gem_version(text)
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*'([\d.]+)'/
+ end
+
+ private
+ def read_environment_rb
+ File.read("#{RAILS_ROOT}/config/environment.rb")
+ end
end
end
-
- Rails::Initializer.run(:set_load_path)
end
+
+# All that for this:
+Rails.boot!