6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/shellator.rb', line 6
def self.noninteractive(cmd, in_directory: nil, stdin_content: [], stdin_newlines: true, stdout:, stderr:)
raise "cmd must be a String or array of Strings." \
unless cmd.is_a?(String) || (cmd.is_a?(Array) && cmd.all? { |c| c.is_a?(Stirng) })
raise "stdout must be a Proc." unless stdout.is_a?(Proc)
raise "stderr must be a Proc." unless stderr.is_a?(Proc)
stdin_content = [stdin_content].flatten
raise "stdin_content must be an Array of Strings." \
unless stdin_content.is_a?(Array) && stdin_content.all? { |c| c.is_a?(String) }
computed_cmd = [cmd].flatten.join(" ")
Dir.chdir(in_directory || Dir.pwd) do
Open3.popen3(computed_cmd) do |stdin_stream, stdout_stream, stderr_stream, thread|
stdin_content.each do |input_line|
stdin_stream.write(input_line)
stdin_stream.write("\n") unless input_line[-1] == "\n" || !stdin_newlines
end
stdin_stream.close
{ stdout_stream => stdout, stderr_stream => stderr }.each_pair do |stream, callback|
Thread.new do
until (line = stream.gets).nil?
callback.call(line)
end
end
end
thread.join
thread.value
end
end
end
|