Sha256: ba9762c7485c2edaa196747ebdced5fc0d4ead13db904592387342722fda3e7a

Contents?: true

Size: 1.81 KB

Versions: 15

Compression:

Stored size: 1.81 KB

Contents

require 'fileutils'

module FileUtils

  if defined?(Win32Exts)
    Win32Exts |= %w{.exe .com .bat .cmd}
  else
    Win32Exts = %w{.exe .com .bat .cmd}
  end

  # In block form, yields each ((*program*)) within ((*path*)).  In non-block
  # form, returns an array of each ((*program*)) within ((*path*)).  Returns
  # (({nil})) if not found.
  #
  # On the MS Windows platform, it looks for executables ending with .exe,
  # .bat and .com, which you may optionally include in the program name.
  #
  #    FileUtils.whereis("ruby") -> ['/usr/local/bin/ruby','/opt/bin/ruby']
  #
  # CREDIT Daniel J. Berger

  module_function
  def whereis(prog, path=ENV['PATH']) #:yield:
    dirs = []
    path.split(File::PATH_SEPARATOR).each{|dir|
        # Windows checks against specific extensions
        if File::ALT_SEPARATOR
          if prog.include?('.')
              f = File.join(dir,prog)
              if File.executable?(f) && !File.directory?(f)
                if block_given?
                    yield f.gsub(/\//,'\\')
                else
                    dirs << f.gsub(/\//,'\\')
                end
              end
          else
              Win32Exts.find_all{|ext|
                f = File.join(dir,prog+ext)
                if File.executable?(f) && !File.directory?(f)
                    if block_given?
                      yield f.gsub(/\//,'\\')
                    else
                      dirs << f.gsub(/\//,'\\')
                    end
                end
              }
          end
        else
          f = File.join(dir,prog)
          # Avoid /usr/lib/ruby, for example
          if File.executable?(f) && !File.directory?(f)
              if block_given?
                yield f
              else
                dirs << f
              end
          end
        end
    }
    dirs.empty? ? nil : dirs
  end

end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
facets-2.8.4 lib/more/facets/fileutils/whereis.rb
facets-2.8.3 lib/more/facets/fileutils/whereis.rb
facets-2.8.2 lib/more/facets/fileutils/whereis.rb
facets-2.8.1 lib/more/facets/fileutils/whereis.rb
facets-2.8.0 lib/more/facets/fileutils/whereis.rb
facets-2.7.0 lib/more/facets/fileutils/whereis.rb
facets-2.6.0 lib/lore/facets/fileutils/whereis.rb
facets-2.4.4 lib/lore/facets/fileutils/whereis.rb
facets-2.4.3 lib/lore/facets/fileutils/whereis.rb
facets-2.4.2 lib/lore/facets/fileutils/whereis.rb
facets-2.5.0 lib/lore/facets/fileutils/whereis.rb
facets-2.4.5 lib/lore/facets/fileutils/whereis.rb
facets-2.5.1 lib/lore/facets/fileutils/whereis.rb
facets-2.5.2 lib/lore/facets/fileutils/whereis.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/lore/facets/fileutils/whereis.rb