100 lines
3.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "formula"
2019-04-17 18:25:08 +09:00
require "cli/parser"
2013-06-26 11:03:08 -07:00
# `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
2016-09-26 01:44:51 +02:00
module_function
2018-11-11 19:03:08 +05:30
def uses_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`uses` [<options>] <formula>
2018-11-11 19:03:08 +05:30
Show formulae that specify <formula> as a dependency. When given multiple
formula arguments, show the intersection of formulae that use <formula>.
By default, `uses` shows all formulae that specify <formula> as a required
or recommended dependency for their stable builds.
2018-11-11 19:03:08 +05:30
EOS
switch "--recursive",
2019-04-30 08:44:35 +01:00
description: "Resolve more than one level of dependencies."
2018-11-11 19:03:08 +05:30
switch "--installed",
description: "Only list formulae that are currently installed."
2018-11-11 19:03:08 +05:30
switch "--include-build",
2019-04-30 08:44:35 +01:00
description: "Include all formulae that specify <formula> as `:build` type dependency."
2018-11-11 19:03:08 +05:30
switch "--include-test",
2019-04-30 08:44:35 +01:00
description: "Include all formulae that specify <formula> as `:test` type dependency."
2018-11-11 19:03:08 +05:30
switch "--include-optional",
2019-04-30 08:44:35 +01:00
description: "Include all formulae that specify <formula> as `:optional` type dependency."
2018-11-11 19:03:08 +05:30
switch "--skip-recommended",
2019-04-30 08:44:35 +01:00
description: "Skip all formulae that specify <formula> as `:recommended` type dependency."
2018-11-11 19:03:08 +05:30
switch "--devel",
2019-08-06 14:22:24 -04:00
description: "Show usage of <formula> by development builds."
2018-11-11 19:03:08 +05:30
switch "--HEAD",
2019-08-06 14:22:24 -04:00
description: "Show usage of <formula> by HEAD builds."
2018-11-11 19:03:08 +05:30
switch :debug
conflicts "--devel", "--HEAD"
2018-11-11 19:03:08 +05:30
end
end
def uses
2018-11-11 19:03:08 +05:30
uses_args.parse
raise FormulaUnspecifiedError if args.remaining.empty?
2012-01-29 21:42:09 -08:00
used_formulae_missing = false
used_formulae = begin
ARGV.formulae
rescue FormulaUnavailableError => e
opoo e
used_formulae_missing = true
# If the formula doesn't exist: fake the needed formula object name.
ARGV.named.map { |name| OpenStruct.new name: name, full_name: name }
end
2018-11-11 19:03:08 +05:30
formulae = args.installed? ? Formula.installed : Formula
recursive = args.recursive?
only_installed_arg = args.installed? &&
!args.include_build? &&
!args.include_test? &&
!args.include_optional? &&
!args.skip_recommended?
includes, ignores = argv_includes_ignores(ARGV)
uses = formulae.select do |f|
used_formulae.all? do |ff|
begin
deps = f.runtime_dependencies if only_installed_arg
deps ||= if recursive
recursive_includes(Dependency, f, includes, ignores)
else
reject_ignores(f.deps, ignores, includes)
2016-09-10 10:24:57 +01:00
end
deps.any? do |dep|
begin
dep.to_formula.full_name == ff.full_name
rescue
dep.name == ff.name
end
end
rescue FormulaUnavailableError
# Silently ignore this case as we don't care about things used in
# taps that aren't currently tapped.
next
end
end
end
2012-01-29 21:42:09 -08:00
return if uses.empty?
2018-09-17 02:45:00 +02:00
puts Formatter.columns(uses.map(&:full_name).sort)
odie "Missing formulae should not have dependents!" if used_formulae_missing
end
end