aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/examples/brew-which.rb
blob: 9e5eb64fb71897cf8fe16d929bc9c237856a7490 (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
45
46
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


def audit
  brew_links = Array.new
  version_map = Hash.new

  # 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.starts_with?("../")}
  brew_links.each do |p|
    parts = p.split("/")
    next if parts.count < 2 # Shouldn't happen
    brew = parts.shift
    version = parts.shift

    versions = version_map[brew] || []
    versions << version unless versions.include? version
    version_map[brew] = versions
  end

  return version_map
end

brews = audit
brews.keys.sort.each do |b|
  puts "#{b}: #{brews[b].sort*' '}"
end
puts