Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Formula#loaded_from_api?. #15434

Merged
merged 1 commit into from May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions Library/Homebrew/formula.rb
Expand Up @@ -427,6 +427,12 @@ def bottle_for_tag(tag = nil)
# @see .version
delegate version: :active_spec

# Whether this formula was loaded using the formulae.brew.sh API
# @!method loaded_from_api?
# @private
# @see .loaded_from_api?
delegate loaded_from_api?: :"self.class"

def update_head_version
return unless head?
return unless head.downloader.is_a?(VCSDownloadStrategy)
Expand Down Expand Up @@ -2238,7 +2244,7 @@ def to_hash_with_variations
hash = to_hash

# Take from API, merging in local install status.
if self.class.loaded_from_api && !Homebrew::EnvConfig.no_install_from_api?
if loaded_from_api? && !Homebrew::EnvConfig.no_install_from_api?
json_formula = Homebrew::API::Formula.all_formulae[name].dup
return json_formula.merge(
hash.slice("name", "installed", "linked_keg", "pinned", "outdated"),
Expand Down Expand Up @@ -2758,6 +2764,7 @@ def inherited(child)
@skip_clean_paths = Set.new
@link_overwrite_paths = Set.new
@allowed_missing_libraries = Set.new
@loaded_from_api = false
end
end

Expand All @@ -2784,7 +2791,7 @@ def freeze

# Whether this formula was loaded using the formulae.brew.sh API
# @private
attr_accessor :loaded_from_api
attr_predicate :loaded_from_api?

# Whether this formula contains OS/arch-specific blocks
# (e.g. `on_macos`, `on_arm`, `on_monterey :or_older`, `on_system :linux, macos: :big_sur_or_newer`).
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/formula.rbi
Expand Up @@ -3,6 +3,8 @@
# This file provides definitions for Forwardable#delegate, which is currently not supported by Sorbet.

class Formula
sig { returns(T::Boolean) }
def loaded_from_api?; end
def bottle_defined?; end
def bottle_tag?(tag = nil); end
def bottled?(tag = nil); end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/formula_installer.rb
Expand Up @@ -1191,7 +1191,7 @@ def fetch
if pour_bottle?(output_warning: true)
formula.fetch_bottle_tab
else
@formula = Homebrew::API::Formula.source_download(formula) if formula.class.loaded_from_api
@formula = Homebrew::API::Formula.source_download(formula) if formula.loaded_from_api?

formula.fetch_patches
formula.resources.each(&:fetch)
Expand Down Expand Up @@ -1227,7 +1227,7 @@ def pour
tab.unused_options = []
tab.built_as_bottle = true
tab.poured_from_bottle = true
tab.loaded_from_api = formula.class.loaded_from_api
tab.loaded_from_api = formula.loaded_from_api?
tab.installed_as_dependency = installed_as_dependency?
tab.installed_on_request = installed_on_request?
tab.time = Time.now.to_i
Expand Down
7 changes: 4 additions & 3 deletions Library/Homebrew/formulary.rb
Expand Up @@ -136,7 +136,7 @@ def self.load_formula_from_api(name, flags:)

mod.const_set(:BUILD_FLAGS, flags)

class_s = Formulary.class_s(name)
class_name = class_s(name)
json_formula = Homebrew::API::Formula.all_formulae[name]
json_formula = Homebrew::API.merge_variations(json_formula)

Expand All @@ -147,6 +147,8 @@ def self.load_formula_from_api(name, flags:)
end

klass = Class.new(::Formula) do
@loaded_from_api = true

desc json_formula["desc"]
homepage json_formula["homepage"]
license SPDX.string_to_license_expression(json_formula["license"])
Expand Down Expand Up @@ -311,8 +313,7 @@ def ruby_source_checksum
end
end

T.cast(klass, T.class_of(Formula)).loaded_from_api = true
mod.const_set(class_s, klass)
mod.const_set(class_name, klass)

cache[:api] ||= {}
cache[:api][name] = klass
Expand Down