| 
									
										
										
										
											2016-05-26 17:52:58 +02:00
										 |  |  | #:  * `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. | 
					
						
							|  |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-05-26 07:45:25 -07:00
										 |  |  | #:  * `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. | 
					
						
							|  |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-05-26 07:45:25 -07:00
										 |  |  | #:    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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2015-09-09 13:00:43 +08:00
										 |  |  | require "descriptions" | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-16 22:24:33 -05:00
										 |  |  |   SEARCH_ERROR_QUEUE = Queue.new | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   def search | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |     if ARGV.empty? | 
					
						
							| 
									
										
										
										
											2016-10-02 20:39:43 +02:00
										 |  |  |       puts Formatter.columns(Formula.full_names) | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |     elsif 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 | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |       regex = query_regexp(query) | 
					
						
							|  |  |  |       Descriptions.search(regex, :desc).print | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 20:39:43 +02:00
										 |  |  |       results = Array(result) | 
					
						
							|  |  |  |       puts Formatter.columns(results) unless results.empty? | 
					
						
							| 
									
										
										
										
											2010-11-14 03:52:59 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2014-04-27 15:21:30 -05:00
										 |  |  |       query = ARGV.first | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |       regex = query_regexp(query) | 
					
						
							|  |  |  |       local_results = search_formulae(regex) | 
					
						
							| 
									
										
										
										
											2016-10-02 20:39:43 +02:00
										 |  |  |       puts Formatter.columns(local_results) unless local_results.empty? | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |       tap_results = search_taps(regex) | 
					
						
							| 
									
										
										
										
											2016-10-02 20:39:43 +02:00
										 |  |  |       puts Formatter.columns(tap_results) unless tap_results.empty? | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |       if $stdout.tty? | 
					
						
							|  |  |  |         count = local_results.length + tap_results.length | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if msg = blacklisted?(query) | 
					
						
							|  |  |  |           if count > 0
 | 
					
						
							|  |  |  |             puts | 
					
						
							|  |  |  |             puts "If you meant #{query.inspect} precisely:" | 
					
						
							|  |  |  |             puts | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           puts msg | 
					
						
							| 
									
										
										
										
											2016-09-10 10:24:57 +01:00
										 |  |  |         elsif count.zero? | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |           puts "No formula found for #{query.inspect}." | 
					
						
							|  |  |  |           begin | 
					
						
							|  |  |  |             GitHub.print_pull_requests_matching(query) | 
					
						
							|  |  |  |           rescue GitHub::Error => e | 
					
						
							|  |  |  |             SEARCH_ERROR_QUEUE << e | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											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-10-15 17:10:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if $stdout.tty? | 
					
						
							| 
									
										
										
										
											2016-08-25 20:40:26 +01:00
										 |  |  |       metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?] | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |       bad_regex = metacharacters.any? do |char| | 
					
						
							|  |  |  |         ARGV.any? do |arg| | 
					
						
							|  |  |  |           arg.include?(char) && !arg.start_with?("/") | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-05 22:01:32 +08:00
										 |  |  |       if !ARGV.empty? && bad_regex | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |         ohai "Did you mean to perform a regular expression search?" | 
					
						
							|  |  |  |         ohai "Surround your query with /slashes/ to search by regex." | 
					
						
							| 
									
										
										
										
											2015-04-17 12:07:45 -07:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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] } + [ | 
					
						
							| 
									
										
										
										
											2016-01-11 20:34:55 +11:00
										 |  |  |     %w[Caskroom cask], | 
					
						
							| 
									
										
										
										
											2016-09-10 10:24:57 +01:00
										 |  |  |     %w[Caskroom versions], | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2016-03-16 21:27:55 +00:00
										 |  |  |   rescue RegexpError | 
					
						
							|  |  |  |     odie "#{query} is not a valid regex" | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:16 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |   def search_taps(regex_or_string) | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:17 -05:00
										 |  |  |     SEARCHABLE_TAPS.map do |user, repo| | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |       Thread.new { search_tap(user, repo, regex_or_string) } | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:17 -05:00
										 |  |  |     end.inject([]) do |results, t| | 
					
						
							|  |  |  |       results.concat(t.value) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |   def search_tap(user, repo, regex_or_string) | 
					
						
							|  |  |  |     regex = regex_or_string.is_a?(String) ? /^#{Regexp.escape(regex_or_string)}$/ : regex_or_string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 15:02:42 +00:00
										 |  |  |     if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}/homebrew-#{repo.downcase}").directory? && \ | 
					
						
							| 
									
										
										
										
											2016-01-11 20:34:55 +11:00
										 |  |  |        user != "Caskroom" | 
					
						
							| 
									
										
										
										
											2014-11-28 15:02:42 +00:00
										 |  |  |       return [] | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 21:29:49 +01:00
										 |  |  |     remote_tap_formulae = Hash.new do |cache, key| | 
					
						
							| 
									
										
										
										
											2015-06-16 17:55:19 +08:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-08 09:25:34 +01:00
										 |  |  |       paths = tree["Formula"] || tree["HomebrewFormula"] || tree["."] || [] | 
					
						
							|  |  |  |       paths += tree["Casks"] || [] | 
					
						
							| 
									
										
										
										
											2015-06-16 17:55:19 +08:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 21:29:49 +01:00
										 |  |  |     names = remote_tap_formulae["#{user}/#{repo}"] | 
					
						
							| 
									
										
										
										
											2015-06-16 17:55:19 +08:00
										 |  |  |     user = user.downcase if user == "Homebrew" # special handling for the Homebrew organization | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |     names.select { |name| name =~ regex }.map { |name| "#{user}/#{repo}/#{name}" } | 
					
						
							| 
									
										
										
										
											2016-09-10 10:24:57 +01:00
										 |  |  |   rescue GitHub::HTTPNotFoundError | 
					
						
							| 
									
										
										
										
											2014-02-16 22:24:33 -05:00
										 |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |   def search_formulae(regex) | 
					
						
							| 
									
										
										
										
											2015-09-13 17:47:53 +08:00
										 |  |  |     aliases = Formula.alias_full_names | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |     results = (Formula.full_names+aliases).grep(regex).sort | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 00:23:30 -07:00
										 |  |  |     results.map do |name| | 
					
						
							| 
									
										
										
										
											2015-10-09 15:24:27 +08:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 00:23:30 -07: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? | 
					
						
							| 
									
										
										
										
											2015-12-07 13:11:34 +00:00
										 |  |  |         pretty_installed(name) | 
					
						
							| 
									
										
										
										
											2015-08-31 00:23:30 -07:00
										 |  |  |       else | 
					
						
							|  |  |  |         name | 
					
						
							| 
									
										
										
										
											2015-08-08 12:13:58 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-08-31 00:23:30 -07:00
										 |  |  |     end.compact | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |