328 lines
11 KiB
Ruby
Raw Normal View History

2023-03-06 09:49:53 -08:00
# typed: true
# frozen_string_literal: true
require "formula"
2019-04-17 18:25:08 +09:00
require "cli/parser"
require "cask/caskroom"
require "dependencies_helpers"
module Homebrew
extend DependenciesHelpers
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
2023-03-06 09:49:53 -08:00
def self.deps_args
2019-01-23 08:34:24 +05:30
Homebrew::CLI::Parser.new do
description <<~EOS
Show dependencies for <formula>. Additional options specific to <formula>
may be appended to the command. When given multiple formula arguments,
show the intersection of dependencies for each formula.
2019-01-23 08:34:24 +05:30
EOS
switch "-n", "--topological",
description: "Sort dependencies in topological order."
switch "-1", "--direct", "--declared", "--1",
description: "Show only the direct dependencies declared in the formula."
2019-01-23 08:34:24 +05:30
switch "--union",
2019-04-30 08:44:35 +01:00
description: "Show the union of dependencies for multiple <formula>, instead of the intersection."
2019-01-23 08:34:24 +05:30
switch "--full-name",
2019-04-30 08:44:35 +01:00
description: "List dependencies by their full name."
2019-01-23 08:34:24 +05:30
switch "--include-build",
2019-04-30 08:44:35 +01:00
description: "Include `:build` dependencies for <formula>."
2019-01-23 08:34:24 +05:30
switch "--include-optional",
2019-04-30 08:44:35 +01:00
description: "Include `:optional` dependencies for <formula>."
2019-01-23 08:34:24 +05:30
switch "--include-test",
2019-04-30 08:44:35 +01:00
description: "Include `:test` dependencies for <formula> (non-recursive)."
2019-01-23 08:34:24 +05:30
switch "--skip-recommended",
2019-04-30 08:44:35 +01:00
description: "Skip `:recommended` dependencies for <formula>."
2019-01-23 08:34:24 +05:30
switch "--include-requirements",
2019-04-30 08:44:35 +01:00
description: "Include requirements in addition to dependencies for <formula>."
2019-01-23 08:34:24 +05:30
switch "--tree",
2022-06-28 10:09:59 +01:00
description: "Show dependencies as a tree. When given multiple formula arguments, " \
2019-04-30 08:44:35 +01:00
"show individual trees for each formula."
switch "--graph",
description: "Show dependencies as a directed graph."
switch "--dot",
depends_on: "--graph",
description: "Show text-based graph description in DOT format."
2019-03-06 23:44:46 -05:00
switch "--annotate",
2022-06-28 10:09:59 +01:00
description: "Mark any build, test, optional, or recommended dependencies as " \
2019-04-30 08:44:35 +01:00
"such in the output."
switch "--installed",
2022-06-28 10:09:59 +01:00
description: "List dependencies for formulae that are currently installed. If <formula> is " \
2019-04-30 08:44:35 +01:00
"specified, list only its dependencies that are currently installed."
2023-05-17 14:53:19 -05:00
switch "--missing",
description: "Show only missing dependencies."
switch "--eval-all",
description: "Evaluate all available formulae and casks, whether installed or not, to list " \
"their dependencies."
switch "--all",
hidden: true
2019-01-23 08:34:24 +05:30
switch "--for-each",
2022-06-28 10:09:59 +01:00
description: "Switch into the mode used by the `--all` option, but only list dependencies " \
"for each provided <formula>, one formula per line. This is used for " \
2019-04-30 08:44:35 +01:00
"debugging the `--installed`/`--all` display mode."
2020-12-04 10:11:17 +09:00
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
2020-11-27 11:41:08 -05:00
conflicts "--tree", "--graph"
2023-05-17 14:53:19 -05:00
conflicts "--installed", "--missing"
conflicts "--installed", "--eval-all"
conflicts "--installed", "--all"
2020-12-04 10:11:17 +09:00
conflicts "--formula", "--cask"
formula_options
2021-01-10 14:26:40 -05:00
named_args [:formula, :cask]
2019-01-23 08:34:24 +05:30
end
end
2023-03-06 09:49:53 -08:00
def self.deps
2020-07-30 18:40:10 +02:00
args = deps_args.parse
2019-11-06 10:20:33 +00:00
all = args.eval_all?
if args.all?
unless all
odisabled "brew deps --all",
"brew deps --eval-all or HOMEBREW_EVAL_ALL"
end
all = true
end
2019-11-06 10:20:33 +00:00
Formulary.enable_factory_cache!
recursive = !args.direct?
installed = args.installed? || dependents(args.named.to_formulae_and_casks).all?(&:any_version_installed?)
@use_runtime_dependencies = installed && recursive &&
!args.tree? &&
!args.graph? &&
!args.include_build? &&
!args.include_test? &&
!args.include_optional? &&
!args.skip_recommended?
2019-11-06 10:20:20 +00:00
if args.tree? || args.graph?
dependents = if args.named.present?
sorted_dependents(args.named.to_formulae_and_casks)
elsif args.installed?
2020-12-29 21:06:32 +09:00
case args.only_formula_or_cask
when :formula
2020-12-04 10:11:17 +09:00
sorted_dependents(Formula.installed)
2020-12-29 21:06:32 +09:00
when :cask
sorted_dependents(Cask::Caskroom.casks)
2020-12-04 10:11:17 +09:00
else
sorted_dependents(Formula.installed + Cask::Caskroom.casks)
2020-12-04 10:11:17 +09:00
end
2017-08-03 00:41:51 -04:00
else
raise FormulaUnspecifiedError
2017-08-03 00:41:51 -04:00
end
if args.graph?
dot_code = dot_code(dependents, recursive: recursive, args: args)
if args.dot?
puts dot_code
else
exec_browser "https://dreampuf.github.io/GraphvizOnline/##{ERB::Util.url_encode(dot_code)}"
end
return
end
2020-08-19 17:12:32 +01:00
puts_deps_tree dependents, recursive: recursive, args: args
return
elsif all
puts_deps sorted_dependents(Formula.all + Cask::Cask.all), recursive: recursive, args: args
return
elsif !args.no_named? && args.for_each?
puts_deps sorted_dependents(args.named.to_formulae_and_casks), recursive: recursive, args: args
return
end
if args.no_named?
2019-11-06 10:20:20 +00:00
raise FormulaUnspecifiedError unless args.installed?
2018-09-17 02:45:00 +02:00
2020-12-29 21:06:32 +09:00
sorted_dependents_formulae_and_casks = case args.only_formula_or_cask
when :formula
2020-12-04 10:11:17 +09:00
sorted_dependents(Formula.installed)
2020-12-29 21:06:32 +09:00
when :cask
sorted_dependents(Cask::Caskroom.casks)
2020-12-04 10:11:17 +09:00
else
sorted_dependents(Formula.installed + Cask::Caskroom.casks)
2020-12-04 10:11:17 +09:00
end
puts_deps sorted_dependents_formulae_and_casks, recursive: recursive, args: args
return
end
dependents = dependents(args.named.to_formulae_and_casks)
2020-08-19 17:12:32 +01:00
all_deps = deps_for_dependents(dependents, recursive: recursive, args: args, &(args.union? ? :| : :&))
2020-08-01 12:52:37 -04:00
condense_requirements(all_deps, args: args)
all_deps.map! { |d| dep_display_name(d, args: args) }
all_deps.uniq!
all_deps.sort! unless args.topological?
puts all_deps
end
2023-03-06 09:49:53 -08:00
def self.sorted_dependents(formulae_or_casks)
dependents(formulae_or_casks).sort_by(&:name)
end
2023-03-06 09:49:53 -08:00
def self.condense_requirements(deps, args:)
deps.select! { |dep| dep.is_a?(Dependency) } unless args.include_requirements?
deps.select! { |dep| dep.is_a?(Requirement) || dep.installed? } if args.installed?
2017-08-03 00:41:51 -04:00
end
2023-03-06 09:49:53 -08:00
def self.dep_display_name(dep, args:)
2017-08-03 00:41:51 -04:00
str = if dep.is_a? Requirement
2019-01-23 08:34:24 +05:30
if args.include_requirements?
":#{dep.display_s}"
2017-08-03 00:41:51 -04:00
else
# This shouldn't happen, but we'll put something here to help debugging
"::#{dep.name}"
end
2019-01-23 08:34:24 +05:30
elsif args.full_name?
dep.to_formula.full_name
2017-08-03 00:41:51 -04:00
else
dep.name
2017-08-03 00:41:51 -04:00
end
2019-01-23 08:34:24 +05:30
if args.annotate?
2019-03-06 23:44:46 -05:00
str = "#{str} " if args.tree?
str = "#{str} [build]" if dep.build?
str = "#{str} [test]" if dep.test?
str = "#{str} [optional]" if dep.optional?
str = "#{str} [recommended]" if dep.recommended?
2017-08-03 00:41:51 -04:00
end
2017-08-03 00:41:51 -04:00
str
end
def self.deps_for_dependent(dependency, args:, recursive: false)
includes, ignores = args_includes_ignores(args)
deps = dependency.runtime_dependencies if @use_runtime_dependencies
if recursive
deps ||= recursive_includes(Dependency, dependency, includes, ignores)
reqs = recursive_includes(Requirement, dependency, includes, ignores)
else
deps ||= reject_ignores(dependency.deps, ignores, includes)
reqs = reject_ignores(dependency.requirements, ignores, includes)
end
2014-02-27 12:56:42 -06:00
2017-08-03 00:41:51 -04:00
deps + reqs.to_a
end
2023-03-06 09:49:53 -08:00
def self.deps_for_dependents(dependents, args:, recursive: false, &block)
2020-08-19 17:12:32 +01:00
dependents.map { |d| deps_for_dependent(d, recursive: recursive, args: args) }.reduce(&block)
2013-06-22 12:54:45 -05:00
end
2023-03-06 09:49:53 -08:00
def self.puts_deps(dependents, args:, recursive: false)
dependents.each do |dependent|
2020-08-19 17:12:32 +01:00
deps = deps_for_dependent(dependent, recursive: recursive, args: args)
2020-08-01 12:52:37 -04:00
condense_requirements(deps, args: args)
deps.sort_by!(&:name)
deps.map! { |d| dep_display_name(d, args: args) }
puts "#{dependent.full_name}: #{deps.join(" ")}"
end
2013-06-22 12:54:45 -05:00
end
2023-03-06 09:49:53 -08:00
def self.dot_code(dependents, recursive:, args:)
dep_graph = {}
dependents.each do |d|
graph_deps(d, dep_graph: dep_graph, recursive: recursive, args: args)
end
dot_code = dep_graph.map do |d, deps|
deps.map do |dep|
attributes = []
attributes << "style = dotted" if dep.build?
attributes << "arrowhead = empty" if dep.test?
if dep.optional?
attributes << "color = red"
elsif dep.recommended?
attributes << "color = green"
end
comment = " # #{dep.tags.map(&:inspect).join(", ")}" if dep.tags.any?
2022-03-19 16:21:01 +09:00
" \"#{d.name}\" -> \"#{dep}\"#{" [#{attributes.join(", ")}]" if attributes.any?}#{comment}"
end
end.flatten.join("\n")
"digraph {\n#{dot_code}\n}"
end
def self.graph_deps(formula, dep_graph:, recursive:, args:)
return if dep_graph.key?(formula)
dependables = dependables(formula, args: args)
dep_graph[formula] = dependables
return unless recursive
dependables.each do |dep|
next unless dep.is_a? Dependency
graph_deps(Formulary.factory(dep.name),
dep_graph: dep_graph,
recursive: true,
args: args)
end
end
2023-03-06 09:49:53 -08:00
def self.puts_deps_tree(dependents, args:, recursive: false)
dependents.each do |d|
puts d.full_name
2021-11-12 13:50:53 -08:00
recursive_deps_tree(d, dep_stack: [], prefix: "", recursive: recursive, args: args)
2013-06-22 12:54:45 -05:00
puts
end
end
def self.dependables(formula, args:)
includes, ignores = args_includes_ignores(args)
deps = @use_runtime_dependencies ? formula.runtime_dependencies : formula.deps
deps = reject_ignores(deps, ignores, includes)
reqs = reject_ignores(formula.requirements, ignores, includes) if args.include_requirements?
reqs ||= []
reqs + deps
end
def self.recursive_deps_tree(formula, dep_stack:, prefix:, recursive:, args:)
dependables = dependables(formula, args: args)
2017-08-03 00:41:51 -04:00
max = dependables.length - 1
dep_stack.push formula.name
2017-08-03 00:41:51 -04:00
dependables.each_with_index do |dep, i|
tree_lines = if i == max
"└──"
else
"├──"
end
display_s = "#{tree_lines} #{dep_display_name(dep, args: args)}"
# Detect circular dependencies and consider them a failure if present.
2021-11-12 13:50:53 -08:00
is_circular = dep_stack.include?(dep.name)
if is_circular
display_s = "#{display_s} (CIRCULAR DEPENDENCY)"
Homebrew.failed = true
end
2017-08-03 00:41:51 -04:00
puts "#{prefix}#{display_s}"
2017-08-03 00:41:51 -04:00
next if !recursive || is_circular
2017-08-03 00:41:51 -04:00
prefix_addition = if i == max
" "
else
2017-08-03 00:41:51 -04:00
""
end
2021-11-12 13:50:53 -08:00
next unless dep.is_a? Dependency
recursive_deps_tree(Formulary.factory(dep.name),
dep_stack: dep_stack,
prefix: prefix + prefix_addition,
recursive: true,
args: args)
end
2021-11-12 13:50:53 -08:00
dep_stack.pop
end
end