Module: Pod::Executable
- Included in:
- Command::Lib::Create, Command::Repo, Command::Repo::Push, Command::Search, Command::Setup, Generator::BridgeSupport, Installer::PodSourcePreparer, SourcesManager
- Defined in:
- lib/cocoapods/executable.rb
Overview
Module which provides support for running executables.
In a class it can be used as:
extend Executable
executable :git
This will create two methods ‘git` and `git!` both accept a command but the later will raise on non successful executions. The methods return the output of the command.
Defined Under Namespace
Classes: Indenter
Class Method Summary collapse
-
.execute_command(executable, command, raise_on_failure) ⇒ String
Executes the given command displaying it if in verbose mode.
-
.which(program) ⇒ String, Nil
Returns the absolute path to the binary with the given name on the current ‘PATH`, or `nil` if none is found.
Instance Method Summary collapse
-
#executable(name) ⇒ void
Creates the methods for the executable with the given name.
Class Method Details
.execute_command(executable, command, raise_on_failure) ⇒ String
Find a way to display the live output of the commands.
Executes the given command displaying it if in verbose mode.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cocoapods/executable.rb', line 50 def self.execute_command(executable, command, raise_on_failure) bin = which(executable) raise Informative, "Unable to locate the executable `#{executable}`" unless bin require 'shellwords' command = command.map(&:to_s) full_command = "#{bin} #{command.join(' ')}" if Config.instance.verbose? UI.("$ #{full_command}") stdout, stderr = Indenter.new(STDOUT), Indenter.new(STDERR) else stdout, stderr = Indenter.new, Indenter.new end status = popen3(bin, command, stdout, stderr) output = stdout.join + stderr.join unless status.success? if raise_on_failure raise Informative, "#{full_command}\n\n#{output}" else UI.("[!] Failed: #{full_command}".red) end end output end |
.which(program) ⇒ String, Nil
Returns the absolute path to the binary with the given name on the current ‘PATH`, or `nil` if none is found.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cocoapods/executable.rb', line 87 def self.which(program) program = program.to_s ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| bin = File.(program, path) if File.file?(bin) && File.executable?(bin) return bin end end nil end |
Instance Method Details
#executable(name) ⇒ void
This method returns an undefined value.
Creates the methods for the executable with the given name.
21 22 23 24 25 26 27 28 29 |
# File 'lib/cocoapods/executable.rb', line 21 def executable(name) define_method(name) do |*command| Executable.execute_command(name, Array(command).flatten, false) end define_method(name.to_s + '!') do |*command| Executable.execute_command(name, Array(command).flatten, true) end end |