Commit 811f7eff authored by Samuel E. Giddins's avatar Samuel E. Giddins

[Executable] Add a safe replacement for backticks

parent 5f9e19c1
...@@ -20,11 +20,15 @@ module Pod ...@@ -20,11 +20,15 @@ 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) Executable.execute_command(name, Array(command).flatten, false, :message => true)
end end
define_method(name.to_s + '!') do |*command| define_method(name.to_s + '!') do |*command|
Executable.execute_command(name, Array(command).flatten, true) Executable.execute_command(name, Array(command).flatten, true, :message => true)
end
define_method(name.to_s + '?') do |*command|
Executable.execute_command(name, Array(command).flatten, false)
end end
end end
...@@ -45,18 +49,14 @@ module Pod ...@@ -45,18 +49,14 @@ module Pod
# #
# @return [String] the output of the command (STDOUT and STDERR). # @return [String] the output of the command (STDOUT and STDERR).
# #
# @todo Find a way to display the live output of the commands. def self.execute_command(executable, command, raise_on_failure = true, message: false, output: :both)
#
def self.execute_command(executable, command, raise_on_failure)
bin = which(executable) bin = which(executable)
raise Informative, "Unable to locate the executable `#{executable}`" unless bin raise Informative, "Unable to locate the executable `#{executable}`" if bin.empty?
require 'shellwords'
command = command.map(&:to_s) command = command.map(&:to_s)
full_command = "#{bin} #{command.join(' ')}" full_command = "#{bin} #{command.join(' ')}"
if Config.instance.verbose? if message && 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
...@@ -64,15 +64,20 @@ module Pod ...@@ -64,15 +64,20 @@ module Pod
end end
status = popen3(bin, command, stdout, stderr) status = popen3(bin, command, stdout, stderr)
output = stdout.join + stderr.join stdout, stderr = stdout.join, stderr.join
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#{stdout + stderr}"
else elsif message
UI.message("[!] Failed: #{full_command}".red) UI.message("[!] Failed: #{full_command}".red)
end end
end end
output
case 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
......
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