Method: Howzit::Util.command_exist?

Defined in:
lib/howzit/util.rb

.command_exist?(command) ⇒ Boolean

Test if external command exists

Parameters:

  • command (String)

    The command

Returns:

  • (Boolean)

    command exists



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/howzit/util.rb', line 40

def command_exist?(command)
  exts = ENV.fetch('PATHEXT', '').split(::File::PATH_SEPARATOR)
  command = File.expand_path(command) if command =~ /^~/
  if Pathname.new(command).absolute?
    ::File.exist?(command) || exts.any? { |ext| ::File.exist?("#{command}#{ext}") }
  else
    ENV.fetch('PATH', '').split(::File::PATH_SEPARATOR).any? do |dir|
      file = ::File.join(dir, command)
      ::File.exist?(file) || exts.any? { |ext| ::File.exist?("#{file}#{ext}") }
    end
  end
end