Class: Hbc::SystemCommand

Inherits:
Object
  • Object
show all
Extended by:
Predicable
Defined in:
Library/Homebrew/cask/lib/hbc/system_command.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Predicable

attr_predicate

Constructor Details

#initialize(executable, args: [], sudo: false, input: [], print_stdout: false, print_stderr: true, must_succeed: false, **options) ⇒ SystemCommand

Returns a new instance of SystemCommand



40
41
42
43
44
45
46
47
48
49
50
# File 'Library/Homebrew/cask/lib/hbc/system_command.rb', line 40

def initialize(executable, args: [], sudo: false, input: [], print_stdout: false, print_stderr: true, must_succeed: false, **options)
  @executable = executable
  @args = args
  @sudo = sudo
  @input = input
  @print_stdout = print_stdout
  @print_stderr = print_stderr
  @must_succeed = must_succeed
  options.extend(HashValidator).assert_valid_keys(:chdir)
  @options = options
end

Class Method Details

.run(executable, **options) ⇒ Object



13
14
15
# File 'Library/Homebrew/cask/lib/hbc/system_command.rb', line 13

def self.run(executable, **options)
  new(executable, **options).run!
end

.run!(command, **options) ⇒ Object



17
18
19
# File 'Library/Homebrew/cask/lib/hbc/system_command.rb', line 17

def self.run!(command, **options)
  run(command, **options, must_succeed: true)
end

Instance Method Details

#commandObject



52
53
54
# File 'Library/Homebrew/cask/lib/hbc/system_command.rb', line 52

def command
  [*sudo_prefix, executable, *args]
end

#run!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'Library/Homebrew/cask/lib/hbc/system_command.rb', line 21

def run!
  @processed_output = { stdout: "", stderr: "" }
  odebug "Executing: #{expanded_command}"

  each_output_line do |type, line|
    case type
    when :stdout
      processed_output[:stdout] << line
      ohai line.chomp if print_stdout?
    when :stderr
      processed_output[:stderr] << line
      ohai line.chomp if print_stderr?
    end
  end

  assert_success if must_succeed?
  result
end