Class: RemoteRuby::TmpFileAdapter

Inherits:
ConnectionAdapter show all
Defined in:
lib/remote_ruby/tmp_file_adapter.rb

Overview

An adapter to expecute Ruby code on the local machine inside a temporary file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(working_dir: Dir.pwd) ⇒ TmpFileAdapter

Returns a new instance of TmpFileAdapter.



13
14
15
16
# File 'lib/remote_ruby/tmp_file_adapter.rb', line 13

def initialize(working_dir: Dir.pwd)
  super
  @working_dir = working_dir
end

Instance Attribute Details

#working_dirObject (readonly)

Returns the value of attribute working_dir.



11
12
13
# File 'lib/remote_ruby/tmp_file_adapter.rb', line 11

def working_dir
  @working_dir
end

Instance Method Details

#connection_nameObject



43
44
45
# File 'lib/remote_ruby/tmp_file_adapter.rb', line 43

def connection_name
  "#{ENV.fetch('USER', nil)}@localhost:#{working_dir}> "
end

#open(code, stdin, stdout, stderr) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/remote_ruby/tmp_file_adapter.rb', line 18

def open(code, stdin, stdout, stderr)
  result = nil

  stdin = RemoteRuby::CompatIOReader.new(stdin)
  stdout = RemoteRuby::CompatIOWriter.new(stdout)
  stderr = RemoteRuby::CompatIOWriter.new(stderr)

  with_temp_file(code) do |filename|
    pid = Process.spawn(
      command(filename),
      in: stdin.readable,
      out: stdout.writeable,
      err: stderr.writeable
    )

    _, status = Process.wait2(pid)
    raise "Process exited with code #{status}" unless status.success?

    [stdin, stdout, stderr].each(&:join)

    result = File.binread(filename)
  end
  result
end