diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 15f51bb6bb..6751e27f01 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,6 +1,6 @@ *SVN* -* script/runner can run files, pass on arguments, and be used as a shebang. #6286 [Tuxie] +* script/runner can run files, pass on arguments, and be used as a shebang. #6286 [Tuxie, dlpond] #!/usr/bin/env /path/to/my/app/script/runner # Example: just start using your models as if you are in script/console Product.find(:all).each { |product| product.check_inventory } diff --git a/railties/lib/commands/runner.rb b/railties/lib/commands/runner.rb index b2efba4dca..62db8b75e1 100644 --- a/railties/lib/commands/runner.rb +++ b/railties/lib/commands/runner.rb @@ -1,6 +1,7 @@ require 'optparse' options = { :environment => (ENV['RAILS_ENV'] || "development").dup } +code_or_file = nil ARGV.clone.options do |opts| script_name = File.basename($0) @@ -27,19 +28,21 @@ opts.separator "-------------------------------------------------------------" end - opts.parse! rescue retry + opts.order! { |o| code_or_file ||= o } rescue retry end +ARGV.delete(code_or_file) + ENV["RAILS_ENV"] = options[:environment] RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV) require RAILS_ROOT + '/config/environment' -if ARGV.empty? +if code_or_file.nil? $stderr.puts "Run '#{$0} -h' for help." exit 1 -elsif File.exists?(ARGV.first) - eval(File.read(ARGV.shift)) +elsif File.exists?(code_or_file) + eval(File.read(code_or_file)) else - eval(ARGV.first) + eval(code_or_file) end