Merge pull request #20359 from Homebrew/replace-ensure_formula_installed!

Replace `ensure_formula_installed!` with `Formula#ensure_installed!`
This commit is contained in:
Carlo Cabrera 2025-08-01 21:13:59 +00:00 committed by GitHub
commit a29a643c66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 55 additions and 21 deletions

View File

@ -354,9 +354,7 @@ module Homebrew
end end
return if gnu_tar_formula.blank? return if gnu_tar_formula.blank?
ensure_formula_installed!(gnu_tar_formula.name, reason: "bottling") gnu_tar_formula.ensure_installed!(reason: "bottling")
gnu_tar_formula
end end
sig { params(mtime: String, default_tar: T::Boolean).returns([String, T::Array[String]]) } sig { params(mtime: String, default_tar: T::Boolean).returns([String, T::Array[String]]) }

View File

@ -135,10 +135,7 @@ module Homebrew
if args.repology? && !Utils::Curl.curl_supports_tls13? if args.repology? && !Utils::Curl.curl_supports_tls13?
begin begin
unless HOMEBREW_BREWED_CURL_PATH.exist? Formula["curl"].ensure_installed!(reason: "Repology queries") unless HOMEBREW_BREWED_CURL_PATH.exist?
require "formula"
ensure_formula_installed!("curl", reason: "Repology queries")
end
rescue FormulaUnavailableError rescue FormulaUnavailableError
opoo "A newer `curl` is required for Repology queries." opoo "A newer `curl` is required for Repology queries."
end end

View File

@ -31,8 +31,7 @@ module Homebrew
ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path
ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme
require "formula" require "formula"
ensure_formula_installed!( Formula["bat"].ensure_installed!(
"bat",
reason: "displaying <formula>/<cask> source", reason: "displaying <formula>/<cask> source",
# The user might want to capture the output of `brew cat ...` # The user might want to capture the output of `brew cat ...`
# Redirect stdout to stderr # Redirect stdout to stderr

View File

@ -449,6 +449,8 @@ module Kernel
} }
def ensure_formula_installed!(formula_name, reason: "", latest: false, def ensure_formula_installed!(formula_name, reason: "", latest: false,
output_to_stderr: true, quiet: false) output_to_stderr: true, quiet: false)
odeprecated "ensure_formula_installed!", "Formula[\"#{formula_name}\"].ensure_installed!"
if output_to_stderr || quiet if output_to_stderr || quiet
file = if quiet file = if quiet
File::NULL File::NULL
@ -496,7 +498,7 @@ module Kernel
return executable if executable.exist? return executable if executable.exist?
require "formula" require "formula"
ensure_formula_installed!(formula_name, reason:, latest:).opt_bin/name Formula[formula_name].ensure_installed!(reason:, latest:).opt_bin/name
end end
sig { returns(T::Array[Pathname]) } sig { returns(T::Array[Pathname]) }

View File

@ -302,6 +302,44 @@ class Formula
Requirement.clear_cache Requirement.clear_cache
end end
# Ensure the given formula is installed
# This is useful for installing a utility formula (e.g. `shellcheck` for `brew style`)
sig {
params(
reason: String,
latest: T::Boolean,
output_to_stderr: T::Boolean,
quiet: T::Boolean,
).returns(T.self_type)
}
def ensure_installed!(reason: "", latest: false, output_to_stderr: true, quiet: false)
if output_to_stderr || quiet
file = if quiet
File::NULL
else
$stderr
end
# Call this method itself with redirected stdout
redirect_stdout(file) do
return ensure_installed!(latest:, reason:, output_to_stderr: false)
end
end
reason = " for #{reason}" if reason.present?
unless any_version_installed?
ohai "Installing `#{name}`#{reason}..."
safe_system HOMEBREW_BREW_FILE, "install", "--formula", full_name
end
if latest && !latest_version_installed?
ohai "Upgrading `#{name}`#{reason}..."
safe_system HOMEBREW_BREW_FILE, "upgrade", "--formula", full_name
end
self
end
private private
# Allow full name logic to be re-used between names, aliases and installed aliases. # Allow full name logic to be re-used between names, aliases and installed aliases.

View File

@ -322,21 +322,18 @@ module Homebrew
def self.shellcheck def self.shellcheck
require "formula" require "formula"
ensure_formula_installed!("shellcheck", latest: true, Formula["shellcheck"].ensure_installed!(latest: true, reason: "shell style checks").opt_bin/"shellcheck"
reason: "shell style checks").opt_bin/"shellcheck"
end end
def self.shfmt def self.shfmt
require "formula" require "formula"
ensure_formula_installed!("shfmt", latest: true, Formula["shfmt"].ensure_installed!(latest: true, reason: "formatting shell scripts")
reason: "formatting shell scripts")
HOMEBREW_LIBRARY/"Homebrew/utils/shfmt.sh" HOMEBREW_LIBRARY/"Homebrew/utils/shfmt.sh"
end end
def self.actionlint def self.actionlint
require "formula" require "formula"
ensure_formula_installed!("actionlint", latest: true, Formula["actionlint"].ensure_installed!(latest: true, reason: "GitHub Actions checks").opt_bin/"actionlint"
reason: "GitHub Actions checks").opt_bin/"actionlint"
end end
# Collection of style offenses. # Collection of style offenses.

View File

@ -186,7 +186,10 @@ RSpec.describe Utils::Git do
unless ENV["HOMEBREW_TEST_GENERIC_OS"] unless ENV["HOMEBREW_TEST_GENERIC_OS"]
it "installs git" do it "installs git" do
expect(described_class).to receive(:available?).and_return(false) expect(described_class).to receive(:available?).and_return(false)
expect(described_class).to receive(:ensure_formula_installed!).with("git") allow(CoreTap.instance).to receive(:installed?).and_return(true)
formula_double = instance_double(Formula)
allow(Formula).to receive(:[]).with("git").and_return(formula_double)
allow(formula_double).to receive(:ensure_installed!).and_return(formula_double)
expect(described_class).to receive(:available?).and_return(true) expect(described_class).to receive(:available?).and_return(true)
described_class.ensure_installed! described_class.ensure_installed!

View File

@ -104,7 +104,7 @@ module Utils
raise "Refusing to install Git on a generic OS." if ENV["HOMEBREW_TEST_GENERIC_OS"] raise "Refusing to install Git on a generic OS." if ENV["HOMEBREW_TEST_GENERIC_OS"]
require "formula" require "formula"
ensure_formula_installed!("git") Formula["git"].ensure_installed!
clear_available_cache clear_available_cache
rescue rescue
raise "Git is unavailable" raise "Git is unavailable"

View File

@ -153,7 +153,7 @@ module PyPI
@version ||= T.let(match[2], T.nilable(String)) @version ||= T.let(match[2], T.nilable(String))
elsif @is_url elsif @is_url
require "formula" require "formula"
ensure_formula_installed!(@python_name) Formula[@python_name].ensure_installed!
# The URL might be a source distribution hosted somewhere; # The URL might be a source distribution hosted somewhere;
# try and use `pip install -q --no-deps --dry-run --report ...` to get its # try and use `pip install -q --no-deps --dry-run --report ...` to get its
@ -262,7 +262,7 @@ module PyPI
odie "Missing #{missing_msg}" unless install_dependencies odie "Missing #{missing_msg}" unless install_dependencies
ohai "Installing #{missing_msg}" ohai "Installing #{missing_msg}"
require "formula" require "formula"
missing_dependencies.each(&:ensure_formula_installed!) missing_dependencies.each { |dep| Formula[dep].ensure_installed! }
end end
python_deps = formula.deps python_deps = formula.deps
@ -337,7 +337,7 @@ module PyPI
end end
require "formula" require "formula"
ensure_formula_installed!(python_name) Formula[python_name].ensure_installed!
# Resolve the dependency tree of all input packages # Resolve the dependency tree of all input packages
show_info = !print_only && !silent show_info = !print_only && !silent