Skip to content

Commit

Permalink
Merge pull request #17223 from reitermarkus/resource-sig
Browse files Browse the repository at this point in the history
Add `resource` signature.
  • Loading branch information
MikeMcQuaid authored May 6, 2024
2 parents 3d31594 + e39a421 commit 80b35e8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
13 changes: 12 additions & 1 deletion Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,17 @@ def synced_with_other_formulae?
# ```ruby
# resource("additional_files").stage { bin.install "my/extra/tool" }
# ```
delegate resource: :active_spec
#
# FIXME: This should not actually take a block. All resources should be defined
# at the top-level using {Formula.resource} instead
# (see https://github.com/Homebrew/brew/issues/17203#issuecomment-2093654431).
#
# @api public
sig {
params(name: String, klass: T.class_of(Resource), block: T.nilable(T.proc.bind(Resource).void))
.returns(T.nilable(Resource))
}
def resource(name, klass = Resource, &block) = active_spec.resource(name, klass, &block)

# An old name for the formula.
sig { returns(T.nilable(String)) }
Expand Down Expand Up @@ -3710,6 +3720,7 @@ def head(val = nil, specs = {}, &block)
# ```
#
# @api public
sig { params(name: String, klass: T.class_of(Resource), block: T.nilable(T.proc.bind(Resource).void)).void }
def resource(name, klass = Resource, &block)
specs.each do |spec|
spec.resource(name, klass, &block) unless spec.resource_defined?(name)
Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def resource_defined?(name)
resources.key?(name)
end

sig {
params(name: String, klass: T.class_of(Resource), block: T.nilable(T.proc.bind(Resource).void))
.returns(T.nilable(Resource))
}
def resource(name, klass = Resource, &block)
if block
raise DuplicateResourceError, name if resource_defined?(name)
Expand All @@ -132,6 +136,7 @@ def resource(name, klass = Resource, &block)

resources[name] = res
dependency_collector.add(res)
res
else
resources.fetch(name) { raise ResourceMissingError.new(owner, name) }
end
Expand Down
3 changes: 0 additions & 3 deletions Library/Homebrew/sorbet/rbi/parlour.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ class Formula
sig { params(args: T.untyped, options: T.untyped, block: T.untyped).returns(T::Boolean) }
def loaded_from_api?(*args, **options, &block); end

sig { params(args: T.untyped, options: T.untyped, block: T.untyped).returns(T.untyped) }
def resource(*args, **options, &block); end

sig { params(args: T.untyped, options: T.untyped, block: T.untyped).returns(T.untyped) }
def deps(*args, **options, &block); end

Expand Down

0 comments on commit 80b35e8

Please sign in to comment.