Sha256: 6457f597120c8f27441d25dd6aaba906c2050178631618e58f40c98669402f4d

Contents?: true

Size: 1.78 KB

Versions: 36

Compression:

Stored size: 1.78 KB

Contents

require "vagrant/util/platform"

module Vagrant
  module Util
    class Which
      # 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
      #
      # @param [String] cmd The command to search for in the PATH.
      # @param [Hash] opts Optional flags
      #   @option [Boolean] :original_path Search within original path if available
      # @return [String] The full path to the executable or `nil` if not found.
      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
    end
  end
end

Version data entries

36 entries across 32 versions & 5 rubygems

Version Path
vagrant-unbundled-2.3.6.0 lib/vagrant/util/which.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/lib/vagrant/util/which.rb
vagrant-unbundled-2.3.3.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.3.2.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.19.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.18.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.16.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.14.0 lib/vagrant/util/which.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/lib/vagrant/util/which.rb
vagrant-unbundled-2.2.10.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.9.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.8.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.7.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.6.2 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.6.1 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.6.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.5.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.4.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.3.0 lib/vagrant/util/which.rb
vagrant-unbundled-2.2.2.0 lib/vagrant/util/which.rb