Module: Bindep::Helpers

Defined in:
lib/bindep/helpers.rb

Class Method Summary collapse

Class Method Details

.cmd(command, stdin = nil, raise_on_error = true) ⇒ Object

Run a command, supply stdin and capture stdout and stderr.



4
5
6
7
8
9
10
11
12
# File 'lib/bindep/helpers.rb', line 4

def self.cmd(command, stdin = nil, raise_on_error = true)
  stdout, stderr, status = Open3.capture3 command, stdin_data: stdin, binmode: true

  if raise_on_error && (status.exitstatus != 0 || stderr =~ /error/i)
    raise RuntimeError, "Command '#{command}' returned status #{status.exitstatus}"
  end

  [ stdout, stderr, status.exitstatus ]
end

.cmd_interactive(command, raise_on_error = true) ⇒ Object

Run a command interactively, return exit status



15
16
17
18
19
20
21
22
23
# File 'lib/bindep/helpers.rb', line 15

def self.cmd_interactive(command, raise_on_error = true)
  system command

  if raise_on_error && !$?.exitstatus.zero?
    raise RuntimeError, "Command '#{command}' returned status #{$?.exitstatus}"
  end

  $?.exitstatus
end

.command_exists?(cmd) ⇒ Boolean

Check if a shell command exists (using “which”).

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/bindep/helpers.rb', line 26

def self.command_exists?(cmd)
  _, _, status = Helpers.cmd "which #{cmd.strip}", nil, false
  status.zero?
end

.which_testObject

Check if “which” command is available and works correctly.



32
33
34
# File 'lib/bindep/helpers.rb', line 32

def self.which_test
  command_exists?("which") && !command_exists?("foo_bar_command_does_not_exist")
end