2005-06-16 01:50:24 -04:00
|
|
|
require 'optparse'
|
|
|
|
|
2005-10-13 22:51:33 -04:00
|
|
|
options = { :environment => (ENV['RAILS_ENV'] || "development").dup }
|
2006-09-29 03:45:08 -04:00
|
|
|
code_or_file = nil
|
2005-06-16 01:50:24 -04:00
|
|
|
|
2006-09-26 03:10:08 -04:00
|
|
|
ARGV.clone.options do |opts|
|
2005-06-16 01:50:24 -04:00
|
|
|
script_name = File.basename($0)
|
2010-02-02 19:16:26 -05:00
|
|
|
opts.banner = "Usage: runner [options] ('Some.ruby(code)' or a filename)"
|
2005-06-16 01:50:24 -04:00
|
|
|
|
|
|
|
opts.separator ""
|
|
|
|
|
|
|
|
opts.on("-e", "--environment=name", String,
|
|
|
|
"Specifies the environment for the runner to operate under (test/development/production).",
|
2006-06-28 16:53:00 -04:00
|
|
|
"Default: development") { |v| options[:environment] = v }
|
2005-06-16 01:50:24 -04:00
|
|
|
|
|
|
|
opts.separator ""
|
|
|
|
|
|
|
|
opts.on("-h", "--help",
|
2006-09-26 03:10:08 -04:00
|
|
|
"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
|
|
|
|
|
2006-09-29 03:45:08 -04:00
|
|
|
opts.order! { |o| code_or_file ||= o } rescue retry
|
2005-06-16 01:50:24 -04:00
|
|
|
end
|
|
|
|
|
2006-09-29 03:45:08 -04:00
|
|
|
ARGV.delete(code_or_file)
|
|
|
|
|
2005-10-12 08:44:03 -04:00
|
|
|
ENV["RAILS_ENV"] = options[:environment]
|
2005-06-16 01:50:24 -04:00
|
|
|
|
2010-02-09 11:40:06 -05:00
|
|
|
require ENV_PATH
|
|
|
|
|
2008-11-22 14:32:36 -05:00
|
|
|
begin
|
|
|
|
if code_or_file.nil?
|
|
|
|
$stderr.puts "Run '#{$0} -h' for help."
|
|
|
|
exit 1
|
|
|
|
elsif File.exist?(code_or_file)
|
|
|
|
eval(File.read(code_or_file), nil, code_or_file)
|
|
|
|
else
|
|
|
|
eval(code_or_file)
|
|
|
|
end
|
|
|
|
ensure
|
2008-12-10 19:07:02 -05:00
|
|
|
if defined? Rails
|
|
|
|
Rails.logger.flush if Rails.logger.respond_to?(:flush)
|
|
|
|
end
|
2006-09-26 03:10:08 -04:00
|
|
|
end
|