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

View file

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

View file

@ -4,8 +4,12 @@ require 'optparse'
module Rails
class DBConsole
def self.start
new.start
def self.start(app)
new(app).start
end
def initialize(app)
@app = app
end
def start
@ -31,7 +35,7 @@ module Rails
end
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}'"
end

View file

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

View file

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

View file

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

View file

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

View file

@ -1,6 +1,6 @@
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.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

View file

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

View file

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

View file

@ -1,12 +1,10 @@
module Rails
module Rack
class LogTailer
EnvironmentLog = "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log"
def initialize(app, log = nil)
@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)
@last_checked = Time.now.to_f

View file

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

View file

@ -7,21 +7,8 @@ module ApplicationTests
include ActiveSupport::Testing::Isolation
def rackup
config = "#{app_path}/config.ru"
# Copied from ActionDispatch::Utils.parse_config
# 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
require "rack"
Rack::Builder.parse_file("#{app_path}/config.ru")
end
def setup
@ -40,14 +27,14 @@ module ApplicationTests
test "Rails.application is available after config.ru has been racked up" do
rackup
assert Rails.application < Rails::Application
assert Rails.application.is_a?(Rails::Application)
end
# Passenger still uses AC::Dispatcher, so we need to
# keep it working for now
test "deprecated ActionController::Dispatcher still works" do
rackup
assert ActionController::Dispatcher.new < Rails::Application
assert ActionController::Dispatcher.new.is_a?(Rails::Application)
end
test "the config object is available on the application object" do