Skip to content

Commit

Permalink
Use the first unparsed argument as the code or file to run. Closes #6286
Browse files Browse the repository at this point in the history
.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5203 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Sep 29, 2006
1 parent 643d17c commit 3c877ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion railties/CHANGELOG
@@ -1,6 +1,6 @@
*SVN* *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 #!/usr/bin/env /path/to/my/app/script/runner
# Example: just start using your models as if you are in script/console # Example: just start using your models as if you are in script/console
Product.find(:all).each { |product| product.check_inventory } Product.find(:all).each { |product| product.check_inventory }
Expand Down
13 changes: 8 additions & 5 deletions railties/lib/commands/runner.rb
@@ -1,6 +1,7 @@
require 'optparse' require 'optparse'


options = { :environment => (ENV['RAILS_ENV'] || "development").dup } options = { :environment => (ENV['RAILS_ENV'] || "development").dup }
code_or_file = nil


ARGV.clone.options do |opts| ARGV.clone.options do |opts|
script_name = File.basename($0) script_name = File.basename($0)
Expand All @@ -27,19 +28,21 @@
opts.separator "-------------------------------------------------------------" opts.separator "-------------------------------------------------------------"
end end


opts.parse! rescue retry opts.order! { |o| code_or_file ||= o } rescue retry
end end


ARGV.delete(code_or_file)

ENV["RAILS_ENV"] = options[:environment] ENV["RAILS_ENV"] = options[:environment]
RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV) RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)


require RAILS_ROOT + '/config/environment' require RAILS_ROOT + '/config/environment'


if ARGV.empty? if code_or_file.nil?
$stderr.puts "Run '#{$0} -h' for help." $stderr.puts "Run '#{$0} -h' for help."
exit 1 exit 1
elsif File.exists?(ARGV.first) elsif File.exists?(code_or_file)
eval(File.read(ARGV.shift)) eval(File.read(code_or_file))
else else
eval(ARGV.first) eval(code_or_file)
end end

0 comments on commit 3c877ec

Please sign in to comment.