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

[Executable] Rewrite executable to use PTY.spawn.

parent 1e4c4ef3
...@@ -55,50 +55,67 @@ module Pod ...@@ -55,50 +55,67 @@ module Pod
command = command.map(&:to_s) command = command.map(&:to_s)
full_command = "#{bin} #{command.join(' ')}" full_command = "#{bin} #{command.join(' ')}"
if Config.instance.verbose? UI.message("$ #{full_command}") if Config.instance.verbose?
UI.message("$ #{full_command}")
stdout, stderr = Indenter.new(STDOUT), Indenter.new(STDERR) output = StringIO.new
else status = with_verbose do
stdout, stderr = Indenter.new, Indenter.new spawn(bin, command, Indenter.new(output))
end end
status = popen3(bin, command, stdout, stderr)
output = stdout.join("\n") + stderr.join("\n")
unless status.success? unless status.success?
if raise_on_failure if raise_on_failure
raise Informative, "#{full_command}\n\n#{output}" raise Informative, "#{full_command}\n\n#{output.string}"
else else
UI.message("[!] Failed: #{full_command}".red) UI.message("[!] Failed: #{full_command}".red)
end end
end end
output
output.string
end end
private private
def self.popen3(bin, command, stdout, stderr) def self.spawn bin, command, output
require 'open3' require 'pty'
Open3.popen3(bin, *command) do |i, o, e, t| PTY.spawn(bin, *command) do |r, _, pid|
Thread.new { while s = o.gets; stdout << s; end } begin
Thread.new { while s = e.gets; stderr << s; end } r.each { |line| output.write line }
i.close status = PTY.check(pid)
status = t.value return status if status
end
o.flush end
e.flush end
sleep(0.1)
# Yields to a block, redirecting STDOUT/STDERR if verbose output is used.
status #
def self.with_verbose &block
if Config.instance.verbose?
with_redirected(Indenter.new(STDOUT), Indenter.new(STDERR), &block)
else
block.call
end end
end end
# Redirects the output to the given stdout/stderr.
#
def self.with_redirected stdout, stderr
old_stdout = $stdout
old_stderr = $stderr
$stdout = stdout if stdout
$stderr = stderr if stderr
yield
ensure
$stdout = old_stdout
$stderr = old_stderr
end
#-------------------------------------------------------------------------# #-------------------------------------------------------------------------#
# Helper class that allows to write to an {IO} instance taking into account # Helper class that allows to write to a {IO} instance taking into account
# the UI indentation level. # the UI indentation level.
# #
class Indenter < ::Array class Indenter
# @return [Fixnum] The indentation level of the UI. # @return [Fixnum] The indentation level of the UI.
# #
attr_accessor :indent attr_accessor :indent
...@@ -109,7 +126,7 @@ module Pod ...@@ -109,7 +126,7 @@ module Pod
# @param [IO] io @see io # @param [IO] io @see io
# #
def initialize(io = nil) def initialize(io)
@io = io @io = io
@indent = ' ' * UI.indentation_level @indent = ' ' * UI.indentation_level
end end
...@@ -121,10 +138,16 @@ module Pod ...@@ -121,10 +138,16 @@ module Pod
# #
# @return [void] # @return [void]
# #
def <<(value) def write(value)
super @io.write "#{ indent }#{ value }"
ensure end
@io << "#{ indent }#{ value }" if @io
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