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
-
.all(commands) ⇒ Array<String>
Finds all executable paths for the given command(s) on
PATH. -
.enum(commands) ⇒ Enumerator::Lazy
Lazily enumerates executable paths for the given command(s) on
PATH. -
.find(commands) ⇒ String?
Finds the first executable path for the given command(s) on
PATH. -
.lazy_find(cmds) ⇒ Object
rubocop:disable Style/CollectionCompact.
-
.prepare_paths ⇒ Array(Array<String>, Array<String>)
Directories on
PATHand the executable extensions to try against them.
Class Method Details
.all(commands) ⇒ Array<String>
Finds all executable paths for the given command(s) on PATH.
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.
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.
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.("#{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_paths ⇒ Array(Array<String>, Array<String>)
Directories on PATH and the executable extensions to try against them.
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 |