2005-02-19 16:57:43 -05:00
|
|
|
require 'optparse'
|
2009-11-23 19:57:34 -05:00
|
|
|
require 'irb'
|
2010-01-23 17:02:43 -05:00
|
|
|
require 'irb/completion'
|
2008-09-11 11:06:36 -04:00
|
|
|
|
2009-11-23 19:57:34 -05:00
|
|
|
module Rails
|
|
|
|
class Console
|
2009-11-24 16:03:24 -05:00
|
|
|
def self.start(app)
|
|
|
|
new(app).start
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
2009-11-23 19:57:34 -05:00
|
|
|
end
|
2005-02-19 16:57:43 -05:00
|
|
|
|
2009-11-23 19:57:34 -05:00
|
|
|
def start
|
|
|
|
options = {}
|
2005-02-19 16:57:43 -05:00
|
|
|
|
2009-11-23 19:57:34 -05:00
|
|
|
OptionParser.new do |opt|
|
|
|
|
opt.banner = "Usage: console [environment] [options]"
|
|
|
|
opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v }
|
|
|
|
opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v }
|
2010-02-09 08:19:54 -05:00
|
|
|
opt.on('--irb', "DEPRECATED: Invoke `/your/choice/of/ruby script/rails console` instead") { |v| abort '--irb option is no longer supported. Invoke `/your/choice/of/ruby script/rails console` instead' }
|
2009-11-23 19:57:34 -05:00
|
|
|
opt.parse!(ARGV)
|
|
|
|
end
|
|
|
|
|
2010-07-17 04:59:41 -04:00
|
|
|
@app.load_console(options[:sandbox])
|
2008-09-11 11:06:36 -04:00
|
|
|
|
2009-11-23 19:57:34 -05:00
|
|
|
if options[:debugger]
|
|
|
|
begin
|
|
|
|
require 'ruby-debug'
|
|
|
|
puts "=> Debugger enabled"
|
|
|
|
rescue Exception
|
|
|
|
puts "You need to install ruby-debug to run the console in debugging mode. With gems, use 'gem install ruby-debug'"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
2006-11-30 19:08:33 -05:00
|
|
|
|
2009-11-23 19:57:34 -05:00
|
|
|
if options[:sandbox]
|
2009-11-24 13:46:16 -05:00
|
|
|
puts "Loading #{Rails.env} environment in sandbox (Rails #{Rails.version})"
|
2009-11-23 19:57:34 -05:00
|
|
|
puts "Any modifications you make will be rolled back on exit"
|
|
|
|
else
|
2009-11-24 13:46:16 -05:00
|
|
|
puts "Loading #{Rails.env} environment (Rails #{Rails.version})"
|
2009-11-23 19:57:34 -05:00
|
|
|
end
|
|
|
|
IRB.start
|
|
|
|
end
|
|
|
|
end
|
2009-11-24 01:07:37 -05:00
|
|
|
end
|
2010-01-11 17:01:28 -05:00
|
|
|
|
|
|
|
# Has to set the RAILS_ENV before config/application is required
|
|
|
|
if ARGV.first && !ARGV.first.index("-") && env = ARGV.pop # has to pop the env ARGV so IRB doesn't freak
|
2010-09-01 23:05:34 -04:00
|
|
|
ENV['RAILS_ENV'] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
|
2010-01-11 17:01:28 -05:00
|
|
|
end
|