Commit 6f3230b3 authored by Samuel E. Giddins's avatar Samuel E. Giddins

[Executable] Add capture_command method

parent 0d44a527
...@@ -125,7 +125,7 @@ module Pod ...@@ -125,7 +125,7 @@ module Pod
class Base class Base
override_api do override_api do
def execute_command(executable, command, raise_on_failure = false) def execute_command(executable, command, raise_on_failure = false)
Executable.execute_command(executable, command, raise_on_failure, :message => true) Executable.execute_command(executable, command, raise_on_failure)
rescue CLAide::InformativeError => e rescue CLAide::InformativeError => e
raise DownloaderError, e.message raise DownloaderError, e.message
end end
......
...@@ -20,15 +20,11 @@ module Pod ...@@ -20,15 +20,11 @@ module Pod
# #
def executable(name) def executable(name)
define_method(name) do |*command| define_method(name) do |*command|
Executable.execute_command(name, Array(command).flatten, false, :message => true) Executable.execute_command(name, Array(command).flatten, false)
end end
define_method(name.to_s + '!') do |*command| define_method(name.to_s + '!') do |*command|
Executable.execute_command(name, Array(command).flatten, true, :message => true) Executable.execute_command(name, Array(command).flatten, true)
end
define_method(name.to_s + '?') do |*command|
Executable.execute_command(name, Array(command).flatten, false)
end end
end end
...@@ -49,14 +45,14 @@ module Pod ...@@ -49,14 +45,14 @@ module Pod
# #
# @return [String] the output of the command (STDOUT and STDERR). # @return [String] the output of the command (STDOUT and STDERR).
# #
def self.execute_command(executable, command, raise_on_failure = true, message: false, output: :both) def self.execute_command(executable, command, raise_on_failure = true)
bin = which(executable) bin = which(executable)
raise Informative, "Unable to locate the executable `#{executable}`" if bin.empty? raise Informative, "Unable to locate the executable `#{executable}`" unless bin
command = command.map(&:to_s) command = command.map(&:to_s)
full_command = "#{bin} #{command.join(' ')}" full_command = "#{bin} #{command.join(' ')}"
if message && Config.instance.verbose? if Config.instance.verbose?
UI.message("$ #{full_command}") UI.message("$ #{full_command}")
stdout, stderr = Indenter.new(STDOUT), Indenter.new(STDERR) stdout, stderr = Indenter.new(STDOUT), Indenter.new(STDERR)
else else
...@@ -65,19 +61,16 @@ module Pod ...@@ -65,19 +61,16 @@ module Pod
status = popen3(bin, command, stdout, stderr) status = popen3(bin, command, stdout, stderr)
stdout, stderr = stdout.join, stderr.join stdout, stderr = stdout.join, stderr.join
output = stdout + stderr
unless status.success? unless status.success?
if raise_on_failure if raise_on_failure
raise Informative, "#{full_command}\n\n#{stdout + stderr}" raise Informative, "#{full_command}\n\n#{output}"
elsif message else message
UI.message("[!] Failed: #{full_command}".red) UI.message("[!] Failed: #{full_command}".red)
end end
end end
case output output
when :stdout then stdout
when :stderr then stderr
else stdout + stderr
end
end end
# Returns the absolute path to the binary with the given name on the current # Returns the absolute path to the binary with the given name on the current
...@@ -100,6 +93,37 @@ module Pod ...@@ -100,6 +93,37 @@ module Pod
nil nil
end end
# Runs the given command, capturing the desired output.
#
# @param [String] bin
# The binary to use.
#
# @param [Array<#to_s>] command
# The command to send to the binary.
#
# @param [Symbol] capture
# Whether it should raise if the command fails.
#
# @raise If the executable could not be located.
#
# @return [(String, Process::Status)]
# The desired captured output from the command, and the status from
# running the command.
#
def capture_command(executable, command, capture: :merge)
bin = which(executable)
raise Informative, "Unable to locate the executable `#{executable}`" unless bin
require 'open3'
case capture
when :merge then Open3.capture2e(bin, *command)
when :both then Open3.capture3(bin, *command)
when :out then Open3.capture2(bin, *command)
when :err then Open3.capture3(bin, *command).drop(1)
when :none then Open3.capture2(bin, *command).last
end
end
private private
def self.popen3(bin, command, stdout, stderr) def self.popen3(bin, command, stdout, stderr)
......
...@@ -337,7 +337,8 @@ module Pod ...@@ -337,7 +337,8 @@ module Pod
def dynamic_binary?(binary) def dynamic_binary?(binary)
return unless binary.file? return unless binary.file?
Executable.execute_command('file', [binary], false) =~ /dynamically linked/ output, status = Executable.capture_command('file', [binary], :capture => :out)
status.success? && output =~ /dynamically linked/
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