brew/Library/Homebrew/search.rb

104 lines
2.6 KiB
Ruby
Raw Normal View History

2018-06-05 15:39:09 +02:00
require "searchable"
module Homebrew
module Search
def query_regexp(query)
if m = query.match(%r{^/(.*)/$})
Regexp.new(m[1])
else
2018-06-05 15:39:09 +02:00
query
end
rescue RegexpError
raise "#{query} is not a valid regex."
end
2018-06-18 16:09:13 +02:00
def search_descriptions(string_or_regex)
ohai "Formulae"
Descriptions.search(string_or_regex, :desc).print
end
def search_taps(query, silent: false)
2018-06-13 07:49:01 +02:00
if query.match?(Regexp.union(HOMEBREW_TAP_FORMULA_REGEX, HOMEBREW_TAP_CASK_REGEX))
_, _, query = query.split("/", 3)
end
2018-06-06 13:24:55 +02:00
results = { formulae: [], casks: [] }
return results if ENV["HOMEBREW_NO_GITHUB_API"]
unless silent
# Use stderr to avoid breaking parsed output
$stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue)
end
2018-06-06 13:24:55 +02:00
matches = begin
GitHub.search_code(
user: "Homebrew",
path: ["Formula", "Casks", "."],
filename: query,
extension: "rb",
)
rescue GitHub::Error => error
opoo "Error searching on GitHub: #{error}\n"
return results
end
2018-06-06 13:24:55 +02:00
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?
2018-06-06 13:24:55 +02:00
if match["path"].start_with?("Casks/")
results[:casks] = [*results[:casks], full_name].sort
else
2018-06-06 13:24:55 +02:00
results[:formulae] = [*results[:formulae], full_name].sort
end
end
2018-06-06 13:24:55 +02:00
results
end
2018-06-05 15:39:09 +02:00
def search_formulae(string_or_regex)
2018-06-13 07:49:01 +02:00
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
2018-06-05 10:55:00 +02:00
results = (Formula.full_names + aliases)
2018-06-05 15:39:09 +02:00
.extend(Searchable)
.search(string_or_regex)
2018-06-05 10:55:00 +02:00
.sort
results.map do |name|
2018-06-07 17:57:26 +02:00
formula, canonical_full_name = begin
f = Formulary.factory(name)
[f, f.full_name]
rescue
2018-06-07 17:57:26 +02:00
[nil, name]
end
# Ignore aliases from results when the full name was also found
next if aliases.include?(name) && results.include?(canonical_full_name)
2018-06-07 17:57:26 +02:00
if formula&.any_version_installed?
pretty_installed(name)
else
name
end
end.compact
end
2018-06-07 14:42:58 +02:00
2018-06-13 07:49:01 +02:00
def search_casks(_string_or_regex)
[]
2018-06-07 14:42:58 +02:00
end
end
end
2018-06-13 07:49:01 +02:00
require "extend/os/search"