Commit ea8ff3cc authored by Florian R. Hanke's avatar Florian R. Hanke

[Executable] Handle output outside of #spawn.

parent e26766c3
...@@ -59,8 +59,11 @@ module Pod ...@@ -59,8 +59,11 @@ module Pod
UI.message("$ #{full_command}") if Config.instance.verbose? UI.message("$ #{full_command}") if Config.instance.verbose?
output = StringIO.new output = StringIO.new
indented = Indenter.new(output)
status = with_verbose do status = with_verbose do
spawn(bin, command, Indenter.new(output)) spawn(bin, command) do |line|
indented << line
end
end end
unless status.success? unless status.success?
...@@ -76,11 +79,18 @@ module Pod ...@@ -76,11 +79,18 @@ module Pod
private private
def self.spawn bin, command, output # Spawn a subprocess.
#
# @param bin [String] Name of the binary to use.
# @param arguments [Array<String>] Command arguments.
#
# @yield [String] Each output line.
#
def self.spawn bin, arguments, &block
require 'pty' require 'pty'
PTY.spawn(bin, *command) do |r, _, pid| PTY.spawn(bin, *arguments) do |r, _, pid|
begin begin
r.each { |line| output.write line } r.each(&block) if block_given?
status = PTY.check(pid) status = PTY.check(pid)
return status if status return status if status
end end
...@@ -89,6 +99,8 @@ module Pod ...@@ -89,6 +99,8 @@ module Pod
# Yields to a block, redirecting STDOUT/STDERR if verbose output is used. # Yields to a block, redirecting STDOUT/STDERR if verbose output is used.
# #
# @yield [Array<Indenter, Indenter>] If verbose, will yield an indenter.
#
def self.with_verbose &block def self.with_verbose &block
if Config.instance.verbose? if Config.instance.verbose?
with_redirected(Indenter.new(STDOUT), Indenter.new(STDERR), &block) with_redirected(Indenter.new(STDOUT), Indenter.new(STDERR), &block)
...@@ -99,6 +111,8 @@ module Pod ...@@ -99,6 +111,8 @@ module Pod
# Redirects the output to the given stdout/stderr. # Redirects the output to the given stdout/stderr.
# #
# @yield [void]
#
def self.with_redirected stdout, stderr def self.with_redirected stdout, stderr
old_stdout = $stdout old_stdout = $stdout
old_stderr = $stderr old_stderr = $stderr
...@@ -138,16 +152,8 @@ module Pod ...@@ -138,16 +152,8 @@ module Pod
# #
# @return [void] # @return [void]
# #
def write(value) def <<(value)
@io.write "#{ indent }#{ value }" @io << "#{ indent }#{ value }"
end
def string
@io.string
end
def flush
# We ignore calls to flush.
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment