Port Homebrew::Cmd::Deps
This commit is contained in:
parent
3615e5e648
commit
84222ec006
@ -1,17 +1,16 @@
|
||||
# typed: true
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_command"
|
||||
require "formula"
|
||||
require "cli/parser"
|
||||
require "cask/caskroom"
|
||||
require "dependencies_helpers"
|
||||
|
||||
module Homebrew
|
||||
extend DependenciesHelpers
|
||||
|
||||
sig { returns(CLI::Parser) }
|
||||
def self.deps_args
|
||||
Homebrew::CLI::Parser.new do
|
||||
module Cmd
|
||||
class Deps < AbstractCommand
|
||||
include DependenciesHelpers
|
||||
cmd_args do
|
||||
description <<~EOS
|
||||
Show dependencies for <formula>. When given multiple formula arguments,
|
||||
show the intersection of dependencies for each formula. By default, `deps`
|
||||
@ -80,11 +79,9 @@ module Homebrew
|
||||
|
||||
named_args [:formula, :cask]
|
||||
end
|
||||
end
|
||||
|
||||
def self.deps
|
||||
args = deps_args.parse
|
||||
|
||||
sig { override.void }
|
||||
def run
|
||||
all = args.eval_all?
|
||||
|
||||
Formulary.enable_factory_cache!
|
||||
@ -119,7 +116,7 @@ module Homebrew
|
||||
end
|
||||
|
||||
if args.graph?
|
||||
dot_code = dot_code(dependents, recursive:, args:)
|
||||
dot_code = dot_code(dependents, recursive:)
|
||||
if args.dot?
|
||||
puts dot_code
|
||||
else
|
||||
@ -128,15 +125,15 @@ module Homebrew
|
||||
return
|
||||
end
|
||||
|
||||
puts_deps_tree(dependents, recursive:, args:)
|
||||
puts_deps_tree(dependents, recursive:)
|
||||
return
|
||||
elsif all
|
||||
puts_deps(sorted_dependents(
|
||||
Formula.all(eval_all: args.eval_all?) + Cask::Cask.all(eval_all: args.eval_all?),
|
||||
), recursive:, args:)
|
||||
), recursive:)
|
||||
return
|
||||
elsif !args.no_named? && args.for_each?
|
||||
puts_deps(sorted_dependents(args.named.to_formulae_and_casks), recursive:, args:)
|
||||
puts_deps(sorted_dependents(args.named.to_formulae_and_casks), recursive:)
|
||||
return
|
||||
end
|
||||
|
||||
@ -151,31 +148,31 @@ module Homebrew
|
||||
else
|
||||
sorted_dependents(Formula.installed + Cask::Caskroom.casks)
|
||||
end
|
||||
puts_deps(sorted_dependents_formulae_and_casks, recursive:, args:)
|
||||
puts_deps(sorted_dependents_formulae_and_casks, recursive:)
|
||||
return
|
||||
end
|
||||
|
||||
dependents = dependents(args.named.to_formulae_and_casks)
|
||||
check_head_spec(dependents) if args.HEAD?
|
||||
|
||||
all_deps = deps_for_dependents(dependents, recursive:, args:, &(args.union? ? :| : :&))
|
||||
condense_requirements(all_deps, args:)
|
||||
all_deps.map! { |d| dep_display_name(d, args:) }
|
||||
all_deps = deps_for_dependents(dependents, recursive:, &(args.union? ? :| : :&))
|
||||
condense_requirements(all_deps)
|
||||
all_deps.map! { |d| dep_display_name(d) }
|
||||
all_deps.uniq!
|
||||
all_deps.sort! unless args.topological?
|
||||
puts all_deps
|
||||
end
|
||||
|
||||
def self.sorted_dependents(formulae_or_casks)
|
||||
def sorted_dependents(formulae_or_casks)
|
||||
dependents(formulae_or_casks).sort_by(&:name)
|
||||
end
|
||||
|
||||
def self.condense_requirements(deps, args:)
|
||||
def condense_requirements(deps)
|
||||
deps.select! { |dep| dep.is_a?(Dependency) } unless args.include_requirements?
|
||||
deps.select! { |dep| dep.is_a?(Requirement) || dep.installed? } if args.installed?
|
||||
end
|
||||
|
||||
def self.dep_display_name(dep, args:)
|
||||
def dep_display_name(dep)
|
||||
str = if dep.is_a? Requirement
|
||||
if args.include_requirements?
|
||||
":#{dep.display_s}"
|
||||
@ -201,7 +198,7 @@ module Homebrew
|
||||
str
|
||||
end
|
||||
|
||||
def self.deps_for_dependent(dependency, args:, recursive: false)
|
||||
def deps_for_dependent(dependency, recursive: false)
|
||||
includes, ignores = args_includes_ignores(args)
|
||||
|
||||
deps = dependency.runtime_dependencies if @use_runtime_dependencies
|
||||
@ -217,31 +214,31 @@ module Homebrew
|
||||
deps + reqs.to_a
|
||||
end
|
||||
|
||||
def self.deps_for_dependents(dependents, args:, recursive: false, &block)
|
||||
dependents.map { |d| deps_for_dependent(d, recursive:, args:) }.reduce(&block)
|
||||
def deps_for_dependents(dependents, recursive: false, &block)
|
||||
dependents.map { |d| deps_for_dependent(d, recursive:) }.reduce(&block)
|
||||
end
|
||||
|
||||
def self.check_head_spec(dependents)
|
||||
def check_head_spec(dependents)
|
||||
headless = dependents.select { |d| d.is_a?(Formula) && d.active_spec_sym != :head }
|
||||
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
||||
opoo "No head spec for #{headless}, using stable spec instead" unless headless.empty?
|
||||
end
|
||||
|
||||
def self.puts_deps(dependents, args:, recursive: false)
|
||||
def puts_deps(dependents, recursive: false)
|
||||
check_head_spec(dependents) if args.HEAD?
|
||||
dependents.each do |dependent|
|
||||
deps = deps_for_dependent(dependent, recursive:, args:)
|
||||
condense_requirements(deps, args:)
|
||||
deps = deps_for_dependent(dependent, recursive:)
|
||||
condense_requirements(deps)
|
||||
deps.sort_by!(&:name)
|
||||
deps.map! { |d| dep_display_name(d, args:) }
|
||||
deps.map! { |d| dep_display_name(d) }
|
||||
puts "#{dependent.full_name}: #{deps.join(" ")}"
|
||||
end
|
||||
end
|
||||
|
||||
def self.dot_code(dependents, recursive:, args:)
|
||||
def dot_code(dependents, recursive:)
|
||||
dep_graph = {}
|
||||
dependents.each do |d|
|
||||
graph_deps(d, dep_graph:, recursive:, args:)
|
||||
graph_deps(d, dep_graph:, recursive:)
|
||||
end
|
||||
|
||||
dot_code = dep_graph.map do |d, deps|
|
||||
@ -261,10 +258,10 @@ module Homebrew
|
||||
"digraph {\n#{dot_code}\n}"
|
||||
end
|
||||
|
||||
def self.graph_deps(formula, dep_graph:, recursive:, args:)
|
||||
def graph_deps(formula, dep_graph:, recursive:)
|
||||
return if dep_graph.key?(formula)
|
||||
|
||||
dependables = dependables(formula, args:)
|
||||
dependables = dependables(formula)
|
||||
dep_graph[formula] = dependables
|
||||
return unless recursive
|
||||
|
||||
@ -273,21 +270,20 @@ module Homebrew
|
||||
|
||||
graph_deps(Formulary.factory(dep.name),
|
||||
dep_graph:,
|
||||
recursive: true,
|
||||
args:)
|
||||
recursive: true)
|
||||
end
|
||||
end
|
||||
|
||||
def self.puts_deps_tree(dependents, args:, recursive: false)
|
||||
def puts_deps_tree(dependents, recursive: false)
|
||||
check_head_spec(dependents) if args.HEAD?
|
||||
dependents.each do |d|
|
||||
puts d.full_name
|
||||
recursive_deps_tree(d, dep_stack: [], prefix: "", recursive:, args:)
|
||||
recursive_deps_tree(d, dep_stack: [], prefix: "", recursive:)
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
def self.dependables(formula, args:)
|
||||
def dependables(formula)
|
||||
includes, ignores = args_includes_ignores(args)
|
||||
deps = @use_runtime_dependencies ? formula.runtime_dependencies : formula.deps
|
||||
deps = select_includes(deps, ignores, includes)
|
||||
@ -296,8 +292,8 @@ module Homebrew
|
||||
reqs + deps
|
||||
end
|
||||
|
||||
def self.recursive_deps_tree(formula, dep_stack:, prefix:, recursive:, args:)
|
||||
dependables = dependables(formula, args:)
|
||||
def recursive_deps_tree(formula, dep_stack:, prefix:, recursive:)
|
||||
dependables = dependables(formula)
|
||||
max = dependables.length - 1
|
||||
dep_stack.push formula.name
|
||||
dependables.each_with_index do |dep, i|
|
||||
@ -307,7 +303,7 @@ module Homebrew
|
||||
"├──"
|
||||
end
|
||||
|
||||
display_s = "#{tree_lines} #{dep_display_name(dep, args:)}"
|
||||
display_s = "#{tree_lines} #{dep_display_name(dep)}"
|
||||
|
||||
# Detect circular dependencies and consider them a failure if present.
|
||||
is_circular = dep_stack.include?(dep.name)
|
||||
@ -331,10 +327,11 @@ module Homebrew
|
||||
recursive_deps_tree(Formulary.factory(dep.name),
|
||||
dep_stack:,
|
||||
prefix: prefix + prefix_addition,
|
||||
recursive: true,
|
||||
args:)
|
||||
recursive: true)
|
||||
end
|
||||
|
||||
dep_stack.pop
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "cmd/deps"
|
||||
require "cmd/shared_examples/args_parse"
|
||||
|
||||
RSpec.describe "brew deps" do
|
||||
RSpec.describe Homebrew::Cmd::Deps do
|
||||
it_behaves_like "parseable arguments"
|
||||
|
||||
it "outputs all of a Formula's dependencies and their dependencies on separate lines", :integration_test do
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user