1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Have script/* and Rakefile use the application object

This commit is contained in:
Carlhuda 2009-11-24 13:03:24 -08:00
parent a2cb90c0c2
commit 530b8ff5ae
15 changed files with 61 additions and 47 deletions

View file

@ -3,11 +3,6 @@ module Rails
include Initializable include Initializable
class << self class << self
def inherited(klass)
Rails.application ||= klass unless klass.name =~ /Rails/
super
end
# Stub out App initialize # Stub out App initialize
def initialize! def initialize!
new new
@ -32,12 +27,21 @@ module Rails
config.root config.root
end end
def load_tasks
require "rails/tasks"
task :environment do
$rails_rake_task = true
initialize!
end
end
def call(env) def call(env)
new.call(env) new.call(env)
end end
end end
def initialize def initialize
Rails.application ||= self
run_initializers(self) run_initializers(self)
end end
@ -45,6 +49,10 @@ module Rails
self.class.config self.class.config
end end
def root
config.root
end
alias configuration config alias configuration config
def middleware def middleware

View file

@ -6,8 +6,12 @@ module Rails
class Console class Console
ENVIRONMENTS = %w(production development test) ENVIRONMENTS = %w(production development test)
def self.start def self.start(app)
new.start new(app).start
end
def initialize(app)
@app = app
end end
def start def start
@ -25,7 +29,7 @@ module Rails
ENV['RAILS_ENV'] = ENVIRONMENTS.find { |e| e.index(env) } || env ENV['RAILS_ENV'] = ENVIRONMENTS.find { |e| e.index(env) } || env
end end
require "#{Rails.root}/config/environment" @app.initialize!
require "rails/console_app" require "rails/console_app"
require "rails/console_sandbox" if options[:sandbox] require "rails/console_sandbox" if options[:sandbox]
require "rails/console_with_helpers" require "rails/console_with_helpers"

View file

@ -4,8 +4,12 @@ require 'optparse'
module Rails module Rails
class DBConsole class DBConsole
def self.start def self.start(app)
new.start new(app).start
end
def initialize(app)
@app = app
end end
def start def start
@ -31,7 +35,7 @@ module Rails
end end
env = ARGV.first || ENV['RAILS_ENV'] || 'development' env = ARGV.first || ENV['RAILS_ENV'] || 'development'
unless config = YAML::load(ERB.new(IO.read("#{Rails.root}/config/database.yml")).result)[env] unless config = YAML::load(ERB.new(IO.read("#{@app.root}/config/database.yml")).result)[env]
abort "No database is configured for the environment '#{env}'" abort "No database is configured for the environment '#{env}'"
end end

View file

@ -37,6 +37,15 @@ module Rails
Options.new Options.new
end end
def self.start(app)
new(app).start
end
def initialize(app)
super() # Call Rack::Server#initialize without passing any options to use.
@app = app
end
def start def start
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}" puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}"
@ -54,20 +63,24 @@ module Rails
def middleware def middleware
middlewares = [] middlewares = []
middlewares << [Rails::Rack::LogTailer] unless options[:daemonize] middlewares << [Rails::Rack::LogTailer, log_path] unless options[:daemonize]
middlewares << [Rails::Rack::Debugger] if options[:debugger] middlewares << [Rails::Rack::Debugger] if options[:debugger]
Hash.new(middlewares) Hash.new(middlewares)
end end
def log_path
"#{File.expand_path(@app.root)}/log/#{options[:environment]}.log"
end
def default_options def default_options
{ {
:Port => 3000, :Port => 3000,
:Host => "0.0.0.0", :Host => "0.0.0.0",
:environment => (ENV['RAILS_ENV'] || "development").dup, :environment => (ENV['RAILS_ENV'] || "development").dup,
:rack_file => "#{Rails.root}/config.ru", :rack_file => "#{@app.root}/config.ru",
:daemonize => false, :daemonize => false,
:debugger => false, :debugger => false,
:pid => "#{Rails.root}/tmp/pids/server.pid", :pid => "#{@app.root}/tmp/pids/server.pid",
:AccessLog => [] :AccessLog => []
} }
end end

View file

@ -48,9 +48,9 @@ module Rails::Generators
end end
def create_root_files def create_root_files
copy_file "Rakefile"
copy_file "README" copy_file "README"
copy_file "config.ru" template "Rakefile"
template "config.ru"
template "Gemfile" template "Gemfile"
end end
@ -181,6 +181,10 @@ module Rails::Generators
@app_name ||= File.basename(destination_root) @app_name ||= File.basename(destination_root)
end end
def app_const
@app_const ||= app_name.classify
end
def app_secret def app_secret
ActiveSupport::SecureRandom.hex(64) ActiveSupport::SecureRandom.hex(64)
end end

View file

@ -7,4 +7,4 @@ require 'rake'
require 'rake/testtask' require 'rake/testtask'
require 'rake/rdoctask' require 'rake/rdoctask'
require 'rails/tasks' <%= app_const %>.load_tasks

View file

@ -2,4 +2,4 @@
require ::File.expand_path('../config/environment', __FILE__) require ::File.expand_path('../config/environment', __FILE__)
# Dispatch the request # Dispatch the request
run Rails.application run <%= app_const%>

View file

@ -1,6 +1,6 @@
require File.expand_path('../boot', __FILE__) require File.expand_path('../boot', __FILE__)
Rails::Initializer.run do |config| class <%= app_const %> < Rails::Application
# Settings in config/environments/* take precedence over those specified here. # Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers # Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded. # -- all .rb files in that directory are automatically loaded.

View file

@ -2,4 +2,4 @@
require File.expand_path('../application', __FILE__) require File.expand_path('../application', __FILE__)
# Initialize the rails application # Initialize the rails application
Rails.initialize! <%= app_const %>.initialize!

View file

@ -1,3 +1,3 @@
require File.expand_path('../../config/application', __FILE__) require File.expand_path('../../config/application', __FILE__)
require 'rails/commands/console' require 'rails/commands/console'
Rails::Console.start Rails::Console.start(<%= app_const %>)

View file

@ -1,3 +1,3 @@
require File.expand_path('../../config/application', __FILE__) require File.expand_path('../../config/application', __FILE__)
require 'rails/commands/dbconsole' require 'rails/commands/dbconsole'
Rails::DBConsole.start Rails::DBConsole.start(<%= app_const %>)

View file

@ -1,3 +1,3 @@
require File.expand_path('../../config/application', __FILE__) require File.expand_path('../../config/application', __FILE__)
require 'rails/commands/server' require 'rails/commands/server'
Rails::Server.start Rails::Server.start(<%= app_const %>)

View file

@ -1,12 +1,10 @@
module Rails module Rails
module Rack module Rack
class LogTailer class LogTailer
EnvironmentLog = "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log"
def initialize(app, log = nil) def initialize(app, log = nil)
@app = app @app = app
path = Pathname.new(log || EnvironmentLog).cleanpath path = Pathname.new(log || "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log").cleanpath
@cursor = ::File.size(path) @cursor = ::File.size(path)
@last_checked = Time.now.to_f @last_checked = Time.now.to_f

View file

@ -1,8 +1,4 @@
task :default => :test task :default => :test
task :environment do
$rails_rake_task = true
require(File.join(Rails.root, 'config', 'environment'))
end
task :rails_env do task :rails_env do
unless defined? RAILS_ENV unless defined? RAILS_ENV

View file

@ -7,21 +7,8 @@ module ApplicationTests
include ActiveSupport::Testing::Isolation include ActiveSupport::Testing::Isolation
def rackup def rackup
config = "#{app_path}/config.ru" require "rack"
# Copied from ActionDispatch::Utils.parse_config Rack::Builder.parse_file("#{app_path}/config.ru")
# ActionDispatch is not necessarily available at this point.
require 'rack'
if config =~ /\.ru$/
cfgfile = ::File.read(config)
if cfgfile[/^#\\(.*)/]
opts.parse! $1.split(/\s+/)
end
inner_app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
nil, config
else
require config
inner_app = Object.const_get(::File.basename(config, '.rb').capitalize)
end
end end
def setup def setup
@ -40,14 +27,14 @@ module ApplicationTests
test "Rails.application is available after config.ru has been racked up" do test "Rails.application is available after config.ru has been racked up" do
rackup rackup
assert Rails.application < Rails::Application assert Rails.application.is_a?(Rails::Application)
end end
# Passenger still uses AC::Dispatcher, so we need to # Passenger still uses AC::Dispatcher, so we need to
# keep it working for now # keep it working for now
test "deprecated ActionController::Dispatcher still works" do test "deprecated ActionController::Dispatcher still works" do
rackup rackup
assert ActionController::Dispatcher.new < Rails::Application assert ActionController::Dispatcher.new.is_a?(Rails::Application)
end end
test "the config object is available on the application object" do test "the config object is available on the application object" do