Skip to content

Commit fda1645

Browse files
authored
Merge pull request #20059 from Homebrew/improve_caveats_completions
Improve completions (and elisp) output in caveats
2 parents dd7b954 + ff710f8 commit fda1645

File tree

5 files changed

+60
-40
lines changed

5 files changed

+60
-40
lines changed

Library/Homebrew/caveats.rb

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,54 @@ class Caveats
1414
sig { params(formula: Formula).void }
1515
def initialize(formula)
1616
@formula = formula
17+
@caveats = T.let(nil, T.nilable(String))
18+
@completions_and_elisp = T.let(nil, T.nilable(T::Array[String]))
1719
end
1820

1921
sig { returns(String) }
2022
def caveats
21-
caveats = []
22-
build = formula.build
23-
begin
24-
formula.build = Tab.for_formula(formula)
25-
string = formula.caveats.to_s
26-
caveats << "#{string.chomp}\n" unless string.empty?
27-
ensure
28-
formula.build = build
29-
end
30-
caveats << keg_only_text
31-
32-
valid_shells = [:bash, :zsh, :fish, :pwsh].freeze
33-
current_shell = Utils::Shell.preferred || Utils::Shell.parent
34-
shells = if current_shell.present? &&
35-
(shell_sym = current_shell.to_sym) &&
36-
valid_shells.include?(shell_sym)
37-
[shell_sym]
38-
else
39-
valid_shells
40-
end
41-
shells.each do |shell|
42-
caveats << function_completion_caveats(shell)
23+
@caveats ||= begin
24+
caveats = []
25+
build = formula.build
26+
begin
27+
formula.build = Tab.for_formula(formula)
28+
string = formula.caveats.to_s
29+
caveats << "#{string.chomp}\n" unless string.empty?
30+
ensure
31+
formula.build = build
32+
end
33+
caveats << keg_only_text
34+
caveats << service_caveats
35+
caveats.compact.join("\n")
4336
end
37+
end
4438

45-
caveats << service_caveats
46-
caveats << elisp_caveats
47-
caveats.compact.join("\n")
39+
sig { returns(T::Boolean) }
40+
def empty?
41+
caveats.blank? && completions_and_elisp.blank?
4842
end
4943

50-
delegate [:empty?, :to_s] => :caveats
44+
delegate [:to_s] => :caveats
45+
46+
sig { returns(T::Array[String]) }
47+
def completions_and_elisp
48+
@completions_and_elisp ||= begin
49+
valid_shells = [:bash, :zsh, :fish, :pwsh].freeze
50+
current_shell = Utils::Shell.preferred || Utils::Shell.parent
51+
shells = if current_shell.present? &&
52+
(shell_sym = current_shell.to_sym) &&
53+
valid_shells.include?(shell_sym)
54+
[shell_sym]
55+
else
56+
valid_shells
57+
end
58+
completions_and_elisp = shells.map do |shell|
59+
function_completion_caveats(shell)
60+
end
61+
completions_and_elisp << elisp_caveats
62+
completions_and_elisp.compact
63+
end
64+
end
5165

5266
sig { params(skip_reason: T::Boolean).returns(T.nilable(String)) }
5367
def keg_only_text(skip_reason: false)

Library/Homebrew/formula_installer.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,9 +892,11 @@ def caveats
892892
return if quiet?
893893

894894
caveats = Caveats.new(formula)
895-
896895
return if caveats.empty?
897896

897+
Homebrew.messages.record_completions_and_elisp(caveats.completions_and_elisp)
898+
return if caveats.caveats.empty?
899+
898900
@show_summary_heading = true
899901
ohai "Caveats", caveats.to_s
900902
Homebrew.messages.record_caveats(formula.name, caveats)

Library/Homebrew/messages.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Messages
1616
sig { void }
1717
def initialize
1818
@caveats = T.let([], T::Array[T::Hash[Symbol, Symbol]])
19+
@completions_and_elisp = T.let(Set.new, T::Set[String])
1920
@package_count = T.let(0, Integer)
2021
@install_times = T.let([], T::Array[T::Hash[String, Float]])
2122
end
@@ -25,6 +26,11 @@ def record_caveats(package, caveats)
2526
@caveats.push(package:, caveats:)
2627
end
2728

29+
sig { params(completions_and_elisp: T::Array[String]).void }
30+
def record_completions_and_elisp(completions_and_elisp)
31+
@completions_and_elisp.merge(completions_and_elisp)
32+
end
33+
2834
sig { params(package: String, elapsed_time: Float).void }
2935
def package_installed(package, elapsed_time)
3036
@package_count += 1
@@ -40,13 +46,14 @@ def display_messages(force_caveats: false, display_times: false)
4046
sig { params(force: T::Boolean).void }
4147
def display_caveats(force: false)
4248
return if @package_count.zero?
49+
return if @caveats.empty? && @completions_and_elisp.empty?
50+
51+
oh1 "Caveats" unless @completions_and_elisp.empty?
52+
@completions_and_elisp.each { |c| puts c }
4353
return if @package_count == 1 && !force
44-
return if @caveats.empty?
4554

46-
oh1 "Caveats"
47-
@caveats.each do |c|
48-
ohai c[:package], c[:caveats]
49-
end
55+
oh1 "Caveats" if @completions_and_elisp.empty?
56+
@caveats.each { |c| ohai c[:package], c[:caveats] }
5057
end
5158

5259
sig { void }

Library/Homebrew/sorbet/rbi/dsl/caveats.rbi

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Library/Homebrew/test/caveats_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def caveats
242242
url "foo-1.0"
243243
end
244244
end
245-
let(:caveats) { described_class.new(f).caveats }
245+
let(:caveats) { described_class.new(f) }
246246
let(:path) { f.prefix.resolved_path }
247247

248248
let(:bash_completion_dir) { path/"etc/bash_completion.d" }
@@ -261,25 +261,25 @@ def caveats
261261
it "includes where Bash completions have been installed to" do
262262
bash_completion_dir.mkpath
263263
FileUtils.touch bash_completion_dir/f.name
264-
expect(caveats).to include(HOMEBREW_PREFIX/"etc/bash_completion.d")
264+
expect(caveats.completions_and_elisp.join).to include(HOMEBREW_PREFIX/"etc/bash_completion.d")
265265
end
266266

267267
it "includes where fish completions have been installed to" do
268268
fish_vendor_completions.mkpath
269269
FileUtils.touch fish_vendor_completions/f.name
270-
expect(caveats).to include(HOMEBREW_PREFIX/"share/fish/vendor_completions.d")
270+
expect(caveats.completions_and_elisp.join).to include(HOMEBREW_PREFIX/"share/fish/vendor_completions.d")
271271
end
272272

273273
it "includes where zsh completions have been installed to" do
274274
zsh_site_functions.mkpath
275275
FileUtils.touch zsh_site_functions/f.name
276-
expect(caveats).to include(HOMEBREW_PREFIX/"share/zsh/site-functions")
276+
expect(caveats.completions_and_elisp.join).to include(HOMEBREW_PREFIX/"share/zsh/site-functions")
277277
end
278278

279279
it "includes where pwsh completions have been installed to" do
280280
pwsh_completion_dir.mkpath
281281
FileUtils.touch pwsh_completion_dir/f.name
282-
expect(caveats).to include(HOMEBREW_PREFIX/"share/pwsh/completions")
282+
expect(caveats.completions_and_elisp.join).to include(HOMEBREW_PREFIX/"share/pwsh/completions")
283283
end
284284
end
285285
end

0 commit comments

Comments
 (0)