| 
									
										
										
										
											2020-11-25 17:03:23 +01:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2020-06-29 10:16:58 -05:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 09:47:19 -05:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  | require "livecheck/livecheck" | 
					
						
							| 
									
										
										
										
											2020-06-29 09:47:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 09:47:19 -05:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2020-06-29 09:47:19 -05:00
										 |  |  |   def bump_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2020-07-28 09:30:19 -05:00
										 |  |  |         Display out-of-date brew formulae and the latest version available. | 
					
						
							|  |  |  |         Also displays whether a pull request has been opened with the URL. | 
					
						
							| 
									
										
										
										
											2020-06-29 09:47:19 -05:00
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:41 -05:00
										 |  |  |       flag   "--limit=", | 
					
						
							|  |  |  |              description: "Limit number of package results returned." | 
					
						
							| 
									
										
										
										
											2021-01-15 15:46:24 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       named_args :formula | 
					
						
							| 
									
										
										
										
											2020-06-29 09:47:19 -05:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def bump | 
					
						
							| 
									
										
										
										
											2020-08-05 10:10:05 -05:00
										 |  |  |     args = bump_args.parse | 
					
						
							| 
									
										
										
										
											2020-07-01 10:21:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |     requested_formulae = args.named.to_formulae.presence | 
					
						
							| 
									
										
										
										
											2020-08-11 15:29:22 +00:00
										 |  |  |     requested_limit = args.limit.to_i if args.limit.present? | 
					
						
							| 
									
										
										
										
											2020-07-28 09:30:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |     if requested_formulae | 
					
						
							|  |  |  |       Livecheck.load_other_tap_strategies(requested_formulae) | 
					
						
							| 
									
										
										
										
											2020-08-16 20:38:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |       requested_formulae.each_with_index do |formula, i| | 
					
						
							|  |  |  |         puts if i.positive? | 
					
						
							| 
									
										
										
										
											2020-07-02 17:06:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |         if formula.head_only? | 
					
						
							|  |  |  |           ohai formula.name | 
					
						
							|  |  |  |           puts "Formula is HEAD-only." | 
					
						
							|  |  |  |           next | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-07-28 09:30:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |         package_data = Repology.single_package_query(formula.name) | 
					
						
							| 
									
										
										
										
											2021-01-25 17:12:43 +05:30
										 |  |  |         retrieve_and_display_info(formula, package_data&.values&.first) | 
					
						
							| 
									
										
										
										
											2020-08-16 20:38:03 -05:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |     else | 
					
						
							|  |  |  |       outdated_packages = Repology.parse_api_response(requested_limit) | 
					
						
							|  |  |  |       outdated_packages.each_with_index do |(_name, repositories), i| | 
					
						
							|  |  |  |         puts if i.positive? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         homebrew_repo = repositories.find do |repo| | 
					
						
							|  |  |  |           repo["repo"] == "homebrew" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 22:12:34 +05:30
										 |  |  |         next if homebrew_repo.blank? | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         formula = begin | 
					
						
							|  |  |  |           Formula[homebrew_repo["srcname"]] | 
					
						
							|  |  |  |         rescue | 
					
						
							|  |  |  |           next | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-08-16 20:38:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-25 17:12:43 +05:30
										 |  |  |         retrieve_and_display_info(formula, repositories) | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         break if requested_limit && i >= requested_limit | 
					
						
							| 
									
										
										
										
											2020-08-16 20:38:03 -05:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2020-08-16 20:38:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |   def livecheck_result(formula) | 
					
						
							| 
									
										
										
										
											2021-01-15 15:46:24 +05:30
										 |  |  |     skip_result = Livecheck::SkipConditions.skip_information(formula) | 
					
						
							|  |  |  |     if skip_result.present? | 
					
						
							| 
									
										
										
										
											2021-01-11 09:37:02 +05:30
										 |  |  |       return "#{skip_result[:status]}#{" - #{skip_result[:messages].join(", ")}" if skip_result[:messages].present?}" | 
					
						
							| 
									
										
										
										
											2020-08-05 10:10:05 -05:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-08-16 19:22:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |     version_info = Livecheck.latest_version( | 
					
						
							|  |  |  |       formula, | 
					
						
							|  |  |  |       json: true, full_name: false, verbose: false, debug: false, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     latest = version_info[:latest] if version_info.present? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-24 19:07:17 +05:30
										 |  |  |     return "unable to get versions" if latest.blank? | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     latest.to_s | 
					
						
							| 
									
										
										
										
											2020-07-02 17:06:39 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |   def retrieve_pull_requests(formula) | 
					
						
							|  |  |  |     pull_requests = GitHub.fetch_pull_requests(formula.name, formula.tap&.full_name, state: "open") | 
					
						
							|  |  |  |     if pull_requests.try(:any?) | 
					
						
							|  |  |  |       pull_requests = pull_requests.map { |pr| "#{pr["title"]} (#{Formatter.url(pr["html_url"])})" }.join(", ") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return "none" if pull_requests.blank? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pull_requests | 
					
						
							| 
									
										
										
										
											2020-07-06 03:32:18 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-25 17:12:43 +05:30
										 |  |  |   def retrieve_and_display_info(formula, repositories) | 
					
						
							|  |  |  |     current_version = formula.stable.version.to_s | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     repology_latest = if repositories.present? | 
					
						
							|  |  |  |       Repology.latest_version(repositories) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       "not found" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     livecheck_latest = livecheck_result(formula) | 
					
						
							|  |  |  |     pull_requests = retrieve_pull_requests(formula) | 
					
						
							| 
									
										
										
										
											2020-07-31 10:52:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-24 19:07:17 +05:30
										 |  |  |     title = if current_version == repology_latest && | 
					
						
							|  |  |  |                current_version == livecheck_latest | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  |       "#{formula} is up to date!" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       formula.name | 
					
						
							| 
									
										
										
										
											2020-07-02 17:06:39 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2021-01-11 08:29:34 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     ohai title | 
					
						
							| 
									
										
										
										
											2021-01-24 21:25:12 -05:00
										 |  |  |     puts <<~EOS | 
					
						
							|  |  |  |       Current formula version:  #{current_version} | 
					
						
							|  |  |  |       Latest Repology version:  #{repology_latest} | 
					
						
							|  |  |  |       Latest livecheck version: #{livecheck_latest} | 
					
						
							|  |  |  |       Open pull requests:       #{pull_requests} | 
					
						
							|  |  |  |     EOS | 
					
						
							| 
									
										
										
										
											2020-06-29 09:47:19 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | end |