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|
        if recursive
          f.recursive_dependencies.any? { |dep| dep.name == ff.name } ||
            f.recursive_requirements.any? { |req| req.name == ff.name }
        else
          f.deps.any? { |dep| dep.name == ff.name } ||
            f.requirements.any? { |req| req.name == ff.name }
        end
      end
    end

    puts_columns uses.map(&:name)
  end
end

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|
        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
      end
    end

    puts_columns uses.map(&:name)
  end
end

Convert dependencies to formulas for name matching
Convert dependencies to formulas for name matching

Dependency names are prefixed with the tap location, therefore
dependency names do not match formula names. We convert dependencies
into formulas to ensure proper name checking.

Closes Homebrew/homebrew#35058.

Signed-off-by: Jack Nagel <43386ce32af96f5c56f2a88e458cb94cebee3751@gmail.com>
