From 43c4d3002dd65753facea8f836e41c79a99a705c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 26 Sep 2006 07:10:08 +0000 Subject: script/runner can run files, pass on arguments, and be used as a shebang. Closes #6286. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5189 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/CHANGELOG | 5 +++++ railties/lib/commands/runner.rb | 30 ++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) (limited to 'railties') diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 5f2b332430..15f51bb6bb 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,10 @@ *SVN* +* script/runner can run files, pass on arguments, and be used as a shebang. #6286 [Tuxie] + #!/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 } + * Look for rake tasks in plugin subdirs. #6259 [obrie] * Added map.connect ':controller/:action/:id.:format' as a default route to config/routes.rb [DHH] diff --git a/railties/lib/commands/runner.rb b/railties/lib/commands/runner.rb index 47186d52e4..b2efba4dca 100644 --- a/railties/lib/commands/runner.rb +++ b/railties/lib/commands/runner.rb @@ -2,9 +2,9 @@ require 'optparse' options = { :environment => (ENV['RAILS_ENV'] || "development").dup } -ARGV.options do |opts| +ARGV.clone.options do |opts| script_name = File.basename($0) - opts.banner = "Usage: runner 'puts Person.find(1).name' [options]" + opts.banner = "Usage: #{$0} [options] ('Some.ruby(code)' or a filename)" opts.separator "" @@ -15,13 +15,31 @@ ARGV.options do |opts| opts.separator "" opts.on("-h", "--help", - "Show this help message.") { puts opts; exit } - - opts.parse! + "Show this help message.") { $stderr.puts opts; exit } + + if RUBY_PLATFORM !~ /mswin/ + opts.separator "" + opts.separator "You can also use runner as a shebang line for your scripts like this:" + opts.separator "-------------------------------------------------------------" + opts.separator "#!/usr/bin/env #{File.expand_path($0)}" + opts.separator "" + opts.separator "Product.find(:all).each { |p| p.price *= 2 ; p.save! }" + opts.separator "-------------------------------------------------------------" + end + + opts.parse! rescue retry end ENV["RAILS_ENV"] = options[:environment] RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV) require RAILS_ROOT + '/config/environment' -ARGV.empty? ? puts("Usage: runner 'code' [options]") : eval(ARGV.first) + +if ARGV.empty? + $stderr.puts "Run '#{$0} -h' for help." + exit 1 +elsif File.exists?(ARGV.first) + eval(File.read(ARGV.shift)) +else + eval(ARGV.first) +end -- cgit v1.2.3