diff options
author | José Valim <jose.valim@gmail.com> | 2009-11-24 09:24:40 -0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-11-24 09:24:40 -0200 |
commit | 41e607dee20b15d8dc71dc16a08d4bbe9e36ac70 (patch) | |
tree | a99108c3e60bb9f44f5f1870d885971de383b9e4 /railties | |
parent | 16cf25e717c9b13e402fda33fe859684e1d8a154 (diff) | |
parent | e62e6d409986cd5c99234689aa49e3162d7b3a59 (diff) | |
download | rails-41e607dee20b15d8dc71dc16a08d4bbe9e36ac70.tar.gz rails-41e607dee20b15d8dc71dc16a08d4bbe9e36ac70.tar.bz2 rails-41e607dee20b15d8dc71dc16a08d4bbe9e36ac70.zip |
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/commands/console.rb | 85 | ||||
-rw-r--r-- | railties/lib/rails/commands/server.rb | 120 | ||||
-rw-r--r-- | railties/lib/rails/configuration.rb | 2 | ||||
-rwxr-xr-x | railties/lib/rails/generators/rails/app/templates/script/console | 1 | ||||
-rwxr-xr-x | railties/lib/rails/generators/rails/app/templates/script/server | 1 | ||||
-rw-r--r-- | railties/test/application/notifications_test.rb | 4 | ||||
-rw-r--r-- | railties/test/rails_info_controller_test.rb | 5 |
7 files changed, 113 insertions, 105 deletions
diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb index b977b7162f..fc22ad64a9 100644 --- a/railties/lib/rails/commands/console.rb +++ b/railties/lib/rails/commands/console.rb @@ -1,45 +1,52 @@ -irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb' - require 'optparse' +require 'irb' +require "irb/completion" -options = { :sandbox => false, :irb => irb } -OptionParser.new do |opt| - opt.banner = "Usage: console [environment] [options]" - opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v } - opt.on("--irb=[#{irb}]", 'Invoke a different irb.') { |v| options[:irb] = v } - opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v } - opt.parse!(ARGV) -end +module Rails + class Console + ENVIRONMENTS = %w(production development test) -libs = " -r irb/completion" -libs << %( -r "#{Rails.root}/config/environment") -libs << " -r rails/console_app" -libs << " -r rails/console_sandbox" if options[:sandbox] -libs << " -r rails/console_with_helpers" - -if options[:debugger] - begin - require 'ruby-debug' - libs << " -r ruby-debug" - puts "=> Debugger enabled" - rescue Exception - puts "You need to install ruby-debug to run the console in debugging mode. With gems, use 'gem install ruby-debug'" - exit - end -end + def self.start + new.start + end -ENV['RAILS_ENV'] = case ARGV.first - when "p"; "production" - when "d"; "development" - when "t"; "test" - else - ARGV.first || ENV['RAILS_ENV'] || 'development' -end + def start + options = {} -if options[:sandbox] - puts "Loading #{ENV['RAILS_ENV']} environment in sandbox (Rails #{Rails.version})" - puts "Any modifications you make will be rolled back on exit" -else - puts "Loading #{ENV['RAILS_ENV']} environment (Rails #{Rails.version})" + OptionParser.new do |opt| + opt.banner = "Usage: console [environment] [options]" + opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v } + opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v } + opt.on('--irb') { |v| abort '--irb option is no longer supported. Invoke `/your/choice/of/ruby script/console` instead' } + opt.parse!(ARGV) + end + + if env = ARGV.first + ENV['RAILS_ENV'] = ENVIRONMENTS.find { |e| e.index(env) } || env + end + + require "#{Rails.root}/config/environment" + require "rails/console_app" + require "rails/console_sandbox" if options[:sandbox] + require "rails/console_with_helpers" + + if options[:debugger] + begin + require 'ruby-debug' + puts "=> Debugger enabled" + rescue Exception + puts "You need to install ruby-debug to run the console in debugging mode. With gems, use 'gem install ruby-debug'" + exit + end + end + + if options[:sandbox] + puts "Loading #{ENV['RAILS_ENV']} environment in sandbox (Rails #{Rails.version})" + puts "Any modifications you make will be rolled back on exit" + else + puts "Loading #{ENV['RAILS_ENV']} environment (Rails #{Rails.version})" + end + IRB.start + end + end end -exec "#{options[:irb]} #{libs} --simple-prompt" diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index 2c90851fb2..ff2282a534 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -1,73 +1,75 @@ -require 'action_dispatch' - require 'fileutils' require 'optparse' +require 'action_dispatch' -options = { - :Port => 3000, - :Host => "0.0.0.0", - :environment => (ENV['RAILS_ENV'] || "development").dup, - :config => "#{Rails.root}/config.ru", - :detach => false, - :debugger => false -} +module Rails + class Server < ::Rack::Server + class Options + def parse!(args) + options = {} + args = args.dup + opt_parser = OptionParser.new do |opts| + opts.on("-p", "--port=port", Integer, + "Runs Rails on the specified port.", "Default: #{options[:Port]}") { |v| options[:Port] = v } + opts.on("-b", "--binding=ip", String, + "Binds Rails to the specified ip.", "Default: #{options[:Host]}") { |v| options[:Host] = v } + opts.on("-c", "--config=file", String, + "Use custom rackup configuration file") { |v| options[:config] = v } + opts.on("-d", "--daemon", "Make server run as a Daemon.") { options[:daemonize] = true } + opts.on("-u", "--debugger", "Enable ruby-debugging for the server.") { options[:debugger] = true } + opts.on("-e", "--environment=name", String, + "Specifies the environment to run this server under (test/development/production).", + "Default: #{options[:environment]}") { |v| options[:environment] = v } -ARGV.clone.options do |opts| - opts.on("-p", "--port=port", Integer, - "Runs Rails on the specified port.", "Default: #{options[:Port]}") { |v| options[:Port] = v } - opts.on("-b", "--binding=ip", String, - "Binds Rails to the specified ip.", "Default: #{options[:Host]}") { |v| options[:Host] = v } - opts.on("-c", "--config=file", String, - "Use custom rackup configuration file") { |v| options[:config] = v } - opts.on("-d", "--daemon", "Make server run as a Daemon.") { options[:detach] = true } - opts.on("-u", "--debugger", "Enable ruby-debugging for the server.") { options[:debugger] = true } - opts.on("-e", "--environment=name", String, - "Specifies the environment to run this server under (test/development/production).", - "Default: #{options[:environment]}") { |v| options[:environment] = v } + opts.separator "" - opts.separator "" + opts.on("-h", "--help", "Show this help message.") { puts opts; exit } + end - opts.on("-h", "--help", "Show this help message.") { puts opts; exit } + opt_parser.parse! args - opts.parse! -end + options[:server] = args.shift + options + end + end -server = Rack::Handler.get(ARGV.first) rescue nil -unless server - begin - server = Rack::Handler::Mongrel - rescue LoadError => e - server = Rack::Handler::WEBrick - end -end + def opt_parser + Options.new + end -puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" -puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}" + def start + puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" + puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}" + puts "=> Call with -d to detach" unless options[:daemonize] + trap(:INT) { exit } + puts "=> Ctrl-C to shutdown server" unless options[:daemonize] -if options[:detach] - Process.daemon - pid = "#{Rails.root}/tmp/pids/server.pid" - File.open(pid, 'w'){ |f| f.write(Process.pid) } - at_exit { File.delete(pid) if File.exist?(pid) } -end - -ENV["RAILS_ENV"] = options[:environment] -RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV) - -app = Rack::Builder.new { - use Rails::Rack::LogTailer unless options[:detach] - use Rails::Rack::Debugger if options[:debugger] - run ActionDispatch::Utils.parse_config(options[:config]) -}.to_app + ENV["RAILS_ENV"] = options[:environment] + RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV) -puts "=> Call with -d to detach" + super + ensure + puts 'Exiting' unless options[:daemonize] + end -trap(:INT) { exit } + def middleware + middlewares = [] + middlewares << [Rails::Rack::LogTailer] unless options[:daemonize] + middlewares << [Rails::Rack::Debugger] if options[:debugger] + Hash.new(middlewares) + end -puts "=> Ctrl-C to shutdown server" - -begin - server.run(app, options.merge(:AccessLog => [])) -ensure - puts 'Exiting' + def default_options + { + :Port => 3000, + :Host => "0.0.0.0", + :environment => (ENV['RAILS_ENV'] || "development").dup, + :rack_file => "#{Rails.root}/config.ru", + :daemonize => false, + :debugger => false, + :pid => "#{Rails.root}/tmp/pids/server.pid", + :AccessLog => [] + } + end + end end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 102a0836dc..3f43a48e2e 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -31,7 +31,7 @@ module Rails def root @root ||= begin call_stack = caller.map { |p| p.split(':').first } - root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails] } + root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] } root_path = File.dirname(root_path) while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru") diff --git a/railties/lib/rails/generators/rails/app/templates/script/console b/railties/lib/rails/generators/rails/app/templates/script/console index 20aa799d2f..6043f3792b 100755 --- a/railties/lib/rails/generators/rails/app/templates/script/console +++ b/railties/lib/rails/generators/rails/app/templates/script/console @@ -1,2 +1,3 @@ require File.expand_path('../../config/application', __FILE__) require 'rails/commands/console' +Rails::Console.start diff --git a/railties/lib/rails/generators/rails/app/templates/script/server b/railties/lib/rails/generators/rails/app/templates/script/server index a7aaee2953..709ca002df 100755 --- a/railties/lib/rails/generators/rails/app/templates/script/server +++ b/railties/lib/rails/generators/rails/app/templates/script/server @@ -1,2 +1,3 @@ require File.expand_path('../../config/application', __FILE__) require 'rails/commands/server' +Rails::Server.start diff --git a/railties/test/application/notifications_test.rb b/railties/test/application/notifications_test.rb index 62ed4f4ad4..28dfdfcd83 100644 --- a/railties/test/application/notifications_test.rb +++ b/railties/test/application/notifications_test.rb @@ -10,13 +10,15 @@ module ApplicationTests def initialize @events = [] @subscribers = [] + @listeners = [] end def publish(name, *args) @events << name end - def subscribe(pattern=nil, &block) + def subscribe(listener, pattern=nil, &block) + @listeners << listener @subscribers << pattern end end diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb index a0484c0868..39f23fa0be 100644 --- a/railties/test/rails_info_controller_test.rb +++ b/railties/test/rails_info_controller_test.rb @@ -14,17 +14,12 @@ class InfoControllerTest < ActionController::TestCase tests Rails::InfoController def setup - ActionController::Routing.use_controllers!(['rails/info']) ActionController::Routing::Routes.draw do |map| map.connect ':controller/:action/:id' end @controller.stubs(:consider_all_requests_local => false, :local_request? => true) end - def teardown - ActionController::Routing.use_controllers! nil - end - test "info controller does not allow remote requests" do @controller.stubs(:consider_all_requests_local => false, :local_request? => false) get :properties |