-
Notifications
You must be signed in to change notification settings - Fork 4
/
console.rb
63 lines (48 loc) 路 1.39 KB
/
console.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'rack'
require 'rack/console/methods'
require 'rack/console/session'
require 'rack/console/version'
module Rack
class Console
def self.start(options = {})
self.new(options).start
end
def initialize(options = {})
@options = default_options.merge(options)
ENV['RACK_ENV'] = @options[:environment]
set_preamble
$LOAD_PATH.unshift(*@options[:include]) if @options[:include]
require @options[:require] if @options[:require]
end
def start
puts ENV['RACK_CONSOLE_INTRO'] unless ENV['IGNORE_RACK_CONSOLE_INTRO']
app = Rack::Builder.parse_file(@options[:config]).first
# Add convenience methods to the top-level binding (main)
main.extend(Rack::Console::Methods)
main.instance_variable_set(:@app, Rack::Console::Session.new(app))
begin
require 'pry'
Pry.start
rescue LoadError
require 'irb'
IRB.start
end
end
private
def main
TOPLEVEL_BINDING.eval('self')
end
def set_preamble
return if ENV['RACK_CONSOLE_INTRO']
loading = "Loading #{ENV['RACK_ENV']} environment"
version = "(Rack::Console #{Rack::Console::VERSION})"
ENV['RACK_CONSOLE_INTRO'] = "#{loading} #{version}"
end
def default_options
{
config: 'config.ru',
environment: ENV['RACK_ENV'] || 'development'
}
end
end
end