From 66c0b06d72bb55e1ac288a69336ae858095e5c32 Mon Sep 17 00:00:00 2001 From: Xu Cheng Date: Sat, 2 Jan 2016 23:08:51 +0800 Subject: 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`. --- Library/Homebrew/requirement.rb | 4 ++++ Library/Homebrew/utils.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) (limited to 'Library') diff --git a/Library/Homebrew/requirement.rb b/Library/Homebrew/requirement.rb index 91eedd72d..0aa4ed78d 100644 --- a/Library/Homebrew/requirement.rb +++ b/Library/Homebrew/requirement.rb @@ -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 diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 50e226faa..a709827a5 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -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? -- cgit v1.2.3