Added mock build. Part 5
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
require "open3"
|
||||
require "logger"
|
||||
|
||||
class Runner
|
||||
attr_reader :cmd, :exit_status, :stdout, :stderr, :pid
|
||||
attr_reader :cmd, :exit_status, :stdout, :stderr, :pid, :log
|
||||
|
||||
# Run a command, return runner instance
|
||||
# @param cmd [String,Array<String>] command to execute
|
||||
@@ -26,11 +27,12 @@ class Runner
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
# @param cmd [String,Array<String>] command to execute
|
||||
def initialize(cmd)
|
||||
def initialize(cmd, log = nil)
|
||||
@cmd = cmd.is_a?(Array) ? cmd.join(" ") : cmd
|
||||
@stdout = +""
|
||||
@stderr = +""
|
||||
@exit_status = nil
|
||||
@log = log
|
||||
end
|
||||
|
||||
# @return [Boolean] success or failure?
|
||||
@@ -58,10 +60,54 @@ class Runner
|
||||
|
||||
if stream == stdout
|
||||
@stdout << data
|
||||
$stdout.write(data)
|
||||
if log.nil?
|
||||
$stdout.write(data)
|
||||
else
|
||||
log.info(data)
|
||||
end
|
||||
else
|
||||
@stderr << data
|
||||
$stderr.write(data)
|
||||
if log.nil?
|
||||
$stderr.write(data)
|
||||
else
|
||||
log.error(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@exit_status = wait_thr.value.exitstatus
|
||||
@pid = wait_thr.pid
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
# Run the command no output, return self
|
||||
# @return [Runner]
|
||||
def run_clean
|
||||
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
|
||||
until [stdout, stderr].all?(&:eof?)
|
||||
readable = IO.select([stdout, stderr])
|
||||
next unless readable&.first
|
||||
|
||||
readable.first.each do |stream|
|
||||
data = +""
|
||||
# rubocop:disable Lint/HandleExceptions
|
||||
begin
|
||||
stream.read_nonblock(1024, data)
|
||||
rescue EOFError
|
||||
# ignore, it's expected for read_nonblock to raise EOFError
|
||||
# when all is read
|
||||
end
|
||||
|
||||
if stream == stdout
|
||||
unless log.nil?
|
||||
log.info(data)
|
||||
end
|
||||
else
|
||||
unless log.nil?
|
||||
log.error(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user