Skip to content

Commit

Permalink
Use API for description searches
Browse files Browse the repository at this point in the history
Both `brew search --desc` and `brew desc --search` use API for cask and
formula searches unless `--eval-all` or `HOMEBREW_EVAL_ALL` set.
Description searches do not use the description cache or eval any
formulas/casks.

With `--eval-all`, description search reverts to the old behavior.
  • Loading branch information
rrotter committed Jun 29, 2024
1 parent a40f327 commit 1e86ced
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
4 changes: 0 additions & 4 deletions Library/Homebrew/cmd/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ def run
end

if search_type.present?
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
raise UsageError, "`brew desc --search` needs `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!"
end

query = args.named.join(" ")
string_or_regex = Search.query_regexp(query)
return Search.search_descriptions(string_or_regex, args, search_type:)
Expand Down
4 changes: 0 additions & 4 deletions Library/Homebrew/cmd/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ def run
string_or_regex = Search.query_regexp(query)

if args.desc?
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
raise UsageError, "`brew search --desc` needs `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!"
end

Search.search_descriptions(string_or_regex, args)
elsif args.pull_request?
search_pull_requests(query)
Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/descriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
class Descriptions
# Given a regex, find all formulae whose specified fields contain a match.
def self.search(string_or_regex, field, cache_store, eval_all = Homebrew::EnvConfig.eval_all?)
cache_store.populate_if_empty!(eval_all:)
# allow searching a simple hash, not only cache_store objects
cache_store.populate_if_empty!(eval_all:) if cache_store.methods.include?(:populate_if_empty!)

results = case field
when :name
Expand Down
22 changes: 16 additions & 6 deletions Library/Homebrew/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@ def self.search_descriptions(string_or_regex, args, search_type: :desc)

if args.formula? || both
ohai "Formulae"
CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
if eval_all
CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
end
else
descriptions = Homebrew::API::Formula.all_formulae.transform_values { |data| data["desc"] }
Descriptions.search(string_or_regex, search_type, descriptions, eval_all).print
end
end
return if !args.cask? && !both

puts if both

ohai "Casks"
CacheStoreDatabase.use(:cask_descriptions) do |db|
cache_store = CaskDescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
if eval_all
CacheStoreDatabase.use(:cask_descriptions) do |db|
cache_store = CaskDescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
end
else
descriptions = Homebrew::API::Cask.all_casks.transform_values { |data| data["desc"] }
Descriptions.search(string_or_regex, search_type, descriptions, eval_all).print
end
end

Expand Down

0 comments on commit 1e86ced

Please sign in to comment.