Module: Homebrew::Search Private

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.

Class Method Summary collapse

Class Method Details

.query_regexp(query) ⇒ Object

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.



9
10
11
12
13
14
15
16
17
# File 'search.rb', line 9

def self.query_regexp(query)
  if (m = query.match(%r{^/(.*)/$}))
    Regexp.new(m[1])
  else
    query
  end
rescue RegexpError
  raise "#{query} is not a valid regex."
end

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

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.



147
148
149
150
151
152
153
154
# File 'search.rb', line 147

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) ⇒ Object

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.



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
# File 'search.rb', line 97

def self.search_casks(string_or_regex)
  if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_CASK_REGEX)
    return begin
      [Cask::CaskLoader.load(string_or_regex).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 = search(cask_tokens, string_or_regex)
  results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
                                     .correct(string_or_regex)

  results.sort.map do |name|
    cask = Cask::CaskLoader.load(name)
    if cask.installed?
      pretty_installed(cask.full_name)
    else
      cask.full_name
    end
  end.uniq
end

.search_descriptions(string_or_regex, args, search_type: :desc) ⇒ Object

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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'search.rb', line 19

def self.search_descriptions(string_or_regex, args, search_type: :desc)
  both = !args.formula? && !args.cask?
  eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?

  if args.formula? || both
    ohai "Formulae"
    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
      unofficial = Tap.all.sum { |tap| tap.official? ? 0 : tap.formula_files.size }
      if unofficial.positive?
        opoo "Use `--eval-all` to search #{unofficial} additional " \
             "#{Utils.pluralize("formula", unofficial, plural: "e")} in third party taps."
      end
      descriptions = Homebrew::API::Formula.all_formulae.transform_values { |data| data["desc"] }
      Descriptions.search(string_or_regex, search_type, descriptions, eval_all, cache_store_hash: true).print
    end
  end
  return if !args.cask? && !both

  puts if both

  ohai "Casks"
  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
    unofficial = Tap.all.sum { |tap| tap.official? ? 0 : tap.cask_files.size }
    if unofficial.positive?
      opoo "Use `--eval-all` to search #{unofficial} additional " \
           "#{Utils.pluralize("cask", unofficial)} in third party taps."
    end
    descriptions = Homebrew::API::Cask.all_casks.transform_values { |c| [c["name"].join(", "), c["desc"]] }
    Descriptions.search(string_or_regex, search_type, descriptions, eval_all, cache_store_hash: true).print
  end
end

.search_formulae(string_or_regex) ⇒ Object

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'search.rb', line 61

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 = search(Formula.full_names + aliases, string_or_regex).sort
  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)

    if formula&.any_version_installed?
      pretty_installed(name)
    elsif formula.nil? || formula.valid_platform?
      name
    end
  end
end

.search_names(string_or_regex, args) ⇒ Object

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.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'search.rb', line 129

def self.search_names(string_or_regex, args)
  both = !args.formula? && !args.cask?

  all_formulae = if args.formula? || both
    search_formulae(string_or_regex)
  else
    []
  end

  all_casks = if args.cask? || both
    search_casks(string_or_regex)
  else
    []
  end

  [all_formulae, all_casks]
end

.search_regex(selectable, regex) ⇒ Object

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.



160
161
162
163
164
165
166
# File 'search.rb', line 160

def self.search_regex(selectable, regex)
  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) ⇒ Object

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.



168
169
170
171
172
173
174
175
# File 'search.rb', line 168

def self.search_string(selectable, string)
  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) ⇒ Object

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.



156
157
158
# File 'search.rb', line 156

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