| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2010-11-14 03:52:59 +00:00
										 |  |  | require "blacklist" | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew extend self | 
					
						
							|  |  |  |   def search | 
					
						
							|  |  |  |     if ARGV.include? '--macports' | 
					
						
							|  |  |  |       exec "open", "http://www.macports.org/ports.php?by=name&substr=#{ARGV.next}" | 
					
						
							|  |  |  |     elsif ARGV.include? '--fink' | 
					
						
							|  |  |  |       exec "open", "http://pdb.finkproject.org/pdb/browse.php?summary=#{ARGV.next}" | 
					
						
							| 
									
										
										
										
											2010-11-14 03:52:59 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       query = ARGV.first | 
					
						
							| 
									
										
										
										
											2012-01-12 18:52:22 -06:00
										 |  |  |       rx = case query | 
					
						
							|  |  |  |       when nil then "" | 
					
						
							|  |  |  |       when %r{^/(.*)/$} then Regexp.new($1) | 
					
						
							| 
									
										
										
										
											2012-01-11 20:49:08 -06:00
										 |  |  |       else | 
					
						
							|  |  |  |         /.*#{Regexp.escape query}.*/i | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       search_results = search_brews rx | 
					
						
							| 
									
										
										
										
											2010-11-14 03:52:59 +00:00
										 |  |  |       puts_columns search_results | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-09 20:50:01 -08:00
										 |  |  |       if not query.to_s.empty? and $stdout.tty? and msg = blacklisted?(query) | 
					
						
							| 
									
										
										
										
											2010-11-14 03:52:59 +00:00
										 |  |  |         unless search_results.empty? | 
					
						
							|  |  |  |           puts | 
					
						
							|  |  |  |           puts "If you meant `#{query}' precisely:" | 
					
						
							|  |  |  |           puts | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         puts msg | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-01-11 20:49:08 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 16:03:27 +00:00
										 |  |  |       if query | 
					
						
							|  |  |  |         $found = search_results.length | 
					
						
							| 
									
										
										
										
											2012-03-25 11:06:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-08 22:25:04 +01:00
										 |  |  |         threads = [] | 
					
						
							|  |  |  |         results = [] | 
					
						
							|  |  |  |         threads << Thread.new { search_tap "josegonzalez", "php", rx } | 
					
						
							| 
									
										
										
										
											2012-09-01 19:01:56 -07:00
										 |  |  |         threads << Thread.new { search_tap "alanthing", "apachemod", rx } | 
					
						
							| 
									
										
										
										
											2012-08-08 22:25:04 +01:00
										 |  |  |         threads << Thread.new { search_tap "Homebrew", "versions", rx } | 
					
						
							|  |  |  |         threads << Thread.new { search_tap "Homebrew", "dupes", rx } | 
					
						
							|  |  |  |         threads << Thread.new { search_tap "Homebrew", "games", rx } | 
					
						
							|  |  |  |         threads << Thread.new { search_tap "Homebrew", "science", rx } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         threads.each do |t| | 
					
						
							|  |  |  |           results << t.value | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results.each { |r| puts_columns r } | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-20 16:03:27 +00:00
										 |  |  |         if $found == 0 and not blacklisted? query | 
					
						
							|  |  |  |           puts "No formula found for \"#{query}\". Searching open pull requests..." | 
					
						
							|  |  |  |           GitHub.find_pull_requests(rx) { |pull| puts pull } | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |     require 'open-uri' | 
					
						
							| 
									
										
										
										
											2012-04-05 13:06:36 -04:00
										 |  |  |     require 'vendor/multi_json' | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2012-04-05 13:06:36 -04:00
										 |  |  |     open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |f| | 
					
						
							| 
									
										
										
										
											2012-03-18 01:51:36 +00:00
										 |  |  |       user.downcase! if user == "Homebrew" # special handling for the Homebrew organization | 
					
						
							| 
									
										
										
										
											2012-04-05 13:06:36 -04:00
										 |  |  |       MultiJson.decode(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file| | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  |         name = File.basename(file, '.rb') | 
					
						
							|  |  |  |         if file =~ /\.rb$/ and name =~ rx | 
					
						
							|  |  |  |           results << "#{user}/#{repo}/#{name}" | 
					
						
							|  |  |  |           $found += 1
 | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     results | 
					
						
							|  |  |  |   rescue | 
					
						
							|  |  |  |     [] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-11 20:49:08 -06:00
										 |  |  |   def search_brews rx | 
					
						
							|  |  |  |     if rx.to_s.empty? | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |       Formula.names | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       aliases = Formula.aliases | 
					
						
							|  |  |  |       results = (Formula.names+aliases).grep rx | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Filter out aliases when the full name was also found | 
					
						
							|  |  |  |       results.reject do |alias_name| | 
					
						
							|  |  |  |         if aliases.include? alias_name | 
					
						
							| 
									
										
										
										
											2010-12-31 11:00:15 -08:00
										 |  |  |           resolved_name = (HOMEBREW_REPOSITORY+"Library/Aliases"+alias_name).readlink.basename('.rb').to_s | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |           results.include? resolved_name | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |