utils: add which_all

Similar to which, except it returns all of paths where binary is found.
i.e. it's equivalent to `which -a`.
This commit is contained in:
Xu Cheng 2016-01-02 23:08:51 +08:00
parent f29699f77c
commit 66c0b06d72
2 changed files with 17 additions and 0 deletions

View File

@ -134,6 +134,10 @@ class Requirement
super(cmd, ORIGINAL_PATHS.join(File::PATH_SEPARATOR))
end
def which_all(cmd)
super(cmd, ORIGINAL_PATHS.join(File::PATH_SEPARATOR))
end
class << self
include BuildEnvironmentDSL

View File

@ -361,6 +361,19 @@ def which(cmd, path = ENV["PATH"])
nil
end
def which_all(cmd, path = ENV["PATH"])
path.split(File::PATH_SEPARATOR).map do |p|
begin
pcmd = File.expand_path(cmd, p)
rescue ArgumentError
# File.expand_path will raise an ArgumentError if the path is malformed.
# See https://github.com/Homebrew/homebrew/issues/32789
next
end
Pathname.new(pcmd) if File.file?(pcmd) && File.executable?(pcmd)
end.compact.uniq
end
def which_editor
editor = ENV.values_at("HOMEBREW_EDITOR", "VISUAL", "EDITOR").compact.first
return editor unless editor.nil?