Skip to content

Commit f3a83f7

Browse files
committed
Improvements to HOMEBREW_USE_INTERNAL_API
1 parent 4486245 commit f3a83f7

File tree

8 files changed

+55
-13
lines changed

8 files changed

+55
-13
lines changed

Library/Homebrew/attestation.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ def self.gh_executable
9898
# @api private
9999
sig { params(formulae: T::Array[Formula]).returns(T::Array[Formula]) }
100100
def self.sort_formulae_for_install(formulae)
101-
if formulae.include?(Formula["gh"])
102-
[Formula["gh"]] | formulae
101+
gh = formulae.find { |f| f.full_name == "gh" }
102+
if gh
103+
[gh] | formulae
103104
else
104105
Homebrew::Attestation.gh_executable
105106
formulae

Library/Homebrew/cli/named_args.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ def load_formula_or_cask(name, only: nil, method: nil, warn: false)
364364
begin
365365
case method
366366
when nil, :factory
367-
Formulary.factory(name, *@override_spec,
368-
warn:, force_bottle: @force_bottle, flags: @flags, prefer_stub: true)
367+
Formulary.factory_stub(name, *@override_spec, warn:, force_bottle: @force_bottle, flags: @flags)
369368
when :resolve
370369
resolve_formula(name)
371370
when :latest_kegs

Library/Homebrew/cli/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def formulae(argv)
733733
next if arg.match?(HOMEBREW_CASK_TAP_CASK_REGEX)
734734

735735
begin
736-
Formulary.factory(arg, spec, flags: argv.select { |a| a.start_with?("--") }, prefer_stub: true)
736+
Formulary.factory_stub(arg, spec, flags: argv.select { |a| a.start_with?("--") })
737737
rescue FormulaUnavailableError, FormulaSpecificationError
738738
nil
739739
end

Library/Homebrew/extend/kernel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def ensure_executable!(name, formula_name = nil, reason: "", latest: false)
201201
return executable if executable.exist?
202202

203203
require "formula"
204-
Formula[formula_name].ensure_installed!(reason:, latest:).opt_bin/name
204+
Formulary.factory_stub(formula_name).ensure_installed!(reason:, latest:).opt_bin/name
205205
end
206206

207207
sig { params(size_in_bytes: T.any(Integer, Float)).returns(String) }

Library/Homebrew/extend/os/mac/pkgconf.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def macos_sdk_mismatch
1111
return if OS::Mac.version.prerelease? || OS::Mac.version.outdated_release?
1212

1313
pkgconf = begin
14-
::Formula["pkgconf"]
14+
::Formulary.factory_stub("pkgconf")
1515
rescue FormulaUnavailableError
1616
nil
1717
end

Library/Homebrew/extend/pathname.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,8 @@ def which_install_info
474474
@which_install_info ||=
475475
if File.executable?("/usr/bin/install-info")
476476
"/usr/bin/install-info"
477-
elsif Formula["texinfo"].any_version_installed?
478-
(Formula["texinfo"].opt_bin/"install-info").to_s
477+
elsif (texinfo_formula = Formulary.factory_stub("texinfo")).any_version_installed?
478+
(texinfo_formula.opt_bin/"install-info").to_s
479479
end
480480
end
481481
end

Library/Homebrew/formulary.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def self.resolve(
559559
end
560560
else
561561
rack = to_rack(name)
562-
alias_path = factory(name, force_bottle:, flags:, prefer_stub: true).alias_path
562+
alias_path = factory_stub(name, force_bottle:, flags:).alias_path
563563
f = from_rack(rack, *spec, alias_path:, force_bottle:, flags:)
564564
end
565565

@@ -1172,6 +1172,48 @@ def self.factory(
11721172
formula
11731173
end
11741174

1175+
# A shortcut for calling `factory` with `prefer_stub: true`.
1176+
#
1177+
# Note: this method returns a stubbed formula which will include only:
1178+
#
1179+
# * name
1180+
# * version
1181+
# * revision
1182+
# * version_scheme
1183+
# * bottle information (for the current OS's bottle, only)
1184+
# * aliases
1185+
# * oldnames
1186+
# * any other data that can be computed using only this information
1187+
#
1188+
# Only use the output for operations that do not require full formula data.
1189+
#
1190+
# @see .factory
1191+
# @api internal
1192+
sig {
1193+
params(
1194+
ref: T.any(Pathname, String),
1195+
spec: Symbol,
1196+
alias_path: T.nilable(T.any(Pathname, String)),
1197+
from: T.nilable(Symbol),
1198+
warn: T::Boolean,
1199+
force_bottle: T::Boolean,
1200+
flags: T::Array[String],
1201+
ignore_errors: T::Boolean,
1202+
).returns(Formula)
1203+
}
1204+
def self.factory_stub(
1205+
ref,
1206+
spec = :stable,
1207+
alias_path: nil,
1208+
from: nil,
1209+
warn: false,
1210+
force_bottle: false,
1211+
flags: [],
1212+
ignore_errors: false
1213+
)
1214+
factory(ref, spec, alias_path:, from:, warn:, force_bottle:, flags:, ignore_errors:, prefer_stub: true)
1215+
end
1216+
11751217
# Return a {Formula} instance for the given rack.
11761218
#
11771219
# @param spec when nil, will auto resolve the formula's spec.

Library/Homebrew/style.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,20 @@ def self.github_workflow_files
327327

328328
def self.shellcheck
329329
require "formula"
330-
shellcheck_stub = Formulary.factory("shellcheck", prefer_stub: true)
330+
shellcheck_stub = Formulary.factory_stub("shellcheck")
331331
shellcheck_stub.ensure_installed!(latest: true, reason: "shell style checks").opt_bin/"shellcheck"
332332
end
333333

334334
def self.shfmt
335335
require "formula"
336-
shfmt_stub = Formulary.factory("shfmt", prefer_stub: true)
336+
shfmt_stub = Formulary.factory_stub("shfmt")
337337
shfmt_stub.ensure_installed!(latest: true, reason: "formatting shell scripts")
338338
HOMEBREW_LIBRARY/"Homebrew/utils/shfmt.sh"
339339
end
340340

341341
def self.actionlint
342342
require "formula"
343-
actionlint_stub = Formulary.factory("actionlint", prefer_stub: true)
343+
actionlint_stub = Formulary.factory_stub("actionlint")
344344
actionlint_stub.ensure_installed!(latest: true, reason: "GitHub Actions checks").opt_bin/"actionlint"
345345
end
346346

0 commit comments

Comments
 (0)