diff options
Diffstat (limited to 'railties/lib')
20 files changed, 197 insertions, 122 deletions
diff --git a/railties/lib/commands/about.rb b/railties/lib/commands/about.rb index 7f53ac8a2e..bc2cfcb948 100644 --- a/railties/lib/commands/about.rb +++ b/railties/lib/commands/about.rb @@ -1,3 +1,3 @@ -require 'environment' +require "#{RAILS_ROOT}/config/environment" require 'rails/info' puts Rails::Info diff --git a/railties/lib/commands/dbconsole.rb b/railties/lib/commands/dbconsole.rb index 6ff895aa30..06848d3c91 100644 --- a/railties/lib/commands/dbconsole.rb +++ b/railties/lib/commands/dbconsole.rb @@ -41,7 +41,7 @@ when "mysql" if config['password'] && include_password args << "--password=#{config['password']}" - elsif config['password'] && !config['password'].empty? + elsif config['password'] && !config['password'].to_s.empty? args << "-p" end diff --git a/railties/lib/commands/runner.rb b/railties/lib/commands/runner.rb index 2411c3d270..510128318a 100644 --- a/railties/lib/commands/runner.rb +++ b/railties/lib/commands/runner.rb @@ -48,5 +48,7 @@ begin eval(code_or_file) end ensure - RAILS_DEFAULT_LOGGER.flush if RAILS_DEFAULT_LOGGER + if defined? Rails + Rails.logger.flush if Rails.logger.respond_to?(:flush) + end end diff --git a/railties/lib/commands/server.rb b/railties/lib/commands/server.rb index 7057fcc33f..43b18004c0 100644 --- a/railties/lib/commands/server.rb +++ b/railties/lib/commands/server.rb @@ -84,7 +84,7 @@ else end app = Rack::Builder.new { - use Rails::Rack::Logger + use Rails::Rack::LogTailer unless options[:detach] use Rails::Rack::Static use Rails::Rack::Debugger if options[:debugger] run inner_app diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 06a3332c42..619701460d 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -39,7 +39,7 @@ module Rails nil end end - + def backtrace_cleaner @@backtrace_cleaner ||= begin # Relies on ActiveSupport, so we have to lazy load to postpone definition until AS has been loaded @@ -148,7 +148,6 @@ module Rails initialize_dependency_mechanism initialize_whiny_nils - initialize_temporary_session_directory initialize_time_zone initialize_i18n @@ -156,6 +155,8 @@ module Rails initialize_framework_settings initialize_framework_views + initialize_metal + add_support_load_paths load_gems @@ -369,8 +370,9 @@ Run `rake gems:install` to install the missing gems. def load_view_paths if configuration.frameworks.include?(:action_view) if configuration.cache_classes - ActionController::Base.view_paths.load if configuration.frameworks.include?(:action_controller) - ActionMailer::Base.template_root.load if configuration.frameworks.include?(:action_mailer) + view_path = ActionView::Template::EagerPath.new(configuration.view_path) + ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) + ActionMailer::Base.template_root = view_path if configuration.frameworks.include?(:action_mailer) end end end @@ -472,7 +474,7 @@ Run `rake gems:install` to install the missing gems. # set to use Configuration#view_path. def initialize_framework_views if configuration.frameworks.include?(:action_view) - view_path = ActionView::PathSet::Path.new(configuration.view_path, false) + view_path = ActionView::Template::Path.new(configuration.view_path) ActionMailer::Base.template_root ||= view_path if configuration.frameworks.include?(:action_mailer) ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.empty? end @@ -501,13 +503,6 @@ Run `rake gems:install` to install the missing gems. require('active_support/whiny_nil') if configuration.whiny_nils end - def initialize_temporary_session_directory - if configuration.frameworks.include?(:action_controller) - session_path = "#{configuration.root_path}/tmp/sessions/" - ActionController::Base.session_options[:tmpdir] = File.exist?(session_path) ? session_path : Dir::tmpdir - end - end - # Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes. # If assigned value cannot be matched to a TimeZone, an exception will be raised. def initialize_time_zone @@ -529,7 +524,7 @@ Run `rake gems:install` to install the missing gems. end end - # Set the i18n configuration from config.i18n but special-case for the load_path which should be + # Set the i18n configuration from config.i18n but special-case for the load_path which should be # appended to what's already set instead of overwritten. def initialize_i18n configuration.i18n.each do |setting, value| @@ -541,6 +536,10 @@ Run `rake gems:install` to install the missing gems. end end + def initialize_metal + configuration.middleware.insert_before(:"ActionController::VerbPiggybacking", Rails::Rack::Metal) + end + # Initializes framework-specific settings for each of the loaded frameworks # (Configuration#frameworks). The available settings map to the accessors # on each of the corresponding Base classes. @@ -923,6 +922,7 @@ Run `rake gems:install` to install the missing gems. # Followed by the standard includes. paths.concat %w( app + app/metal app/models app/controllers app/helpers @@ -941,6 +941,7 @@ Run `rake gems:install` to install the missing gems. def default_eager_load_paths %w( + app/metal app/models app/controllers app/helpers diff --git a/railties/lib/rails/backtrace_cleaner.rb b/railties/lib/rails/backtrace_cleaner.rb index f344c6477d..e1b422716d 100644 --- a/railties/lib/rails/backtrace_cleaner.rb +++ b/railties/lib/rails/backtrace_cleaner.rb @@ -2,21 +2,25 @@ module Rails class BacktraceCleaner < ActiveSupport::BacktraceCleaner ERB_METHOD_SIG = /:in `_run_erb_.*/ - VENDOR_DIRS = %w( vendor/plugins vendor/gems vendor/rails ) + VENDOR_DIRS = %w( vendor/gems vendor/rails ) SERVER_DIRS = %w( lib/mongrel bin/mongrel lib/passenger bin/passenger-spawn-server lib/rack ) RAILS_NOISE = %w( script/server ) RUBY_NOISE = %w( rubygems/custom_require benchmark.rb ) + GEMS_DIR = Gem.default_dir + ALL_NOISE = VENDOR_DIRS + SERVER_DIRS + RAILS_NOISE + RUBY_NOISE def initialize super - add_filter { |line| line.sub(RAILS_ROOT, '') } + add_filter { |line| line.sub("#{RAILS_ROOT}/", '') } add_filter { |line| line.sub(ERB_METHOD_SIG, '') } add_filter { |line| line.sub('./', '/') } # for tests + add_filter { |line| line.sub(/(#{GEMS_DIR})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')} # http://gist.github.com/30430 add_silencer { |line| ALL_NOISE.any? { |dir| line.include?(dir) } } + add_silencer { |line| line =~ %r(vendor/plugins/[^\/]+/lib) } end end diff --git a/railties/lib/rails/plugin/locator.rb b/railties/lib/rails/plugin/locator.rb index 678b295dc9..a6fc388a8e 100644 --- a/railties/lib/rails/plugin/locator.rb +++ b/railties/lib/rails/plugin/locator.rb @@ -30,7 +30,7 @@ module Rails end # The Rails::Plugin::FileSystemLocator will try to locate plugins by examining the directories - # the the paths given in configuration.plugin_paths. Any plugins that can be found are returned + # in the paths given in configuration.plugin_paths. Any plugins that can be found are returned # in a list. # # The criteria for a valid plugin in this case is found in Rails::Plugin#valid?, although diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb index 90535674e9..9705f65e52 100644 --- a/railties/lib/rails/rack.rb +++ b/railties/lib/rails/rack.rb @@ -1,7 +1,8 @@ module Rails module Rack autoload :Debugger, "rails/rack/debugger" - autoload :Logger, "rails/rack/logger" + autoload :LogTailer, "rails/rack/log_tailer" + autoload :Metal, "rails/rack/metal" autoload :Static, "rails/rack/static" end end diff --git a/railties/lib/rails/rack/log_tailer.rb b/railties/lib/rails/rack/log_tailer.rb new file mode 100644 index 0000000000..a237cee6bc --- /dev/null +++ b/railties/lib/rails/rack/log_tailer.rb @@ -0,0 +1,35 @@ +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 + @cursor = ::File.size(path) + @last_checked = Time.now.to_f + + @file = ::File.open(path, 'r') + end + + def call(env) + response = @app.call(env) + tail_log + response + end + + def tail_log + @file.seek @cursor + + mod = @file.mtime.to_f + if mod > @last_checked + contents = @file.read + @last_checked = mod + @cursor += contents.size + $stdout.print contents + end + end + end + end +end diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb deleted file mode 100644 index 89d02e45a9..0000000000 --- a/railties/lib/rails/rack/logger.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Rails - module Rack - class Logger - EnvironmentLog = "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log" - - def initialize(app, log = nil) - @app = app - @path = Pathname.new(log || EnvironmentLog).cleanpath - @cursor = ::File.size(@path) - @last_checked = Time.now - end - - def call(env) - response = @app.call(env) - ::File.open(@path, 'r') do |f| - f.seek @cursor - if f.mtime > @last_checked - contents = f.read - @last_checked = f.mtime - @cursor += contents.length - print contents - end - end - response - end - end - end -end diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb new file mode 100644 index 0000000000..b185227234 --- /dev/null +++ b/railties/lib/rails/rack/metal.rb @@ -0,0 +1,36 @@ +require 'active_support/ordered_hash' + +module Rails + module Rack + class Metal + NotFoundResponse = [404, {}, []].freeze + NotFound = lambda { NotFoundResponse } + + def self.metals + base = "#{Rails.root}/app/metal" + matcher = /\A#{Regexp.escape(base)}\/(.*)\.rb\Z/ + + Dir["#{base}/**/*.rb"].sort.map do |file| + file.sub!(matcher, '\1') + require file + file.classify.constantize + end + end + + def initialize(app) + @app = app + @metals = ActiveSupport::OrderedHash.new + self.class.metals.each { |app| @metals[app] = true } + freeze + end + + def call(env) + @metals.keys.each do |app| + result = app.call(env) + return result unless result[0].to_i == 404 + end + @app.call(env) + end + end + end +end diff --git a/railties/lib/rails_generator/commands.rb b/railties/lib/rails_generator/commands.rb index cacb3807d6..299044c3d7 100644 --- a/railties/lib/rails_generator/commands.rb +++ b/railties/lib/rails_generator/commands.rb @@ -294,7 +294,7 @@ HELP file(relative_source, relative_destination, template_options) do |file| # Evaluate any assignments in a temporary, throwaway binding. vars = template_options[:assigns] || {} - b = binding + b = template_options[:binding] || binding vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b } # Render the source file with the temporary binding. diff --git a/railties/lib/rails_generator/generators/applications/app/template_runner.rb b/railties/lib/rails_generator/generators/applications/app/template_runner.rb index c6113648e6..bb7bd0e6f4 100644 --- a/railties/lib/rails_generator/generators/applications/app/template_runner.rb +++ b/railties/lib/rails_generator/generators/applications/app/template_runner.rb @@ -8,23 +8,24 @@ require 'fileutils' module Rails class TemplateRunner attr_reader :root + attr_writer :logger def initialize(template, root = '') # :nodoc: - @root = File.join(Dir.pwd, root) + @root = File.expand_path(File.directory?(root) ? root : File.join(Dir.pwd, root)) - puts "applying template: #{template}" + log 'applying', "template: #{template}" load_template(template) - puts "#{template} applied." + log 'applied', "#{template}" end def load_template(template) begin code = open(template).read in_root { self.instance_eval(code) } - rescue LoadError - raise "The template [#{template}] could not be loaded." + rescue LoadError, Errno::ENOENT => e + raise "The template [#{template}] could not be loaded. Error: #{e}" end end @@ -41,8 +42,8 @@ module Rails # # file("config/apach.conf", "your apache config") # - def file(filename, data = nil, &block) - puts "creating file #{filename}" + def file(filename, data = nil, log_action = true, &block) + log 'file', filename if log_action dir, file = [File.dirname(filename), File.basename(filename)] inside(dir) do @@ -66,7 +67,7 @@ module Rails # plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk' # def plugin(name, options) - puts "installing plugin #{name}" + log 'plugin', name if options[:git] && options[:submodule] in_root do @@ -74,28 +75,36 @@ module Rails end elsif options[:git] || options[:svn] in_root do - `script/plugin install #{options[:svn] || options[:git]}` + run("script/plugin install #{options[:svn] || options[:git]}", false) end else - puts "! no git or svn provided for #{name}. skipping..." + log "! no git or svn provided for #{name}. skipping..." end end # Adds an entry into config/environment.rb for the supplied gem : def gem(name, options = {}) - puts "adding gem #{name}" + log 'gem', name - sentinel = 'Rails::Initializer.run do |config|' gems_code = "config.gem '#{name}'" if options.any? - opts = options.inject([]) {|result, h| result << [":#{h[0]} => '#{h[1]}'"] }.join(", ") + opts = options.inject([]) {|result, h| result << [":#{h[0]} => '#{h[1]}'"] }.sort.join(", ") gems_code << ", #{opts}" end + environment gems_code + end + + # Adds a line inside the Initializer block for config/environment.rb. Used by #gem + def environment(data = nil, &block) + sentinel = 'Rails::Initializer.run do |config|' + + data = block.call if !data && block_given? + in_root do gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match| - "#{match}\n #{gems_code}" + "#{match}\n " << data end end end @@ -111,11 +120,11 @@ module Rails def git(command = {}) in_root do if command.is_a?(Symbol) - puts "running git #{command}" + log 'running', "git #{command}" Git.run(command.to_s) else command.each do |command, options| - puts "running git #{command} #{options}" + log 'running', "git #{command} #{options}" Git.run("#{command} #{options}") end end @@ -135,16 +144,8 @@ module Rails # vendor("foreign.rb", "# Foreign code is fun") # def vendor(filename, data = nil, &block) - puts "vendoring file #{filename}" - inside("vendor") do |folder| - File.open("#{folder}/#{filename}", "w") do |f| - if block_given? - f.write(block.call) - else - f.write(data) - end - end - end + log 'vendoring', filename + file("vendor/#{filename}", data, false, &block) end # Create a new file in the lib/ directory. Code can be specified @@ -158,17 +159,9 @@ module Rails # # lib("foreign.rb", "# Foreign code is fun") # - def lib(filename, data = nil) - puts "add lib file #{filename}" - inside("lib") do |folder| - File.open("#{folder}/#{filename}", "w") do |f| - if block_given? - f.write(block.call) - else - f.write(data) - end - end - end + def lib(filename, data = nil, &block) + log 'lib', filename + file("lib/#{filename}", data, false, &block) end # Create a new Rakefile with the provided code (either in a block or a string). @@ -190,16 +183,8 @@ module Rails # rakefile("seed.rake", "puts 'im plantin ur seedz'") # def rakefile(filename, data = nil, &block) - puts "adding rakefile #{filename}" - inside("lib/tasks") do |folder| - File.open("#{folder}/#{filename}", "w") do |f| - if block_given? - f.write(block.call) - else - f.write(data) - end - end - end + log 'rakefile', filename + file("lib/tasks/#{filename}", data, false, &block) end # Create a new initializer with the provided code (either in a block or a string). @@ -219,16 +204,8 @@ module Rails # initializer("api.rb", "API_KEY = '123456'") # def initializer(filename, data = nil, &block) - puts "adding initializer #{filename}" - inside("config/initializers") do |folder| - File.open("#{folder}/#{filename}", "w") do |f| - if block_given? - f.write(block.call) - else - f.write(data) - end - end - end + log 'initializer', filename + file("config/initializers/#{filename}", data, false, &block) end # Generate something using a generator from Rails or a plugin. @@ -240,10 +217,10 @@ module Rails # generate(:authenticated, "user session") # def generate(what, *args) - puts "generating #{what}" + log 'generating', what argument = args.map(&:to_s).flatten.join(" ") - in_root { `#{root}/script/generate #{what} #{argument}` } + in_root { run("script/generate #{what} #{argument}", false) } end # Executes a command @@ -254,8 +231,8 @@ module Rails # run('ln -s ~/edge rails) # end # - def run(command) - puts "executing #{command} from #{Dir.pwd}" + def run(command, log_action = true) + log 'executing', "#{command} from #{Dir.pwd}" if log_action `#{command}` end @@ -268,10 +245,10 @@ module Rails # rake("gems:install", :sudo => true) # def rake(command, options = {}) - puts "running rake task #{command}" + log 'rake', command env = options[:env] || 'development' sudo = options[:sudo] ? 'sudo ' : '' - in_root { `#{sudo}rake #{command} RAILS_ENV=#{env}` } + in_root { run("#{sudo}rake #{command} RAILS_ENV=#{env}", false) } end # Just run the capify command in root @@ -281,7 +258,8 @@ module Rails # capify! # def capify! - in_root { `capify .` } + log 'capifying' + in_root { run('capify .', false) } end # Add Rails to /vendor/rails @@ -291,8 +269,8 @@ module Rails # freeze! # def freeze!(args = {}) - puts "vendoring rails edge" - in_root { `rake rails:freeze:edge` } + log 'vendor', 'rails edge' + in_root { run('rake rails:freeze:edge', false) } end # Make an entry in Rails routing file conifg/routes.rb @@ -302,6 +280,7 @@ module Rails # route "map.root :controller => :welcome" # def route(routing_code) + log 'route', routing_code sentinel = 'ActionController::Routing::Routes.draw do |map|' in_root do @@ -321,7 +300,7 @@ module Rails # freeze! if ask("Should I freeze the latest Rails?") == "yes" # def ask(string) - puts string + log '', string gets.strip end @@ -368,5 +347,23 @@ module Rails def destination_path(relative_destination) File.join(root, relative_destination) end + + def log(action, message = '') + logger.log(action, message) + end + + def logger + @logger ||= Rails::Generator::Base.logger + end + + def logger + @logger ||= if defined?(Rails::Generator::Base) + Rails::Generator::Base.logger + else + require 'rails_generator/simple_logger' + Rails::Generator::SimpleLogger.new(STDOUT) + end + end + end end
\ No newline at end of file diff --git a/railties/lib/rails_generator/generators/components/metal/USAGE b/railties/lib/rails_generator/generators/components/metal/USAGE new file mode 100644 index 0000000000..123ec6c03f --- /dev/null +++ b/railties/lib/rails_generator/generators/components/metal/USAGE @@ -0,0 +1,8 @@ +Description: + Cast some metal! + +Examples: + `./script/generate metal poller` + + This will create: + Metal: app/metal/poller.rb diff --git a/railties/lib/rails_generator/generators/components/metal/metal_generator.rb b/railties/lib/rails_generator/generators/components/metal/metal_generator.rb new file mode 100644 index 0000000000..64f49d929d --- /dev/null +++ b/railties/lib/rails_generator/generators/components/metal/metal_generator.rb @@ -0,0 +1,8 @@ +class MetalGenerator < Rails::Generator::NamedBase + def manifest + record do |m| + m.directory 'app/metal' + m.template 'metal.rb', File.join('app/metal', "#{file_name}.rb") + end + end +end diff --git a/railties/lib/rails_generator/generators/components/metal/templates/metal.rb b/railties/lib/rails_generator/generators/components/metal/templates/metal.rb new file mode 100644 index 0000000000..e94982b69a --- /dev/null +++ b/railties/lib/rails_generator/generators/components/metal/templates/metal.rb @@ -0,0 +1,12 @@ +# Allow the metal piece to run in isolation +require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails) + +class <%= class_name %> + def self.call(env) + if env["PATH_INFO"] =~ /^\/<%= file_name %>/ + [200, {"Content-Type" => "text/html"}, ["Hello, World!"]] + else + [404, {"Content-Type" => "text/html"}, ["Not Found"]] + end + end +end diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake index 3a576063fa..68ffefae0b 100644 --- a/railties/lib/tasks/databases.rake +++ b/railties/lib/tasks/databases.rake @@ -380,7 +380,7 @@ namespace :db do end namespace :sessions do - desc "Creates a sessions migration for use with CGI::Session::ActiveRecordStore" + desc "Creates a sessions migration for use with ActiveRecord::SessionStore" task :create => :environment do raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations? require 'rails_generator' diff --git a/railties/lib/tasks/middleware.rake b/railties/lib/tasks/middleware.rake index e0dcf50307..05f159184e 100644 --- a/railties/lib/tasks/middleware.rake +++ b/railties/lib/tasks/middleware.rake @@ -1,6 +1,6 @@ desc 'Prints out your Rack middleware stack' task :middleware => :environment do - ActionController::Dispatcher.middleware.each do |middleware| + ActionController::Dispatcher.middleware.active.each do |middleware| puts "use #{middleware.inspect}" end puts "run ActionController::Dispatcher.new" diff --git a/railties/lib/tasks/tmp.rake b/railties/lib/tasks/tmp.rake index b191039d63..fea15058bb 100644 --- a/railties/lib/tasks/tmp.rake +++ b/railties/lib/tasks/tmp.rake @@ -2,7 +2,7 @@ namespace :tmp do desc "Clear session, cache, and socket files from tmp/" task :clear => [ "tmp:sessions:clear", "tmp:cache:clear", "tmp:sockets:clear"] - desc "Creates tmp directories for sessions, cache, and sockets" + desc "Creates tmp directories for sessions, cache, sockets, and pids" task :create do FileUtils.mkdir_p(%w( tmp/sessions tmp/cache tmp/sockets tmp/pids )) end @@ -34,4 +34,4 @@ namespace :tmp do FileUtils.rm(Dir['tmp/pids/[^.]*']) end end -end
\ No newline at end of file +end diff --git a/railties/lib/test_help.rb b/railties/lib/test_help.rb index b5c92c1790..ee24ea3a45 100644 --- a/railties/lib/test_help.rb +++ b/railties/lib/test_help.rb @@ -3,7 +3,6 @@ silence_warnings { RAILS_ENV = "test" } require 'test/unit' -require 'active_support/test_case' require 'action_controller/test_case' require 'action_view/test_case' require 'action_controller/integration' |