Module: TypeSpecFromSerializers::IO

Defined in:
lib/typespec_from_serializers/io.rb

Overview

Public: Builds on top of Ruby I/O open3 providing a friendlier experience.

Class Method Summary collapse

Class Method Details

.capture(*cmd, with_output: nil, **opts) ⇒ Object

Internal: A modified version of capture3 that can continuously print stdout.



9
10
11
12
13
14
15
16
17
18
# File 'lib/typespec_from_serializers/io.rb', line 9

def capture(*cmd, with_output: nil, **opts)
  return Open3.capture3(*cmd, **opts) unless with_output

  Open3.popen3(*cmd, **opts) do |stdin, stdout, stderr, wait_threads|
    stdin.close
    out = Thread.new { read_lines(stdout, &with_output) }
    err = Thread.new { read_lines(stderr, &with_output) }
    [out.value, err.value, wait_threads.value]
  end
end

.read_lines(io) ⇒ Object

Internal: Reads and yield every line in the stream. Returns the full content.



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

def read_lines(io)
  buffer = +""
  while (line = io.gets)
    buffer << line
    yield line if block_given?
  end
  buffer
end