Skip to content

Commit 9b67db0

Browse files
committed
Move expand action constants to Dependable module
PRUNE, SKIP, and KEEP_BUT_PRUNE_RECURSIVE_DEPS were defined separately on Dependency and Requirement with identical values. This coupling was fragile — if either constant changed, the polymorphic code in dependencies_helpers.rb would silently break. Move the constants to the shared Dependable module (included by both classes) so there is a single source of truth, and update all callers to reference Dependable:: directly.
1 parent e6f0f61 commit 9b67db0

15 files changed

Lines changed: 44 additions & 49 deletions

File tree

Library/Homebrew/build.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def expand_reqs
6262
dependent = T.cast(dependent, Formula)
6363
build = effective_build_options_for(dependent)
6464
if req.prune_from_option?(build) || req.prune_if_build_and_not_dependent?(dependent, formula) || req.test?
65-
next Requirement::PRUNE
65+
next Dependable::PRUNE
6666
end
6767
end
6868
end
@@ -74,9 +74,9 @@ def expand_deps
7474
if dep.prune_from_option?(build) ||
7575
dep.prune_if_build_and_not_dependent?(T.cast(dependent, Formula), formula) ||
7676
(dep.test? && !dep.build?) || dep.implicit?
77-
next Dependency::PRUNE
77+
next Dependable::PRUNE
7878
elsif dep.build?
79-
next Dependency::KEEP_BUT_PRUNE_RECURSIVE_DEPS
79+
next Dependable::KEEP_BUT_PRUNE_RECURSIVE_DEPS
8080
end
8181
end
8282
end

Library/Homebrew/dependable.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
module Dependable
88
extend T::Helpers
99

10+
# Return from an {Dependency.expand} or {Requirement.expand} block to remove
11+
# a dependency/requirement and all of its recursive dependencies from the result list.
12+
PRUNE = :prune
13+
# Return from a {Dependency.expand} block to omit a dependency from the result
14+
# list but continue expanding its children.
15+
SKIP = :skip
16+
# Return from a {Dependency.expand} block to keep a dependency in the result
17+
# list but stop recursing into its own dependencies.
18+
KEEP_BUT_PRUNE_RECURSIVE_DEPS = :keep_but_prune_recursive_deps
19+
1020
# `:run` and `:linked` are no longer used but keep them here to avoid their
1121
# misuse in future.
1222
RESERVED_TAGS = T.let(

Library/Homebrew/dependencies_helpers.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ def recursive_req_includes(root_dependent, includes, ignores)
5050
def recursive_includes(klass, root_dependent, includes, ignores)
5151
cache_key = "recursive_includes_#{includes}_#{ignores}"
5252

53-
# Dependency::PRUNE and Requirement::PRUNE are both :prune, so these
54-
# constants work regardless of which klass is passed.
5553
klass.expand(root_dependent, cache_key:) do |dependent, dep|
56-
next Dependency::PRUNE if ignores.any? { |ignore| dep.public_send(ignore) }
57-
next Dependency::PRUNE if includes.none? do |include|
54+
next Dependable::PRUNE if ignores.any? { |ignore| dep.public_send(ignore) }
55+
next Dependable::PRUNE if includes.none? do |include|
5856
# Ignore indirect test dependencies
5957
next if include == :test? && dependent != root_dependent
6058

@@ -63,7 +61,7 @@ def recursive_includes(klass, root_dependent, includes, ignores)
6361

6462
# If a tap isn't installed, we can't find the dependencies of one of
6563
# its formulae and an exception will be thrown if we try.
66-
next Dependency::KEEP_BUT_PRUNE_RECURSIVE_DEPS if klass == Dependency && (tap = dep.tap) && !tap.installed?
64+
next Dependable::KEEP_BUT_PRUNE_RECURSIVE_DEPS if klass == Dependency && (tap = dep.tap) && !tap.installed?
6765
end
6866
end
6967

Library/Homebrew/dependency.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@
1010
class Dependency
1111
include Dependable
1212

13-
# Return from an {expand} block to keep a dependency in the result list
14-
# but stop recursing into its own dependencies.
15-
KEEP_BUT_PRUNE_RECURSIVE_DEPS = :keep_but_prune_recursive_deps
16-
# Return from an {expand} block to remove a dependency and all of its
17-
# recursive dependencies from the result list.
18-
PRUNE = :prune
19-
# Return from an {expand} block to omit a dependency from the result list
20-
# but continue expanding its children.
21-
SKIP = :skip
22-
2313
sig { returns(String) }
2414
attr_reader :name
2515

@@ -211,13 +201,13 @@ def expand(dependent, deps = dependent.deps, cache_key: nil, cache_timestamp: ni
211201
next if dependent.name == dep.name
212202

213203
case action(dependent, dep, &block)
214-
when PRUNE
204+
when Dependable::PRUNE
215205
next
216-
when SKIP
206+
when Dependable::SKIP
217207
next if @expand_stack.include? dep.name
218208

219209
expanded_deps.concat(expand(dep.to_formula, cache_key:, &block))
220-
when KEEP_BUT_PRUNE_RECURSIVE_DEPS
210+
when Dependable::KEEP_BUT_PRUNE_RECURSIVE_DEPS
221211
expanded_deps << dep
222212
else
223213
next if @expand_stack.include? dep.name
@@ -252,7 +242,7 @@ def action(dependent, dep, &block)
252242
if block
253243
yield dependent, dep
254244
elsif dep.optional? || dep.recommended?
255-
PRUNE unless T.cast(dependent, Formula).build.with?(dep)
245+
Dependable::PRUNE unless T.cast(dependent, Formula).build.with?(dep)
256246
end
257247
end
258248

Library/Homebrew/dev-cmd/test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def run
5858

5959
# Don't test formulae missing test dependencies
6060
missing_test_deps = f.recursive_dependencies do |dependent, dependency|
61-
next Dependency::PRUNE if dependency.installed?
61+
next Dependable::PRUNE if dependency.installed?
6262
next if dependency.test? && dependent == f
6363

64-
next Dependency::PRUNE unless dependency.required?
64+
next Dependable::PRUNE unless dependency.required?
6565
end.map(&:to_s)
6666
unless missing_test_deps.empty?
6767
ofail "#{f.full_name} is missing test dependencies: #{missing_test_deps.join(" ")}"

Library/Homebrew/dev-cmd/unbottled.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def deps_uses_from_formulae(all_formulae)
185185

186186
all_formulae.each do |f|
187187
deps = Dependency.expand(f, cache_key: "unbottled") do |_, dep|
188-
next Dependency::PRUNE if dep.optional?
188+
next Dependable::PRUNE if dep.optional?
189189
end.map(&:to_formula)
190190
deps_hash[f.name] = deps
191191

Library/Homebrew/extend/os/linux/cmd/update-report.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def migrate_gcc_dependents_if_needed
2020
formula,
2121
cache_key: "update-report",
2222
) do |_, dependency|
23-
next Dependency::PRUNE if dependency.build? || dependency.test?
23+
next Dependable::PRUNE if dependency.build? || dependency.test?
2424
end
2525
next unless recursive_runtime_dependencies.map(&:name).include? "gcc"
2626

Library/Homebrew/formula.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,13 +3264,13 @@ def declared_runtime_dependencies
32643264
cache_timestamp = Time.now
32653265
end
32663266
Dependency.expand(self, cache_key:, cache_timestamp:) do |_, dependency|
3267-
next Dependency::PRUNE if dependency.build?
3267+
next Dependable::PRUNE if dependency.build?
32683268
next if dependency.required?
32693269

32703270
if build.any_args_or_options?
3271-
next Dependency::PRUNE if build.without?(dependency)
3271+
next Dependable::PRUNE if build.without?(dependency)
32723272
elsif !dependency.recommended?
3273-
next Dependency::PRUNE
3273+
next Dependable::PRUNE
32743274
end
32753275
end
32763276
ensure

Library/Homebrew/formula_installer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def check_requirements(req_map)
704704
def runtime_requirements(formula)
705705
runtime_deps = formula.runtime_formula_dependencies(undeclared: false)
706706
recursive_requirements = formula.recursive_requirements do |dependent, _|
707-
next Requirement::PRUNE unless runtime_deps.include?(dependent)
707+
next Dependable::PRUNE unless runtime_deps.include?(dependent)
708708
end
709709
(recursive_requirements.to_a + formula.requirements.to_a).reject(&:build?).uniq
710710
end
@@ -733,7 +733,7 @@ def expand_requirements
733733
((req.build? || req.test?) && !keep_build_test) ||
734734
formula_deps_map[dependent.name]&.build? ||
735735
(only_deps? && f == dependent)
736-
next Requirement::PRUNE
736+
next Dependable::PRUNE
737737
else
738738
unsatisfied_reqs[dependent] << req
739739
end
@@ -762,9 +762,9 @@ def expand_dependencies_for_formula(formula)
762762
bottle_os_version = @bottle_built_os_version
763763

764764
if dep.prune_from_option?(build) || ((dep.build? || dep.test?) && !keep_build_test)
765-
next Dependency::PRUNE
765+
next Dependable::PRUNE
766766
elsif dep.satisfied?(minimum_version:, minimum_revision:, bottle_os_version:)
767-
next Dependency::SKIP
767+
next Dependable::SKIP
768768
end
769769
end
770770
end

Library/Homebrew/language/python.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,18 @@ def virtualenv_create(venv_root, python = "python", formula = T.cast(self, Formu
195195
# Find any Python bindings provided by recursive dependencies
196196
pth_contents = []
197197
formula.recursive_dependencies do |dependent, dep|
198-
next Dependency::PRUNE if dep.build? || dep.test?
198+
next Dependable::PRUNE if dep.build? || dep.test?
199199
# Apply default filter
200-
next Dependency::PRUNE if (dep.optional? || dep.recommended?) && !T.cast(dependent,
200+
next Dependable::PRUNE if (dep.optional? || dep.recommended?) && !T.cast(dependent,
201201
Formula).build.with?(dep)
202202
# Do not add the main site-package provided by the brewed
203203
# Python formula, to keep the virtual-env's site-package pristine
204-
next Dependency::PRUNE if python_names.include? dep.name
204+
next Dependable::PRUNE if python_names.include? dep.name
205205
# Skip uses_from_macos dependencies as these imply no Python bindings
206-
next Dependency::PRUNE if dep.uses_from_macos?
206+
next Dependable::PRUNE if dep.uses_from_macos?
207207

208208
dep_site_packages = dep.to_formula.opt_prefix/Language::Python.site_packages(python)
209-
next Dependency::PRUNE unless dep_site_packages.exist?
209+
next Dependable::PRUNE unless dep_site_packages.exist?
210210

211211
pth_contents << "import site; site.addsitedir('#{dep_site_packages}')\n"
212212
nil # Return nil to satisfy T.nilable(Symbol) block sig (Array from << would violate it).

0 commit comments

Comments
 (0)