Skip to content

Commit b533b38

Browse files
Merge pull request #13033 from EricFromCanada/cask-search-descriptions
desc, search: also search cask descriptions
2 parents b2f3081 + 2fdc70c commit b533b38

17 files changed

+174
-34
lines changed

Library/Homebrew/cmd/desc.rb

+29-9
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ def desc_args
3030
switch "-d", "--description",
3131
description: "Search just descriptions for <text>. If <text> is flanked by slashes, "\
3232
"it is interpreted as a regular expression."
33+
switch "--formula", "--formulae",
34+
description: "Treat all named arguments as formulae."
35+
switch "--cask", "--casks",
36+
description: "Treat all named arguments as casks."
3337

3438
conflicts "--search", "--name", "--description"
3539

36-
named_args [:formula, :text_or_regex], min: 1
40+
named_args [:formula, :cask, :text_or_regex], min: 1
3741
end
3842
end
3943

@@ -48,19 +52,35 @@ def desc
4852
:desc
4953
end
5054

51-
results = if search_type.nil?
55+
if search_type.blank?
5256
desc = {}
53-
args.named.to_formulae.each { |f| desc[f.full_name] = f.desc }
54-
Descriptions.new(desc)
57+
args.named.to_formulae_and_casks.each do |formula_or_cask|
58+
if formula_or_cask.is_a? Formula
59+
desc[formula_or_cask.full_name] = formula_or_cask.desc
60+
else
61+
description = formula_or_cask.desc.presence || Formatter.warning("[no description]")
62+
desc[formula_or_cask.full_name] = "(#{formula_or_cask.name.join(", ")}) #{description}"
63+
end
64+
end
65+
Descriptions.new(desc).print
5566
else
5667
query = args.named.join(" ")
5768
string_or_regex = query_regexp(query)
58-
CacheStoreDatabase.use(:descriptions) do |db|
59-
cache_store = DescriptionCacheStore.new(db)
60-
Descriptions.search(string_or_regex, search_type, cache_store)
69+
unless args.cask?
70+
ohai "Formulae"
71+
CacheStoreDatabase.use(:descriptions) do |db|
72+
cache_store = DescriptionCacheStore.new(db)
73+
Descriptions.search(string_or_regex, search_type, cache_store).print
74+
end
75+
end
76+
unless args.formula?
77+
puts unless args.cask?
78+
ohai "Casks"
79+
CacheStoreDatabase.use(:cask_descriptions) do |db|
80+
cache_store = CaskDescriptionCacheStore.new(db)
81+
Descriptions.search(string_or_regex, search_type, cache_store).print
82+
end
6183
end
6284
end
63-
64-
results.print
6585
end
6686
end

Library/Homebrew/cmd/search.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def search_args
4343
description: "Search online and locally for casks."
4444
switch "--desc",
4545
description: "Search for formulae with a description matching <text> and casks with "\
46-
"a name matching <text>."
46+
"a name or description matching <text>."
4747
switch "--pull-request",
4848
description: "Search for GitHub pull requests containing <text>."
4949
switch "--open",

Library/Homebrew/cmd/update-report.rb

+4
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ def output_update_report
197197
DescriptionCacheStore.new(db)
198198
.update_from_report!(hub)
199199
end
200+
CacheStoreDatabase.use(:cask_descriptions) do |db|
201+
CaskDescriptionCacheStore.new(db)
202+
.update_from_report!(hub)
203+
end
200204

201205
if !args.preinstall? && !args.quiet?
202206
outdated_formulae = Formula.installed.count(&:outdated?)

Library/Homebrew/description_cache_store.rb

+49-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
require "searchable"
77

88
#
9-
# {DescriptionCacheStore} provides methods to fetch and mutate linkage-specific data used
10-
# by the `brew linkage` command.
9+
# {DescriptionCacheStore} provides methods to fetch and mutate formula descriptions used
10+
# by the `brew desc` and `brew search` commands.
1111
#
1212
class DescriptionCacheStore < CacheStore
1313
include Searchable
@@ -81,6 +81,7 @@ def delete_from_formula_names!(formula_names)
8181

8282
formula_names.each(&method(:delete!))
8383
end
84+
alias delete_from_cask_tokens! delete_from_formula_names!
8485

8586
private
8687

@@ -89,3 +90,49 @@ def select(&block)
8990
database.select(&block)
9091
end
9192
end
93+
94+
#
95+
# {CaskDescriptionCacheStore} provides methods to fetch and mutate cask descriptions used
96+
# by the `brew desc` and `brew search` commands.
97+
#
98+
class CaskDescriptionCacheStore < DescriptionCacheStore
99+
# If the database is empty `update!` it with all known casks.
100+
#
101+
# @return [nil]
102+
def populate_if_empty!
103+
return unless database.empty?
104+
105+
# TODO: 3.6.0: consider if we want to actually read all contents of all casks or odeprecate.
106+
Cask::Cask.all.each { |c| update!(c.full_name, [c.name.join(", "), c.desc.presence]) }
107+
end
108+
109+
# Use an update report to update the {CaskDescriptionCacheStore}.
110+
#
111+
# @param report [Report] an update report generated by cmd/update.rb
112+
# @return [nil]
113+
def update_from_report!(report)
114+
return populate_if_empty! if database.empty?
115+
return if report.empty?
116+
117+
alterations = report.select_formula(:AC) +
118+
report.select_formula(:MC)
119+
120+
update_from_cask_tokens!(alterations)
121+
delete_from_cask_tokens!(report.select_formula(:DC))
122+
end
123+
124+
# Use an array of cask tokens to update the {CaskDescriptionCacheStore}.
125+
#
126+
# @param cask_tokens [Array] the casks to update
127+
# @return [nil]
128+
def update_from_cask_tokens!(cask_tokens)
129+
return populate_if_empty! if database.empty?
130+
131+
cask_tokens.each do |token|
132+
c = Cask::CaskLoader.load(token)
133+
update!(c.full_name, [c.name.join(", "), c.desc.presence])
134+
rescue Cask::CaskUnavailableError, *FormulaVersions::IGNORED_EXCEPTIONS
135+
delete!(c.full_name) if c.present?
136+
end
137+
end
138+
end

Library/Homebrew/descriptions.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ def print
4545
full_name
4646
end
4747
description = @descriptions[full_name] || blank
48-
puts "#{Tty.bold}#{printed_name}:#{Tty.reset} #{description}"
48+
if description.is_a?(Array)
49+
names = description[0]
50+
description = description[1] || blank
51+
puts "#{Tty.bold}#{printed_name}:#{Tty.reset} (#{names}) #{description}"
52+
else
53+
puts "#{Tty.bold}#{printed_name}:#{Tty.reset} #{description}"
54+
end
4955
end
5056
end
5157

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ module Extension
1010
def search_descriptions(string_or_regex, args)
1111
super
1212

13-
puts
14-
1513
return if args.formula?
1614

15+
puts unless args.cask?
1716
ohai "Casks"
18-
Cask::Cask.all.extend(Searchable)
19-
.search(string_or_regex, &:name)
20-
.each do |cask|
21-
puts "#{Tty.bold}#{cask.token}:#{Tty.reset} #{cask.name.join(", ")}"
17+
CacheStoreDatabase.use(:cask_descriptions) do |db|
18+
cache_store = CaskDescriptionCacheStore.new(db)
19+
Descriptions.search(string_or_regex, :desc, cache_store).print
2220
end
2321
end
2422

@@ -42,9 +40,9 @@ def search_casks(string_or_regex)
4240
results.sort.map do |name|
4341
cask = Cask::CaskLoader.load(name)
4442
if cask.installed?
45-
pretty_installed(cask.token)
43+
pretty_installed(cask.full_name)
4644
else
47-
cask.token
45+
cask.full_name
4846
end
4947
end
5048
end

Library/Homebrew/formula.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,7 @@ def self.aliases
17591759
@aliases ||= (core_aliases + tap_aliases.map { |name| name.split("/").last }).uniq.sort
17601760
end
17611761

1762-
# an array of all aliases, , which the tap formulae have the fully-qualified name
1762+
# an array of all aliases as fully-qualified names
17631763
# @private
17641764
def self.alias_full_names
17651765
@alias_full_names ||= core_aliases + tap_aliases

Library/Homebrew/search.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def search_formulae(string_or_regex)
8989
.search(string_or_regex)
9090
.sort
9191

92-
results |= Formula.fuzzy_search(string_or_regex)
92+
results |= Formula.fuzzy_search(string_or_regex).map { |n| Formulary.factory(n).full_name }
9393

9494
results.map do |name|
9595
formula, canonical_full_name = begin

Library/Homebrew/searchable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def search_string(string)
3232
simplified_string = simplify_string(string)
3333
select do |*args|
3434
args = yield(*args) if block_given?
35-
args = Array(args).compact
35+
args = Array(args).flatten.compact
3636
args.any? { |arg| simplify_string(arg).include?(simplified_string) }
3737
end
3838
end

Library/Homebrew/tap.rb

+8
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ def install(quiet: false, clone_target: nil, force_auto_update: nil, custom_remo
330330
DescriptionCacheStore.new(db)
331331
.update_from_formula_names!(formula_names)
332332
end
333+
CacheStoreDatabase.use(:cask_descriptions) do |db|
334+
CaskDescriptionCacheStore.new(db)
335+
.update_from_cask_tokens!(cask_tokens)
336+
end
333337

334338
if official?
335339
untapped = self.class.untapped_official_taps
@@ -411,6 +415,10 @@ def uninstall(manual: false)
411415
DescriptionCacheStore.new(db)
412416
.delete_from_formula_names!(formula_names)
413417
end
418+
CacheStoreDatabase.use(:cask_descriptions) do |db|
419+
CaskDescriptionCacheStore.new(db)
420+
.delete_from_cask_tokens!(cask_tokens)
421+
end
414422
Utils::Link.unlink_manpages(path)
415423
Utils::Link.unlink_completions(path)
416424
path.rmtree

Library/Homebrew/test/description_cache_store_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,34 @@
5353
cache_store.delete_from_formula_names!([formula_name])
5454
end
5555
end
56+
57+
describe CaskDescriptionCacheStore do
58+
subject(:cache_store) { described_class.new(database) }
59+
60+
let(:database) { double("database") }
61+
62+
describe "#update_from_report!" do
63+
let(:report) { double(select_formula: [], empty?: false) }
64+
65+
it "reads from the report" do
66+
expect(database).to receive(:empty?).at_least(:once).and_return(false)
67+
cache_store.update_from_report!(report)
68+
end
69+
end
70+
71+
describe "#update_from_cask_tokens!" do
72+
it "sets the cask descriptions" do
73+
c = Cask::Cask.new("cask-names-desc") do
74+
url "url-1"
75+
name "Name 1"
76+
name "Name 2"
77+
desc "description"
78+
end
79+
expect(Cask::CaskLoader).to receive(:load).with("cask-names-desc", any_args).and_return(c)
80+
expect(database).to receive(:empty?).and_return(false)
81+
expect(database).to receive(:set).with(c.full_name, [c.name.join(", "), c.desc.presence])
82+
cache_store.update_from_cask_tokens!([c.token])
83+
end
84+
end
85+
end
5686
end

Library/Homebrew/test/descriptions_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@
4343
EOS
4444
).to_stdout
4545
end
46+
47+
it "can print description for a cask" do
48+
descriptions_hash["homebrew/cask/foo"] = ["Foo", "Cask foo"]
49+
expect { descriptions.print }.to output("foo: (Foo) Cask foo\n").to_stdout
50+
end
4651
end

completions/bash/brew

+3
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,10 @@ _brew_desc() {
741741
case "${cur}" in
742742
-*)
743743
__brewcomp "
744+
--cask
744745
--debug
745746
--description
747+
--formula
746748
--help
747749
--name
748750
--quiet
@@ -754,6 +756,7 @@ _brew_desc() {
754756
*)
755757
esac
756758
__brew_complete_formulae
759+
__brew_complete_casks
757760
}
758761

759762
_brew_developer() {

completions/fish/brew.fish

+6-3
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ __fish_brew_complete_arg '-S' -l cask -d 'Search online and locally for casks'
279279
__fish_brew_complete_arg '-S' -l closed -d 'Search for only closed GitHub pull requests'
280280
__fish_brew_complete_arg '-S' -l debian -d 'Search for text in the given database'
281281
__fish_brew_complete_arg '-S' -l debug -d 'Display any debugging information'
282-
__fish_brew_complete_arg '-S' -l desc -d 'Search for formulae with a description matching text and casks with a name matching text'
282+
__fish_brew_complete_arg '-S' -l desc -d 'Search for formulae with a description matching text and casks with a name or description matching text'
283283
__fish_brew_complete_arg '-S' -l fedora -d 'Search for text in the given database'
284284
__fish_brew_complete_arg '-S' -l fink -d 'Search for text in the given database'
285285
__fish_brew_complete_arg '-S' -l formula -d 'Search online and locally for formulae'
@@ -578,14 +578,17 @@ __fish_brew_complete_arg 'deps; and not __fish_seen_argument -l formula -l formu
578578

579579

580580
__fish_brew_complete_cmd 'desc' 'Display formula\'s name and one-line description'
581+
__fish_brew_complete_arg 'desc' -l cask -d 'Treat all named arguments as casks'
581582
__fish_brew_complete_arg 'desc' -l debug -d 'Display any debugging information'
582583
__fish_brew_complete_arg 'desc' -l description -d 'Search just descriptions for text. If text is flanked by slashes, it is interpreted as a regular expression'
584+
__fish_brew_complete_arg 'desc' -l formula -d 'Treat all named arguments as formulae'
583585
__fish_brew_complete_arg 'desc' -l help -d 'Show this message'
584586
__fish_brew_complete_arg 'desc' -l name -d 'Search just names for text. If text is flanked by slashes, it is interpreted as a regular expression'
585587
__fish_brew_complete_arg 'desc' -l quiet -d 'Make some output more quiet'
586588
__fish_brew_complete_arg 'desc' -l search -d 'Search both names and descriptions for text. If text is flanked by slashes, it is interpreted as a regular expression'
587589
__fish_brew_complete_arg 'desc' -l verbose -d 'Make some output more verbose'
588-
__fish_brew_complete_arg 'desc' -a '(__fish_brew_suggest_formulae_all)'
590+
__fish_brew_complete_arg 'desc; and not __fish_seen_argument -l cask -l casks' -a '(__fish_brew_suggest_formulae_all)'
591+
__fish_brew_complete_arg 'desc; and not __fish_seen_argument -l formula -l formulae' -a '(__fish_brew_suggest_casks_all)'
589592

590593

591594
__fish_brew_complete_cmd 'developer' 'Control Homebrew\'s developer mode'
@@ -1240,7 +1243,7 @@ __fish_brew_complete_arg 'search' -l cask -d 'Search online and locally for cask
12401243
__fish_brew_complete_arg 'search' -l closed -d 'Search for only closed GitHub pull requests'
12411244
__fish_brew_complete_arg 'search' -l debian -d 'Search for text in the given database'
12421245
__fish_brew_complete_arg 'search' -l debug -d 'Display any debugging information'
1243-
__fish_brew_complete_arg 'search' -l desc -d 'Search for formulae with a description matching text and casks with a name matching text'
1246+
__fish_brew_complete_arg 'search' -l desc -d 'Search for formulae with a description matching text and casks with a name or description matching text'
12441247
__fish_brew_complete_arg 'search' -l fedora -d 'Search for text in the given database'
12451248
__fish_brew_complete_arg 'search' -l fink -d 'Search for text in the given database'
12461249
__fish_brew_complete_arg 'search' -l formula -d 'Search online and locally for formulae'

completions/zsh/_brew

+7-3
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ _brew__s() {
354354
'(--open)--closed[Search for only closed GitHub pull requests]' \
355355
'(--repology --macports --fink --opensuse --fedora --archlinux --ubuntu)--debian[Search for text in the given database]' \
356356
'--debug[Display any debugging information]' \
357-
'(--pull-request)--desc[Search for formulae with a description matching text and casks with a name matching text]' \
357+
'(--pull-request)--desc[Search for formulae with a description matching text and casks with a name or description matching text]' \
358358
'(--repology --macports --fink --opensuse --archlinux --debian --ubuntu)--fedora[Search for text in the given database]' \
359359
'(--repology --macports --opensuse --fedora --archlinux --debian --ubuntu)--fink[Search for text in the given database]' \
360360
'--formula[Search online and locally for formulae]' \
@@ -717,7 +717,11 @@ _brew_desc() {
717717
'(--name --description)--search[Search both names and descriptions for text. If text is flanked by slashes, it is interpreted as a regular expression]' \
718718
'--verbose[Make some output more verbose]' \
719719
- formula \
720-
'*::formula:__brew_formulae'
720+
'--formula[Treat all named arguments as formulae]' \
721+
'*::formula:__brew_formulae' \
722+
- cask \
723+
'--cask[Treat all named arguments as casks]' \
724+
'*::cask:__brew_casks'
721725
}
722726

723727
# brew developer
@@ -1513,7 +1517,7 @@ _brew_search() {
15131517
'(--open)--closed[Search for only closed GitHub pull requests]' \
15141518
'(--repology --macports --fink --opensuse --fedora --archlinux --ubuntu)--debian[Search for text in the given database]' \
15151519
'--debug[Display any debugging information]' \
1516-
'(--pull-request)--desc[Search for formulae with a description matching text and casks with a name matching text]' \
1520+
'(--pull-request)--desc[Search for formulae with a description matching text and casks with a name or description matching text]' \
15171521
'(--repology --macports --fink --opensuse --archlinux --debian --ubuntu)--fedora[Search for text in the given database]' \
15181522
'(--repology --macports --opensuse --fedora --archlinux --debian --ubuntu)--fink[Search for text in the given database]' \
15191523
'--formula[Search online and locally for formulae]' \

0 commit comments

Comments
 (0)