Skip to content

Commit

Permalink
Merge pull request #5587 from GauthamGoli/deps-args
Browse files Browse the repository at this point in the history
deps: Use CLI::Parser to parse args
  • Loading branch information
MikeMcQuaid authored Jan 24, 2019
2 parents 6a912c3 + 7b6d1f7 commit b4d02d0
Showing 1 changed file with 70 additions and 26 deletions.
96 changes: 70 additions & 26 deletions Library/Homebrew/cmd/deps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,51 +54,95 @@

require "formula"
require "ostruct"
require "cli_parser"

module Homebrew
module_function

def deps_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`deps` [<options>] <formulae>
Show dependencies for <formulae>. When given multiple formula arguments,
show the intersection of dependencies for <formulae>.
EOS
switch "--1",
description: "Only show dependencies one level down, instead of recursing."
switch "-n",
description: "Show dependencies in topological order."
switch "--union",
description: "Show the union of dependencies for <formulae>, instead of the intersection."
switch "--full-name",
description: "List dependencies by their full name."
switch "--installed",
description: "Only list those dependencies that are currently installed."
switch "--all",
description: "List all the dependencies for all available formuale."
switch "--include-build",
description: "Show `:build` type dependencies for <formulae>."
switch "--include-optional",
description: "Show `:optional` dependecies for <formulae>."
switch "--include-test",
description: "Show `:test` dependencies for <formulae> (non-recursive)."
switch "--skip-recommended",
description: "Skip `:recommended` type dependencies for <formulae>."
switch "--include-requirements",
description: "Include requirements in addition to dependencies for <formulae>."
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
end
end

def deps
deps_args.parse
mode = OpenStruct.new(
installed?: ARGV.include?("--installed"),
tree?: ARGV.include?("--tree"),
all?: ARGV.include?("--all"),
topo_order?: ARGV.include?("-n"),
union?: ARGV.include?("--union"),
for_each?: ARGV.include?("--for-each"),
installed?: args.installed?,
tree?: args.tree?,
all?: args.all?,
topo_order?: args.n?,
union?: args.union?,
for_each?: args.for_each?,
)

if mode.tree?
if mode.installed?
puts_deps_tree Formula.installed.sort, !ARGV.one?
puts_deps_tree Formula.installed.sort, !args.send("1?")
else
raise FormulaUnspecifiedError if ARGV.named.empty?
raise FormulaUnspecifiedError if args.remaining.empty?

puts_deps_tree ARGV.formulae, !ARGV.one?
puts_deps_tree ARGV.formulae, !args.send("1?")
end
return
elsif mode.all?
puts_deps Formula.sort
return
elsif !ARGV.named.empty? && mode.for_each?
elsif !args.remaining.empty? && mode.for_each?
puts_deps ARGV.formulae
return
end

@only_installed_arg = ARGV.include?("--installed") &&
!ARGV.include?("--include-build") &&
!ARGV.include?("--include-test") &&
!ARGV.include?("--include-optional") &&
!ARGV.include?("--skip-recommended")
@only_installed_arg = args.installed? &&
!args.include_build? &&
!args.include_test? &&
!args.include_optional? &&
!args.skip_recommended?

if ARGV.named.empty?
if args.remaining.empty?
raise FormulaUnspecifiedError unless mode.installed?

puts_deps Formula.installed.sort
return
end

all_deps = deps_for_formulae(ARGV.formulae, !ARGV.one?, &(mode.union? ? :| : :&))
all_deps = deps_for_formulae(ARGV.formulae, !args.send("1?"), &(mode.union? ? :| : :&))
all_deps = condense_requirements(all_deps)
all_deps.select!(&:installed?) if mode.installed?
all_deps.map!(&method(:dep_display_name))
Expand All @@ -108,26 +152,26 @@ def deps
end

def condense_requirements(deps)
return deps if ARGV.include?("--include-requirements")
return deps if args.include_requirements?

deps.select { |dep| dep.is_a? Dependency }
end

def dep_display_name(dep)
str = if dep.is_a? Requirement
if ARGV.include?("--include-requirements")
if args.include_requirements?
":#{dep.display_s}"
else
# This shouldn't happen, but we'll put something here to help debugging
"::#{dep.name}"
end
elsif ARGV.include?("--full-name")
elsif args.full_name?
dep.to_formula.full_name
else
dep.name
end

if ARGV.include?("--annotate")
if args.annotate?
str = "#{str} [build]" if dep.build?
str = "#{str} [test]" if dep.test?
str = "#{str} [optional]" if dep.optional?
Expand Down Expand Up @@ -180,14 +224,14 @@ def recursive_deps_tree(f, prefix, recursive)
reqs = f.requirements
deps = f.deps
dependables = reqs + deps
dependables.reject!(&:optional?) unless ARGV.include?("--include-optional")
dependables.reject!(&:build?) unless ARGV.include?("--include-build")
dependables.reject!(&:test?) unless ARGV.include?("--include-test")
dependables.reject!(&:recommended?) if ARGV.include?("--skip-recommended")
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?
max = dependables.length - 1
@dep_stack.push f.name
dependables.each_with_index do |dep, i|
next if !ARGV.include?("--include-requirements") && dep.is_a?(Requirement)
next if !args.include_requirements? && dep.is_a?(Requirement)

tree_lines = if i == max
"└──"
Expand Down

0 comments on commit b4d02d0

Please sign in to comment.