Class: Vagrant::Util::Which

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/which.rb

Class Method Summary collapse

Class Method Details

.which(cmd, **opts) ⇒ String

Cross-platform way of finding an executable in the PATH.

which('ruby') #=> /usr/bin/ruby

This code is adapted from the following post by mislav: http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby

Parameters:

  • cmd (String)

    The command to search for in the PATH.

  • opts (Hash)

    Optional flags @option [Boolean] :original_path Search within original path if available

Returns:

  • (String)

    The full path to the executable or nil if not found.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant/util/which.rb', line 17

def self.which(cmd, **opts)
  exts = nil

  if !Platform.windows? || ENV['PATHEXT'].nil?
    # If the PATHEXT variable is empty, we're on *nix and need to find
    # the exact filename
    exts = ['']
  elsif File.extname(cmd).length != 0
    # On Windows: if filename contains an extension, we must match that
    # exact filename
    exts = ['']
  else
    # On Windows: otherwise try to match all possible executable file
    # extensions (.EXE .COM .BAT etc.)
    exts = ENV['PATHEXT'].split(';')
  end

  if opts[:original_path]
    search_path = ENV.fetch('VAGRANT_OLD_ENV_PATH', ENV['PATH'])
  else
    search_path = ENV['PATH']
  end

  SilenceWarnings.silence! do
    search_path.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').split(File::PATH_SEPARATOR).each do |path|
      exts.each do |ext|
        exe = "#{path}#{File::SEPARATOR}#{cmd}#{ext}"
        return exe if File.executable? exe
      end
    end
  end

  return nil
end