which: allow <formulae> args

Also clean-up this external command in preparation
for becoming a built-in command.

Make which_versions available in Homebrew module so
that it can be used by other commands in the future.
This commit is contained in:
Adam Vandenberg 2011-06-13 10:26:20 -07:00
parent f409e4f22f
commit 2b9c6def6d

View File

@ -1,19 +1,13 @@
require 'extend/pathname' require 'extend/pathname'
REAL_CELLAR = HOMEBREW_CELLAR.realpath
class String module Homebrew extend self
def starts_with? prefix def which_versions which_brews=nil
prefix = prefix.to_s
self[0, prefix.length] == prefix
end
end
def audit
brew_links = Array.new brew_links = Array.new
version_map = Hash.new version_map = Hash.new
real_cellar = HOMEBREW_CELLAR.realpath
# paths=%w[bin sbin etc lib include share].collect {|d| HOMEBREW_PREFIX+d} # paths=%w[bin sbin etc lib include share].collect {|d| HOMEBREW_PREFIX+d}
paths=%w[bin].collect {|d| HOMEBREW_PREFIX+d} paths=%w[bin].collect {|d| HOMEBREW_PREFIX+d}
@ -24,23 +18,33 @@ def audit
end end
end end
brew_links = brew_links.collect{|p|p.relative_path_from(REAL_CELLAR).to_s}.reject{|p|p.starts_with?("../")} brew_links = brew_links.collect{|p|p.relative_path_from(real_cellar).to_s}.reject{|p|p.start_with?("../")}
brew_links.each do |p| brew_links.each do |p|
parts = p.split("/") parts = p.split("/")
next if parts.count < 2 # Shouldn't happen next if parts.count < 2 # Shouldn't happen for normally installed brews
brew = parts.shift brew = parts.shift
version = parts.shift version = parts.shift
next unless which_brews.include? brew if which_brews
versions = version_map[brew] || [] versions = version_map[brew] || []
versions << version unless versions.include? version versions << version unless versions.include? version
version_map[brew] = versions version_map[brew] = versions
end end
return version_map return version_map
end
def which
which_brews = ARGV.named.empty? ? nil : ARGV.named
brews = which_versions which_brews
brews.keys.sort.each do |b|
puts "#{b}: #{brews[b].sort*' '}"
end
puts
end
end end
brews = audit Homebrew.which
brews.keys.sort.each do |b|
puts "#{b}: #{brews[b].sort*' '}"
end
puts