Module: Ferrum::Browser::Binary

Defined in:
lib/ferrum/browser/binary.rb

Overview

Locates an executable on the system PATH, mirroring what a shell's which/where would find. Used to resolve the browser (and Xvfb) binary when no explicit path is configured.

Class Method Summary collapse

Class Method Details

.all(commands) ⇒ Array<String>

Finds all executable paths for the given command(s) on PATH.

Parameters:

  • commands (String, Array<String>)

    Command name(s) to look up.

Returns:

  • (Array<String>)

    Absolute paths to matching executables.



35
36
37
# File 'lib/ferrum/browser/binary.rb', line 35

def all(commands)
  enum(commands).force
end

.enum(commands) ⇒ Enumerator::Lazy

Lazily enumerates executable paths for the given command(s) on PATH.

Parameters:

  • commands (String, Array<String>)

    Command name(s) to look up.

Returns:

  • (Enumerator::Lazy)

    Lazy enumerator yielding matching executable paths.



48
49
50
51
52
# File 'lib/ferrum/browser/binary.rb', line 48

def enum(commands)
  paths, exts = prepare_paths
  cmds = Array(commands).product(paths, exts)
  lazy_find(cmds)
end

.find(commands) ⇒ String?

Finds the first executable path for the given command(s) on PATH.

Parameters:

  • commands (String, Array<String>)

    Command name(s) to look up.

Returns:

  • (String, nil)

    Absolute path to the executable, or nil if none is found.



22
23
24
# File 'lib/ferrum/browser/binary.rb', line 22

def find(commands)
  enum(commands).first
end

.lazy_find(cmds) ⇒ Object

rubocop:disable Style/CollectionCompact



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ferrum/browser/binary.rb', line 72

def lazy_find(cmds)
  cmds.lazy.map do |cmd, path, ext|
    absolute_path = File.absolute_path(cmd)
    is_absolute_path = absolute_path == cmd
    cmd = File.expand_path("#{cmd}#{ext}", path) unless is_absolute_path

    next unless File.executable?(cmd)
    next if File.directory?(cmd)

    cmd
  end.reject(&:nil?) # .compact isn't defined on Enumerator::Lazy
end

.prepare_pathsArray(Array<String>, Array<String>)

Directories on PATH and the executable extensions to try against them.

Returns:

  • (Array(Array<String>, Array<String>))

    The PATH directories, and the extensions from PATHEXT (plus "").

Raises:



63
64
65
66
67
68
69
# File 'lib/ferrum/browser/binary.rb', line 63

def prepare_paths
  exts = (ENV.key?("PATHEXT") ? ENV.fetch("PATHEXT").split(";") : []) << ""
  paths = ENV["PATH"].split(File::PATH_SEPARATOR)
  raise EmptyPathError if paths.empty?

  [paths, exts]
end