aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/uses.rb
blob: 1c029b0f042c6146d5c9bffd582f49871440e699 (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
require 'formula'

# `brew uses foo bar` returns formulae that use both foo and bar
# If you want the union, run the command twice and concatenate the results.
# The intersection is harder to achieve with shell tools.

module Homebrew
  def uses
    raise FormulaUnspecifiedError if ARGV.named.empty?

    used_formulae = ARGV.formulae
    formulae = (ARGV.include? "--installed") ? Formula.installed : Formula
    recursive = ARGV.flag? "--recursive"

    uses = formulae.select do |f|
      used_formulae.all? do |ff|
        begin
          if recursive
            f.recursive_dependencies.any? { |dep| dep.to_formula.name == ff.name } ||
              f.recursive_requirements.any? { |req| req.name == ff.name }
          else
            f.deps.any? { |dep| dep.to_formula.name == ff.name } ||
              f.requirements.any? { |req| req.name == ff.name }
          end
        rescue FormulaUnavailableError => e
          # Silently ignore this case as we don't care about things used in
          # taps that aren't currently tapped.
        end
      end
    end

    puts_columns uses.map(&:name)
  end
end