| 
									
										
										
										
											2013-06-11 22:23:19 -05:00
										 |  |  | require 'formula' | 
					
						
							|  |  |  | require 'blacklist' | 
					
						
							|  |  |  | require 'utils' | 
					
						
							| 
									
										
										
										
											2014-02-16 22:24:33 -05:00
										 |  |  | require 'thread' | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2013-10-22 11:58:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-16 22:24:33 -05:00
										 |  |  |   SEARCH_ERROR_QUEUE = Queue.new | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   def search | 
					
						
							|  |  |  |     if ARGV.include? '--macports' | 
					
						
							| 
									
										
										
										
											2012-12-27 23:34:29 -06:00
										 |  |  |       exec_browser "http://www.macports.org/ports.php?by=name&substr=#{ARGV.next}" | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +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}" | 
					
						
							| 
									
										
										
										
											2013-05-27 17:30:45 -07:00
										 |  |  |     elsif ARGV.include? '--debian' | 
					
						
							|  |  |  |       exec_browser "http://packages.debian.org/search?keywords=#{ARGV.next}&searchon=names&suite=all§ion=all" | 
					
						
							| 
									
										
										
										
											2013-08-05 15:05:02 -07:00
										 |  |  |     elsif ARGV.include? '--opensuse' | 
					
						
							|  |  |  |       exec_browser "http://software.opensuse.org/search?q=#{ARGV.next}" | 
					
						
							| 
									
										
										
										
											2013-08-09 09:32:51 -07:00
										 |  |  |     elsif ARGV.include? '--fedora' | 
					
						
							|  |  |  |       exec_browser "https://admin.fedoraproject.org/pkgdb/acls/list/*#{ARGV.next}*" | 
					
						
							| 
									
										
										
										
											2013-08-14 22:14:35 -07:00
										 |  |  |     elsif ARGV.include? '--ubuntu' | 
					
						
							|  |  |  |       exec_browser "http://packages.ubuntu.com/search?keywords=#{ARGV.next}&searchon=names&suite=all§ion=all" | 
					
						
							| 
									
										
										
										
											2014-04-27 15:21:30 -05:00
										 |  |  |     elsif ARGV.empty? | 
					
						
							| 
									
										
										
										
											2013-07-15 10:56:38 -05:00
										 |  |  |       puts_columns Formula.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-15 10:56:38 -05:00
										 |  |  |       if not query.empty? and $stdout.tty? and 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-15 10:56:38 -05:00
										 |  |  |       if count == 0 and not 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 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:17 -05:00
										 |  |  |   SEARCHABLE_TAPS = [ | 
					
						
							| 
									
										
										
										
											2014-04-18 10:59:12 +01:00
										 |  |  |     %w{Homebrew nginx}, | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:17 -05:00
										 |  |  |     %w{Homebrew apache}, | 
					
						
							|  |  |  |     %w{Homebrew versions}, | 
					
						
							|  |  |  |     %w{Homebrew dupes}, | 
					
						
							|  |  |  |     %w{Homebrew games}, | 
					
						
							|  |  |  |     %w{Homebrew science}, | 
					
						
							|  |  |  |     %w{Homebrew completions}, | 
					
						
							| 
									
										
										
										
											2013-08-03 10:27:45 -07:00
										 |  |  |     %w{Homebrew binary}, | 
					
						
							| 
									
										
										
										
											2013-12-28 12:14:38 +00:00
										 |  |  |     %w{Homebrew python}, | 
					
						
							| 
									
										
										
										
											2014-04-10 11:21:31 +01:00
										 |  |  |     %w{Homebrew php}, | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  |   def search_tap user, repo, rx | 
					
						
							| 
									
										
										
										
											2014-04-24 11:26:45 +09:00
										 |  |  |     return [] if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}/homebrew-#{repo.downcase}").directory? | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2014-04-27 17:27:22 -05:00
										 |  |  |     tree = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 16:04:53 -05:00
										 |  |  |     GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |json| | 
					
						
							| 
									
										
										
										
											2014-02-08 16:04:52 -05:00
										 |  |  |       user = user.downcase if user == "Homebrew" # special handling for the Homebrew organization | 
					
						
							| 
									
										
										
										
											2014-02-08 17:53:00 -05:00
										 |  |  |       json["tree"].each do |object| | 
					
						
							|  |  |  |         next unless object["type"] == "blob" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-27 17:27:22 -05:00
										 |  |  |         subtree, file = File.split(object["path"]) | 
					
						
							| 
									
										
										
										
											2014-02-08 17:53:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-27 17:27:22 -05:00
										 |  |  |         if File.extname(file) == ".rb" | 
					
						
							|  |  |  |           tree[subtree] ||= [] | 
					
						
							|  |  |  |           tree[subtree] << file | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-04-27 17:27:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     paths = tree["Formula"] || tree["HomebrewFormula"] || tree["."] || [] | 
					
						
							|  |  |  |     paths.each do |path| | 
					
						
							|  |  |  |       name = File.basename(path, ".rb") | 
					
						
							|  |  |  |       results << "#{user}/#{repo}/#{name}" if rx === name | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     [] | 
					
						
							| 
									
										
										
										
											2014-02-16 22:24:33 -05:00
										 |  |  |   else | 
					
						
							|  |  |  |     results | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:16 -05:00
										 |  |  |   def search_formulae rx | 
					
						
							| 
									
										
										
										
											2013-07-15 10:56:38 -05:00
										 |  |  |     aliases = Formula.aliases | 
					
						
							| 
									
										
										
										
											2014-04-27 16:01:46 -05:00
										 |  |  |     results = (Formula.names+aliases).grep(rx).sort | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-15 10:56:38 -05:00
										 |  |  |     # Filter out aliases when the full name was also found | 
					
						
							| 
									
										
										
										
											2014-04-27 16:01:54 -05:00
										 |  |  |     results.reject do |name| | 
					
						
							|  |  |  |       canonical_name = Formulary.canonical_name(name) | 
					
						
							|  |  |  |       aliases.include?(name) && results.include?(canonical_name) | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |