Sha256: c11b59b8d557c349646aebfa2d38598bebf771cc5645b3aa680399f4615c1ea4

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

# encoding: utf-8
require 'rbconfig'
require 'win32/file' if File::ALT_SEPARATOR

# File class
class File
  if File::ALT_SEPARATOR
    MSWINDOWS = true
    if ENV['PATHEXT']
      WIN32EXTS = ('.{' + ENV['PATHEXT'].tr(';', ',').tr('.', '') + '}').downcase
    else
      WIN32EXTS = '.{exe,com,bat}'
    end
  else
    MSWINDOWS = false
  end

  class << self
    def which(program, path = ENV['PATH'])
      fail ArgumentError, 'path cannot be empty' if path.nil? || path.empty?

      # Bail out early if an absolute path is provided.
      if program =~ %r{^/|^[a-z]:[/]}i
        program += WIN32EXTS if MSWINDOWS && File.extname(program).empty?
        found = Dir[program].first
        if found && File.executable?(found) && !File.directory?(found)
          return found
        else
          return nil
        end
      end

      # Iterate over each path glob the dir + program.
      path.split(File::PATH_SEPARATOR).each do |dir|
        dir = File.expand_path(dir)

        next unless File.exist?(dir) # In case of bogus second argument
        file = File.join(dir, program)

        # Dir[] doesn't handle backslashes properly, so convert them. Also, if
        # the program name doesn't have an extension, try them all.
        if MSWINDOWS
          file = file.tr('\\', '/')
          file += WIN32EXTS if File.extname(program).empty?
        end

        found = Dir[file].first

        next if !found || !File.executable?(found) || File.directory?(found)

        # Convert all forward slashes to backslashes if supported
        found.tr!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR

        return found
      end

      nil
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
middleman-presentation-0.15.8 lib/middleman-presentation/file_extensions.rb
middleman-presentation-0.15.7 lib/middleman-presentation/file_extensions.rb
middleman-presentation-0.15.6 lib/middleman-presentation/file_extensions.rb
middleman-presentation-0.15.5 lib/middleman-presentation/file_extensions.rb