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,22 +1,21 @@
# typed: true
# frozen_string_literal: true
# `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.
require "abstract_command"
require "formula"
require "cli/parser"
require "cask/caskroom"
require "dependencies_helpers"
require "ostruct"
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) }
def self.uses_args
Homebrew::CLI::Parser.new do
cmd_args do
description <<~EOS
Show formulae and casks that specify <formula> as a dependency; that is, show dependents
of <formula>. When given multiple formula arguments, show the intersection
@ -53,11 +52,9 @@ module Homebrew
named_args :formula, min: 1
end
end
def self.uses
args = uses_args.parse
sig { override.void }
def run
Formulary.enable_factory_cache!
used_formulae_missing = false
@ -80,7 +77,7 @@ module Homebrew
!args.include_optional? &&
!args.skip_recommended?
uses = intersection_of_dependents(use_runtime_dependents, used_formulae, args:)
uses = intersection_of_dependents(use_runtime_dependents, used_formulae)
return if uses.empty?
@ -88,7 +85,9 @@ module Homebrew
odie "Missing formulae should not have dependents!" if used_formulae_missing
end
def self.intersection_of_dependents(use_runtime_dependents, used_formulae, args:)
private
def intersection_of_dependents(use_runtime_dependents, used_formulae)
recursive = args.recursive?
show_formulae_and_casks = !args.formula? && !args.cask?
includes, ignores = args_includes_ignores(args)
@ -138,7 +137,7 @@ module Homebrew
end
end
def self.select_used_dependents(dependents, used_formulae, recursive, includes, ignores)
def select_used_dependents(dependents, used_formulae, recursive, includes, ignores)
dependents.select do |d|
deps = if recursive
recursive_includes(Dependency, d, includes, ignores)
@ -165,3 +164,5 @@ module Homebrew
end
end
end
end
end

View File

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