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