Class: Rpremote::Runner

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

Constant Summary collapse

REMOTE_DIRECTORY =

Returns:

  • (String)
"/home"
RUN_REMOTE_PATH =
"#{REMOTE_DIRECTORY}/.rpremote-run.rb".freeze
DEPLOY_REMOTE_PATH =
"#{REMOTE_DIRECTORY}/.rpremote-deploy.rb".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, timeout: Shell::DEFAULT_TIMEOUT, modem_class: PicoModem, shell_class: Shell, path_factory: nil) ⇒ Runner

Returns a new instance of Runner.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rpremote/runner.rb', line 11

def initialize(
  io,
  timeout: Shell::DEFAULT_TIMEOUT,
  modem_class: PicoModem,
  shell_class: Shell,
  path_factory: nil
)
  @io = io
  @timeout = timeout
  @modem_class = modem_class
  @shell_class = shell_class
  @path_factory = path_factory || method(:temporary_path)
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.

Returns:

  • (Object)


9
10
11
# File 'lib/rpremote/runner.rb', line 9

def io
  @io
end

#timeoutFloat (readonly)

Returns the value of attribute timeout.

Returns:

  • (Float)


9
10
11
# File 'lib/rpremote/runner.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#run(data, output: nil, cleanup: true, remote_path: nil, diagnostics: nil) ⇒ String

Parameters:

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

Returns:

  • (String)


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/rpremote/runner.rb', line 25

def run(data, output: nil, cleanup: true, remote_path: nil, diagnostics: nil)
  remote_path ||= @path_factory.call
  shell = @shell_class.new(io, timeout: timeout)
  trace(diagnostics, "SYNC_BEFORE_UPLOAD_START")
  shell.synchronize!
  trace(diagnostics, "SYNC_BEFORE_UPLOAD_DONE")
  trace(diagnostics, "UPLOAD_START", "bytes=#{data.bytesize},path=#{remote_path}")
  @modem_class.new(io, timeout: timeout).upload(remote_path, data)
  trace(diagnostics, "UPLOAD_DONE")
  trace(diagnostics, "SYNC_BEFORE_RUN_START")
  shell.synchronize!
  trace(diagnostics, "SYNC_BEFORE_RUN_DONE")
  shell_ready = true
  command = "./#{File.basename(remote_path)}"
  trace(diagnostics, "EXECUTE_START", "command=#{command}")
  result = output ? shell.execute(command, output: output, idle_timeout: true) : shell.execute(command, idle_timeout: true)
  trace(diagnostics, "EXECUTE_DONE", "bytes=#{result.to_s.bytesize}")
  result
rescue StandardError => e
  trace(diagnostics, "ERROR", "class=#{e.class},message=#{e.message.to_s.gsub("\n", " ")}")
  raise
ensure
  if cleanup && shell_ready && remote_path
    trace(diagnostics, "CLEANUP_START", "path=#{remote_path}")
    cleanup(shell, remote_path)
    trace(diagnostics, "CLEANUP_DONE")
  end
end