Module: TTY::Which
- Defined in:
- lib/tty/which.rb,
lib/tty/which/version.rb
Overview
A class responsible for finding an executable in the PATH
Constant Summary collapse
- VERSION =
"0.4.2"
Class Method Summary collapse
-
.executable_file?(filename, dir = nil) ⇒ Boolean
private
Determines if filename is an executable file.
-
.exist?(cmd, paths: search_paths) ⇒ Boolean
Check if executable exists in the path.
-
.extensions(path_ext = ENV['PATHEXT']) ⇒ Array[String]
private
All possible file extensions.
-
.file_with_exec_ext?(filename) ⇒ Boolean
private
Check if command itself has executable extension.
-
.file_with_path?(cmd) ⇒ Boolean
private
Check if executable file is part of absolute/relative path.
-
.search_paths(path = ENV['PATH']) ⇒ Array[String]
private
Find default system paths.
-
.which(cmd, paths: search_paths) ⇒ String?
Find an executable in a platform independent way.
Class Method Details
.executable_file?(filename, dir = nil) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines if filename is an executable file
126 127 128 129 130 |
# File 'lib/tty/which.rb', line 126 def executable_file?(filename, dir = nil) path = ::File.join(dir, filename) if dir path ||= filename ::File.file?(path) && ::File.executable?(path) end |
.exist?(cmd, paths: search_paths) ⇒ Boolean
Check if executable exists in the path
62 63 64 |
# File 'lib/tty/which.rb', line 62 def exist?(cmd, paths: search_paths) !which(cmd, paths: paths).nil? end |
.extensions(path_ext = ENV['PATHEXT']) ⇒ Array[String]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
All possible file extensions
103 104 105 106 |
# File 'lib/tty/which.rb', line 103 def extensions(path_ext = ENV['PATHEXT']) return [''] unless path_ext path_ext.split(::File::PATH_SEPARATOR).select { |part| part.include?('.') } end |
.file_with_exec_ext?(filename) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if command itself has executable extension
145 146 147 148 149 |
# File 'lib/tty/which.rb', line 145 def file_with_exec_ext?(filename) extension = ::File.extname(filename) return false if extension.empty? extensions.any? { |ext| extension.casecmp(ext).zero? } end |
.file_with_path?(cmd) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if executable file is part of absolute/relative path
160 161 162 |
# File 'lib/tty/which.rb', line 160 def file_with_path?(cmd) ::File.(cmd) == cmd end |
.search_paths(path = ENV['PATH']) ⇒ Array[String]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find default system paths
80 81 82 83 84 85 86 87 |
# File 'lib/tty/which.rb', line 80 def search_paths(path = ENV['PATH']) paths = if path && !path.empty? path.split(::File::PATH_SEPARATOR) else %w(/usr/local/bin /usr/ucb /usr/bin /bin) end paths.select(&Dir.method(:exist?)) end |
.which(cmd, paths: search_paths) ⇒ String?
Find an executable in a platform independent way
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tty/which.rb', line 27 def which(cmd, paths: search_paths) if file_with_path?(cmd) return cmd if executable_file?(cmd) extensions.each do |ext| exe = ::File.join(cmd, ext) return ::File.absolute_path(exe) if executable_file?(exe) end return nil end paths.each do |path| if file_with_exec_ext?(cmd) exe = ::File.join(path, cmd) return ::File.absolute_path(exe) if executable_file?(exe) end extensions.each do |ext| exe = ::File.join(path, "#{cmd}#{ext}") return ::File.absolute_path(exe) if executable_file?(exe) end end nil end |