Module: Prometheus::Repl

Defined in:
lib/prometheus/repl.rb,
lib/prometheus/repl/command.rb

Defined Under Namespace

Modules: Command

Class Method Summary collapse

Class Method Details

.execute(line) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/prometheus/repl.rb', line 71

def execute(line)
  if command = Command.find(line)
    arg = line.split(/\s+/, 2)[1] rescue nil
    command.call(arg)
  else
    invoke(line.strip)
  end
end

.invoke(line) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/prometheus/repl.rb', line 80

def invoke(line)
  start = Time.now

  name, *args = line.split(/\s+/)
  pid = fork do
    args.each do |arg|
      env, value = arg.split('=')
      next unless env && !env.empty? && value && !value.empty?
      ENV[env] = value
    end
    begin
      @context.invoke(name, args)
    rescue Exception => e
      raise unless e.to_s == "undefined method `<=' for nil:NilClass"
      @context.say "I don't know that task, try 'help' to see what's available", :red
    end
  end
  Process.waitpid(pid)
end

.promptObject



54
55
56
57
58
# File 'lib/prometheus/repl.rb', line 54

def prompt
  ns = @context.class.namespace.split(':').last
  ns = 'debug' if @debug_mode
  "\e[34m#{PROMPT}:\e[35m#{ns}\e[0m> "
end

.setup_readlineObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/prometheus/repl.rb', line 60

def setup_readline
  Readline.basic_word_break_characters = ""
  Readline.completion_proc = lambda do |word|
    (
      @context.class.tasks.keys.map { |name| name } +
      Command.command_names.map { |name| name.to_s } +
      ['help']
    ).grep(/#{Regexp.quote(word)}/)
  end
end

.start(context) ⇒ Object



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
# File 'lib/prometheus/repl.rb', line 25

def start(context)
  @context = context
  @debug_mode = false
  setup_readline

  puts @context.class.repl_banner

  while buf = Readline.readline(prompt, true)
    line = buf.strip
    next if line.empty?
    begin
      if @debug_mode
        if line == '!!'
          @debug_mode = false
        else
          @context.say eval(line), :cyan
        end
      else
        execute line
      end
    rescue SystemExit
      raise
    rescue Exception => e
      @context.say "#{e.message}\n#{e.backtrace.join("\n")}", :red
    end
    setup_readline
  end
end