Sha256: a2084eba723d50678843d257a084e0d891bf34b6ab756e81215db2cec1d5acb3

Contents?: true

Size: 805 Bytes

Versions: 1

Compression:

Stored size: 805 Bytes

Contents

module Acouchi
  class Which
    def self.find_executable *aliases
      if executable = aliases.find {|a| which? a}
        executable
      else
        raise %{Couldn't find any matches for the aliases "#{aliases.join(", ")}"}
      end
    end

    private
      def self.which? command
        ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
          exe = File.join(path, "#{command}")
          return exe if executable? exe
        end
        return nil
      end

      def self.executable? file
        return true if File.executable?(file)
        extensions = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : []
        File.exist?(file) &&
          File.file?(file) &&
          extensions.any? &&
          extensions.any? {|e| file.downcase.end_with?(e.downcase)}
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acouchi-0.0.8 lib/acouchi/which.rb