aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/cmd/brew-which.rb
blob: 870ff67a52f2afd39cf4409f19422f3e7852c460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'extend/pathname'

module Homebrew
  def which_versions which_brews=nil
    brew_links = Array.new
    version_map = Hash.new

    real_cellar = HOMEBREW_CELLAR.realpath

    (HOMEBREW_PREFIX/'opt').subdirs.each do |path|
      next unless path.symlink? && path.resolved_path_exists?
      brew_links << Pathname.new(path.realpath)
    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

  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

Homebrew.which