2015-08-03 13:09:07 +01:00
|
|
|
require "formula"
|
|
|
|
require "blacklist"
|
|
|
|
require "utils"
|
|
|
|
require "thread"
|
2015-05-29 19:16:01 +08:00
|
|
|
require "official_taps"
|
2010-09-11 20:22:54 +01:00
|
|
|
|
2014-06-18 22:41:47 -05:00
|
|
|
module Homebrew
|
2014-02-16 22:24:33 -05:00
|
|
|
SEARCH_ERROR_QUEUE = Queue.new
|
|
|
|
|
2010-09-11 20:22:54 +01:00
|
|
|
def search
|
2015-08-03 13:09:07 +01:00
|
|
|
if ARGV.include? "--macports"
|
2015-01-04 05:02:27 +01:00
|
|
|
exec_browser "https://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
|
2015-08-03 13:09:07 +01:00
|
|
|
elsif ARGV.include? "--fink"
|
2012-12-27 23:34:29 -06:00
|
|
|
exec_browser "http://pdb.finkproject.org/pdb/browse.php?summary=#{ARGV.next}"
|
2015-08-03 13:09:07 +01:00
|
|
|
elsif ARGV.include? "--debian"
|
2015-01-04 05:02:27 +01:00
|
|
|
exec_browser "https://packages.debian.org/search?keywords=#{ARGV.next}&searchon=names&suite=all§ion=all"
|
2015-08-03 13:09:07 +01:00
|
|
|
elsif ARGV.include? "--opensuse"
|
2015-04-20 14:51:33 +01:00
|
|
|
exec_browser "https://software.opensuse.org/search?q=#{ARGV.next}"
|
2015-08-03 13:09:07 +01:00
|
|
|
elsif ARGV.include? "--fedora"
|
2015-02-24 20:29:27 +01:00
|
|
|
exec_browser "https://admin.fedoraproject.org/pkgdb/packages/%2A#{ARGV.next}%2A/"
|
2015-08-03 13:09:07 +01:00
|
|
|
elsif ARGV.include? "--ubuntu"
|
2013-08-14 22:14:35 -07:00
|
|
|
exec_browser "http://packages.ubuntu.com/search?keywords=#{ARGV.next}&searchon=names&suite=all§ion=all"
|
2015-08-03 13:09:07 +01:00
|
|
|
elsif ARGV.include? "--desc"
|
2015-05-19 14:14:49 -04:00
|
|
|
query = ARGV.next
|
2015-06-16 23:08:00 +02:00
|
|
|
rx = query_regexp(query)
|
2015-05-19 14:14:49 -04:00
|
|
|
Formula.each do |formula|
|
2015-06-16 23:08:00 +02:00
|
|
|
if formula.desc =~ rx
|
2015-06-14 13:54:46 -07:00
|
|
|
puts "#{Tty.white}#{formula.full_name}:#{Tty.reset} #{formula.desc}"
|
2015-05-19 14:14:49 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-27 15:21:30 -05:00
|
|
|
elsif ARGV.empty?
|
2015-05-08 19:54:29 +08:00
|
|
|
puts_columns Formula.full_names
|
2014-04-24 11:26:45 +09:00
|
|
|
elsif ARGV.first =~ HOMEBREW_TAP_FORMULA_REGEX
|
2014-04-27 15:21:30 -05:00
|
|
|
query = ARGV.first
|
|
|
|
user, repo, name = query.split("/", 3)
|
|
|
|
|
|
|
|
begin
|
|
|
|
result = Formulary.factory(query).name
|
|
|
|
rescue FormulaUnavailableError
|
|
|
|
result = search_tap(user, repo, name)
|
2013-01-29 23:34:55 +01:00
|
|
|
end
|
2014-04-27 15:21:30 -05:00
|
|
|
|
|
|
|
puts_columns Array(result)
|
2010-11-14 03:52:59 +00:00
|
|
|
else
|
2014-04-27 15:21:30 -05:00
|
|
|
query = ARGV.first
|
2013-06-12 14:48:16 -05:00
|
|
|
rx = query_regexp(query)
|
2013-06-12 14:48:17 -05:00
|
|
|
local_results = search_formulae(rx)
|
|
|
|
puts_columns(local_results)
|
2010-09-11 20:22:54 +01:00
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
if !query.empty? && $stdout.tty? && msg = blacklisted?(query)
|
2013-06-12 14:48:17 -05:00
|
|
|
unless local_results.empty?
|
2010-11-14 03:52:59 +00:00
|
|
|
puts
|
2013-06-12 14:48:16 -05:00
|
|
|
puts "If you meant #{query.inspect} precisely:"
|
2010-11-14 03:52:59 +00:00
|
|
|
puts
|
|
|
|
end
|
|
|
|
puts msg
|
|
|
|
end
|
2012-01-11 20:49:08 -06:00
|
|
|
|
2013-07-15 10:56:38 -05:00
|
|
|
tap_results = search_taps(rx)
|
|
|
|
puts_columns(tap_results)
|
|
|
|
count = local_results.length + tap_results.length
|
2012-03-16 22:37:11 +00:00
|
|
|
|
2015-08-03 20:38:20 +08:00
|
|
|
if count == 0 && !blacklisted?(query)
|
2013-10-22 16:18:31 -07:00
|
|
|
puts "No formula found for #{query.inspect}."
|
2013-07-15 10:56:38 -05:00
|
|
|
begin
|
2014-02-13 23:14:30 -05:00
|
|
|
GitHub.print_pull_requests_matching(query)
|
2013-07-15 10:56:38 -05:00
|
|
|
rescue GitHub::Error => e
|
2014-02-16 22:24:33 -05:00
|
|
|
SEARCH_ERROR_QUEUE << e
|
2012-03-20 16:03:27 +00:00
|
|
|
end
|
2012-01-11 20:49:08 -06:00
|
|
|
end
|
2010-11-14 03:52:59 +00:00
|
|
|
end
|
2015-04-17 12:07:45 -07:00
|
|
|
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ? .]
|
|
|
|
bad_regex = metacharacters.any? do |char|
|
|
|
|
ARGV.any? do |arg|
|
2015-08-03 13:09:07 +01:00
|
|
|
arg.include?(char) && !arg.start_with?("/")
|
2015-04-17 12:07:45 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if ARGV.any? && bad_regex
|
|
|
|
ohai "Did you mean to perform a regular expression search?"
|
|
|
|
ohai "Surround your query with /slashes/ to search by regex."
|
|
|
|
end
|
2014-02-16 22:24:33 -05:00
|
|
|
raise SEARCH_ERROR_QUEUE.pop unless SEARCH_ERROR_QUEUE.empty?
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
|
|
|
|
2015-05-29 19:16:01 +08:00
|
|
|
SEARCHABLE_TAPS = OFFICIAL_TAPS.map { |tap| ["Homebrew", tap] } + [
|
2015-08-03 13:09:07 +01:00
|
|
|
%w[Caskroom cask]
|
2013-06-12 14:48:17 -05:00
|
|
|
]
|
|
|
|
|
2013-06-12 14:48:16 -05:00
|
|
|
def query_regexp(query)
|
|
|
|
case query
|
|
|
|
when %r{^/(.*)/$} then Regexp.new($1)
|
|
|
|
else /.*#{Regexp.escape(query)}.*/i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-12 14:48:17 -05:00
|
|
|
def search_taps(rx)
|
|
|
|
SEARCHABLE_TAPS.map do |user, repo|
|
|
|
|
Thread.new { search_tap(user, repo, rx) }
|
|
|
|
end.inject([]) do |results, t|
|
|
|
|
results.concat(t.value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def search_tap(user, repo, rx)
|
2014-11-28 15:02:42 +00:00
|
|
|
if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}/homebrew-#{repo.downcase}").directory? && \
|
|
|
|
"#{user}/#{repo}" != "Caskroom/cask"
|
|
|
|
return []
|
|
|
|
end
|
2012-03-16 22:37:11 +00:00
|
|
|
|
2015-06-16 17:55:19 +08:00
|
|
|
@@remote_tap_formulae ||= Hash.new do |cache, key|
|
|
|
|
user, repo = key.split("/", 2)
|
|
|
|
tree = {}
|
2014-04-27 17:27:22 -05:00
|
|
|
|
2015-06-16 17:55:19 +08:00
|
|
|
GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |json|
|
|
|
|
json["tree"].each do |object|
|
|
|
|
next unless object["type"] == "blob"
|
2014-02-08 17:53:00 -05:00
|
|
|
|
2015-06-16 17:55:19 +08:00
|
|
|
subtree, file = File.split(object["path"])
|
2014-02-08 17:53:00 -05:00
|
|
|
|
2015-06-16 17:55:19 +08:00
|
|
|
if File.extname(file) == ".rb"
|
|
|
|
tree[subtree] ||= []
|
|
|
|
tree[subtree] << file
|
|
|
|
end
|
2012-03-16 22:37:11 +00:00
|
|
|
end
|
|
|
|
end
|
2014-04-27 17:27:22 -05:00
|
|
|
|
2015-06-16 17:55:19 +08:00
|
|
|
paths = tree["Formula"] || tree["HomebrewFormula"] || tree["Casks"] || tree["."] || []
|
|
|
|
cache[key] = paths.map { |path| File.basename(path, ".rb") }
|
2014-04-27 17:27:22 -05:00
|
|
|
end
|
2015-06-16 17:55:19 +08:00
|
|
|
|
|
|
|
names = @@remote_tap_formulae["#{user}/#{repo}"]
|
|
|
|
user = user.downcase if user == "Homebrew" # special handling for the Homebrew organization
|
|
|
|
names.select { |name| rx === name }.map { |name| "#{user}/#{repo}/#{name}" }
|
2014-02-16 22:24:33 -05:00
|
|
|
rescue GitHub::HTTPNotFoundError => e
|
|
|
|
opoo "Failed to search tap: #{user}/#{repo}. Please run `brew update`"
|
2014-02-08 16:04:53 -05:00
|
|
|
[]
|
|
|
|
rescue GitHub::Error => e
|
2014-02-16 22:24:33 -05:00
|
|
|
SEARCH_ERROR_QUEUE << e
|
2013-07-01 16:58:16 -05:00
|
|
|
[]
|
2012-03-16 22:37:11 +00:00
|
|
|
end
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def search_formulae(rx)
|
2013-07-15 10:56:38 -05:00
|
|
|
aliases = Formula.aliases
|
2015-05-08 19:54:29 +08:00
|
|
|
results = (Formula.full_names+aliases).grep(rx).sort
|
2010-09-11 20:22:54 +01:00
|
|
|
|
2015-08-08 12:13:58 -07:00
|
|
|
results.each_with_index do |name, i|
|
2014-04-27 16:01:54 -05:00
|
|
|
canonical_name = Formulary.canonical_name(name)
|
2015-08-08 12:13:58 -07:00
|
|
|
# Remove aliases from results when the full name was also found
|
|
|
|
if aliases.include?(name) && results.include?(canonical_name)
|
|
|
|
results.delete_at(i)
|
|
|
|
# Notify the user if the formula is installed
|
|
|
|
elsif (HOMEBREW_CELLAR/canonical_name).directory?
|
|
|
|
results[i] = "#{name} (installed)"
|
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|