rails-boot-reporting 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/bin/install-boot-reporting +6 -0
- data/lib/bundler-1.0/runtime.rb +159 -0
- data/lib/rails-2.3.11/initializer.rb +1177 -0
- data/lib/rails-boot-reporting.rb +33 -0
- data/test/helper.rb +18 -0
- data/test/test_rails-boot-reporting.rb +7 -0
- metadata +109 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.3"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.3)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.9.1)
|
10
|
+
rcov (0.9.9)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.6.3)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Aaron Quint
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= rails-boot-reporting
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to rails-boot-reporting
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Aaron Quint. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
require 'jeweler'
|
14
|
+
Jeweler::Tasks.new do |gem|
|
15
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
16
|
+
gem.name = "rails-boot-reporting"
|
17
|
+
gem.homepage = "http://github.com/quirkey/rails-boot-reporting"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = %Q{A set of monkey patches for rails and bundler to print require/boot times}
|
20
|
+
gem.description = %Q{Patches, specifically for rails 2 (now) that print out require and boot times for different parts of the initialization process}
|
21
|
+
gem.email = "aaron@quirkey.com"
|
22
|
+
gem.authors = ["Aaron Quint"]
|
23
|
+
# dependencies defined in Gemfile
|
24
|
+
end
|
25
|
+
Jeweler::RubygemsDotOrgTasks.new
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
Rake::TestTask.new(:test) do |test|
|
29
|
+
test.libs << 'lib' << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'rcov/rcovtask'
|
35
|
+
Rcov::RcovTask.new do |test|
|
36
|
+
test.libs << 'test'
|
37
|
+
test.pattern = 'test/**/test_*.rb'
|
38
|
+
test.verbose = true
|
39
|
+
test.rcov_opts << '--exclude "gems/*"'
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "rails-boot-reporting #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,159 @@
|
|
1
|
+
# patches Bundler.require specifically to print require times
|
2
|
+
require 'benchmark'
|
3
|
+
require "digest/sha1"
|
4
|
+
|
5
|
+
module Bundler
|
6
|
+
class Runtime < Environment
|
7
|
+
include SharedHelpers
|
8
|
+
|
9
|
+
def setup(*groups)
|
10
|
+
# Has to happen first
|
11
|
+
clean_load_path
|
12
|
+
|
13
|
+
specs = groups.any? ? @definition.specs_for(groups) : requested_specs
|
14
|
+
|
15
|
+
setup_environment
|
16
|
+
Bundler.rubygems.replace_entrypoints(specs)
|
17
|
+
|
18
|
+
# Activate the specs
|
19
|
+
specs.each do |spec|
|
20
|
+
unless spec.loaded_from
|
21
|
+
raise GemNotFound, "#{spec.full_name} is missing. Run `bundle` to get it."
|
22
|
+
end
|
23
|
+
|
24
|
+
if activated_spec = Bundler.rubygems.loaded_specs(spec.name) and activated_spec.version != spec.version
|
25
|
+
e = Gem::LoadError.new "You have already activated #{activated_spec.name} #{activated_spec.version}, " \
|
26
|
+
"but your Gemfile requires #{spec.name} #{spec.version}. Consider using bundle exec."
|
27
|
+
e.name = spec.name
|
28
|
+
if e.respond_to?(:requirement=)
|
29
|
+
e.requirement = Gem::Requirement.new(spec.version.to_s)
|
30
|
+
else
|
31
|
+
e.version_requirement = Gem::Requirement.new(spec.version.to_s)
|
32
|
+
end
|
33
|
+
raise e
|
34
|
+
end
|
35
|
+
|
36
|
+
Bundler.rubygems.mark_loaded(spec)
|
37
|
+
load_paths = spec.load_paths.reject {|path| $LOAD_PATH.include?(path)}
|
38
|
+
$LOAD_PATH.unshift(*load_paths)
|
39
|
+
end
|
40
|
+
|
41
|
+
lock
|
42
|
+
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
REGEXPS = [
|
47
|
+
/^no such file to load -- (.+)$/i,
|
48
|
+
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
|
49
|
+
/^Missing API definition file in (.+)$/i,
|
50
|
+
/^cannot load such file -- (.+)$/i,
|
51
|
+
]
|
52
|
+
|
53
|
+
# !!! HERE BE THE PATCH
|
54
|
+
def require(*groups)
|
55
|
+
groups.map! { |g| g.to_sym }
|
56
|
+
groups = [:default] if groups.empty?
|
57
|
+
|
58
|
+
@definition.dependencies.each do |dep|
|
59
|
+
# Skip the dependency if it is not in any of the requested
|
60
|
+
# groups
|
61
|
+
next unless ((dep.groups & groups).any? && dep.current_platform?)
|
62
|
+
|
63
|
+
required_file = nil
|
64
|
+
|
65
|
+
begin
|
66
|
+
# Loop through all the specified autorequires for the
|
67
|
+
# dependency. If there are none, use the dependency's name
|
68
|
+
# as the autorequire.
|
69
|
+
Array(dep.autorequire || dep.name).each do |file|
|
70
|
+
required_file = file
|
71
|
+
total = Benchmark.realtime {
|
72
|
+
Kernel.require file
|
73
|
+
}
|
74
|
+
puts "Bundler.require #{file}\t#{total}"
|
75
|
+
end
|
76
|
+
rescue LoadError => e
|
77
|
+
REGEXPS.find { |r| r =~ e.message }
|
78
|
+
raise if dep.autorequire || $1 != required_file
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
# !!!
|
83
|
+
|
84
|
+
def dependencies_for(*groups)
|
85
|
+
if groups.empty?
|
86
|
+
dependencies
|
87
|
+
else
|
88
|
+
dependencies.select { |d| (groups & d.groups).any? }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
alias gems specs
|
93
|
+
|
94
|
+
def cache
|
95
|
+
FileUtils.mkdir_p(cache_path) unless File.exists?(cache_path)
|
96
|
+
|
97
|
+
Bundler.ui.info "Updating .gem files in vendor/cache"
|
98
|
+
specs.each do |spec|
|
99
|
+
next if spec.name == 'bundler'
|
100
|
+
spec.source.cache(spec) if spec.source.respond_to?(:cache)
|
101
|
+
end
|
102
|
+
prune_cache unless Bundler.settings[:no_prune]
|
103
|
+
end
|
104
|
+
|
105
|
+
def prune_cache
|
106
|
+
FileUtils.mkdir_p(cache_path) unless File.exists?(cache_path)
|
107
|
+
|
108
|
+
resolve = @definition.resolve
|
109
|
+
cached = Dir["#{cache_path}/*.gem"]
|
110
|
+
|
111
|
+
cached = cached.delete_if do |path|
|
112
|
+
spec = Bundler.rubygems.spec_from_gem path
|
113
|
+
|
114
|
+
resolve.any? do |s|
|
115
|
+
s.name == spec.name && s.version == spec.version && !s.source.is_a?(Bundler::Source::Git)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
if cached.any?
|
120
|
+
Bundler.ui.info "Removing outdated .gem files from vendor/cache"
|
121
|
+
|
122
|
+
cached.each do |path|
|
123
|
+
Bundler.ui.info " * #{File.basename(path)}"
|
124
|
+
File.delete(path)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
private
|
130
|
+
|
131
|
+
def cache_path
|
132
|
+
root.join("vendor/cache")
|
133
|
+
end
|
134
|
+
|
135
|
+
def setup_environment
|
136
|
+
begin
|
137
|
+
ENV["BUNDLE_BIN_PATH"] = Bundler.rubygems.bin_path("bundler", "bundle", VERSION)
|
138
|
+
rescue Gem::GemNotFoundException
|
139
|
+
ENV["BUNDLE_BIN_PATH"] = File.expand_path("../../../bin/bundle", __FILE__)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Set PATH
|
143
|
+
paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR)
|
144
|
+
paths.unshift "#{Bundler.bundle_path}/bin"
|
145
|
+
ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR)
|
146
|
+
|
147
|
+
# Set BUNDLE_GEMFILE
|
148
|
+
ENV["BUNDLE_GEMFILE"] = default_gemfile.to_s
|
149
|
+
|
150
|
+
# Set RUBYOPT
|
151
|
+
rubyopt = [ENV["RUBYOPT"]].compact
|
152
|
+
if rubyopt.empty? || rubyopt.first !~ /-rbundler\/setup/
|
153
|
+
rubyopt.unshift "-rbundler/setup"
|
154
|
+
rubyopt.unshift "-I#{File.expand_path('../..', __FILE__)}"
|
155
|
+
ENV["RUBYOPT"] = rubyopt.join(' ')
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,1177 @@
|
|
1
|
+
# This file is a patched rails initializer that prints out
|
2
|
+
# require/load times for different aspects of the boot process
|
3
|
+
|
4
|
+
require 'logger'
|
5
|
+
require 'set'
|
6
|
+
require 'pathname'
|
7
|
+
require 'benchmark'
|
8
|
+
|
9
|
+
$LOAD_PATH.unshift File.dirname(__FILE__)
|
10
|
+
require 'railties_path'
|
11
|
+
require 'rails/version'
|
12
|
+
require 'rails/plugin/locator'
|
13
|
+
require 'rails/plugin/loader'
|
14
|
+
require 'rails/gem_dependency'
|
15
|
+
require 'rails/rack'
|
16
|
+
|
17
|
+
|
18
|
+
RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV)
|
19
|
+
|
20
|
+
module Rails
|
21
|
+
class << self
|
22
|
+
# The Configuration instance used to configure the Rails environment
|
23
|
+
def configuration
|
24
|
+
@@configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
def configuration=(configuration)
|
28
|
+
@@configuration = configuration
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialized?
|
32
|
+
@initialized || false
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialized=(initialized)
|
36
|
+
@initialized ||= initialized
|
37
|
+
end
|
38
|
+
|
39
|
+
def logger
|
40
|
+
if defined?(RAILS_DEFAULT_LOGGER)
|
41
|
+
RAILS_DEFAULT_LOGGER
|
42
|
+
else
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def backtrace_cleaner
|
48
|
+
@@backtrace_cleaner ||= begin
|
49
|
+
# Relies on ActiveSupport, so we have to lazy load to postpone definition until AS has been loaded
|
50
|
+
require 'rails/backtrace_cleaner'
|
51
|
+
Rails::BacktraceCleaner.new
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def root
|
56
|
+
Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
|
57
|
+
end
|
58
|
+
|
59
|
+
def env
|
60
|
+
@_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV)
|
61
|
+
end
|
62
|
+
|
63
|
+
def cache
|
64
|
+
RAILS_CACHE
|
65
|
+
end
|
66
|
+
|
67
|
+
def version
|
68
|
+
VERSION::STRING
|
69
|
+
end
|
70
|
+
|
71
|
+
def public_path
|
72
|
+
@@public_path ||= self.root ? File.join(self.root, "public") : "public"
|
73
|
+
end
|
74
|
+
|
75
|
+
def public_path=(path)
|
76
|
+
@@public_path = path
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# The Initializer is responsible for processing the Rails configuration, such
|
81
|
+
# as setting the $LOAD_PATH, requiring the right frameworks, initializing
|
82
|
+
# logging, and more. It can be run either as a single command that'll just
|
83
|
+
# use the default configuration, like this:
|
84
|
+
#
|
85
|
+
# Rails::Initializer.run
|
86
|
+
#
|
87
|
+
# But normally it's more interesting to pass in a custom configuration
|
88
|
+
# through the block running:
|
89
|
+
#
|
90
|
+
# Rails::Initializer.run do |config|
|
91
|
+
# config.frameworks -= [ :action_mailer ]
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# This will use the default configuration options from Rails::Configuration,
|
95
|
+
# but allow for overwriting on select areas.
|
96
|
+
class Initializer
|
97
|
+
# The Configuration instance used by this Initializer instance.
|
98
|
+
attr_reader :configuration
|
99
|
+
|
100
|
+
# The set of loaded plugins.
|
101
|
+
attr_reader :loaded_plugins
|
102
|
+
|
103
|
+
# Whether or not all the gem dependencies have been met
|
104
|
+
attr_reader :gems_dependencies_loaded
|
105
|
+
|
106
|
+
# Runs the initializer. By default, this will invoke the #process method,
|
107
|
+
# which simply executes all of the initialization routines. Alternately,
|
108
|
+
# you can specify explicitly which initialization routine you want:
|
109
|
+
#
|
110
|
+
# Rails::Initializer.run(:set_load_path)
|
111
|
+
#
|
112
|
+
# This is useful if you only want the load path initialized, without
|
113
|
+
# incurring the overhead of completely loading the entire environment.
|
114
|
+
def self.run(command = :process, configuration = Configuration.new)
|
115
|
+
yield configuration if block_given?
|
116
|
+
initializer = new configuration
|
117
|
+
initializer.send(command)
|
118
|
+
initializer
|
119
|
+
end
|
120
|
+
|
121
|
+
# Create a new Initializer instance that references the given Configuration
|
122
|
+
# instance.
|
123
|
+
def initialize(configuration)
|
124
|
+
@configuration = configuration
|
125
|
+
@loaded_plugins = []
|
126
|
+
end
|
127
|
+
|
128
|
+
# Sequentially step through all of the available initialization routines,
|
129
|
+
# in order (view execution order in source).
|
130
|
+
def process
|
131
|
+
Rails.configuration = configuration
|
132
|
+
|
133
|
+
total_time = 0.0
|
134
|
+
tasks = []
|
135
|
+
%w{
|
136
|
+
check_ruby_version
|
137
|
+
install_gem_spec_stubs
|
138
|
+
set_load_path
|
139
|
+
add_gem_load_paths
|
140
|
+
|
141
|
+
require_frameworks
|
142
|
+
set_autoload_paths
|
143
|
+
add_plugin_load_paths
|
144
|
+
load_environment
|
145
|
+
preload_frameworks
|
146
|
+
|
147
|
+
initialize_encoding
|
148
|
+
initialize_database
|
149
|
+
|
150
|
+
initialize_cache
|
151
|
+
initialize_framework_caches
|
152
|
+
|
153
|
+
initialize_logger
|
154
|
+
initialize_framework_logging
|
155
|
+
|
156
|
+
initialize_dependency_mechanism
|
157
|
+
initialize_whiny_nils
|
158
|
+
|
159
|
+
initialize_time_zone
|
160
|
+
initialize_i18n
|
161
|
+
|
162
|
+
initialize_framework_settings
|
163
|
+
initialize_framework_views
|
164
|
+
|
165
|
+
initialize_metal
|
166
|
+
|
167
|
+
add_support_load_paths
|
168
|
+
|
169
|
+
check_for_unbuilt_gems
|
170
|
+
|
171
|
+
load_gems
|
172
|
+
load_plugins
|
173
|
+
|
174
|
+
add_gem_load_paths
|
175
|
+
load_gems
|
176
|
+
check_gem_dependencies
|
177
|
+
|
178
|
+
load_application_initializers
|
179
|
+
|
180
|
+
after_initialize
|
181
|
+
|
182
|
+
initialize_database_middleware
|
183
|
+
|
184
|
+
prepare_dispatcher
|
185
|
+
|
186
|
+
initialize_routing
|
187
|
+
|
188
|
+
load_observers
|
189
|
+
|
190
|
+
load_view_paths
|
191
|
+
|
192
|
+
load_application_classes
|
193
|
+
|
194
|
+
disable_dependency_loading
|
195
|
+
}.each do |task|
|
196
|
+
start = Time.now.to_f
|
197
|
+
send(task)
|
198
|
+
time = Time.now.to_f - start
|
199
|
+
puts "task: #{task} %.3f" % time
|
200
|
+
tasks << [time, task]
|
201
|
+
total_time += time
|
202
|
+
end
|
203
|
+
puts "---- Done Initializing ----"
|
204
|
+
tasks.sort.each {|t|
|
205
|
+
puts "#{t[1]} #{t[0]}"
|
206
|
+
}
|
207
|
+
puts "Total time: #{total_time}"
|
208
|
+
# Flag initialized
|
209
|
+
Rails.initialized = true
|
210
|
+
end
|
211
|
+
|
212
|
+
# Check for valid Ruby version
|
213
|
+
# This is done in an external file, so we can use it
|
214
|
+
# from the `rails` program as well without duplication.
|
215
|
+
def check_ruby_version
|
216
|
+
require 'ruby_version_check'
|
217
|
+
end
|
218
|
+
|
219
|
+
# If Rails is vendored and RubyGems is available, install stub GemSpecs
|
220
|
+
# for Rails, Active Support, Active Record, Action Pack, Action Mailer, and
|
221
|
+
# Active Resource. This allows Gem plugins to depend on Rails even when
|
222
|
+
# the Gem version of Rails shouldn't be loaded.
|
223
|
+
def install_gem_spec_stubs
|
224
|
+
unless Rails.respond_to?(:vendor_rails?)
|
225
|
+
abort %{Your config/boot.rb is outdated: Run "rake rails:update".}
|
226
|
+
end
|
227
|
+
|
228
|
+
if Rails.vendor_rails?
|
229
|
+
begin; require "rubygems"; rescue LoadError; return; end
|
230
|
+
|
231
|
+
stubs = %w(rails activesupport activerecord actionpack actionmailer activeresource)
|
232
|
+
stubs.reject! { |s| Gem.loaded_specs.key?(s) }
|
233
|
+
|
234
|
+
stubs.each do |stub|
|
235
|
+
Gem.loaded_specs[stub] = Gem::Specification.new do |s|
|
236
|
+
s.name = stub
|
237
|
+
s.version = Rails::VERSION::STRING
|
238
|
+
s.loaded_from = ""
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
# Set the <tt>$LOAD_PATH</tt> based on the value of
|
245
|
+
# Configuration#autoload_paths. Duplicates are removed.
|
246
|
+
def set_load_path
|
247
|
+
load_paths = configuration.autoload_paths + configuration.framework_paths
|
248
|
+
load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
|
249
|
+
$LOAD_PATH.uniq!
|
250
|
+
end
|
251
|
+
|
252
|
+
# Set the paths from which Rails will automatically load source files, and
|
253
|
+
# the load_once paths.
|
254
|
+
def set_autoload_paths
|
255
|
+
ActiveSupport::Dependencies.autoload_paths = configuration.autoload_paths.uniq
|
256
|
+
ActiveSupport::Dependencies.autoload_once_paths = configuration.autoload_once_paths.uniq
|
257
|
+
|
258
|
+
extra = ActiveSupport::Dependencies.autoload_once_paths - ActiveSupport::Dependencies.autoload_paths
|
259
|
+
unless extra.empty?
|
260
|
+
abort <<-end_error
|
261
|
+
autoload_once_paths must be a subset of the autoload_paths.
|
262
|
+
Extra items in autoload_once_paths: #{extra * ','}
|
263
|
+
end_error
|
264
|
+
end
|
265
|
+
|
266
|
+
# Freeze the arrays so future modifications will fail rather than do nothing mysteriously
|
267
|
+
configuration.autoload_once_paths.freeze
|
268
|
+
end
|
269
|
+
|
270
|
+
# Requires all frameworks specified by the Configuration#frameworks
|
271
|
+
# list. By default, all frameworks (Active Record, Active Support,
|
272
|
+
# Action Pack, Action Mailer, and Active Resource) are loaded.
|
273
|
+
def require_frameworks
|
274
|
+
configuration.frameworks.each { |framework| require(framework.to_s) }
|
275
|
+
rescue LoadError => e
|
276
|
+
# Re-raise as RuntimeError because Mongrel would swallow LoadError.
|
277
|
+
raise e.to_s
|
278
|
+
end
|
279
|
+
|
280
|
+
# Preload all frameworks specified by the Configuration#frameworks.
|
281
|
+
# Used by Passenger to ensure everything's loaded before forking and
|
282
|
+
# to avoid autoload race conditions in JRuby.
|
283
|
+
def preload_frameworks
|
284
|
+
if configuration.preload_frameworks
|
285
|
+
configuration.frameworks.each do |framework|
|
286
|
+
# String#classify and #constantize aren't available yet.
|
287
|
+
toplevel = Object.const_get(framework.to_s.gsub(/(?:^|_)(.)/) { $1.upcase })
|
288
|
+
toplevel.load_all! if toplevel.respond_to?(:load_all!)
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
# Add the load paths used by support functions such as the info controller
|
294
|
+
def add_support_load_paths
|
295
|
+
end
|
296
|
+
|
297
|
+
# Adds all load paths from plugins to the global set of load paths, so that
|
298
|
+
# code from plugins can be required (explicitly or automatically via ActiveSupport::Dependencies).
|
299
|
+
def add_plugin_load_paths
|
300
|
+
plugin_loader.add_plugin_load_paths
|
301
|
+
end
|
302
|
+
|
303
|
+
def add_gem_load_paths
|
304
|
+
# Rails::GemDependency.add_frozen_gem_path
|
305
|
+
unless @configuration.gems.empty?
|
306
|
+
puts "add_gem_load_paths! #{@configuration.gems.inspect}"
|
307
|
+
require "rubygems"
|
308
|
+
@configuration.gems.each { |gem| gem.add_load_paths }
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
def load_gems
|
313
|
+
unless $gems_rake_task
|
314
|
+
puts @configuration.gems.each {|g| puts g.inspect}
|
315
|
+
@configuration.gems.each { |gem| gem.load }
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
def check_for_unbuilt_gems
|
320
|
+
unbuilt_gems = @configuration.gems.select(&:frozen?).reject(&:built?)
|
321
|
+
if unbuilt_gems.size > 0
|
322
|
+
# don't print if the gems:build rake tasks are being run
|
323
|
+
unless $gems_build_rake_task
|
324
|
+
abort <<-end_error
|
325
|
+
The following gems have native components that need to be built
|
326
|
+
#{unbuilt_gems.map { |gem| "#{gem.name} #{gem.requirement}" } * "\n "}
|
327
|
+
|
328
|
+
You're running:
|
329
|
+
ruby #{Gem.ruby_version} at #{Gem.ruby}
|
330
|
+
rubygems #{Gem::RubyGemsVersion} at #{Gem.path * ', '}
|
331
|
+
|
332
|
+
Run `rake gems:build` to build the unbuilt gems.
|
333
|
+
end_error
|
334
|
+
end
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
def check_gem_dependencies
|
339
|
+
unloaded_gems = @configuration.gems.reject { |g| g.loaded? }
|
340
|
+
if unloaded_gems.size > 0
|
341
|
+
@gems_dependencies_loaded = false
|
342
|
+
# don't print if the gems rake tasks are being run
|
343
|
+
unless $gems_rake_task
|
344
|
+
abort <<-end_error
|
345
|
+
Missing these required gems:
|
346
|
+
#{unloaded_gems.map { |gem| "#{gem.name} #{gem.requirement}" } * "\n "}
|
347
|
+
|
348
|
+
You're running:
|
349
|
+
ruby #{Gem.ruby_version} at #{Gem.ruby}
|
350
|
+
rubygems #{Gem::RubyGemsVersion} at #{Gem.path * ', '}
|
351
|
+
|
352
|
+
Run `rake gems:install` to install the missing gems.
|
353
|
+
end_error
|
354
|
+
end
|
355
|
+
else
|
356
|
+
@gems_dependencies_loaded = true
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
# Loads all plugins in <tt>config.plugin_paths</tt>. <tt>plugin_paths</tt>
|
361
|
+
# defaults to <tt>vendor/plugins</tt> but may also be set to a list of
|
362
|
+
# paths, such as
|
363
|
+
# config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
|
364
|
+
#
|
365
|
+
# In the default implementation, as each plugin discovered in <tt>plugin_paths</tt> is initialized:
|
366
|
+
# * its +lib+ directory, if present, is added to the load path (immediately after the applications lib directory)
|
367
|
+
# * <tt>init.rb</tt> is evaluated, if present
|
368
|
+
#
|
369
|
+
# After all plugins are loaded, duplicates are removed from the load path.
|
370
|
+
# If an array of plugin names is specified in config.plugins, only those plugins will be loaded
|
371
|
+
# and they plugins will be loaded in that order. Otherwise, plugins are loaded in alphabetical
|
372
|
+
# order.
|
373
|
+
#
|
374
|
+
# if config.plugins ends contains :all then the named plugins will be loaded in the given order and all other
|
375
|
+
# plugins will be loaded in alphabetical order
|
376
|
+
def load_plugins
|
377
|
+
plugin_loader.load_plugins
|
378
|
+
end
|
379
|
+
|
380
|
+
def plugin_loader
|
381
|
+
@plugin_loader ||= configuration.plugin_loader.new(self)
|
382
|
+
end
|
383
|
+
|
384
|
+
# Loads the environment specified by Configuration#environment_path, which
|
385
|
+
# is typically one of development, test, or production.
|
386
|
+
def load_environment
|
387
|
+
silence_warnings do
|
388
|
+
return if @environment_loaded
|
389
|
+
@environment_loaded = true
|
390
|
+
|
391
|
+
config = configuration
|
392
|
+
constants = self.class.constants
|
393
|
+
|
394
|
+
eval(IO.read(configuration.environment_path), binding, configuration.environment_path)
|
395
|
+
|
396
|
+
(self.class.constants - constants).each do |const|
|
397
|
+
Object.const_set(const, self.class.const_get(const))
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
def load_observers
|
403
|
+
if gems_dependencies_loaded && configuration.frameworks.include?(:active_record)
|
404
|
+
ActiveRecord::Base.instantiate_observers
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
def load_view_paths
|
409
|
+
if configuration.frameworks.include?(:action_view)
|
410
|
+
ActionController::Base.view_paths.load! if configuration.frameworks.include?(:action_controller)
|
411
|
+
ActionMailer::Base.view_paths.load! if configuration.frameworks.include?(:action_mailer)
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
# Eager load application classes
|
416
|
+
def load_application_classes
|
417
|
+
return if $rails_rake_task && configuration.dependency_loading
|
418
|
+
if configuration.cache_classes
|
419
|
+
tasks = []
|
420
|
+
configuration.eager_load_paths.each do |load_path|
|
421
|
+
matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
|
422
|
+
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
|
423
|
+
f = nil
|
424
|
+
start = Time.now.to_f
|
425
|
+
f = file.sub(matcher, '\1')
|
426
|
+
time = Time.now.to_f - start
|
427
|
+
puts "require #{f}"
|
428
|
+
require_dependency f
|
429
|
+
tasks << [time, f]
|
430
|
+
end
|
431
|
+
end
|
432
|
+
puts "---- Totals ----"
|
433
|
+
tasks.sort.each {|t|
|
434
|
+
puts "#{t[1]}\t#{t[0]}"
|
435
|
+
}
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
# For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
|
440
|
+
# multibyte safe operations. Plugin authors supporting other encodings
|
441
|
+
# should override this behaviour and set the relevant +default_charset+
|
442
|
+
# on ActionController::Base.
|
443
|
+
#
|
444
|
+
# For Ruby 1.9, this does nothing. Specify the default encoding in the Ruby
|
445
|
+
# shebang line if you don't want UTF-8.
|
446
|
+
def initialize_encoding
|
447
|
+
$KCODE='u' if RUBY_VERSION < '1.9'
|
448
|
+
end
|
449
|
+
|
450
|
+
# This initialization routine does nothing unless <tt>:active_record</tt>
|
451
|
+
# is one of the frameworks to load (Configuration#frameworks). If it is,
|
452
|
+
# this sets the database configuration from Configuration#database_configuration
|
453
|
+
# and then establishes the connection.
|
454
|
+
def initialize_database
|
455
|
+
if configuration.frameworks.include?(:active_record)
|
456
|
+
ActiveRecord::Base.configurations = configuration.database_configuration
|
457
|
+
ActiveRecord::Base.establish_connection
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
def initialize_database_middleware
|
462
|
+
if configuration.frameworks.include?(:active_record)
|
463
|
+
if configuration.frameworks.include?(:action_controller) &&
|
464
|
+
ActionController::Base.session_store.name == 'ActiveRecord::SessionStore'
|
465
|
+
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
|
466
|
+
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache
|
467
|
+
else
|
468
|
+
configuration.middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
469
|
+
configuration.middleware.use ActiveRecord::QueryCache
|
470
|
+
end
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
474
|
+
def initialize_cache
|
475
|
+
unless defined?(RAILS_CACHE)
|
476
|
+
silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(configuration.cache_store) }
|
477
|
+
|
478
|
+
if RAILS_CACHE.respond_to?(:middleware)
|
479
|
+
# Insert middleware to setup and teardown local cache for each request
|
480
|
+
configuration.middleware.insert_after(:"ActionController::Failsafe", RAILS_CACHE.middleware)
|
481
|
+
end
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
def initialize_framework_caches
|
486
|
+
if configuration.frameworks.include?(:action_controller)
|
487
|
+
ActionController::Base.cache_store ||= RAILS_CACHE
|
488
|
+
end
|
489
|
+
end
|
490
|
+
|
491
|
+
# If the RAILS_DEFAULT_LOGGER constant is already set, this initialization
|
492
|
+
# routine does nothing. If the constant is not set, and Configuration#logger
|
493
|
+
# is not +nil+, this also does nothing. Otherwise, a new logger instance
|
494
|
+
# is created at Configuration#log_path, with a default log level of
|
495
|
+
# Configuration#log_level.
|
496
|
+
#
|
497
|
+
# If the log could not be created, the log will be set to output to
|
498
|
+
# +STDERR+, with a log level of +WARN+.
|
499
|
+
def initialize_logger
|
500
|
+
# if the environment has explicitly defined a logger, use it
|
501
|
+
return if Rails.logger
|
502
|
+
|
503
|
+
unless logger = configuration.logger
|
504
|
+
begin
|
505
|
+
logger = ActiveSupport::BufferedLogger.new(configuration.log_path)
|
506
|
+
logger.level = ActiveSupport::BufferedLogger.const_get(configuration.log_level.to_s.upcase)
|
507
|
+
if configuration.environment == "production"
|
508
|
+
logger.auto_flushing = false
|
509
|
+
end
|
510
|
+
rescue StandardError => e
|
511
|
+
logger = ActiveSupport::BufferedLogger.new(STDERR)
|
512
|
+
logger.level = ActiveSupport::BufferedLogger::WARN
|
513
|
+
logger.warn(
|
514
|
+
"Rails Error: Unable to access log file. Please ensure that #{configuration.log_path} exists and is chmod 0666. " +
|
515
|
+
"The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
|
516
|
+
)
|
517
|
+
end
|
518
|
+
end
|
519
|
+
|
520
|
+
silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
|
521
|
+
end
|
522
|
+
|
523
|
+
# Sets the logger for Active Record, Action Controller, and Action Mailer
|
524
|
+
# (but only for those frameworks that are to be loaded). If the framework's
|
525
|
+
# logger is already set, it is not changed, otherwise it is set to use
|
526
|
+
# RAILS_DEFAULT_LOGGER.
|
527
|
+
def initialize_framework_logging
|
528
|
+
for framework in ([ :active_record, :action_controller, :action_mailer ] & configuration.frameworks)
|
529
|
+
framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
|
530
|
+
end
|
531
|
+
|
532
|
+
ActiveSupport::Dependencies.logger ||= Rails.logger
|
533
|
+
Rails.cache.logger ||= Rails.logger
|
534
|
+
end
|
535
|
+
|
536
|
+
# Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
|
537
|
+
# (but only for those frameworks that are to be loaded). If the framework's
|
538
|
+
# paths have already been set, it is not changed, otherwise it is
|
539
|
+
# set to use Configuration#view_path.
|
540
|
+
def initialize_framework_views
|
541
|
+
if configuration.frameworks.include?(:action_view)
|
542
|
+
view_path = ActionView::PathSet.type_cast(configuration.view_path)
|
543
|
+
ActionMailer::Base.template_root = view_path if configuration.frameworks.include?(:action_mailer) && ActionMailer::Base.view_paths.blank?
|
544
|
+
ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.blank?
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
# If Action Controller is not one of the loaded frameworks (Configuration#frameworks)
|
549
|
+
# this does nothing. Otherwise, it loads the routing definitions and sets up
|
550
|
+
# loading module used to lazily load controllers (Configuration#controller_paths).
|
551
|
+
def initialize_routing
|
552
|
+
return unless configuration.frameworks.include?(:action_controller)
|
553
|
+
|
554
|
+
ActionController::Routing.controller_paths += configuration.controller_paths
|
555
|
+
ActionController::Routing::Routes.add_configuration_file(configuration.routes_configuration_file)
|
556
|
+
ActionController::Routing::Routes.reload!
|
557
|
+
end
|
558
|
+
|
559
|
+
# Sets the dependency loading mechanism based on the value of
|
560
|
+
# Configuration#cache_classes.
|
561
|
+
def initialize_dependency_mechanism
|
562
|
+
ActiveSupport::Dependencies.mechanism = configuration.cache_classes ? :require : :load
|
563
|
+
end
|
564
|
+
|
565
|
+
# Loads support for "whiny nil" (noisy warnings when methods are invoked
|
566
|
+
# on +nil+ values) if Configuration#whiny_nils is true.
|
567
|
+
def initialize_whiny_nils
|
568
|
+
require('active_support/whiny_nil') if configuration.whiny_nils
|
569
|
+
end
|
570
|
+
|
571
|
+
# Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes.
|
572
|
+
# If assigned value cannot be matched to a TimeZone, an exception will be raised.
|
573
|
+
def initialize_time_zone
|
574
|
+
if configuration.time_zone
|
575
|
+
zone_default = nil
|
576
|
+
time = Benchmark.realtime do
|
577
|
+
# zone_default = Time.__send__(:get_zone, configuration.time_zone)
|
578
|
+
zone_default = ActiveSupport::TimeZone.create('UTC')
|
579
|
+
end
|
580
|
+
|
581
|
+
unless zone_default
|
582
|
+
raise \
|
583
|
+
'Value assigned to config.time_zone not recognized.' +
|
584
|
+
'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
|
585
|
+
end
|
586
|
+
|
587
|
+
Time.zone_default = zone_default
|
588
|
+
|
589
|
+
if configuration.frameworks.include?(:active_record)
|
590
|
+
ActiveRecord::Base.time_zone_aware_attributes = true
|
591
|
+
ActiveRecord::Base.default_timezone = :utc
|
592
|
+
end
|
593
|
+
end
|
594
|
+
end
|
595
|
+
|
596
|
+
# Set the i18n configuration from config.i18n but special-case for the load_path which should be
|
597
|
+
# appended to what's already set instead of overwritten.
|
598
|
+
def initialize_i18n
|
599
|
+
configuration.i18n.each do |setting, value|
|
600
|
+
if setting == :load_path
|
601
|
+
I18n.load_path += value
|
602
|
+
else
|
603
|
+
I18n.send("#{setting}=", value)
|
604
|
+
end
|
605
|
+
end
|
606
|
+
end
|
607
|
+
|
608
|
+
def initialize_metal
|
609
|
+
Rails::Rack::Metal.requested_metals = configuration.metals
|
610
|
+
Rails::Rack::Metal.metal_paths += plugin_loader.engine_metal_paths
|
611
|
+
|
612
|
+
configuration.middleware.insert_before(
|
613
|
+
:"ActionController::ParamsParser",
|
614
|
+
Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
|
615
|
+
end
|
616
|
+
|
617
|
+
# Initializes framework-specific settings for each of the loaded frameworks
|
618
|
+
# (Configuration#frameworks). The available settings map to the accessors
|
619
|
+
# on each of the corresponding Base classes.
|
620
|
+
def initialize_framework_settings
|
621
|
+
configuration.frameworks.each do |framework|
|
622
|
+
base_class = framework.to_s.camelize.constantize.const_get("Base")
|
623
|
+
|
624
|
+
configuration.send(framework).each do |setting, value|
|
625
|
+
base_class.send("#{setting}=", value)
|
626
|
+
end
|
627
|
+
end
|
628
|
+
configuration.active_support.each do |setting, value|
|
629
|
+
ActiveSupport.send("#{setting}=", value)
|
630
|
+
end
|
631
|
+
end
|
632
|
+
|
633
|
+
# Fires the user-supplied after_initialize block (Configuration#after_initialize)
|
634
|
+
def after_initialize
|
635
|
+
if gems_dependencies_loaded
|
636
|
+
configuration.after_initialize_blocks.each do |block|
|
637
|
+
block.call
|
638
|
+
end
|
639
|
+
end
|
640
|
+
end
|
641
|
+
|
642
|
+
def load_application_initializers
|
643
|
+
if gems_dependencies_loaded
|
644
|
+
Dir["#{configuration.root_path}/config/initializers/**/*.rb"].sort.each do |initializer|
|
645
|
+
start = Time.now.to_f
|
646
|
+
load(initializer)
|
647
|
+
time = Time.now.to_f - start
|
648
|
+
puts "#{initializer} #{time}"
|
649
|
+
end
|
650
|
+
end
|
651
|
+
end
|
652
|
+
|
653
|
+
def prepare_dispatcher
|
654
|
+
return unless configuration.frameworks.include?(:action_controller)
|
655
|
+
require 'dispatcher' unless defined?(::Dispatcher)
|
656
|
+
Dispatcher.define_dispatcher_callbacks(configuration.cache_classes)
|
657
|
+
Dispatcher.run_prepare_callbacks
|
658
|
+
end
|
659
|
+
|
660
|
+
def disable_dependency_loading
|
661
|
+
if configuration.cache_classes && !configuration.dependency_loading
|
662
|
+
ActiveSupport::Dependencies.unhook!
|
663
|
+
end
|
664
|
+
end
|
665
|
+
end
|
666
|
+
|
667
|
+
# The Configuration class holds all the parameters for the Initializer and
|
668
|
+
# ships with defaults that suites most Rails applications. But it's possible
|
669
|
+
# to overwrite everything. Usually, you'll create an Configuration file
|
670
|
+
# implicitly through the block running on the Initializer, but it's also
|
671
|
+
# possible to create the Configuration instance in advance and pass it in
|
672
|
+
# like this:
|
673
|
+
#
|
674
|
+
# config = Rails::Configuration.new
|
675
|
+
# Rails::Initializer.run(:process, config)
|
676
|
+
class Configuration
|
677
|
+
# The application's base directory.
|
678
|
+
attr_reader :root_path
|
679
|
+
|
680
|
+
# A stub for setting options on ActionController::Base.
|
681
|
+
attr_accessor :action_controller
|
682
|
+
|
683
|
+
# A stub for setting options on ActionMailer::Base.
|
684
|
+
attr_accessor :action_mailer
|
685
|
+
|
686
|
+
# A stub for setting options on ActionView::Base.
|
687
|
+
attr_accessor :action_view
|
688
|
+
|
689
|
+
# A stub for setting options on ActiveRecord::Base.
|
690
|
+
attr_accessor :active_record
|
691
|
+
|
692
|
+
# A stub for setting options on ActiveResource::Base.
|
693
|
+
attr_accessor :active_resource
|
694
|
+
|
695
|
+
# A stub for setting options on ActiveSupport.
|
696
|
+
attr_accessor :active_support
|
697
|
+
|
698
|
+
# Whether to preload all frameworks at startup.
|
699
|
+
attr_accessor :preload_frameworks
|
700
|
+
|
701
|
+
# Whether or not classes should be cached (set to false if you want
|
702
|
+
# application classes to be reloaded on each request)
|
703
|
+
attr_accessor :cache_classes
|
704
|
+
|
705
|
+
# The list of paths that should be searched for controllers. (Defaults
|
706
|
+
# to <tt>app/controllers</tt>.)
|
707
|
+
attr_accessor :controller_paths
|
708
|
+
|
709
|
+
# The path to the database configuration file to use. (Defaults to
|
710
|
+
# <tt>config/database.yml</tt>.)
|
711
|
+
attr_accessor :database_configuration_file
|
712
|
+
|
713
|
+
# The path to the routes configuration file to use. (Defaults to
|
714
|
+
# <tt>config/routes.rb</tt>.)
|
715
|
+
attr_accessor :routes_configuration_file
|
716
|
+
|
717
|
+
# The list of rails framework components that should be loaded. (Defaults
|
718
|
+
# to <tt>:active_record</tt>, <tt>:action_controller</tt>,
|
719
|
+
# <tt>:action_view</tt>, <tt>:action_mailer</tt>, and
|
720
|
+
# <tt>:active_resource</tt>).
|
721
|
+
attr_accessor :frameworks
|
722
|
+
|
723
|
+
# An array of additional paths to prepend to the load path. By default,
|
724
|
+
# all +app+, +lib+, +vendor+ and mock paths are included in this list.
|
725
|
+
attr_accessor :autoload_paths
|
726
|
+
|
727
|
+
# Deprecated, use autoload_paths.
|
728
|
+
def load_paths
|
729
|
+
$stderr.puts("config.load_paths is deprecated and removed in Rails 3, please use autoload_paths instead")
|
730
|
+
autoload_paths
|
731
|
+
end
|
732
|
+
|
733
|
+
# Deprecated, use autoload_paths=.
|
734
|
+
def load_paths=(paths)
|
735
|
+
$stderr.puts("config.load_paths= is deprecated and removed in Rails 3, please use autoload_paths= instead")
|
736
|
+
self.autoload_paths = paths
|
737
|
+
end
|
738
|
+
|
739
|
+
# An array of paths from which Rails will automatically load from only once.
|
740
|
+
# All elements of this array must also be in +autoload_paths+.
|
741
|
+
attr_accessor :autoload_once_paths
|
742
|
+
|
743
|
+
# Deprecated, use autoload_once_paths.
|
744
|
+
def load_once_paths
|
745
|
+
$stderr.puts("config.load_once_paths is deprecated and removed in Rails 3, please use autoload_once_paths instead")
|
746
|
+
autoload_once_paths
|
747
|
+
end
|
748
|
+
|
749
|
+
# Deprecated, use autoload_once_paths=.
|
750
|
+
def load_once_paths=(paths)
|
751
|
+
$stderr.puts("config.load_once_paths= is deprecated and removed in Rails 3, please use autoload_once_paths= instead")
|
752
|
+
self.autoload_once_paths = paths
|
753
|
+
end
|
754
|
+
|
755
|
+
# An array of paths from which Rails will eager load on boot if cache
|
756
|
+
# classes is enabled. All elements of this array must also be in
|
757
|
+
# +autoload_paths+.
|
758
|
+
attr_accessor :eager_load_paths
|
759
|
+
|
760
|
+
# The log level to use for the default Rails logger. In production mode,
|
761
|
+
# this defaults to <tt>:info</tt>. In development mode, it defaults to
|
762
|
+
# <tt>:debug</tt>.
|
763
|
+
attr_accessor :log_level
|
764
|
+
|
765
|
+
# The path to the log file to use. Defaults to log/#{environment}.log
|
766
|
+
# (e.g. log/development.log or log/production.log).
|
767
|
+
attr_accessor :log_path
|
768
|
+
|
769
|
+
# The specific logger to use. By default, a logger will be created and
|
770
|
+
# initialized using #log_path and #log_level, but a programmer may
|
771
|
+
# specifically set the logger to use via this accessor and it will be
|
772
|
+
# used directly.
|
773
|
+
attr_accessor :logger
|
774
|
+
|
775
|
+
# The specific cache store to use. By default, the ActiveSupport::Cache::Store will be used.
|
776
|
+
attr_accessor :cache_store
|
777
|
+
|
778
|
+
# The root of the application's views. (Defaults to <tt>app/views</tt>.)
|
779
|
+
attr_accessor :view_path
|
780
|
+
|
781
|
+
# Set to +true+ if you want to be warned (noisily) when you try to invoke
|
782
|
+
# any method of +nil+. Set to +false+ for the standard Ruby behavior.
|
783
|
+
attr_accessor :whiny_nils
|
784
|
+
|
785
|
+
# The list of plugins to load. If this is set to <tt>nil</tt>, all plugins will
|
786
|
+
# be loaded. If this is set to <tt>[]</tt>, no plugins will be loaded. Otherwise,
|
787
|
+
# plugins will be loaded in the order specified.
|
788
|
+
attr_reader :plugins
|
789
|
+
def plugins=(plugins)
|
790
|
+
@plugins = plugins.nil? ? nil : plugins.map { |p| p.to_sym }
|
791
|
+
end
|
792
|
+
|
793
|
+
# The list of metals to load. If this is set to <tt>nil</tt>, all metals will
|
794
|
+
# be loaded in alphabetical order. If this is set to <tt>[]</tt>, no metals will
|
795
|
+
# be loaded. Otherwise metals will be loaded in the order specified
|
796
|
+
attr_accessor :metals
|
797
|
+
|
798
|
+
# The path to the root of the plugins directory. By default, it is in
|
799
|
+
# <tt>vendor/plugins</tt>.
|
800
|
+
attr_accessor :plugin_paths
|
801
|
+
|
802
|
+
# The classes that handle finding the desired plugins that you'd like to load for
|
803
|
+
# your application. By default it is the Rails::Plugin::FileSystemLocator which finds
|
804
|
+
# plugins to load in <tt>vendor/plugins</tt>. You can hook into gem location by subclassing
|
805
|
+
# Rails::Plugin::Locator and adding it onto the list of <tt>plugin_locators</tt>.
|
806
|
+
attr_accessor :plugin_locators
|
807
|
+
|
808
|
+
# The class that handles loading each plugin. Defaults to Rails::Plugin::Loader, but
|
809
|
+
# a sub class would have access to fine grained modification of the loading behavior. See
|
810
|
+
# the implementation of Rails::Plugin::Loader for more details.
|
811
|
+
attr_accessor :plugin_loader
|
812
|
+
|
813
|
+
# Enables or disables plugin reloading. You can get around this setting per plugin.
|
814
|
+
# If <tt>reload_plugins?</tt> is false, add this to your plugin's <tt>init.rb</tt>
|
815
|
+
# to make it reloadable:
|
816
|
+
#
|
817
|
+
# ActiveSupport::Dependencies.autoload_once_paths.delete lib_path
|
818
|
+
#
|
819
|
+
# If <tt>reload_plugins?</tt> is true, add this to your plugin's <tt>init.rb</tt>
|
820
|
+
# to only load it once:
|
821
|
+
#
|
822
|
+
# ActiveSupport::Dependencies.autoload_once_paths << lib_path
|
823
|
+
#
|
824
|
+
attr_accessor :reload_plugins
|
825
|
+
|
826
|
+
# Returns true if plugin reloading is enabled.
|
827
|
+
def reload_plugins?
|
828
|
+
!!@reload_plugins
|
829
|
+
end
|
830
|
+
|
831
|
+
# Enables or disables dependency loading during the request cycle. Setting
|
832
|
+
# <tt>dependency_loading</tt> to true will allow new classes to be loaded
|
833
|
+
# during a request. Setting it to false will disable this behavior.
|
834
|
+
#
|
835
|
+
# Those who want to run in a threaded environment should disable this
|
836
|
+
# option and eager load or require all there classes on initialization.
|
837
|
+
#
|
838
|
+
# If <tt>cache_classes</tt> is disabled, dependency loaded will always be
|
839
|
+
# on.
|
840
|
+
attr_accessor :dependency_loading
|
841
|
+
|
842
|
+
# An array of gems that this rails application depends on. Rails will automatically load
|
843
|
+
# these gems during installation, and allow you to install any missing gems with:
|
844
|
+
#
|
845
|
+
# rake gems:install
|
846
|
+
#
|
847
|
+
# You can add gems with the #gem method.
|
848
|
+
attr_accessor :gems
|
849
|
+
|
850
|
+
# Adds a single Gem dependency to the rails application. By default, it will require
|
851
|
+
# the library with the same name as the gem. Use :lib to specify a different name.
|
852
|
+
#
|
853
|
+
# # gem 'aws-s3', '>= 0.4.0'
|
854
|
+
# # require 'aws/s3'
|
855
|
+
# config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \
|
856
|
+
# :source => "http://code.whytheluckystiff.net"
|
857
|
+
#
|
858
|
+
# To require a library be installed, but not attempt to load it, pass :lib => false
|
859
|
+
#
|
860
|
+
# config.gem 'qrp', :version => '0.4.1', :lib => false
|
861
|
+
def gem(name, options = {})
|
862
|
+
@gems << Rails::GemDependency.new(name, options)
|
863
|
+
end
|
864
|
+
|
865
|
+
# Deprecated options:
|
866
|
+
def breakpoint_server(_ = nil)
|
867
|
+
$stderr.puts %(
|
868
|
+
*******************************************************************
|
869
|
+
* config.breakpoint_server has been deprecated and has no effect. *
|
870
|
+
*******************************************************************
|
871
|
+
)
|
872
|
+
end
|
873
|
+
alias_method :breakpoint_server=, :breakpoint_server
|
874
|
+
|
875
|
+
# Sets the default +time_zone+. Setting this will enable +time_zone+
|
876
|
+
# awareness for Active Record models and set the Active Record default
|
877
|
+
# timezone to <tt>:utc</tt>.
|
878
|
+
attr_accessor :time_zone
|
879
|
+
|
880
|
+
# Accessor for i18n settings.
|
881
|
+
attr_accessor :i18n
|
882
|
+
|
883
|
+
# Create a new Configuration instance, initialized with the default
|
884
|
+
# values.
|
885
|
+
def initialize
|
886
|
+
set_root_path!
|
887
|
+
|
888
|
+
self.frameworks = default_frameworks
|
889
|
+
self.autoload_paths = default_autoload_paths
|
890
|
+
self.autoload_once_paths = default_autoload_once_paths
|
891
|
+
self.eager_load_paths = default_eager_load_paths
|
892
|
+
self.log_path = default_log_path
|
893
|
+
self.log_level = default_log_level
|
894
|
+
self.view_path = default_view_path
|
895
|
+
self.controller_paths = default_controller_paths
|
896
|
+
self.preload_frameworks = default_preload_frameworks
|
897
|
+
self.cache_classes = default_cache_classes
|
898
|
+
self.dependency_loading = default_dependency_loading
|
899
|
+
self.whiny_nils = default_whiny_nils
|
900
|
+
self.plugins = default_plugins
|
901
|
+
self.plugin_paths = default_plugin_paths
|
902
|
+
self.plugin_locators = default_plugin_locators
|
903
|
+
self.plugin_loader = default_plugin_loader
|
904
|
+
self.database_configuration_file = default_database_configuration_file
|
905
|
+
self.routes_configuration_file = default_routes_configuration_file
|
906
|
+
self.gems = default_gems
|
907
|
+
self.i18n = default_i18n
|
908
|
+
|
909
|
+
for framework in default_frameworks
|
910
|
+
self.send("#{framework}=", Rails::OrderedOptions.new)
|
911
|
+
end
|
912
|
+
self.active_support = Rails::OrderedOptions.new
|
913
|
+
end
|
914
|
+
|
915
|
+
# Set the root_path to RAILS_ROOT and canonicalize it.
|
916
|
+
def set_root_path!
|
917
|
+
raise 'RAILS_ROOT is not set' unless defined?(::RAILS_ROOT)
|
918
|
+
raise 'RAILS_ROOT is not a directory' unless File.directory?(::RAILS_ROOT)
|
919
|
+
|
920
|
+
@root_path =
|
921
|
+
# Pathname is incompatible with Windows, but Windows doesn't have
|
922
|
+
# real symlinks so File.expand_path is safe.
|
923
|
+
if RUBY_PLATFORM =~ /(:?mswin|mingw)/
|
924
|
+
File.expand_path(::RAILS_ROOT)
|
925
|
+
|
926
|
+
# Otherwise use Pathname#realpath which respects symlinks.
|
927
|
+
else
|
928
|
+
Pathname.new(::RAILS_ROOT).realpath.to_s
|
929
|
+
end
|
930
|
+
|
931
|
+
Object.const_set(:RELATIVE_RAILS_ROOT, ::RAILS_ROOT.dup) unless defined?(::RELATIVE_RAILS_ROOT)
|
932
|
+
::RAILS_ROOT.replace @root_path
|
933
|
+
end
|
934
|
+
|
935
|
+
# Enable threaded mode. Allows concurrent requests to controller actions and
|
936
|
+
# multiple database connections. Also disables automatic dependency loading
|
937
|
+
# after boot, and disables reloading code on every request, as these are
|
938
|
+
# fundamentally incompatible with thread safety.
|
939
|
+
def threadsafe!
|
940
|
+
self.preload_frameworks = true
|
941
|
+
self.cache_classes = true
|
942
|
+
self.dependency_loading = false
|
943
|
+
self.action_controller.allow_concurrency = true
|
944
|
+
self
|
945
|
+
end
|
946
|
+
|
947
|
+
# Loads and returns the contents of the #database_configuration_file. The
|
948
|
+
# contents of the file are processed via ERB before being sent through
|
949
|
+
# YAML::load.
|
950
|
+
def database_configuration
|
951
|
+
require 'erb'
|
952
|
+
YAML::load(ERB.new(IO.read(database_configuration_file)).result)
|
953
|
+
end
|
954
|
+
|
955
|
+
# The path to the current environment's file (<tt>development.rb</tt>, etc.). By
|
956
|
+
# default the file is at <tt>config/environments/#{environment}.rb</tt>.
|
957
|
+
def environment_path
|
958
|
+
"#{root_path}/config/environments/#{environment}.rb"
|
959
|
+
end
|
960
|
+
|
961
|
+
# Return the currently selected environment. By default, it returns the
|
962
|
+
# value of the RAILS_ENV constant.
|
963
|
+
def environment
|
964
|
+
::RAILS_ENV
|
965
|
+
end
|
966
|
+
|
967
|
+
# Adds a block which will be executed after rails has been fully initialized.
|
968
|
+
# Useful for per-environment configuration which depends on the framework being
|
969
|
+
# fully initialized.
|
970
|
+
def after_initialize(&after_initialize_block)
|
971
|
+
after_initialize_blocks << after_initialize_block if after_initialize_block
|
972
|
+
end
|
973
|
+
|
974
|
+
# Returns the blocks added with Configuration#after_initialize
|
975
|
+
def after_initialize_blocks
|
976
|
+
@after_initialize_blocks ||= []
|
977
|
+
end
|
978
|
+
|
979
|
+
# Add a preparation callback that will run before every request in development
|
980
|
+
# mode, or before the first request in production.
|
981
|
+
#
|
982
|
+
# See Dispatcher#to_prepare.
|
983
|
+
def to_prepare(&callback)
|
984
|
+
after_initialize do
|
985
|
+
require 'dispatcher' unless defined?(::Dispatcher)
|
986
|
+
Dispatcher.to_prepare(&callback)
|
987
|
+
end
|
988
|
+
end
|
989
|
+
|
990
|
+
def middleware
|
991
|
+
require 'action_controller'
|
992
|
+
ActionController::Dispatcher.middleware
|
993
|
+
end
|
994
|
+
|
995
|
+
def builtin_directories
|
996
|
+
# Include builtins only in the development environment.
|
997
|
+
(environment == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
|
998
|
+
end
|
999
|
+
|
1000
|
+
def framework_paths
|
1001
|
+
paths = %w(railties railties/lib activesupport/lib)
|
1002
|
+
paths << 'actionpack/lib' if frameworks.include?(:action_controller) || frameworks.include?(:action_view)
|
1003
|
+
|
1004
|
+
[:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
|
1005
|
+
paths << "#{framework.to_s.gsub('_', '')}/lib" if frameworks.include?(framework)
|
1006
|
+
end
|
1007
|
+
|
1008
|
+
paths.map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
|
1009
|
+
end
|
1010
|
+
|
1011
|
+
private
|
1012
|
+
def framework_root_path
|
1013
|
+
defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root_path}/vendor/rails"
|
1014
|
+
end
|
1015
|
+
|
1016
|
+
def default_frameworks
|
1017
|
+
[ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
|
1018
|
+
end
|
1019
|
+
|
1020
|
+
def default_autoload_paths
|
1021
|
+
paths = []
|
1022
|
+
|
1023
|
+
# Add the old mock paths only if the directories exists
|
1024
|
+
paths.concat(Dir["#{root_path}/test/mocks/#{environment}"]) if File.exists?("#{root_path}/test/mocks/#{environment}")
|
1025
|
+
|
1026
|
+
# Add the app's controller directory
|
1027
|
+
paths.concat(Dir["#{root_path}/app/controllers/"])
|
1028
|
+
|
1029
|
+
# Followed by the standard includes.
|
1030
|
+
paths.concat %w(
|
1031
|
+
app
|
1032
|
+
app/metal
|
1033
|
+
app/models
|
1034
|
+
app/controllers
|
1035
|
+
app/helpers
|
1036
|
+
app/services
|
1037
|
+
lib
|
1038
|
+
vendor
|
1039
|
+
).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
|
1040
|
+
|
1041
|
+
paths.concat builtin_directories
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
# Doesn't matter since plugins aren't in autoload_paths yet.
|
1045
|
+
def default_autoload_once_paths
|
1046
|
+
[]
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
def default_eager_load_paths
|
1050
|
+
%w(
|
1051
|
+
app/metal
|
1052
|
+
app/models
|
1053
|
+
app/controllers
|
1054
|
+
app/helpers
|
1055
|
+
).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
|
1056
|
+
end
|
1057
|
+
|
1058
|
+
def default_log_path
|
1059
|
+
File.join(root_path, 'log', "#{environment}.log")
|
1060
|
+
end
|
1061
|
+
|
1062
|
+
def default_log_level
|
1063
|
+
environment == 'production' ? :info : :debug
|
1064
|
+
end
|
1065
|
+
|
1066
|
+
def default_database_configuration_file
|
1067
|
+
File.join(root_path, 'config', 'database.yml')
|
1068
|
+
end
|
1069
|
+
|
1070
|
+
def default_routes_configuration_file
|
1071
|
+
File.join(root_path, 'config', 'routes.rb')
|
1072
|
+
end
|
1073
|
+
|
1074
|
+
def default_view_path
|
1075
|
+
File.join(root_path, 'app', 'views')
|
1076
|
+
end
|
1077
|
+
|
1078
|
+
def default_controller_paths
|
1079
|
+
paths = [File.join(root_path, 'app', 'controllers')]
|
1080
|
+
paths.concat builtin_directories
|
1081
|
+
paths
|
1082
|
+
end
|
1083
|
+
|
1084
|
+
def default_dependency_loading
|
1085
|
+
true
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
def default_preload_frameworks
|
1089
|
+
false
|
1090
|
+
end
|
1091
|
+
|
1092
|
+
def default_cache_classes
|
1093
|
+
true
|
1094
|
+
end
|
1095
|
+
|
1096
|
+
def default_whiny_nils
|
1097
|
+
false
|
1098
|
+
end
|
1099
|
+
|
1100
|
+
def default_plugins
|
1101
|
+
nil
|
1102
|
+
end
|
1103
|
+
|
1104
|
+
def default_plugin_paths
|
1105
|
+
["#{root_path}/vendor/plugins"]
|
1106
|
+
end
|
1107
|
+
|
1108
|
+
def default_plugin_locators
|
1109
|
+
locators = []
|
1110
|
+
locators << Plugin::GemLocator if defined? Gem
|
1111
|
+
locators << Plugin::FileSystemLocator
|
1112
|
+
end
|
1113
|
+
|
1114
|
+
def default_plugin_loader
|
1115
|
+
Plugin::Loader
|
1116
|
+
end
|
1117
|
+
|
1118
|
+
def default_cache_store
|
1119
|
+
if File.exist?("#{root_path}/tmp/cache/")
|
1120
|
+
[ :file_store, "#{root_path}/tmp/cache/" ]
|
1121
|
+
else
|
1122
|
+
:memory_store
|
1123
|
+
end
|
1124
|
+
end
|
1125
|
+
|
1126
|
+
def default_gems
|
1127
|
+
[]
|
1128
|
+
end
|
1129
|
+
|
1130
|
+
def default_i18n
|
1131
|
+
i18n = Rails::OrderedOptions.new
|
1132
|
+
i18n.load_path = []
|
1133
|
+
|
1134
|
+
if File.exist?(File.join(RAILS_ROOT, 'config', 'locales'))
|
1135
|
+
i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
|
1136
|
+
i18n.load_path.flatten!
|
1137
|
+
end
|
1138
|
+
|
1139
|
+
i18n
|
1140
|
+
end
|
1141
|
+
end
|
1142
|
+
end
|
1143
|
+
|
1144
|
+
# Needs to be duplicated from Active Support since its needed before Active
|
1145
|
+
# Support is available. Here both Options and Hash are namespaced to prevent
|
1146
|
+
# conflicts with other implementations AND with the classes residing in Active Support.
|
1147
|
+
class Rails::OrderedOptions < Array #:nodoc:
|
1148
|
+
def []=(key, value)
|
1149
|
+
key = key.to_sym
|
1150
|
+
|
1151
|
+
if pair = find_pair(key)
|
1152
|
+
pair.pop
|
1153
|
+
pair << value
|
1154
|
+
else
|
1155
|
+
self << [key, value]
|
1156
|
+
end
|
1157
|
+
end
|
1158
|
+
|
1159
|
+
def [](key)
|
1160
|
+
pair = find_pair(key.to_sym)
|
1161
|
+
pair ? pair.last : nil
|
1162
|
+
end
|
1163
|
+
|
1164
|
+
def method_missing(name, *args)
|
1165
|
+
if name.to_s =~ /(.*)=$/
|
1166
|
+
self[$1.to_sym] = args.first
|
1167
|
+
else
|
1168
|
+
self[name]
|
1169
|
+
end
|
1170
|
+
end
|
1171
|
+
|
1172
|
+
private
|
1173
|
+
def find_pair(key)
|
1174
|
+
self.each { |i| return i if i.first == key }
|
1175
|
+
return false
|
1176
|
+
end
|
1177
|
+
end
|