Module: TTY::Which
- Defined in:
- lib/searchlink/which.rb,
lib/searchlink/which.rb
Overview
A class responsible for finding an executable in the PATH
Constant Summary collapse
- VERSION =
"0.5.0"
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
135 136 137 138 139 |
# File 'lib/searchlink/which.rb', line 135 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
70 71 72 |
# File 'lib/searchlink/which.rb', line 70 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
111 112 113 114 115 |
# File 'lib/searchlink/which.rb', line 111 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
154 155 156 157 158 159 |
# File 'lib/searchlink/which.rb', line 154 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
170 171 172 |
# File 'lib/searchlink/which.rb', line 170 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
88 89 90 91 92 93 94 95 |
# File 'lib/searchlink/which.rb', line 88 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 /opt/homebrew/bin] end paths.select(&Dir.method(:exist?)) end |
.which(cmd, paths: search_paths) ⇒ String?
Find an executable in a platform independent way
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/searchlink/which.rb', line 34 def which(cmd, paths: search_paths) if file_with_path?(cmd) return cmd if executable_file?(cmd) extensions.each do |ext| exe = "#{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 |