Port Homebrew::Cmd::Uses

This commit is contained in:
Douglas Eichelberger 2024-04-01 12:01:37 -07:00
parent 1362890f2a
commit fdc95d02c8
2 changed files with 145 additions and 142 deletions

View File

@ -1,166 +1,167 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
# `brew uses foo bar` returns formulae that use both foo and bar require "abstract_command"
# If you want the union, run the command twice and concatenate the results.
# The intersection is harder to achieve with shell tools.
require "formula" require "formula"
require "cli/parser"
require "cask/caskroom" require "cask/caskroom"
require "dependencies_helpers" require "dependencies_helpers"
require "ostruct" require "ostruct"
module Homebrew module Homebrew
extend DependenciesHelpers module Cmd
# `brew uses foo bar` returns formulae that use both foo and bar
# If you want the union, run the command twice and concatenate the results.
# The intersection is harder to achieve with shell tools.
class Uses < AbstractCommand
include DependenciesHelpers
sig { returns(CLI::Parser) } cmd_args do
def self.uses_args description <<~EOS
Homebrew::CLI::Parser.new do Show formulae and casks that specify <formula> as a dependency; that is, show dependents
description <<~EOS of <formula>. When given multiple formula arguments, show the intersection
Show formulae and casks that specify <formula> as a dependency; that is, show dependents of formulae that use <formula>. By default, `uses` shows all formulae and casks that
of <formula>. When given multiple formula arguments, show the intersection specify <formula> as a required or recommended dependency for their stable builds.
of formulae that use <formula>. By default, `uses` shows all formulae and casks that
specify <formula> as a required or recommended dependency for their stable builds.
*Note:* `--missing` and `--skip-recommended` have precedence over `--include-*`. *Note:* `--missing` and `--skip-recommended` have precedence over `--include-*`.
EOS EOS
switch "--recursive", switch "--recursive",
description: "Resolve more than one level of dependencies." description: "Resolve more than one level of dependencies."
switch "--installed", switch "--installed",
description: "Only list formulae and casks that are currently installed." description: "Only list formulae and casks that are currently installed."
switch "--missing", switch "--missing",
description: "Only list formulae and casks that are not currently installed." description: "Only list formulae and casks that are not currently installed."
switch "--eval-all", switch "--eval-all",
description: "Evaluate all available formulae and casks, whether installed or not, to show " \ description: "Evaluate all available formulae and casks, whether installed or not, to show " \
"their dependents." "their dependents."
switch "--include-build", switch "--include-build",
description: "Include formulae that specify <formula> as a `:build` dependency." description: "Include formulae that specify <formula> as a `:build` dependency."
switch "--include-test", switch "--include-test",
description: "Include formulae that specify <formula> as a `:test` dependency." description: "Include formulae that specify <formula> as a `:test` dependency."
switch "--include-optional", switch "--include-optional",
description: "Include formulae that specify <formula> as an `:optional` dependency." description: "Include formulae that specify <formula> as an `:optional` dependency."
switch "--skip-recommended", switch "--skip-recommended",
description: "Skip all formulae that specify <formula> as a `:recommended` dependency." description: "Skip all formulae that specify <formula> as a `:recommended` dependency."
switch "--formula", "--formulae", switch "--formula", "--formulae",
description: "Include only formulae." description: "Include only formulae."
switch "--cask", "--casks", switch "--cask", "--casks",
description: "Include only casks." description: "Include only casks."
conflicts "--formula", "--cask" conflicts "--formula", "--cask"
conflicts "--installed", "--all" conflicts "--installed", "--all"
conflicts "--missing", "--installed" conflicts "--missing", "--installed"
named_args :formula, min: 1 named_args :formula, min: 1
end
end
def self.uses
args = uses_args.parse
Formulary.enable_factory_cache!
used_formulae_missing = false
used_formulae = begin
args.named.to_formulae
rescue FormulaUnavailableError => e
opoo e
used_formulae_missing = true
# If the formula doesn't exist: fake the needed formula object name.
# This is a legacy use of OpenStruct that should be refactored.
# rubocop:disable Style/OpenStructUse
args.named.map { |name| OpenStruct.new name:, full_name: name }
# rubocop:enable Style/OpenStructUse
end
use_runtime_dependents = args.installed? &&
!used_formulae_missing &&
!args.include_build? &&
!args.include_test? &&
!args.include_optional? &&
!args.skip_recommended?
uses = intersection_of_dependents(use_runtime_dependents, used_formulae, args:)
return if uses.empty?
puts Formatter.columns(uses.map(&:full_name).sort)
odie "Missing formulae should not have dependents!" if used_formulae_missing
end
def self.intersection_of_dependents(use_runtime_dependents, used_formulae, args:)
recursive = args.recursive?
show_formulae_and_casks = !args.formula? && !args.cask?
includes, ignores = args_includes_ignores(args)
deps = []
if use_runtime_dependents
if show_formulae_and_casks || args.formula?
deps += used_formulae.map(&:runtime_installed_formula_dependents)
.reduce(&:&)
.select(&:any_version_installed?)
end
if show_formulae_and_casks || args.cask?
deps += select_used_dependents(
dependents(Cask::Caskroom.casks),
used_formulae, recursive, includes, ignores
)
end end
deps sig { override.void }
else def run
all = args.eval_all? Formulary.enable_factory_cache!
if !args.installed? && !(all || Homebrew::EnvConfig.eval_all?) used_formulae_missing = false
raise UsageError, "`brew uses` needs `--installed` or `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!" used_formulae = begin
args.named.to_formulae
rescue FormulaUnavailableError => e
opoo e
used_formulae_missing = true
# If the formula doesn't exist: fake the needed formula object name.
# This is a legacy use of OpenStruct that should be refactored.
# rubocop:disable Style/OpenStructUse
args.named.map { |name| OpenStruct.new name:, full_name: name }
# rubocop:enable Style/OpenStructUse
end
use_runtime_dependents = args.installed? &&
!used_formulae_missing &&
!args.include_build? &&
!args.include_test? &&
!args.include_optional? &&
!args.skip_recommended?
uses = intersection_of_dependents(use_runtime_dependents, used_formulae)
return if uses.empty?
puts Formatter.columns(uses.map(&:full_name).sort)
odie "Missing formulae should not have dependents!" if used_formulae_missing
end end
if show_formulae_and_casks || args.formula? private
deps += args.installed? ? Formula.installed : Formula.all(eval_all: args.eval_all?)
end def intersection_of_dependents(use_runtime_dependents, used_formulae)
if show_formulae_and_casks || args.cask? recursive = args.recursive?
deps += args.installed? ? Cask::Caskroom.casks : Cask::Cask.all(eval_all: args.eval_all?) show_formulae_and_casks = !args.formula? && !args.cask?
includes, ignores = args_includes_ignores(args)
deps = []
if use_runtime_dependents
if show_formulae_and_casks || args.formula?
deps += used_formulae.map(&:runtime_installed_formula_dependents)
.reduce(&:&)
.select(&:any_version_installed?)
end
if show_formulae_and_casks || args.cask?
deps += select_used_dependents(
dependents(Cask::Caskroom.casks),
used_formulae, recursive, includes, ignores
)
end
deps
else
all = args.eval_all?
if !args.installed? && !(all || Homebrew::EnvConfig.eval_all?)
raise UsageError, "`brew uses` needs `--installed` or `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!"
end
if show_formulae_and_casks || args.formula?
deps += args.installed? ? Formula.installed : Formula.all(eval_all: args.eval_all?)
end
if show_formulae_and_casks || args.cask?
deps += args.installed? ? Cask::Caskroom.casks : Cask::Cask.all(eval_all: args.eval_all?)
end
if args.missing?
deps.reject! do |dep|
case dep
when Formula
dep.any_version_installed?
when Cask::Cask
dep.installed?
end
end
ignores.delete(:satisfied?)
end
select_used_dependents(dependents(deps), used_formulae, recursive, includes, ignores)
end
end end
if args.missing? def select_used_dependents(dependents, used_formulae, recursive, includes, ignores)
deps.reject! do |dep| dependents.select do |d|
case dep deps = if recursive
when Formula recursive_includes(Dependency, d, includes, ignores)
dep.any_version_installed? else
when Cask::Cask select_includes(d.deps, ignores, includes)
dep.installed? end
used_formulae.all? do |ff|
deps.any? do |dep|
match = begin
dep.to_formula.full_name == ff.full_name if dep.name.include?("/")
rescue
nil
end
next match unless match.nil?
dep.name == ff.name
end
rescue FormulaUnavailableError
# Silently ignore this case as we don't care about things used in
# taps that aren't currently tapped.
next
end end
end end
ignores.delete(:satisfied?)
end
select_used_dependents(dependents(deps), used_formulae, recursive, includes, ignores)
end
end
def self.select_used_dependents(dependents, used_formulae, recursive, includes, ignores)
dependents.select do |d|
deps = if recursive
recursive_includes(Dependency, d, includes, ignores)
else
select_includes(d.deps, ignores, includes)
end
used_formulae.all? do |ff|
deps.any? do |dep|
match = begin
dep.to_formula.full_name == ff.full_name if dep.name.include?("/")
rescue
nil
end
next match unless match.nil?
dep.name == ff.name
end
rescue FormulaUnavailableError
# Silently ignore this case as we don't care about things used in
# taps that aren't currently tapped.
next
end end
end end
end end

View File

@ -1,8 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
require "cmd/uses"
require "fileutils"
RSpec.describe "brew uses" do RSpec.describe Homebrew::Cmd::Uses do
include FileUtils include FileUtils
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"