From 1b816d502444ce2b3153d8c689d0057f1c257eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 29 Apr 2010 08:39:44 +0200 Subject: The rake task :environment now loads config/environment.rb instead of initializing the application on its own. This fixes [#4492 state:resolved] and also avoids the application being initialized twice in some rake tasks. --- railties/lib/rails/application.rb | 4 +- railties/lib/rails/application/bootstrap.rb | 3 +- railties/lib/rails/application/configuration.rb | 17 ++-- railties/lib/rails/commands.rb | 102 ++++++++++----------- railties/lib/rails/commands/benchmarker.rb | 23 +++++ .../lib/rails/commands/performance/benchmarker.rb | 23 ----- .../lib/rails/commands/performance/profiler.rb | 46 ---------- railties/lib/rails/commands/profiler.rb | 46 ++++++++++ railties/lib/rails/commands/runner.rb | 3 +- .../generators/rails/app/templates/script/rails | 7 +- 10 files changed, 134 insertions(+), 140 deletions(-) create mode 100644 railties/lib/rails/commands/benchmarker.rb delete mode 100644 railties/lib/rails/commands/performance/benchmarker.rb delete mode 100644 railties/lib/rails/commands/performance/profiler.rb create mode 100644 railties/lib/rails/commands/profiler.rb (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 7cec14c738..d39f9a2ae9 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -85,7 +85,7 @@ module Rails delegate :metal_loader, :to => :config def require_environment! - environment = config.paths.config.environment.to_a.first + environment = paths.config.environment.to_a.first require environment if environment end @@ -153,7 +153,7 @@ module Rails require "rails/tasks" task :environment do $rails_rake_task = true - initialize! + require_environment! end end diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index 06243f2e5e..022e1a91d8 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -6,7 +6,8 @@ module Rails include Initializable initializer :load_environment_config do - require_environment! + environment = config.paths.config.environments.to_a.first + require environment if environment end initializer :load_all_active_support do diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 874b3a78b6..a58f7e9079 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -43,14 +43,15 @@ module Rails @paths ||= begin paths = super paths.app.controllers << builtin_controller if builtin_controller - paths.config.database "config/database.yml" - paths.config.environment "config/environments", :glob => "#{Rails.env}.rb" - paths.lib.templates "lib/templates" - paths.log "log/#{Rails.env}.log" - paths.tmp "tmp" - paths.tmp.cache "tmp/cache" - paths.vendor "vendor", :load_path => true - paths.vendor.plugins "vendor/plugins" + paths.config.database "config/database.yml" + paths.config.environment "config/environment.rb" + paths.config.environments "config/environments", :glob => "#{Rails.env}.rb" + paths.lib.templates "lib/templates" + paths.log "log/#{Rails.env}.log" + paths.tmp "tmp" + paths.tmp.cache "tmp/cache" + paths.vendor "vendor", :load_path => true + paths.vendor.plugins "vendor/plugins" if File.exists?("#{root}/test/mocks/#{Rails.env}") ActiveSupport::Deprecation.warn "\"Rails.root/test/mocks/#{Rails.env}\" won't be added " << diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index 12748da18b..c8254f588f 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -1,8 +1,50 @@ -if ARGV.empty? - ARGV << '--help' -end +ARGV << '--help' if ARGV.empty? -HELP_TEXT = <<-EOT +aliases = { + "g" => "generate", + "c" => "console", + "s" => "server", + "db" => "dbconsole" +} + +command = ARGV.shift +command = aliases[command] || command + +case command +when 'generate', 'destroy', 'plugin', 'benchmarker', 'performance' + require APP_PATH + Rails::Application.require_environment! + require "rails/commands/#{command}" + +when 'console' + require 'rails/commands/console' + require APP_PATH + Rails::Application.require_environment! + Rails::Console.start(Rails::Application) + +when 'server' + require 'rails/commands/server' + Rails::Server.new.tap { |server| + require APP_PATH + Dir.chdir(Rails::Application.root) + server.start + } + +when 'dbconsole' + require 'rails/commands/dbconsole' + require APP_PATH + Rails::DBConsole.start(Rails::Application) + +when 'application', 'runner' + require "rails/commands/#{command}" + +when '--version', '-v' + ARGV.unshift '--version' + require 'rails/commands/application' + +else + puts "Error: Command not recognized" unless %w(-h --help).include?(command) + puts <<-EOT Usage: rails COMMAND [ARGS] The most common rails commands are: @@ -21,53 +63,5 @@ In addition to those, there are: runner Run a piece of code in the application environment All commands can be run with -h for more information. -EOT - - -case ARGV.shift -when 'g', 'generate' - require ENV_PATH - require 'rails/commands/generate' -when 'c', 'console' - require 'rails/commands/console' - require ENV_PATH - Rails::Console.start(Rails::Application) -when 's', 'server' - require 'rails/commands/server' - # Initialize the server first, so environment options are set - server = Rails::Server.new - require APP_PATH - - Dir.chdir(Rails::Application.root) - server.start -when 'db', 'dbconsole' - require 'rails/commands/dbconsole' - require APP_PATH - Rails::DBConsole.start(Rails::Application) - -when 'application' - require 'rails/commands/application' -when 'destroy' - require ENV_PATH - require 'rails/commands/destroy' -when 'benchmarker' - require ENV_PATH - require 'rails/commands/performance/benchmarker' -when 'profiler' - require ENV_PATH - require 'rails/commands/performance/profiler' -when 'plugin' - require APP_PATH - require 'rails/commands/plugin' -when 'runner' - require 'rails/commands/runner' - -when '--help', '-h' - puts HELP_TEXT -when '--version', '-v' - ARGV.unshift '--version' - require 'rails/commands/application' -else - puts "Error: Command not recognized" - puts HELP_TEXT -end + EOT +end \ No newline at end of file diff --git a/railties/lib/rails/commands/benchmarker.rb b/railties/lib/rails/commands/benchmarker.rb new file mode 100644 index 0000000000..0432261802 --- /dev/null +++ b/railties/lib/rails/commands/benchmarker.rb @@ -0,0 +1,23 @@ +if [nil, "-h", "--help"].include?(ARGV.first) + puts "Usage: rails benchmarker [times] 'Person.expensive_way' 'Person.another_expensive_way' ..." + exit 1 +end + +begin + N = Integer(ARGV.first) + ARGV.shift +rescue ArgumentError + N = 1 +end + +require 'benchmark' +include Benchmark + +# Don't include compilation in the benchmark +ARGV.each { |expression| eval(expression) } + +bm(6) do |x| + ARGV.each_with_index do |expression, idx| + x.report("##{idx + 1}") { N.times { eval(expression) } } + end +end diff --git a/railties/lib/rails/commands/performance/benchmarker.rb b/railties/lib/rails/commands/performance/benchmarker.rb deleted file mode 100644 index 0432261802..0000000000 --- a/railties/lib/rails/commands/performance/benchmarker.rb +++ /dev/null @@ -1,23 +0,0 @@ -if [nil, "-h", "--help"].include?(ARGV.first) - puts "Usage: rails benchmarker [times] 'Person.expensive_way' 'Person.another_expensive_way' ..." - exit 1 -end - -begin - N = Integer(ARGV.first) - ARGV.shift -rescue ArgumentError - N = 1 -end - -require 'benchmark' -include Benchmark - -# Don't include compilation in the benchmark -ARGV.each { |expression| eval(expression) } - -bm(6) do |x| - ARGV.each_with_index do |expression, idx| - x.report("##{idx + 1}") { N.times { eval(expression) } } - end -end diff --git a/railties/lib/rails/commands/performance/profiler.rb b/railties/lib/rails/commands/performance/profiler.rb deleted file mode 100644 index 6d9717b5cd..0000000000 --- a/railties/lib/rails/commands/performance/profiler.rb +++ /dev/null @@ -1,46 +0,0 @@ -if [nil, "-h", "--help"].include?(ARGV.first) - $stderr.puts "Usage: rails profiler 'Person.expensive_method(10)' [times] [flat|graph|graph_html]" - exit(1) -end - -# Define a method to profile. -if ARGV[1] and ARGV[1].to_i > 1 - eval "def profile_me() #{ARGV[1]}.times { #{ARGV[0]} } end" -else - eval "def profile_me() #{ARGV[0]} end" -end - -# Use the ruby-prof extension if available. Fall back to stdlib profiler. -begin - begin - require "ruby-prof" - $stderr.puts 'Using the ruby-prof extension.' - RubyProf.measure_mode = RubyProf::WALL_TIME - RubyProf.start - profile_me - results = RubyProf.stop - if ARGV[2] - printer_class = RubyProf.const_get((ARGV[2] + "_printer").classify) - else - printer_class = RubyProf::FlatPrinter - end - printer = printer_class.new(results) - printer.print($stderr) - rescue LoadError - require "prof" - $stderr.puts 'Using the old ruby-prof extension.' - Prof.clock_mode = Prof::GETTIMEOFDAY - Prof.start - profile_me - results = Prof.stop - require 'rubyprof_ext' - Prof.print_profile(results, $stderr) - end -rescue LoadError - require 'profiler' - $stderr.puts 'Using the standard Ruby profiler.' - Profiler__.start_profile - profile_me - Profiler__.stop_profile - Profiler__.print_profile($stderr) -end diff --git a/railties/lib/rails/commands/profiler.rb b/railties/lib/rails/commands/profiler.rb new file mode 100644 index 0000000000..6d9717b5cd --- /dev/null +++ b/railties/lib/rails/commands/profiler.rb @@ -0,0 +1,46 @@ +if [nil, "-h", "--help"].include?(ARGV.first) + $stderr.puts "Usage: rails profiler 'Person.expensive_method(10)' [times] [flat|graph|graph_html]" + exit(1) +end + +# Define a method to profile. +if ARGV[1] and ARGV[1].to_i > 1 + eval "def profile_me() #{ARGV[1]}.times { #{ARGV[0]} } end" +else + eval "def profile_me() #{ARGV[0]} end" +end + +# Use the ruby-prof extension if available. Fall back to stdlib profiler. +begin + begin + require "ruby-prof" + $stderr.puts 'Using the ruby-prof extension.' + RubyProf.measure_mode = RubyProf::WALL_TIME + RubyProf.start + profile_me + results = RubyProf.stop + if ARGV[2] + printer_class = RubyProf.const_get((ARGV[2] + "_printer").classify) + else + printer_class = RubyProf::FlatPrinter + end + printer = printer_class.new(results) + printer.print($stderr) + rescue LoadError + require "prof" + $stderr.puts 'Using the old ruby-prof extension.' + Prof.clock_mode = Prof::GETTIMEOFDAY + Prof.start + profile_me + results = Prof.stop + require 'rubyprof_ext' + Prof.print_profile(results, $stderr) + end +rescue LoadError + require 'profiler' + $stderr.puts 'Using the standard Ruby profiler.' + Profiler__.start_profile + profile_me + Profiler__.stop_profile + Profiler__.print_profile($stderr) +end diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb index 1dd11e1241..278548558e 100644 --- a/railties/lib/rails/commands/runner.rb +++ b/railties/lib/rails/commands/runner.rb @@ -36,7 +36,8 @@ ARGV.delete(code_or_file) ENV["RAILS_ENV"] = options[:environment] -require ENV_PATH +require APP_PATH +Rails::Application.require_environment! begin if code_or_file.nil? diff --git a/railties/lib/rails/generators/rails/app/templates/script/rails b/railties/lib/rails/generators/rails/app/templates/script/rails index b01d1ee183..11bc1edde9 100644 --- a/railties/lib/rails/generators/rails/app/templates/script/rails +++ b/railties/lib/rails/generators/rails/app/templates/script/rails @@ -1,8 +1,5 @@ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. -ENV_PATH = File.expand_path('../../config/environment', __FILE__) -BOOT_PATH = File.expand_path('../../config/boot', __FILE__) -APP_PATH = File.expand_path('../../config/application', __FILE__) - -require BOOT_PATH +APP_PATH = File.expand_path('../../config/application', __FILE__) +require File.expand_path('../../config/boot', __FILE__) require 'rails/commands' -- cgit v1.2.3