brew/Library/Homebrew/cmd/search.rb

152 lines
5.1 KiB
Ruby
Raw Normal View History

#: * `search`, `-S`:
2016-04-08 16:28:43 +02:00
#: Display all locally available formulae for brewing (including tapped ones).
#: No online search is performed if called without arguments.
#:
#: * `search` [`--desc`] (<text>|`/`<text>`/`):
2016-04-08 16:28:43 +02:00
#: Perform a substring search of formula names for <text>. If <text> is
#: surrounded with slashes, then it is interpreted as a regular expression.
#: The search for <text> is extended online to some popular taps.
#:
#: If `--desc` is passed, browse available packages matching <text> including a
#: description for each.
#:
2016-04-08 16:28:43 +02:00
#: * `search` (`--debian`|`--fedora`|`--fink`|`--macports`|`--opensuse`|`--ubuntu`) <text>:
#: Search for <text> in the given package manager's list.
require "formula"
require "missing_formula"
require "utils"
2015-05-29 19:16:01 +08:00
require "official_taps"
require "descriptions"
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
def search
if ARGV.empty?
puts Formatter.columns(Formula.full_names)
elsif ARGV.include? "--macports"
exec_browser "https://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
elsif ARGV.include? "--fink"
2012-12-27 23:34:29 -06:00
exec_browser "http://pdb.finkproject.org/pdb/browse.php?summary=#{ARGV.next}"
elsif ARGV.include? "--debian"
exec_browser "https://packages.debian.org/search?keywords=#{ARGV.next}&searchon=names&suite=all&section=all"
elsif ARGV.include? "--opensuse"
exec_browser "https://software.opensuse.org/search?q=#{ARGV.next}"
elsif ARGV.include? "--fedora"
exec_browser "https://apps.fedoraproject.org/packages/s/#{ARGV.next}"
elsif ARGV.include? "--ubuntu"
2017-08-22 10:31:21 +00:00
exec_browser "https://packages.ubuntu.com/search?keywords=#{ARGV.next}&searchon=names&suite=all&section=all"
elsif ARGV.include? "--desc"
query = ARGV.next
regex = query_regexp(query)
Descriptions.search(regex, :desc).print
elsif ARGV.first =~ HOMEBREW_TAP_FORMULA_REGEX
query = ARGV.first
begin
result = Formulary.factory(query).name
2017-08-11 13:04:37 -04:00
results = Array(result)
rescue FormulaUnavailableError
2017-08-11 15:59:08 -04:00
_, _, name = query.split("/", 3)
results = search_taps(name)
end
puts Formatter.columns(results) unless results.empty?
else
query = ARGV.first
regex = query_regexp(query)
local_results = search_formulae(regex)
puts Formatter.columns(local_results) unless local_results.empty?
tap_results = search_taps(query)
2017-04-24 19:31:21 +02:00
puts Formatter.columns(tap_results) unless tap_results.empty?
if $stdout.tty?
count = local_results.length + tap_results.length
ohai "Searching blacklisted, migrated and deleted formulae..."
2017-03-22 21:56:30 +00:00
if reason = Homebrew::MissingFormula.reason(query, silent: true)
if count > 0
puts
puts "If you meant #{query.inspect} specifically:"
end
puts reason
2016-09-10 10:24:57 +01:00
elsif count.zero?
puts "No formula found for #{query.inspect}."
GitHub.print_pull_requests_matching(query)
end
end
end
return unless $stdout.tty?
return if ARGV.empty?
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze
return unless metacharacters.any? do |char|
ARGV.any? do |arg|
arg.include?(char) && !arg.start_with?("/")
end
end
ohai <<-EOS.undent
Did you mean to perform a regular expression search?
Surround your query with /slashes/ to search locally by regex.
EOS
end
2013-06-12 14:48:16 -05:00
def query_regexp(query)
case query
when %r{^/(.*)/$} then Regexp.new(Regexp.last_match(1))
2013-06-12 14:48:16 -05:00
else /.*#{Regexp.escape(query)}.*/i
end
rescue RegexpError
odie "#{query} is not a valid regex"
2013-06-12 14:48:16 -05:00
end
def search_taps(query, silent: false)
return [] if ENV["HOMEBREW_NO_GITHUB_API"]
# Use stderr to avoid breaking parsed output
unless silent
$stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue)
end
2017-04-24 19:31:21 +02:00
valid_dirnames = ["Formula", "HomebrewFormula", "Casks", "."].freeze
matches = GitHub.search_code(user: ["Homebrew", "caskroom"], filename: query, extension: "rb")
2017-08-14 11:08:56 -04:00
matches.map do |match|
2017-04-24 19:31:21 +02:00
dirname, filename = File.split(match["path"])
next unless valid_dirnames.include?(dirname)
tap = Tap.fetch(match["repository"]["full_name"])
next if tap.installed? && match["repository"]["owner"]["login"] != "caskroom"
2017-04-24 19:31:21 +02:00
"#{tap.name}/#{File.basename(filename, ".rb")}"
end.compact
end
def search_formulae(regex)
# Use stderr to avoid breaking parsed output
$stderr.puts Formatter.headline("Searching local taps...", color: :blue)
2015-09-13 17:47:53 +08:00
aliases = Formula.alias_full_names
2017-06-01 16:06:51 +02:00
results = (Formula.full_names + aliases).grep(regex).sort
results.map do |name|
begin
formula = Formulary.factory(name)
canonical_name = formula.name
canonical_full_name = formula.full_name
rescue
canonical_name = canonical_full_name = name
end
2016-09-22 20:12:28 +02:00
# Ignore aliases from results when the full name was also found
2016-09-22 20:12:28 +02:00
next if aliases.include?(name) && results.include?(canonical_full_name)
if (HOMEBREW_CELLAR/canonical_name).directory?
pretty_installed(name)
else
name
end
end.compact
end
end