| 
									
										
										
										
											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. | 
					
						
							|  |  |  | #: | 
					
						
							| 
									
										
										
										
											2017-02-25 17:37:57 -05: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" | 
					
						
							| 
									
										
										
										
											2017-03-18 17:02:08 +02:00
										 |  |  | require "missing_formula" | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   def search | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |     if ARGV.empty? | 
					
						
							| 
									
										
										
										
											2017-10-14 04:17:03 +01:00
										 |  |  |       puts Formatter.columns(Formula.full_names.sort) | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2017-08-07 18:18:54 -07:00
										 |  |  |       exec_browser "https://apps.fedoraproject.org/packages/s/#{ARGV.next}" | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     elsif ARGV.include? "--ubuntu" | 
					
						
							| 
									
										
										
										
											2017-08-22 10:31:21 +00:00
										 |  |  |       exec_browser "https://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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       begin | 
					
						
							|  |  |  |         result = Formulary.factory(query).name | 
					
						
							| 
									
										
										
										
											2017-08-11 13:04:37 -04:00
										 |  |  |         results = Array(result) | 
					
						
							| 
									
										
										
										
											2014-04-27 15:21:30 -05:00
										 |  |  |       rescue FormulaUnavailableError | 
					
						
							| 
									
										
										
										
											2017-08-11 15:59:08 -04:00
										 |  |  |         _, _, name = query.split("/", 3) | 
					
						
							|  |  |  |         results = search_taps(name) | 
					
						
							| 
									
										
										
										
											2013-01-29 23:34:55 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-04-27 15:21:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 04:17:03 +01:00
										 |  |  |       puts Formatter.columns(results.sort) 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) | 
					
						
							| 
									
										
										
										
											2017-10-14 04:17:03 +01:00
										 |  |  |       puts Formatter.columns(local_results.sort) unless local_results.empty? | 
					
						
							| 
									
										
										
										
											2017-08-15 10:25:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 14:11:04 +01:00
										 |  |  |       tap_results = search_taps(query) | 
					
						
							| 
									
										
										
										
											2017-10-14 04:17:03 +01:00
										 |  |  |       puts Formatter.columns(tap_results.sort) 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-15 10:25:51 +01:00
										 |  |  |         ohai "Searching blacklisted, migrated and deleted formulae..." | 
					
						
							| 
									
										
										
										
											2017-03-22 21:56:30 +00:00
										 |  |  |         if reason = Homebrew::MissingFormula.reason(query, silent: true) | 
					
						
							| 
									
										
										
										
											2017-09-24 20:12:58 +01:00
										 |  |  |           if count.positive? | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |             puts | 
					
						
							| 
									
										
										
										
											2017-03-18 17:04:44 +02:00
										 |  |  |             puts "If you meant #{query.inspect} specifically:" | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-03-20 20:37:12 +01:00
										 |  |  |           puts reason | 
					
						
							| 
									
										
										
										
											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}." | 
					
						
							| 
									
										
										
										
											2017-04-24 14:11:04 +01:00
										 |  |  |           GitHub.print_pull_requests_matching(query) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 14:11:04 +01:00
										 |  |  |     return unless $stdout.tty? | 
					
						
							|  |  |  |     return if ARGV.empty? | 
					
						
							|  |  |  |     metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze | 
					
						
							|  |  |  |     return unless metacharacters.any? do |char| | 
					
						
							|  |  |  |       ARGV.any? do |arg| | 
					
						
							|  |  |  |         arg.include?(char) && !arg.start_with?("/") | 
					
						
							| 
									
										
										
										
											2015-04-17 12:07:45 -07:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |     ohai <<~EOS | 
					
						
							| 
									
										
										
										
											2017-04-24 14:11:04 +01:00
										 |  |  |       Did you mean to perform a regular expression search? | 
					
						
							|  |  |  |       Surround your query with /slashes/ to search locally by regex. | 
					
						
							|  |  |  |     EOS | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:16 -05:00
										 |  |  |   def query_regexp(query) | 
					
						
							|  |  |  |     case query | 
					
						
							| 
									
										
										
										
											2017-06-10 20:23:20 +03:00
										 |  |  |     when %r{^/(.*)/$} then Regexp.new(Regexp.last_match(1)) | 
					
						
							| 
									
										
										
										
											2013-06-12 14:48:16 -05:00
										 |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 18:40:30 +01:00
										 |  |  |   def search_taps(query, silent: false) | 
					
						
							| 
									
										
										
										
											2017-08-15 10:25:51 +01:00
										 |  |  |     return [] if ENV["HOMEBREW_NO_GITHUB_API"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Use stderr to avoid breaking parsed output | 
					
						
							| 
									
										
										
										
											2017-08-23 18:40:30 +01:00
										 |  |  |     unless silent | 
					
						
							|  |  |  |       $stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-08-15 10:25:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-06 22:12:29 -07:00
										 |  |  |     matches = begin | 
					
						
							|  |  |  |       GitHub.search_code( | 
					
						
							|  |  |  |         user: ["Homebrew", "caskroom"], | 
					
						
							| 
									
										
										
										
											2018-04-06 23:14:30 -07:00
										 |  |  |         path: ["Formula", "HomebrewFormula", "Casks", "."], | 
					
						
							| 
									
										
										
										
											2018-04-06 22:12:29 -07:00
										 |  |  |         filename: query, | 
					
						
							|  |  |  |         extension: "rb", | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |     rescue GitHub::Error => error | 
					
						
							|  |  |  |       opoo "Error searching on GitHub: #{error}\n" | 
					
						
							|  |  |  |       [] | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-08-14 11:08:56 -04:00
										 |  |  |     matches.map do |match| | 
					
						
							| 
									
										
										
										
											2018-04-06 23:14:30 -07:00
										 |  |  |       filename = File.basename(match["path"], ".rb") | 
					
						
							| 
									
										
										
										
											2017-04-24 19:31:21 +02:00
										 |  |  |       tap = Tap.fetch(match["repository"]["full_name"]) | 
					
						
							| 
									
										
										
										
											2017-06-02 22:19:04 +01:00
										 |  |  |       next if tap.installed? && match["repository"]["owner"]["login"] != "caskroom" | 
					
						
							| 
									
										
										
										
											2018-04-06 23:14:30 -07:00
										 |  |  |       "#{tap.name}/#{filename}" | 
					
						
							| 
									
										
										
										
											2017-04-24 19:31:21 +02:00
										 |  |  |     end.compact | 
					
						
							| 
									
										
										
										
											2012-03-16 22:37:11 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-25 16:22:34 +02:00
										 |  |  |   def search_formulae(regex) | 
					
						
							| 
									
										
										
										
											2017-08-15 10:25:51 +01:00
										 |  |  |     # Use stderr to avoid breaking parsed output | 
					
						
							|  |  |  |     $stderr.puts Formatter.headline("Searching local taps...", color: :blue) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-13 17:47:53 +08:00
										 |  |  |     aliases = Formula.alias_full_names | 
					
						
							| 
									
										
										
										
											2017-06-01 16:06:51 +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 |