Skip to content
This repository
Browse code

script/runner can run files, pass on arguments, and be used as a sheb…

…ang. Closes #6286.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5189 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 43c4d3002dd65753facea8f836e41c79a99a705c 1 parent 1caa763
Jeremy Kemper jeremy authored

Showing 2 changed files with 29 additions and 6 deletions. Show diff stats Hide diff stats

  1. +5 0 railties/CHANGELOG
  2. +24 6 railties/lib/commands/runner.rb
5 railties/CHANGELOG
... ... @@ -1,5 +1,10 @@
1 1 *SVN*
2 2
  3 +* script/runner can run files, pass on arguments, and be used as a shebang. #6286 [Tuxie]
  4 + #!/usr/bin/env /path/to/my/app/script/runner
  5 + # Example: just start using your models as if you are in script/console
  6 + Product.find(:all).each { |product| product.check_inventory }
  7 +
3 8 * Look for rake tasks in plugin subdirs. #6259 [obrie]
4 9
5 10 * Added map.connect ':controller/:action/:id.:format' as a default route to config/routes.rb [DHH]
30 railties/lib/commands/runner.rb
@@ -2,9 +2,9 @@
2 2
3 3 options = { :environment => (ENV['RAILS_ENV'] || "development").dup }
4 4
5   -ARGV.options do |opts|
  5 +ARGV.clone.options do |opts|
6 6 script_name = File.basename($0)
7   - opts.banner = "Usage: runner 'puts Person.find(1).name' [options]"
  7 + opts.banner = "Usage: #{$0} [options] ('Some.ruby(code)' or a filename)"
8 8
9 9 opts.separator ""
10 10
@@ -15,13 +15,31 @@
15 15 opts.separator ""
16 16
17 17 opts.on("-h", "--help",
18   - "Show this help message.") { puts opts; exit }
19   -
20   - opts.parse!
  18 + "Show this help message.") { $stderr.puts opts; exit }
  19 +
  20 + if RUBY_PLATFORM !~ /mswin/
  21 + opts.separator ""
  22 + opts.separator "You can also use runner as a shebang line for your scripts like this:"
  23 + opts.separator "-------------------------------------------------------------"
  24 + opts.separator "#!/usr/bin/env #{File.expand_path($0)}"
  25 + opts.separator ""
  26 + opts.separator "Product.find(:all).each { |p| p.price *= 2 ; p.save! }"
  27 + opts.separator "-------------------------------------------------------------"
  28 + end
  29 +
  30 + opts.parse! rescue retry
21 31 end
22 32
23 33 ENV["RAILS_ENV"] = options[:environment]
24 34 RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)
25 35
26 36 require RAILS_ROOT + '/config/environment'
27   -ARGV.empty? ? puts("Usage: runner 'code' [options]") : eval(ARGV.first)
  37 +
  38 +if ARGV.empty?
  39 + $stderr.puts "Run '#{$0} -h' for help."
  40 + exit 1
  41 +elsif File.exists?(ARGV.first)
  42 + eval(File.read(ARGV.shift))
  43 +else
  44 + eval(ARGV.first)
  45 +end

0 comments on commit 43c4d30

Please sign in to comment.
Something went wrong with that request. Please try again.