From 4762f644cb64fbcadeabd9f2505e185f7b0b17c3 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 12 Feb 2024 23:21:28 +0100 Subject: [PATCH 1/3] Remove `@uses_from_macos_elements` variable. --- Library/Homebrew/software_spec.rb | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 788bacfc32..1afb7884bc 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -46,7 +46,6 @@ class SoftwareSpec @deprecated_options = [] @build = BuildOptions.new(Options.create(@flags), options) @compiler_failures = [] - @uses_from_macos_elements = [] end def initialize_dup(other) @@ -62,7 +61,6 @@ class SoftwareSpec @deprecated_options = @deprecated_options.dup @build = @build.dup @compiler_failures = @compiler_failures.dup - @uses_from_macos_elements = @uses_from_macos_elements.dup end def freeze @@ -77,7 +75,6 @@ class SoftwareSpec @deprecated_options.freeze @build.freeze @compiler_failures.freeze - @uses_from_macos_elements.freeze super end @@ -198,24 +195,19 @@ class SoftwareSpec spec, tags = deps.is_a?(Hash) ? deps.first : deps raise TypeError, "Dependency name must be a string!" unless spec.is_a?(String) - @uses_from_macos_elements << deps - depends_on UsesFromMacOSDependency.new(spec, Array(tags), bounds: bounds) end # @deprecated def uses_from_macos_elements - # TODO: remove all @uses_from_macos_elements when removing this method - # Also remember to remove the delegate from formula.rb + # TODO: Remember to remove the delegate from `Formula`. odisabled "#uses_from_macos_elements", "#declared_deps" - @uses_from_macos_elements end # @deprecated def uses_from_macos_names - # TODO: Remember to remove the delegate from formula.rb + # TODO: Remember to remove the delegate from `Formula`. odisabled "#uses_from_macos_names", "#declared_deps" - uses_from_macos_elements.flat_map { |e| e.is_a?(Hash) ? e.keys : e } end def deps From d9712f4d503343b3ebc2e8b0bad18cc2feed49b8 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 12 Feb 2024 23:45:03 +0100 Subject: [PATCH 2/3] Refactor and add type signature for `uses_from_macos`. --- Library/Homebrew/formula.rb | 10 ++++++++-- Library/Homebrew/formulary.rb | 7 ++++--- Library/Homebrew/software_spec.rb | 23 +++++++++++++++-------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 3cce9ccb3f..9120a5cd8d 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -3369,8 +3369,14 @@ class Formula # On macOS this is a no-op (as we use the provided system libraries) unless # `:since` specifies a minimum macOS version. # On Linux this will act as {.depends_on}. - def uses_from_macos(dep, bounds = {}) - specs.each { |spec| spec.uses_from_macos(dep, bounds) } + sig { + params( + dep: String, + bounds: T.any(Symbol, T::Array[Symbol]), + ).void + } + def uses_from_macos(dep = T.unsafe(nil), **bounds) + specs.each { |spec| spec.uses_from_macos(*dep, **bounds) } end # @!attribute [w] option diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 13dccfb851..2cfe6b8f0e 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -234,12 +234,13 @@ module Formulary dep_json["uses_from_macos"]&.each_with_index do |dep, index| bounds = dep_json.fetch("uses_from_macos_bounds", [])[index].dup || {} bounds.deep_transform_keys!(&:to_sym) - bounds.deep_transform_values! { |val| val.is_a?(String) ? val.to_sym : val } + bounds.deep_transform_values!(&:to_sym) if dep.is_a?(Hash) - uses_from_macos dep.deep_transform_values(&:to_sym).merge(bounds) + dep.deep_transform_values!(&:to_sym) + uses_from_macos(**T.unsafe(dep.merge(bounds))) else - uses_from_macos dep, bounds + uses_from_macos dep, **bounds end end end diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 1afb7884bc..a6e9776aa2 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -186,16 +186,23 @@ class SoftwareSpec add_dep_option(dep) if dep end - def uses_from_macos(deps, bounds = {}) - if deps.is_a?(Hash) - bounds = deps.dup - deps = [T.unsafe(bounds).shift].to_h + sig { + params( + dep: String, + bounds: T.any(Symbol, T::Array[Symbol]), + ).void + } + def uses_from_macos(dep = T.unsafe(nil), **bounds) + bounds = bounds.dup + + if dep + tags = [] + else + dep, tags = bounds.shift + tags = [*tags] end - spec, tags = deps.is_a?(Hash) ? deps.first : deps - raise TypeError, "Dependency name must be a string!" unless spec.is_a?(String) - - depends_on UsesFromMacOSDependency.new(spec, Array(tags), bounds: bounds) + depends_on UsesFromMacOSDependency.new(dep, tags, bounds: bounds) end # @deprecated From c6788bb27ec035a0924ca84156539f5fb5a8c35e Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 13 Feb 2024 00:42:54 +0100 Subject: [PATCH 3/3] Avoid `T.unsafe`. --- Library/Homebrew/dependency.rb | 1 + Library/Homebrew/formula.rb | 8 ++++---- Library/Homebrew/formulary.rb | 5 ++--- Library/Homebrew/software_spec.rb | 17 +++++++++-------- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Library/Homebrew/dependency.rb b/Library/Homebrew/dependency.rb index 6f3b88c8a2..8896435b67 100644 --- a/Library/Homebrew/dependency.rb +++ b/Library/Homebrew/dependency.rb @@ -230,6 +230,7 @@ end class UsesFromMacOSDependency < Dependency attr_reader :bounds + sig { params(name: String, tags: T::Array[Symbol], bounds: T::Hash[Symbol, Symbol]).void } def initialize(name, tags = [], bounds:) super(name, tags) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 9120a5cd8d..1a22f760b0 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -3371,12 +3371,12 @@ class Formula # On Linux this will act as {.depends_on}. sig { params( - dep: String, - bounds: T.any(Symbol, T::Array[Symbol]), + dep: T.any(String, T::Hash[T.any(String, Symbol), T.any(Symbol, T::Array[Symbol])]), + bounds: T::Hash[Symbol, Symbol], ).void } - def uses_from_macos(dep = T.unsafe(nil), **bounds) - specs.each { |spec| spec.uses_from_macos(*dep, **bounds) } + def uses_from_macos(dep, bounds = {}) + specs.each { |spec| spec.uses_from_macos(dep, bounds) } end # @!attribute [w] option diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 2cfe6b8f0e..584f4f4431 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -237,10 +237,9 @@ module Formulary bounds.deep_transform_values!(&:to_sym) if dep.is_a?(Hash) - dep.deep_transform_values!(&:to_sym) - uses_from_macos(**T.unsafe(dep.merge(bounds))) + uses_from_macos dep.deep_transform_values(&:to_sym).merge(bounds) else - uses_from_macos dep, **bounds + uses_from_macos dep, bounds end end end diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index a6e9776aa2..8dd3630877 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -188,18 +188,19 @@ class SoftwareSpec sig { params( - dep: String, - bounds: T.any(Symbol, T::Array[Symbol]), + dep: T.any(String, T::Hash[T.any(String, Symbol), T.any(Symbol, T::Array[Symbol])]), + bounds: T::Hash[Symbol, Symbol], ).void } - def uses_from_macos(dep = T.unsafe(nil), **bounds) - bounds = bounds.dup - - if dep - tags = [] - else + def uses_from_macos(dep, bounds = {}) + if dep.is_a?(Hash) + bounds = dep.dup dep, tags = bounds.shift + dep = T.cast(dep, String) tags = [*tags] + bounds = T.cast(bounds, T::Hash[Symbol, Symbol]) + else + tags = [] end depends_on UsesFromMacOSDependency.new(dep, tags, bounds: bounds)