Module: Rya::CoreExtensions::File

Defined in:
lib/rya/core_extensions.rb

Instance Method Summary collapse

Instance Method Details

#command?(cmd) ⇒ Boolean, String

Check if a string specifies an executable command on the PATH.

Examples:

File.extend Rya::CoreExtensions::File
File.command? "ls" #=> "/bin/ls"
File.command? "arstoien" #=> nil
File.command? "/bin/ls" #=> "/bin/ls"

Parameters:

  • cmd

    The name of a command to check, or a path to an actual executable binary.

Returns:

  • (Boolean)

    nil if the cmd is not executable or it is not on the PATH.

  • (String)

    /path/to/cmd if the cmd is executable or is on the PATH.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rya/core_extensions.rb', line 37

def command? cmd
  return cmd if Object::File.executable? cmd

  exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]

  ENV["PATH"].split(Object::File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = Object::File.join path, "#{cmd}#{ext}"

      return exe if Object::File.executable?(exe) && !Object::File.directory?(exe)
    end
  end

  nil
end