Skip to content

Commit

Permalink
Merge pull request Homebrew#16647 from reitermarkus/uses_from_macos_sig
Browse files Browse the repository at this point in the history
Refactor and add type signature for `uses_from_macos`.
  • Loading branch information
reitermarkus authored Feb 13, 2024
2 parents 283c52f + c6788bb commit 1c4b06f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
1 change: 1 addition & 0 deletions Library/Homebrew/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def merge_temporality(deps)
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)

Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3374,6 +3374,12 @@ def depends_on(dep)
# 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}.
sig {
params(
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, bounds = {})
specs.each { |spec| spec.uses_from_macos(dep, bounds) }
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def self.load_formula_from_api(name, flags:)
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)
Expand Down
36 changes: 18 additions & 18 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def initialize(flags: [])
@deprecated_options = []
@build = BuildOptions.new(Options.create(@flags), options)
@compiler_failures = []
@uses_from_macos_elements = []
end

def initialize_dup(other)
Expand All @@ -62,7 +61,6 @@ def initialize_dup(other)
@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
Expand All @@ -77,7 +75,6 @@ def freeze
@deprecated_options.freeze
@build.freeze
@compiler_failures.freeze
@uses_from_macos_elements.freeze
super
end

Expand Down Expand Up @@ -189,33 +186,36 @@ def depends_on(spec)
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: 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, 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

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)
depends_on UsesFromMacOSDependency.new(dep, 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
Expand Down

0 comments on commit 1c4b06f

Please sign in to comment.