Class: Hiiro::Shell
- Inherits:
-
Object
- Object
- Hiiro::Shell
- Defined in:
- lib/hiiro/shell.rb
Class Method Summary collapse
- .capture_output(*command, **env) ⇒ Object
- .pipe(content, *command) ⇒ Object
- .pipe_lines(lines, *command) ⇒ Object
- .run(*command, **env) ⇒ Object
- .run3(*command, **env) ⇒ Object
- .run_combined(*command, **env) ⇒ Object
-
.stream(*command, tee: $stdout, **env) ⇒ Object
Stream stdout live to $stdout as chunks arrive, buffering for Result.
- .stream_combined(*command, tee: $stdout, **env) ⇒ Object
Class Method Details
.capture_output(*command, **env) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/hiiro/shell.rb', line 5 def self.capture_output(*command, **env) env = env.transform_keys(&:to_s).transform_values(&:to_s) stdout, status = Open3.capture2(env, *command) stdout end |
.pipe(content, *command) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/hiiro/shell.rb', line 18 def self.pipe(content, *command) selected, status = Open3.capture2(*command, stdin_data: content) return nil unless status.success? selected&.chomp end |
.pipe_lines(lines, *command) ⇒ Object
12 13 14 15 16 |
# File 'lib/hiiro/shell.rb', line 12 def self.pipe_lines(lines, *command) content = lines.is_a?(Array) ? lines.join("\n") : lines.to_s pipe(content, *command) end |
.run(*command, **env) ⇒ Object
26 27 28 29 30 |
# File 'lib/hiiro/shell.rb', line 26 def self.run(*command, **env) env = env.transform_keys(&:to_s).transform_values(&:to_s) stdout, status = Open3.capture2(env, *command) Result.new(stdout, status) end |
.run3(*command, **env) ⇒ Object
38 39 40 41 42 |
# File 'lib/hiiro/shell.rb', line 38 def self.run3(*command, **env) env = env.transform_keys(&:to_s).transform_values(&:to_s) stdout, stderr, status = Open3.capture3(env, *command) Result.new(stdout, status, stderr: stderr) end |
.run_combined(*command, **env) ⇒ Object
32 33 34 35 36 |
# File 'lib/hiiro/shell.rb', line 32 def self.run_combined(*command, **env) env = env.transform_keys(&:to_s).transform_values(&:to_s) output, status = Open3.capture2e(env, *command) Result.new(output, status) end |
.stream(*command, tee: $stdout, **env) ⇒ Object
Stream stdout live to $stdout as chunks arrive, buffering for Result. stderr passes through to the parent process (not captured).
~/bin/h-tds does this manually with popen2e for Chromatic:
Open3.popen2e('unbuffer pnpm chromatic') do |stdin, stdout_err, wait_thr|
stdin.close
loop do
chunk = stdout_err.readpartial(4096)
$stdout.write(chunk)
@output << chunk
rescue EOFError
break
end
@exit_status = wait_thr.value
end
stream_combined replaces that pattern — stderr merged in, one Result back.
62 63 64 65 66 67 68 |
# File 'lib/hiiro/shell.rb', line 62 def self.stream(*command, tee: $stdout, **env) env = env.transform_keys(&:to_s).transform_values(&:to_s) Open3.popen2(env, *command) do |stdin, stdout, wait_thr| stdin.close return Result.collect_chunks(stdout, wait_thr, tee: tee) end end |
.stream_combined(*command, tee: $stdout, **env) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/hiiro/shell.rb', line 70 def self.stream_combined(*command, tee: $stdout, **env) env = env.transform_keys(&:to_s).transform_values(&:to_s) Open3.popen2e(env, *command) do |stdin, stdout_err, wait_thr| stdin.close return Result.collect_chunks(stdout_err, wait_thr, tee: tee) end end |