aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/examples
diff options
context:
space:
mode:
authorAdam Vandenberg2011-06-13 10:26:20 -0700
committerAdam Vandenberg2011-06-19 21:58:32 -0700
commit0d741a308f5b5c851b2f19496a9baaa3564efacc (patch)
tree175e7e9e72b225b797a9741ffc2672eb82c1fc89 /Library/Contributions/examples
parent2d8d8f3c6ba0a42b22c5fe37c8c087848675e3b3 (diff)
downloadhomebrew-0d741a308f5b5c851b2f19496a9baaa3564efacc.tar.bz2
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.
Diffstat (limited to 'Library/Contributions/examples')
-rwxr-xr-xLibrary/Contributions/examples/brew-which.rb68
1 files changed, 36 insertions, 32 deletions
diff --git a/Library/Contributions/examples/brew-which.rb b/Library/Contributions/examples/brew-which.rb
index 9e5eb64fb..3ce9b497b 100755
--- a/Library/Contributions/examples/brew-which.rb
+++ b/Library/Contributions/examples/brew-which.rb
@@ -1,46 +1,50 @@
require 'extend/pathname'
-REAL_CELLAR = HOMEBREW_CELLAR.realpath
-class String
- def starts_with? prefix
- prefix = prefix.to_s
- self[0, prefix.length] == prefix
- end
-end
+module Homebrew extend self
+ def which_versions which_brews=nil
+ brew_links = Array.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].collect {|d| HOMEBREW_PREFIX+d}
+
+ paths.each do |path|
+ path.find do |path|
+ 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?("../")}
-def audit
- brew_links = Array.new
- version_map = Hash.new
+ 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
- # paths=%w[bin sbin etc lib include share].collect {|d| HOMEBREW_PREFIX+d}
- paths=%w[bin].collect {|d| HOMEBREW_PREFIX+d}
+ next unless which_brews.include? brew if which_brews
- paths.each do |path|
- path.find do |path|
- next unless path.symlink? && path.resolved_path_exists?
- brew_links << Pathname.new(path.realpath)
+ versions = version_map[brew] || []
+ versions << version unless versions.include? version
+ version_map[brew] = versions
end
+
+ return version_map
end
- brew_links = brew_links.collect{|p|p.relative_path_from(REAL_CELLAR).to_s}.reject{|p|p.starts_with?("../")}
- brew_links.each do |p|
- parts = p.split("/")
- next if parts.count < 2 # Shouldn't happen
- brew = parts.shift
- version = parts.shift
+ def which
+ which_brews = ARGV.named.empty? ? nil : ARGV.named
- versions = version_map[brew] || []
- versions << version unless versions.include? version
- version_map[brew] = versions
+ brews = which_versions which_brews
+ brews.keys.sort.each do |b|
+ puts "#{b}: #{brews[b].sort*' '}"
+ end
+ puts
end
-
- return version_map
end
-brews = audit
-brews.keys.sort.each do |b|
- puts "#{b}: #{brews[b].sort*' '}"
-end
-puts
+Homebrew.which