Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5189

Show
Ignore:
Timestamp:
09/26/06 07:10:08 (3 years ago)
Author:
bitsweat
Message:

script/runner can run files, pass on arguments, and be used as a shebang. Closes #6286.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r5159 r5189  
    11*SVN* 
     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 } 
    27 
    38* Look for rake tasks in plugin subdirs.  #6259 [obrie] 
  • trunk/railties/lib/commands/runner.rb

    r4502 r5189  
    33options = { :environment => (ENV['RAILS_ENV'] || "development").dup } 
    44 
    5 ARGV.options do |opts| 
     5ARGV.clone.options do |opts| 
    66  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)
    88 
    99  opts.separator "" 
     
    1616 
    1717  opts.on("-h", "--help", 
    18           "Show this help message.") { puts opts; exit } 
     18          "Show this help message.") { $stderr.puts opts; exit } 
    1919 
    20   opts.parse! 
     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 
    2131end 
    2232 
     
    2535 
    2636require RAILS_ROOT + '/config/environment' 
    27 ARGV.empty? ? puts("Usage: runner 'code' [options]") : eval(ARGV.first) 
     37 
     38if ARGV.empty? 
     39  $stderr.puts "Run '#{$0} -h' for help." 
     40  exit 1 
     41elsif File.exists?(ARGV.first) 
     42  eval(File.read(ARGV.shift)) 
     43else 
     44  eval(ARGV.first) 
     45end