2015-08-03 13:09:07 +01:00
|
|
|
require "formula"
|
|
|
|
require "ostruct"
|
2019-01-23 08:34:24 +05:30
|
|
|
require "cli_parser"
|
2010-09-25 12:49:09 +01:00
|
|
|
|
2014-06-18 22:41:47 -05:00
|
|
|
module Homebrew
|
2016-09-26 01:44:51 +02:00
|
|
|
module_function
|
|
|
|
|
2019-01-23 08:34:24 +05:30
|
|
|
def deps_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
|
|
|
usage_banner <<~EOS
|
2019-01-30 13:18:02 +00:00
|
|
|
`deps` [<options>] <formula>
|
2019-01-23 08:34:24 +05:30
|
|
|
|
2019-01-30 13:18:02 +00:00
|
|
|
Show dependencies for <formula>. When given multiple formula arguments,
|
|
|
|
show the intersection of dependencies for <formula>.
|
2019-01-23 08:34:24 +05:30
|
|
|
EOS
|
|
|
|
switch "--1",
|
|
|
|
description: "Only show dependencies one level down, instead of recursing."
|
|
|
|
switch "-n",
|
|
|
|
description: "Show dependencies in topological order."
|
|
|
|
switch "--union",
|
2019-01-30 13:18:02 +00:00
|
|
|
description: "Show the union of dependencies for <formula>, instead of the intersection."
|
2019-01-23 08:34:24 +05:30
|
|
|
switch "--full-name",
|
|
|
|
description: "List dependencies by their full name."
|
|
|
|
switch "--installed",
|
|
|
|
description: "Only list those dependencies that are currently installed."
|
|
|
|
switch "--all",
|
2019-01-30 21:32:35 +00:00
|
|
|
description: "List all the dependencies for all available formulae."
|
2019-01-23 08:34:24 +05:30
|
|
|
switch "--include-build",
|
2019-01-30 13:18:02 +00:00
|
|
|
description: "Show `:build` type dependencies for <formula>."
|
2019-01-23 08:34:24 +05:30
|
|
|
switch "--include-optional",
|
2019-01-30 21:32:35 +00:00
|
|
|
description: "Show `:optional` dependencies for <formula>."
|
2019-01-23 08:34:24 +05:30
|
|
|
switch "--include-test",
|
2019-01-30 13:18:02 +00:00
|
|
|
description: "Show `:test` dependencies for <formula> (non-recursive)."
|
2019-01-23 08:34:24 +05:30
|
|
|
switch "--skip-recommended",
|
2019-01-30 13:18:02 +00:00
|
|
|
description: "Skip `:recommended` type dependencies for <formula>."
|
2019-01-23 08:34:24 +05:30
|
|
|
switch "--include-requirements",
|
2019-01-30 13:18:02 +00:00
|
|
|
description: "Include requirements in addition to dependencies for <formula>."
|
2019-01-23 08:34:24 +05:30
|
|
|
switch "--tree",
|
|
|
|
description: "Show dependencies as a tree. When given multiple formula arguments "\
|
|
|
|
"output individual trees for every formula."
|
|
|
|
switch "--for-each",
|
|
|
|
description: "Switch into the mode used by `deps --all`, but only list dependencies "\
|
|
|
|
"for specified formula one specified formula per line. This is used for "\
|
|
|
|
"debugging the `--installed`/`--all` display mode."
|
|
|
|
switch :verbose
|
|
|
|
switch :debug
|
2019-01-29 19:39:41 +00:00
|
|
|
conflicts "--installed", "--all"
|
2019-01-30 13:18:02 +00:00
|
|
|
formula_options
|
2019-01-23 08:34:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-11 20:22:54 +01:00
|
|
|
def deps
|
2019-01-23 08:34:24 +05:30
|
|
|
deps_args.parse
|
2013-06-22 12:54:46 -05:00
|
|
|
mode = OpenStruct.new(
|
2019-01-23 08:34:24 +05:30
|
|
|
installed?: args.installed?,
|
|
|
|
tree?: args.tree?,
|
|
|
|
all?: args.all?,
|
|
|
|
topo_order?: args.n?,
|
|
|
|
union?: args.union?,
|
|
|
|
for_each?: args.for_each?,
|
2013-06-22 12:54:46 -05:00
|
|
|
)
|
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
if mode.tree?
|
|
|
|
if mode.installed?
|
2019-01-23 08:34:24 +05:30
|
|
|
puts_deps_tree Formula.installed.sort, !args.send("1?")
|
2017-08-03 00:41:51 -04:00
|
|
|
else
|
2019-01-23 08:34:24 +05:30
|
|
|
raise FormulaUnspecifiedError if args.remaining.empty?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2019-01-23 08:34:24 +05:30
|
|
|
puts_deps_tree ARGV.formulae, !args.send("1?")
|
2017-08-03 00:41:51 -04:00
|
|
|
end
|
2018-03-26 10:55:29 +01:00
|
|
|
return
|
2013-06-22 12:54:46 -05:00
|
|
|
elsif mode.all?
|
2017-10-14 06:42:53 +01:00
|
|
|
puts_deps Formula.sort
|
2018-03-26 10:55:29 +01:00
|
|
|
return
|
2019-01-23 08:34:24 +05:30
|
|
|
elsif !args.remaining.empty? && mode.for_each?
|
2018-03-26 10:55:29 +01:00
|
|
|
puts_deps ARGV.formulae
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-01-23 08:34:24 +05:30
|
|
|
@only_installed_arg = args.installed? &&
|
|
|
|
!args.include_build? &&
|
|
|
|
!args.include_test? &&
|
|
|
|
!args.include_optional? &&
|
|
|
|
!args.skip_recommended?
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2019-01-23 08:34:24 +05:30
|
|
|
if args.remaining.empty?
|
2015-05-31 12:56:00 +02:00
|
|
|
raise FormulaUnspecifiedError unless mode.installed?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-14 06:42:53 +01:00
|
|
|
puts_deps Formula.installed.sort
|
2018-03-26 10:55:29 +01:00
|
|
|
return
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2019-01-23 08:34:24 +05:30
|
|
|
all_deps = deps_for_formulae(ARGV.formulae, !args.send("1?"), &(mode.union? ? :| : :&))
|
2018-03-26 10:55:29 +01:00
|
|
|
all_deps = condense_requirements(all_deps)
|
|
|
|
all_deps.select!(&:installed?) if mode.installed?
|
|
|
|
all_deps.map!(&method(:dep_display_name))
|
|
|
|
all_deps.uniq!
|
|
|
|
all_deps.sort! unless mode.topo_order?
|
|
|
|
puts all_deps
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
2013-06-09 12:59:42 -05:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
def condense_requirements(deps)
|
2019-01-23 08:34:24 +05:30
|
|
|
return deps if args.include_requirements?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-03-26 10:55:29 +01:00
|
|
|
deps.select { |dep| dep.is_a? Dependency }
|
2017-08-03 00:41:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dep_display_name(dep)
|
|
|
|
str = if dep.is_a? Requirement
|
2019-01-23 08:34:24 +05:30
|
|
|
if args.include_requirements?
|
2018-01-14 13:27:43 +00:00
|
|
|
":#{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?
|
2018-03-26 10:55:29 +01:00
|
|
|
dep.to_formula.full_name
|
2017-08-03 00:41:51 -04:00
|
|
|
else
|
2018-03-26 10:55:29 +01:00
|
|
|
dep.name
|
2017-08-03 00:41:51 -04:00
|
|
|
end
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2019-01-23 08:34:24 +05:30
|
|
|
if args.annotate?
|
2017-08-03 00:41:51 -04:00
|
|
|
str = "#{str} [build]" if dep.build?
|
2018-03-05 10:36:39 +00:00
|
|
|
str = "#{str} [test]" if dep.test?
|
2018-11-12 17:56:06 -05:00
|
|
|
str = "#{str} [optional]" if dep.optional?
|
2017-08-03 00:41:51 -04:00
|
|
|
str = "#{str} [recommended]" if dep.recommended?
|
|
|
|
end
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
str
|
2016-12-20 03:39:30 -05:00
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def deps_for_formula(f, recursive = false)
|
2018-03-24 16:55:16 +00:00
|
|
|
includes, ignores = argv_includes_ignores(ARGV)
|
2015-03-20 21:31:33 +00:00
|
|
|
|
2018-03-26 10:55:29 +01:00
|
|
|
deps = f.runtime_dependencies if @only_installed_arg
|
|
|
|
|
2013-10-29 17:27:21 -04:00
|
|
|
if recursive
|
2018-03-26 10:55:29 +01:00
|
|
|
deps ||= recursive_includes(Dependency, f, includes, ignores)
|
|
|
|
reqs = recursive_includes(Requirement, f, includes, ignores)
|
2013-10-29 17:27:21 -04:00
|
|
|
else
|
2018-03-26 10:55:29 +01:00
|
|
|
deps ||= reject_ignores(f.deps, ignores, includes)
|
|
|
|
reqs = reject_ignores(f.requirements, ignores, includes)
|
2013-10-29 17:27:21 -04:00
|
|
|
end
|
2014-02-27 12:56:42 -06:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
deps + reqs.to_a
|
2013-10-29 17:27:21 -04:00
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def deps_for_formulae(formulae, recursive = false, &block)
|
2018-09-02 20:14:54 +01:00
|
|
|
formulae.map { |f| deps_for_formula(f, recursive) }.reduce(&block)
|
2013-06-22 12:54:45 -05:00
|
|
|
end
|
|
|
|
|
2013-06-22 12:54:45 -05:00
|
|
|
def puts_deps(formulae)
|
2016-12-20 03:39:30 -05:00
|
|
|
formulae.each do |f|
|
2017-08-03 00:41:51 -04:00
|
|
|
deps = deps_for_formula(f)
|
|
|
|
deps = condense_requirements(deps)
|
2018-03-26 10:55:29 +01:00
|
|
|
deps.sort_by!(&:name)
|
|
|
|
deps.map!(&method(:dep_display_name))
|
2016-12-20 03:39:30 -05:00
|
|
|
puts "#{f.full_name}: #{deps.join(" ")}"
|
|
|
|
end
|
2013-06-22 12:54:45 -05:00
|
|
|
end
|
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
def puts_deps_tree(formulae, recursive = false)
|
2013-06-22 12:54:45 -05:00
|
|
|
formulae.each do |f|
|
2017-08-03 00:41:51 -04:00
|
|
|
puts f.full_name
|
|
|
|
@dep_stack = []
|
|
|
|
recursive_deps_tree(f, "", recursive)
|
2013-06-22 12:54:45 -05:00
|
|
|
puts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
def recursive_deps_tree(f, prefix, recursive)
|
|
|
|
reqs = f.requirements
|
|
|
|
deps = f.deps
|
|
|
|
dependables = reqs + deps
|
2019-01-23 08:34:24 +05:30
|
|
|
dependables.reject!(&:optional?) unless args.include_optional?
|
|
|
|
dependables.reject!(&:build?) unless args.include_build?
|
|
|
|
dependables.reject!(&:test?) unless args.include_test?
|
|
|
|
dependables.reject!(&:recommended?) if args.skip_recommended?
|
2017-08-03 00:41:51 -04:00
|
|
|
max = dependables.length - 1
|
|
|
|
@dep_stack.push f.name
|
|
|
|
dependables.each_with_index do |dep, i|
|
2019-01-23 08:34:24 +05:30
|
|
|
next if !args.include_requirements? && dep.is_a?(Requirement)
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
tree_lines = if i == max
|
2016-12-20 03:59:15 -05:00
|
|
|
"└──"
|
|
|
|
else
|
|
|
|
"├──"
|
|
|
|
end
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
display_s = "#{tree_lines} #{dep_display_name(dep)}"
|
|
|
|
is_circular = @dep_stack.include?(dep.name)
|
|
|
|
display_s = "#{display_s} (CIRCULAR DEPENDENCY)" if is_circular
|
|
|
|
puts "#{prefix}#{display_s}"
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
next if !recursive || is_circular
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
prefix_addition = if i == max
|
|
|
|
" "
|
2016-12-20 03:59:15 -05:00
|
|
|
else
|
2017-08-03 00:41:51 -04:00
|
|
|
"│ "
|
|
|
|
end
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
if dep.is_a? Dependency
|
|
|
|
recursive_deps_tree(Formulary.factory(dep.name), prefix + prefix_addition, true)
|
2016-12-20 03:59:15 -05:00
|
|
|
end
|
2013-06-09 12:59:42 -05:00
|
|
|
end
|
2018-03-26 10:55:29 +01:00
|
|
|
|
2017-08-03 00:41:51 -04:00
|
|
|
@dep_stack.pop
|
2013-06-09 12:59:42 -05:00
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|