Module: Homebrew::Search Private

Extended by:
Utils::Output::Mixin
Defined in:
search.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Helper module for searching formulae or casks.

Constant Summary collapse

QUERY_REGEX =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%r{^/(.*)/$}
SearchBlockType =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.type_alias do
  T.nilable(
    T.proc
     .params(arg0: T.any(T::Array[String], T::Array[T::Array[String]]))
     .returns(T.nilable(T.any(String, T::Array[String]))),
  )
end
SearchResultType =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.type_alias do
  T.any(
    T::Array[String],
    T::Array[T::Array[String]],
    T::Hash[String, T.nilable(String)],
    T::Hash[String, T::Array[T.nilable(String)]],
  )
end
SelectableType =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.type_alias do
  # These must define a `select` method that takes a block and returns an array or hash.
  # Since sorbet has minimal support for overloading sig, the return type must be casted to the actual type.
  # DescriptionCacheStore and Hash instances will return a Hash, other types will return an Array.
  T.any(DescriptionCacheStore, SearchResultType)
end

Class Method Summary collapse

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_once, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning

Class Method Details

.ignore_cask?(_cask) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


50
# File 'search.rb', line 50

def self.ignore_cask?(_cask) = false

.query_regexp(query) ⇒ Regexp, String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



39
40
41
42
43
44
45
46
47
# File 'search.rb', line 39

def self.query_regexp(query)
  if (regex = query[QUERY_REGEX, 1])
    Regexp.new(regex)
  else
    query
  end
rescue RegexpError
  raise "#{query} is not a valid regex."
end

.search(selectable, string_or_regex, &block) ⇒ SearchResultType

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



201
202
203
204
205
206
207
208
# File 'search.rb', line 201

def self.search(selectable, string_or_regex, &block)
  case string_or_regex
  when Regexp
    search_regex(selectable, string_or_regex, &block)
  else
    search_string(selectable, string_or_regex.to_str, &block)
  end
end

.search_casks(string_or_regex) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • string_or_regex (Regexp, String)

Returns:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'search.rb', line 134

def self.search_casks(string_or_regex)
  require "cask/cask_loader"

  if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_CASK_REGEX)
    return begin
      matched_cask = Cask::CaskLoader.load(string_or_regex)
      ignore_cask?(matched_cask) ? [] : [matched_cask.token]
    rescue Cask::CaskUnavailableError
      []
    end
  end

  cask_tokens = Tap.each_with_object([]) do |tap, array|
    # We can exclude the core cask tap because `CoreCaskTap#cask_tokens` returns short names by default.
    if tap.official? && !tap.core_cask_tap?
      tap.cask_tokens.each { |token| array << token.sub(%r{^homebrew/cask.*/}, "") }
    else
      tap.cask_tokens.each { |token| array << token }
    end
  end.uniq

  results = T.cast(search(cask_tokens, string_or_regex), T::Array[String])
  if string_or_regex.is_a?(String)
    results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
                                       .correct(string_or_regex)
  end

  results.sort.filter_map do |name|
    cask = Cask::CaskLoader.load(name.to_s)
    next if ignore_cask?(cask)

    pretty_install_status(
      cask.full_name,
      installed:        cask.installed?,
      deprecated:       cask.deprecated?,
      disabled:         cask.disabled?,
      mark_uninstalled: false,
    )
  rescue Homebrew::UntrustedTapError
    nil
  end.uniq
end

.search_descriptions(string_or_regex, args, search_type: nil, show_missing: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'search.rb', line 62

def self.search_descriptions(string_or_regex, args, search_type: nil, show_missing: false)
  require "descriptions"

  search_type ||= Descriptions::SearchField::Description
  both = !args.formula? && !args.cask?

  if args.formula? || both
    ohai "Formulae"
    CacheStoreDatabase.use(:descriptions) do |db|
      cache_store = DescriptionCacheStore.new(T.cast(db, CacheStoreDatabase[String, T.anything]))
      Descriptions.search(string_or_regex, search_type, cache_store).print
    end
  end
  return if !args.cask? && !both

  puts if both

  ohai "Casks"
  CacheStoreDatabase.use(:cask_descriptions) do |db|
    cache_store = CaskDescriptionCacheStore.new(T.cast(db, CacheStoreDatabase[String, T.anything]))
    Descriptions.search(string_or_regex, search_type, cache_store).print(show_missing:)
  end
end

.search_formulae(string_or_regex) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • string_or_regex (Regexp, String)

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'search.rb', line 87

def self.search_formulae(string_or_regex)
  if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX)
    return begin
      [Formulary.factory(string_or_regex).name]
    rescue FormulaUnavailableError
      []
    end
  end

  aliases = Formula.alias_full_names
  results = T.cast(search(Formula.full_names + aliases, string_or_regex), T::Array[String]).sort_by do |name|
    base_name, version_str = name.split("@", 2)
    # unversioned formulae sort higher than versioned ones
    [base_name, Version.new(version_str || "0")]
  end

  if string_or_regex.is_a?(String)
    results |= Formula.fuzzy_search(string_or_regex).map do |n|
      Formulary.factory(n).full_name
    end
  end

  results.filter_map do |name|
    formula, canonical_full_name = begin
      f = Formulary.factory(name)
      [f, f.full_name]
    rescue
      [nil, name]
    end

    # Ignore aliases from results when the full name was also found
    next if aliases.include?(name) && results.include?(canonical_full_name)

    installed = formula&.any_version_installed? == true
    next if formula && !formula.valid_platform? && !installed

    pretty_install_status(
      name,
      installed:,
      deprecated:       formula&.deprecated? == true,
      disabled:         formula&.disabled? == true,
      mark_uninstalled: false,
    )
  end
end

.search_names(string_or_regex, args) ⇒ Array<(Array<String>, Array<String>)>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



185
186
187
188
189
190
191
192
193
194
195
# File 'search.rb', line 185

def self.search_names(string_or_regex, args)
  if !args.formula? && !args.cask? # both
    [search_formulae(string_or_regex), search_casks(string_or_regex)]
  elsif args.formula?
    [search_formulae(string_or_regex), []]
  elsif args.cask?
    [[], search_casks(string_or_regex)]
  else
    [[], []]
  end
end

.search_regex(selectable, regex, &_block) ⇒ SearchResultType

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



216
217
218
219
220
221
222
# File 'search.rb', line 216

def self.search_regex(selectable, regex, &_block)
  selectable.select do |*args|
    args = yield(*args) if block_given?
    args = Array(args).flatten.compact
    args.any? { |arg| arg.match?(regex) }
  end
end

.search_string(selectable, string, &_block) ⇒ SearchResultType

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



225
226
227
228
229
230
231
232
# File 'search.rb', line 225

def self.search_string(selectable, string, &_block)
  simplified_string = simplify_string(string)
  selectable.select do |*args|
    args = yield(*args) if block_given?
    args = Array(args).flatten.compact
    args.any? { |arg| simplify_string(arg).include?(simplified_string) }
  end
end

.simplify_string(string) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



211
212
213
# File 'search.rb', line 211

def self.simplify_string(string)
  string.downcase.gsub(/[^a-z\d@+]/i, "")
end