aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/list.rb
blob: ba33f2bde17fabecaa2d5c059cf6fad02157aff2 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
module Homebrew extend self
  def list

    # Use of exec means we don't explicitly exit
    list_unbrewed if ARGV.flag? '--unbrewed'

    # Unbrewed uses the PREFIX, which will exist
    # Things below use the CELLAR, which doesn't until the first formula is installed.
    return unless HOMEBREW_CELLAR.exist?

    if ARGV.include? '--versions'
      list_versions
    elsif ARGV.named.empty?
      ENV['CLICOLOR'] = nil
      exec 'ls', *ARGV.options_only << HOMEBREW_CELLAR
    elsif ARGV.verbose? or not $stdout.tty?
      exec "find", *ARGV.kegs + %w[-not -type d -print]
    else
      ARGV.kegs.each{ |keg| PrettyListing.new keg }
    end
  end

private

  def list_unbrewed
    dirs = HOMEBREW_PREFIX.children.select{ |pn| pn.directory? }.map{ |pn| pn.basename.to_s }
    dirs -= %w[Library Cellar .git]

    # Exclude the cache, if it has been located under the prefix
    cache_folder = (HOMEBREW_CACHE.relative_path_from(HOMEBREW_PREFIX)).to_s
    dirs -= [cache_folder]

    cd HOMEBREW_PREFIX
    exec 'find', *dirs + %w[-type f ( ! -iname .ds_store ! -iname brew ! -iname brew-man.1 ! -iname brew.1 )]
  end

  def list_versions
    if ARGV.named.empty?
      HOMEBREW_CELLAR.children.select{ |pn| pn.directory? }
    else
      ARGV.named.map{ |n| HOMEBREW_CELLAR+n }.select{ |pn| pn.exist? }
    end.each do |d|
      versions = d.children.select{ |pn| pn.directory? }.map{ |pn| pn.basename.to_s }
      puts "#{d.basename} #{versions*' '}"
    end
  end
end

class PrettyListing
  def initialize path
    Pathname.new(path).children.sort{ |a,b| a.to_s.downcase <=> b.to_s.downcase }.each do |pn|
      case pn.basename.to_s
      when 'bin', 'sbin'
        pn.find { |pnn| puts pnn unless pnn.directory? }
      when 'lib'
        print_dir pn do |pnn|
          # dylibs have multiple symlinks and we don't care about them
          (pnn.extname == '.dylib' or pnn.extname == '.pc') and not pnn.symlink?
        end
      else
        if pn.directory?
          if pn.symlink?
            puts "#{pn} -> #{pn.readlink}"
          else
            print_dir pn
          end
        elsif FORMULA_META_FILES.should_list? pn.basename.to_s
          puts pn
        end
      end
    end
  end

  def print_dir root
    dirs = []
    remaining_root_files = []
    other = ''

    root.children.sort.each do |pn|
      if pn.directory?
        dirs << pn
      elsif block_given? and yield pn
        puts pn
        other = 'other '
      else
        remaining_root_files << pn unless pn.basename.to_s == '.DS_Store'
      end
    end

    dirs.each do |d|
      files = []
      d.find { |pn| files << pn unless pn.directory? }
      print_remaining_files files, d
    end

    print_remaining_files remaining_root_files, root, other
  end

  def print_remaining_files files, root, other = ''
    case files.length
    when 0
      # noop
    when 1
      puts files
    else
      puts "#{root}/ (#{files.length} #{other}files)"
    end
  end
end