Module: Utils

Defined in:
lib/utils.rb

Overview

Utility functions that are absent from Ruby standard library

Class Method Summary collapse

Class Method Details

.executable_exist?(name) ⇒ Boolean

Check whether provided binary name exists on PATH or default locations

Parameters:

  • binary (String)

    name

Returns:

  • (Boolean)


28
29
30
# File 'lib/utils.rb', line 28

def self.executable_exist?(name)
  !!find_executable(name)
end

.find_executable(binary) ⇒ String

Search on PATH or default locations for provided binary and return its fullpath

Parameters:

  • binary (String)

    name

Returns:

  • (String)

    full path to the binary file



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/utils.rb', line 9

def self.find_executable(binary)
  executable_file = proc { |name| next name if File.file?(name) && File.executable?(name) }

  # Retrieve PATH from ENV or use a fallback
  path = ENV['PATH']&.split(File::PATH_SEPARATOR) || %w[/usr/local/bin /usr/bin /bin]

  # check binary against each PATH
  path.each do |dir|
    file = File.expand_path(binary, dir)

    return file if executable_file.call(file)
  end

  nil
end