Sha256: 5fc133a8c988149cd5e2c9dbbd22079088186b79fddb55e7f38f8c43ff1ff4f3

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

require 'fileutils'

module FileUtils

  if const_defined?(:Win32Exts)
    Win32Exts.concat %w{.exe .com .bat .cmd}
    Win32Exts.uniq!
  else
    Win32Exts = %w{.exe .com .bat .cmd}
  end

  # Looks for the first occurrence of _program_ within _path_.
  #
  # On the MS Windows platform, it looks for executables ending with .exe,
  # .bat and .com, which you may optionally include in the program name.
  # Returns <tt>nil</tt> if not found.
  #
  # CREDIT: Daniel J. Berger, Michael Granger
  #
  #--
  # The which() method was adopted from Daniel J. Berger, via PTools
  # which in in turn was adopted fromt the FileWhich code posted by
  # Michael Granger on http://www.rubygarden.org.
  #++
  def which(prog, path=ENV['PATH'])
    path.split(File::PATH_SEPARATOR).each {|dir|
      # Windows checks against specific extensions
      if File::ALT_SEPARATOR
        ext = Win32Exts.find{|ext|
          if prog.include?('.') # Assume extension already included
            f = File.join(dir,prog)
          else
            f = File.join(dir,prog+ext)
          end
          File.executable?(f) && !File.directory?(f)
        }
        if ext
          # Use backslashes, not forward slashes
          if prog.include?('.') # Assume extension already included
            f = File.join( dir, prog ).gsub(/\//,'\\')
          else
            f = File.join( dir, prog + ext ).gsub(/\//,'\\')
          end
          return f
        end
      else
        f = File.join(dir,prog)
        # Avoid /usr/lib/ruby, for example
        if File.executable?(f) && !File.directory?(f)
          return File::join( dir, prog )
        end
      end
    }
    nil
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.9.0 lib/more/facets/fileutils/which.rb
facets-2.9.0.pre.2 lib/more/facets/fileutils/which.rb
facets-2.9.0.pre.1 lib/more/facets/fileutils/which.rb