Skip to content

Commit a29a643

Browse files
authored
Merge pull request #20359 from Homebrew/replace-ensure_formula_installed!
Replace `ensure_formula_installed!` with `Formula#ensure_installed!`
2 parents 9dc1112 + fd80dd9 commit a29a643

File tree

9 files changed

+55
-21
lines changed

9 files changed

+55
-21
lines changed

Library/Homebrew/dev-cmd/bottle.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,7 @@ def gnu_tar_formula_ensure_installed_if_needed!
354354
end
355355
return if gnu_tar_formula.blank?
356356

357-
ensure_formula_installed!(gnu_tar_formula.name, reason: "bottling")
358-
359-
gnu_tar_formula
357+
gnu_tar_formula.ensure_installed!(reason: "bottling")
360358
end
361359

362360
sig { params(mtime: String, default_tar: T::Boolean).returns([String, T::Array[String]]) }

Library/Homebrew/dev-cmd/bump.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ def run
135135

136136
if args.repology? && !Utils::Curl.curl_supports_tls13?
137137
begin
138-
unless HOMEBREW_BREWED_CURL_PATH.exist?
139-
require "formula"
140-
ensure_formula_installed!("curl", reason: "Repology queries")
141-
end
138+
Formula["curl"].ensure_installed!(reason: "Repology queries") unless HOMEBREW_BREWED_CURL_PATH.exist?
142139
rescue FormulaUnavailableError
143140
opoo "A newer `curl` is required for Repology queries."
144141
end

Library/Homebrew/dev-cmd/cat.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def run
3131
ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path
3232
ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme
3333
require "formula"
34-
ensure_formula_installed!(
35-
"bat",
34+
Formula["bat"].ensure_installed!(
3635
reason: "displaying <formula>/<cask> source",
3736
# The user might want to capture the output of `brew cat ...`
3837
# Redirect stdout to stderr

Library/Homebrew/extend/kernel.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ def redirect_stdout(file)
449449
}
450450
def ensure_formula_installed!(formula_name, reason: "", latest: false,
451451
output_to_stderr: true, quiet: false)
452+
odeprecated "ensure_formula_installed!", "Formula[\"#{formula_name}\"].ensure_installed!"
453+
452454
if output_to_stderr || quiet
453455
file = if quiet
454456
File::NULL
@@ -496,7 +498,7 @@ def ensure_executable!(name, formula_name = nil, reason: "", latest: false)
496498
return executable if executable.exist?
497499

498500
require "formula"
499-
ensure_formula_installed!(formula_name, reason:, latest:).opt_bin/name
501+
Formula[formula_name].ensure_installed!(reason:, latest:).opt_bin/name
500502
end
501503

502504
sig { returns(T::Array[Pathname]) }

Library/Homebrew/formula.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,44 @@ def build=(build_options)
302302
Requirement.clear_cache
303303
end
304304

305+
# Ensure the given formula is installed
306+
# This is useful for installing a utility formula (e.g. `shellcheck` for `brew style`)
307+
sig {
308+
params(
309+
reason: String,
310+
latest: T::Boolean,
311+
output_to_stderr: T::Boolean,
312+
quiet: T::Boolean,
313+
).returns(T.self_type)
314+
}
315+
def ensure_installed!(reason: "", latest: false, output_to_stderr: true, quiet: false)
316+
if output_to_stderr || quiet
317+
file = if quiet
318+
File::NULL
319+
else
320+
$stderr
321+
end
322+
# Call this method itself with redirected stdout
323+
redirect_stdout(file) do
324+
return ensure_installed!(latest:, reason:, output_to_stderr: false)
325+
end
326+
end
327+
328+
reason = " for #{reason}" if reason.present?
329+
330+
unless any_version_installed?
331+
ohai "Installing `#{name}`#{reason}..."
332+
safe_system HOMEBREW_BREW_FILE, "install", "--formula", full_name
333+
end
334+
335+
if latest && !latest_version_installed?
336+
ohai "Upgrading `#{name}`#{reason}..."
337+
safe_system HOMEBREW_BREW_FILE, "upgrade", "--formula", full_name
338+
end
339+
340+
self
341+
end
342+
305343
private
306344

307345
# Allow full name logic to be re-used between names, aliases and installed aliases.

Library/Homebrew/style.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,18 @@ def self.github_workflow_files
322322

323323
def self.shellcheck
324324
require "formula"
325-
ensure_formula_installed!("shellcheck", latest: true,
326-
reason: "shell style checks").opt_bin/"shellcheck"
325+
Formula["shellcheck"].ensure_installed!(latest: true, reason: "shell style checks").opt_bin/"shellcheck"
327326
end
328327

329328
def self.shfmt
330329
require "formula"
331-
ensure_formula_installed!("shfmt", latest: true,
332-
reason: "formatting shell scripts")
330+
Formula["shfmt"].ensure_installed!(latest: true, reason: "formatting shell scripts")
333331
HOMEBREW_LIBRARY/"Homebrew/utils/shfmt.sh"
334332
end
335333

336334
def self.actionlint
337335
require "formula"
338-
ensure_formula_installed!("actionlint", latest: true,
339-
reason: "GitHub Actions checks").opt_bin/"actionlint"
336+
Formula["actionlint"].ensure_installed!(latest: true, reason: "GitHub Actions checks").opt_bin/"actionlint"
340337
end
341338

342339
# Collection of style offenses.

Library/Homebrew/test/utils/git_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@
186186
unless ENV["HOMEBREW_TEST_GENERIC_OS"]
187187
it "installs git" do
188188
expect(described_class).to receive(:available?).and_return(false)
189-
expect(described_class).to receive(:ensure_formula_installed!).with("git")
189+
allow(CoreTap.instance).to receive(:installed?).and_return(true)
190+
formula_double = instance_double(Formula)
191+
allow(Formula).to receive(:[]).with("git").and_return(formula_double)
192+
allow(formula_double).to receive(:ensure_installed!).and_return(formula_double)
190193
expect(described_class).to receive(:available?).and_return(true)
191194

192195
described_class.ensure_installed!

Library/Homebrew/utils/git.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def self.ensure_installed!
104104
raise "Refusing to install Git on a generic OS." if ENV["HOMEBREW_TEST_GENERIC_OS"]
105105

106106
require "formula"
107-
ensure_formula_installed!("git")
107+
Formula["git"].ensure_installed!
108108
clear_available_cache
109109
rescue
110110
raise "Git is unavailable"

Library/Homebrew/utils/pypi.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def basic_metadata
153153
@version ||= T.let(match[2], T.nilable(String))
154154
elsif @is_url
155155
require "formula"
156-
ensure_formula_installed!(@python_name)
156+
Formula[@python_name].ensure_installed!
157157

158158
# The URL might be a source distribution hosted somewhere;
159159
# try and use `pip install -q --no-deps --dry-run --report ...` to get its
@@ -262,7 +262,7 @@ def self.update_python_resources!(formula, version: nil, package_name: nil, extr
262262
odie "Missing #{missing_msg}" unless install_dependencies
263263
ohai "Installing #{missing_msg}"
264264
require "formula"
265-
missing_dependencies.each(&:ensure_formula_installed!)
265+
missing_dependencies.each { |dep| Formula[dep].ensure_installed! }
266266
end
267267

268268
python_deps = formula.deps
@@ -337,7 +337,7 @@ def self.update_python_resources!(formula, version: nil, package_name: nil, extr
337337
end
338338

339339
require "formula"
340-
ensure_formula_installed!(python_name)
340+
Formula[python_name].ensure_installed!
341341

342342
# Resolve the dependency tree of all input packages
343343
show_info = !print_only && !silent

0 commit comments

Comments
 (0)