Module: Homebrew::Search Private

Includes:
Extension
Included in:
Cask::Cmd::AbstractCommand, Descriptions, Homebrew
Defined in:
search.rb,
extend/os/mac/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.

Defined Under Namespace

Modules: Extension

Instance Method Summary collapse

Instance 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.



12
13
14
15
16
17
18
19
20
# File 'search.rb', line 12

def 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_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.



113
114
115
# File 'search.rb', line 113

def search_casks(_string_or_regex)
  []
end

#search_descriptions(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.



22
23
24
25
26
27
28
29
30
# File 'search.rb', line 22

def search_descriptions(string_or_regex, args)
  return if args.cask?

  ohai "Formulae"
  CacheStoreDatabase.use(:descriptions) do |db|
    cache_store = DescriptionCacheStore.new(db)
    Descriptions.search(string_or_regex, :desc, cache_store).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.



77
78
79
80
81
82
83
84
85
86
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
# File 'search.rb', line 77

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

  results |= Formula.fuzzy_search(string_or_regex)

  results.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)
    else
      name
    end
  end.compact
end

#search_taps(query, silent: false) ⇒ 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.



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'search.rb', line 32

def search_taps(query, silent: false)
  if query.match?(Regexp.union(HOMEBREW_TAP_FORMULA_REGEX, HOMEBREW_TAP_CASK_REGEX))
    _, _, query = query.split("/", 3)
  end

  results = { formulae: [], casks: [] }

  return results if Homebrew::EnvConfig.no_github_api?

  unless silent
    # Use stderr to avoid breaking parsed output
    $stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue)
  end

  matches = begin
    GitHub.search_code(
      user:      "Homebrew",
      path:      ["Formula", "Casks", "."],
      filename:  query,
      extension: "rb",
    )
  rescue GitHub::API::Error => e
    opoo "Error searching on GitHub: #{e}\n"
    nil
  end

  return results if matches.blank?

  matches.each do |match|
    name = File.basename(match["path"], ".rb")
    tap = Tap.fetch(match["repository"]["full_name"])
    full_name = "#{tap.name}/#{name}"

    next if tap.installed?

    if match["path"].start_with?("Casks/")
      results[:casks] = [*results[:casks], full_name].sort
    else
      results[:formulae] = [*results[:formulae], full_name].sort
    end
  end

  results
end