Class: ReplRunner::MultiRepl

Inherits:
Object
  • Object
show all
Defined in:
lib/repl_runner/multi_repl.rb

Overview

Takes in a command, to start a REPL session with PTY builds up a list of commands we want to run in our REPL executes them all at a time, reads in the result and returns results back to blocks

Defined Under Namespace

Classes: UnexpectedExit

Constant Summary collapse

DEFAULT_STARTUP_TIMEOUT =
60
DEFAULT_RETURN_CHAR =
"\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options = {}) ⇒ MultiRepl

Returns a new instance of MultiRepl.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/repl_runner/multi_repl.rb', line 15

def initialize(command, options = {})
  @command_parser    = options[:command_parser]    || MultiCommandParser
  @return_char       = options[:return_char]       || DEFAULT_RETURN_CHAR
  @startup_timeout   = options[:startup_timeout]   || DEFAULT_STARTUP_TIMEOUT
  @terminate_command = options[:terminate_command] or raise "must set default `terminate_command`"
  @stync_stdout      = options[:sync_stdout]

  @command  = command
  @commands = []
  @jobs     = []
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



13
14
15
# File 'lib/repl_runner/multi_repl.rb', line 13

def command
  @command
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/repl_runner/multi_repl.rb', line 40

def alive?
  !!::Process.kill(0, pty.pid) rescue false # kill zero will return the status of the proceess without killing it
end

#closeObject



57
58
59
# File 'lib/repl_runner/multi_repl.rb', line 57

def close
  pty.close
end

#dead?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/repl_runner/multi_repl.rb', line 44

def dead?
  !alive?
end

#executeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/repl_runner/multi_repl.rb', line 61

def execute
  write(@sync_stdout) if @sync_stdout

  @commands.each do |command|
    write(command)
  end

  write(@terminate_command)


  output_array = parse_results

  @jobs.each_with_index do |job, index|
    job.call(output_array[index])
  end
rescue NoResultsError => e
  raise e, "Booting up REPL with command: #{command.inspect} \n#{e.message}"
end

#parse_resultsObject



53
54
55
# File 'lib/repl_runner/multi_repl.rb', line 53

def parse_results
  @parsed_results ||= @command_parser.new(@commands, @terminate_command).parse(read)
end

#ptyObject



27
28
29
# File 'lib/repl_runner/multi_repl.rb', line 27

def pty
  @pty ||= PtyParty.new(command)
end

#readObject

Raises:



48
49
50
51
# File 'lib/repl_runner/multi_repl.rb', line 48

def read
  raise UnexpectedExit, "Repl: '#{@command}' exited unexpectedly" if dead?
  @output = pty.read(@startup_timeout)
end

#run(command, &block) ⇒ Object



31
32
33
34
# File 'lib/repl_runner/multi_repl.rb', line 31

def run(command, &block)
  @commands << command
  @jobs     << (block || Proc.new {|result| })
end

#write(command) ⇒ Object



36
37
38
# File 'lib/repl_runner/multi_repl.rb', line 36

def write(command)
  pty.write("#{command}#{@return_char}")
end