aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/runner.rb
diff options
context:
space:
mode:
authorYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-09-24 14:01:31 -0700
committerYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-09-24 16:11:41 -0700
commitf0dd77c6be6a86fe384bb0015151e0a497973d39 (patch)
treead81bb4c807c39edeaa37035882a16d9d95ebaa9 /railties/lib/rails/commands/runner.rb
parent610b81beca461a6fa4f00c7023e0e4315eb2b8be (diff)
downloadrails-f0dd77c6be6a86fe384bb0015151e0a497973d39.tar.gz
rails-f0dd77c6be6a86fe384bb0015151e0a497973d39.tar.bz2
rails-f0dd77c6be6a86fe384bb0015151e0a497973d39.zip
Move railties/lib/* into railties/lib/*
Diffstat (limited to 'railties/lib/rails/commands/runner.rb')
-rw-r--r--railties/lib/rails/commands/runner.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb
new file mode 100644
index 0000000000..510128318a
--- /dev/null
+++ b/railties/lib/rails/commands/runner.rb
@@ -0,0 +1,54 @@
+require 'optparse'
+
+options = { :environment => (ENV['RAILS_ENV'] || "development").dup }
+code_or_file = nil
+
+ARGV.clone.options do |opts|
+ script_name = File.basename($0)
+ opts.banner = "Usage: #{$0} [options] ('Some.ruby(code)' or a filename)"
+
+ opts.separator ""
+
+ opts.on("-e", "--environment=name", String,
+ "Specifies the environment for the runner to operate under (test/development/production).",
+ "Default: development") { |v| options[:environment] = v }
+
+ opts.separator ""
+
+ opts.on("-h", "--help",
+ "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.order! { |o| code_or_file ||= o } rescue retry
+end
+
+ARGV.delete(code_or_file)
+
+ENV["RAILS_ENV"] = options[:environment]
+RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)
+
+require RAILS_ROOT + '/config/environment'
+
+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
+ if defined? Rails
+ Rails.logger.flush if Rails.logger.respond_to?(:flush)
+ end
+end