Class: Rpremote::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/rpremote/shell.rb,
sig/rpremote.rbs

Defined Under Namespace

Classes: CommandError, Error, ProtocolError, TimeoutError

Constant Summary collapse

DEFAULT_TIMEOUT =

Returns:

  • (Float)
20.0
PROMPT_START =

Returns:

  • (String)
"\e[?25l\e[1G$> \e[0K".b
PROMPT_END =

Returns:

  • (String)
"\e[?25h".b
ECHO_START =
"\e[?25l\e[1G$> ".b
ERASE_LINE =
"\e[0K".b
CURSOR_POSITION_QUERY =

Returns:

  • (String)
"\e[6n".b
CURSOR_POSITION_RESPONSE =

Returns:

  • (String)
"\e[1;1R".b
RUBY_EXCEPTION_STATUS =

Returns:

  • (String)
"\x1eR2P2:RUBY_EXCEPTION\x1f".b

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, timeout: DEFAULT_TIMEOUT) ⇒ Shell

Returns a new instance of Shell.

Parameters:

  • io (Object)
  • timeout: (Float) (defaults to: DEFAULT_TIMEOUT)

Raises:

  • (ArgumentError)


32
33
34
35
36
37
# File 'lib/rpremote/shell.rb', line 32

def initialize(io, timeout: DEFAULT_TIMEOUT)
  raise ArgumentError, "timeout must be positive" unless timeout.positive?

  @io = io
  @timeout = timeout
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.

Returns:

  • (Object)


30
31
32
# File 'lib/rpremote/shell.rb', line 30

def io
  @io
end

#timeoutFloat (readonly)

Returns the value of attribute timeout.

Returns:

  • (Float)


30
31
32
# File 'lib/rpremote/shell.rb', line 30

def timeout
  @timeout
end

Class Method Details

.quote_argument(argument) ⇒ String

Parameters:

  • argument (String)

Returns:

  • (String)

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/rpremote/shell.rb', line 21

def self.quote_argument(argument)
  argument = String(argument)
  raise ArgumentError, "shell argument contains a control character" if argument.match?(/[\x00-\x1f\x7f]/)
  return "'#{argument}'" unless argument.include?("'")
  return %("#{argument}") unless argument.include?('"')

  raise ArgumentError, "shell argument cannot contain both quote characters"
end

Instance Method Details

#execute(command, output: nil) ⇒ String

Parameters:

  • command (String)
  • output: (IO, nil) (defaults to: nil)

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rpremote/shell.rb', line 46

def execute(command, output: nil)
  command = validate_command(command)
  send_command(command)
  echo = ECHO_START + command.b + ERASE_LINE
  stream_state = {}
  response = read_until_prompt(after: echo) { |buffer| stream_output(output, buffer, echo, stream_state) }
  command_output = extract_output(response, command)
  ruby_exception = command_output.include?(RUBY_EXCEPTION_STATUS)
  command_output = remove_ruby_exception_status(command_output)
  raise CommandError, "Ruby exception reported by R2P2" if ruby_exception

  command_output
rescue TimeoutError
  interrupt
  raise
end

#send_command(command) ⇒ nil

Parameters:

  • command (String)

Returns:

  • (nil)


63
64
65
66
67
# File 'lib/rpremote/shell.rb', line 63

def send_command(command)
  command = validate_command(command)
  write_all("#{command}\r".b)
  nil
end

#synchronize!nil

Returns:

  • (nil)


39
40
41
42
43
44
# File 'lib/rpremote/shell.rb', line 39

def synchronize!
  drain_input
  write_all("\r".b)
  read_until_prompt
  nil
end