class ReplRunner::MultiRepl

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

Constants

DEFAULT_RETURN_CHAR
DEFAULT_STARTUP_TIMEOUT

Attributes

command[RW]

Public Class Methods

new(command, options = {}) click to toggle source
# 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

Public Instance Methods

alive?() click to toggle source
# 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
close() click to toggle source
# File lib/repl_runner/multi_repl.rb, line 57
def close
  pty.close
end
dead?() click to toggle source
# File lib/repl_runner/multi_repl.rb, line 44
def dead?
  !alive?
end
execute() click to toggle source
# 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_results() click to toggle source
# File lib/repl_runner/multi_repl.rb, line 53
def parse_results
  @parsed_results ||= @command_parser.new(@commands, @terminate_command).parse(read)
end
pty() click to toggle source
# File lib/repl_runner/multi_repl.rb, line 27
def pty
  @pty ||= PtyParty.new(command)
end
read() click to toggle source
# 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) click to toggle source
# File lib/repl_runner/multi_repl.rb, line 31
def run(command, &block)
  @commands << command
  @jobs     << (block || Proc.new {|result| })
end
write(command) click to toggle source
# File lib/repl_runner/multi_repl.rb, line 36
def write(command)
  pty.write("#{command}#{@return_char}")
end