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,46 +1,50 @@
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 brew_links = Array.new
self[0, prefix.length] == prefix version_map = Hash.new
end
end
real_cellar = HOMEBREW_CELLAR.realpath
def audit # paths=%w[bin sbin etc lib include share].collect {|d| HOMEBREW_PREFIX+d}
brew_links = Array.new paths=%w[bin].collect {|d| HOMEBREW_PREFIX+d}
version_map = Hash.new
# paths=%w[bin sbin etc lib include share].collect {|d| HOMEBREW_PREFIX+d} paths.each do |path|
paths=%w[bin].collect {|d| HOMEBREW_PREFIX+d} path.find do |path|
next unless path.symlink? && path.resolved_path_exists?
paths.each do |path| brew_links << Pathname.new(path.realpath)
path.find do |path| end
next unless path.symlink? && path.resolved_path_exists?
brew_links << Pathname.new(path.realpath)
end end
brew_links = brew_links.collect{|p|p.relative_path_from(real_cellar).to_s}.reject{|p|p.start_with?("../")}
brew_links.each do |p|
parts = p.split("/")
next if parts.count < 2 # Shouldn't happen for normally installed brews
brew = parts.shift
version = parts.shift
next unless which_brews.include? brew if which_brews
versions = version_map[brew] || []
versions << version unless versions.include? version
version_map[brew] = versions
end
return version_map
end end
brew_links = brew_links.collect{|p|p.relative_path_from(REAL_CELLAR).to_s}.reject{|p|p.starts_with?("../")} def which
brew_links.each do |p| which_brews = ARGV.named.empty? ? nil : ARGV.named
parts = p.split("/")
next if parts.count < 2 # Shouldn't happen
brew = parts.shift
version = parts.shift
versions = version_map[brew] || [] brews = which_versions which_brews
versions << version unless versions.include? version brews.keys.sort.each do |b|
version_map[brew] = versions puts "#{b}: #{brews[b].sort*' '}"
end
puts
end end
return version_map
end end
brews = audit Homebrew.which
brews.keys.sort.each do |b|
puts "#{b}: #{brews[b].sort*' '}"
end
puts