| 
									
										
										
										
											2013-06-11 22:23:19 -05:00
										 |  |  | require 'formula' | 
					
						
							|  |  |  | require 'blacklist' | 
					
						
							|  |  |  | require 'utils' | 
					
						
							| 
									
										
										
										
											2013-06-22 16:51:08 -05:00
										 |  |  | require 'utils/json' | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew extend self | 
					
						
							| 
									
										
										
										
											2013-01-29 23:34:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # A regular expession to capture the username (one or more char but no `/`, | 
					
						
							|  |  |  |   # which has to be escaped like `\/`), repository, followed by an optional `/` | 
					
						
							|  |  |  |   # and an optional query. | 
					
						
							|  |  |  |   TAP_QUERY_REGEX = /^([^\/]+)\/([^\/]+)\/?(.+)?$/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2013-07-15 10:56:38 -05:00
										 |  |  |     elsif (query = ARGV.first).nil? | 
					
						
							|  |  |  |       puts_columns Formula.names | 
					
						
							| 
									
										
										
										
											2013-01-29 23:34:55 +01:00
										 |  |  |     elsif ARGV.first =~ TAP_QUERY_REGEX | 
					
						
							|  |  |  |       # So look for user/repo/query or list all formulae by the tap | 
					
						
							|  |  |  |       # we downcase to avoid case-insensitive filesystem issues. | 
					
						
							|  |  |  |       user, repo, query = $1.downcase, $2.downcase, $3 | 
					
						
							|  |  |  |       tap_dir = HOMEBREW_LIBRARY/"Taps/#{user}-#{repo}" | 
					
						
							|  |  |  |       # If, instead of `user/repo/query` the user wrote `user/repo query`: | 
					
						
							|  |  |  |       query = ARGV[1] if query.nil? | 
					
						
							|  |  |  |       if tap_dir.directory? | 
					
						
							|  |  |  |         # There is a local tap already: | 
					
						
							|  |  |  |         result = Dir["#{tap_dir}/*.rb"].map{ |f| File.basename(f).chomp('.rb') } | 
					
						
							|  |  |  |         result = result.grep(query_regexp(query)) unless query.nil? | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         # Search online: | 
					
						
							|  |  |  |         query = '' if query.nil? | 
					
						
							|  |  |  |         result = search_tap(user, repo, query_regexp(query)) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       puts_columns result | 
					
						
							| 
									
										
										
										
											2010-11-14 03:52:59 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |         puts "No formula found for #{query.inspect}. Searching open pull requests..." | 
					
						
							|  |  |  |         begin | 
					
						
							|  |  |  |           GitHub.find_pull_requests(rx) { |pull| puts pull } | 
					
						
							|  |  |  |         rescue GitHub::Error => e | 
					
						
							|  |  |  |           opoo e.message | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:17 -05:00
										 |  |  |   SEARCHABLE_TAPS = [ | 
					
						
							|  |  |  |     %w{josegonzalez php}, | 
					
						
							|  |  |  |     %w{samueljohn python}, | 
					
						
							| 
									
										
										
										
											2013-08-05 13:32:23 -07:00
										 |  |  |     %w{marcqualie 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}, | 
					
						
							|  |  |  |     %w{Homebrew x11}, | 
					
						
							| 
									
										
										
										
											2013-08-03 10:27:45 -07:00
										 |  |  |     %w{Homebrew binary}, | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2012-03-18 02:03:19 +00:00
										 |  |  |     return [] if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}-#{repo.downcase}").directory? | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2013-05-07 14:07:25 -04:00
										 |  |  |     GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |f| | 
					
						
							| 
									
										
										
										
											2013-05-18 08:34:28 -04:00
										 |  |  |       user.downcase! if user == "Homebrew" # special handling for the Homebrew organization | 
					
						
							| 
									
										
										
										
											2013-06-22 16:51:08 -05:00
										 |  |  |       Utils::JSON.load(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file| | 
					
						
							| 
									
										
										
										
											2013-05-18 08:34:28 -04:00
										 |  |  |         name = File.basename(file, '.rb') | 
					
						
							|  |  |  |         if file =~ /\.rb$/ and name =~ rx | 
					
						
							|  |  |  |           results << "#{user}/#{repo}/#{name}" | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     results | 
					
						
							| 
									
										
										
										
											2013-07-28 15:38:01 -05:00
										 |  |  |   rescue GitHub::Error, Utils::JSON::Error | 
					
						
							| 
									
										
										
										
											2013-07-01 16:58:16 -05:00
										 |  |  |     [] | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |     results = (Formula.names+aliases).grep(rx) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |     results.reject do |alias_name| | 
					
						
							|  |  |  |       if aliases.include? alias_name | 
					
						
							|  |  |  |         resolved_name = (HOMEBREW_REPOSITORY+"Library/Aliases"+alias_name).readlink.basename('.rb').to_s | 
					
						
							|  |  |  |         results.include? resolved_name | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |