Sha256: eaf1ffb563fbc9427096bfbbe92f6c03426887bf6da2b598ed98f46d103ec264

Contents?: true

Size: 1.36 KB

Versions: 19

Compression:

Stored size: 1.36 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.
      # @return [String] The full path to the executable or `nil` if not found.
      def self.which(cmd)
        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

        ENV['PATH'].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

        return nil
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 6 rubygems

Version Path
tamtam-vagrant-reload-1.1.3 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/which.rb
tamtam-vagrant-reload-1.1.2 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/which.rb
tamtam-vagrant-reload-1.1.1 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/which.rb
tamtam-vagrant-reload-1.1 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/which.rb
tnargav-1.3.6 lib/vagrant/util/which.rb
tnargav-1.3.3 lib/vagrant/util/which.rb
vagrant-shell-0.2.9 demo/templates/vendor/bundle/ruby/1.9.1/gems/tnargav-1.2.2/lib/vagrant/util/which.rb
tnargav-1.2.3 lib/vagrant/util/which.rb
vagrant-shell-0.2.8 demo/templates/vendor/bundle/ruby/1.9.1/gems/tnargav-1.2.2/lib/vagrant/util/which.rb
vagrant-shell-0.2.6 vendor/bundle/gems/tnargav-1.2.2/lib/vagrant/util/which.rb
vagrant-shell-0.2.5 vendor/bundle/gems/tnargav-1.2.2/lib/vagrant/util/which.rb
tnargav-1.2.2 lib/vagrant/util/which.rb
vagrantup-1.1.3 lib/vagrant/util/which.rb
vagrantup-1.1.2 lib/vagrant/util/which.rb
vagrantup-1.1.1 lib/vagrant/util/which.rb
vagrantup-1.1.0 lib/vagrant/util/which.rb
vagrantup-1.1.4 lib/vagrant/util/which.rb
vagrant-actionio-0.0.9 vendor/bundle/bundler/gems/vagrant-c74251a1d9c0/lib/vagrant/util/which.rb
vagrant-lxc-0.0.1 vendor/vagrant/lib/vagrant/util/which.rb