Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/fedux_org_stdlib/core_ext/file/which.rb
Overview
File class
Constant Summary collapse
- MSWINDOWS =
false
- WIN32EXTS =
'.{exe,com,bat}'
Class Method Summary collapse
-
.which(program, path = ENV['PATH']) ⇒ String, NilClass
Search program in search path.
Class Method Details
.which(program, path = ENV['PATH']) ⇒ String, NilClass
Search program in search path
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/fedux_org_stdlib/core_ext/file/which.rb', line 33 def which(program, path = ENV['PATH']) fail ArgumentError, path if path.blank? return nil if program.blank? # Bail out early if an absolute path is provided. if program =~ %r{^/|^[a-z]:[/]}i program += WIN32EXTS if MSWINDOWS && File.extname(program).blank? 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.(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).blank? 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 |