| 
									
										
										
										
											2023-04-04 22:13:58 -07:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "cli/parser" | 
					
						
							|  |  |  | require "utils/github" | 
					
						
							| 
									
										
										
										
											2024-01-31 19:55:13 -08:00
										 |  |  | require "system_command" | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-01-31 19:55:13 -08:00
										 |  |  |   extend SystemCommand::Mixin | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 16:49:41 +01:00
										 |  |  |   NAMED_MONTHLY_AMOUNT = 100
 | 
					
						
							|  |  |  |   URL_MONTHLY_AMOUNT = 1000
 | 
					
						
							| 
									
										
										
										
											2021-02-17 17:53:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2024-01-31 19:55:13 -08:00
										 |  |  |   def self.update_sponsors_args | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |         Update the list of GitHub Sponsors in the `Homebrew/brew` README. | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       named_args :none | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 19:55:13 -08:00
										 |  |  |   def self.sponsor_name(sponsor) | 
					
						
							| 
									
										
										
										
											2022-09-07 16:49:41 +01:00
										 |  |  |     sponsor[:name] || sponsor[:login] | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 19:55:13 -08:00
										 |  |  |   def self.sponsor_logo(sponsor) | 
					
						
							| 
									
										
										
										
											2022-09-07 16:49:41 +01:00
										 |  |  |     "https://github.com/#{sponsor[:login]}.png?size=64" | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 19:55:13 -08:00
										 |  |  |   def self.sponsor_url(sponsor) | 
					
						
							| 
									
										
										
										
											2022-09-07 16:49:41 +01:00
										 |  |  |     "https://github.com/#{sponsor[:login]}" | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 19:55:13 -08:00
										 |  |  |   def self.update_sponsors | 
					
						
							| 
									
										
										
										
											2022-09-02 08:24:33 +01:00
										 |  |  |     update_sponsors_args.parse | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |     named_sponsors = [] | 
					
						
							|  |  |  |     logo_sponsors = [] | 
					
						
							| 
									
										
										
										
											2023-04-04 22:13:58 -07:00
										 |  |  |     # FIXME: This T.let should be unnecessary https://github.com/sorbet/sorbet/issues/6894 | 
					
						
							|  |  |  |     largest_monthly_amount = T.let(0, T.untyped) | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 16:49:41 +01:00
										 |  |  |     GitHub.sponsorships("Homebrew").each do |s| | 
					
						
							| 
									
										
										
										
											2022-09-08 12:19:12 +01:00
										 |  |  |       largest_monthly_amount = [s[:monthly_amount], s[:closest_tier_monthly_amount]].max | 
					
						
							| 
									
										
										
										
											2022-09-07 16:49:41 +01:00
										 |  |  |       named_sponsors << "[#{sponsor_name(s)}](#{sponsor_url(s)})" if largest_monthly_amount >= NAMED_MONTHLY_AMOUNT | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 16:49:41 +01:00
										 |  |  |       next if largest_monthly_amount < URL_MONTHLY_AMOUNT | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 16:49:41 +01:00
										 |  |  |       logo_sponsors << "[})](#{sponsor_url(s)})" | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 12:19:12 +01:00
										 |  |  |     odie "No sponsorships amounts found! Ensure you have sufficient permissions!" if largest_monthly_amount.zero? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |     named_sponsors << "many other users and organisations via [GitHub Sponsors](https://github.com/sponsors/Homebrew)" | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |     readme = HOMEBREW_REPOSITORY/"README.md" | 
					
						
							|  |  |  |     content = readme.read | 
					
						
							|  |  |  |     content.gsub!(/(Homebrew is generously supported by) .*\Z/m, "\\1 #{named_sponsors.to_sentence}.\n") | 
					
						
							|  |  |  |     content << "\n#{logo_sponsors.join}\n" if logo_sponsors.presence | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 14:49:05 -05:00
										 |  |  |     File.write(readme, content) | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     diff = system_command "git", args: [ | 
					
						
							|  |  |  |       "-C", HOMEBREW_REPOSITORY, "diff", "--exit-code", "README.md" | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     if diff.status.success? | 
					
						
							| 
									
										
										
										
											2022-09-02 08:24:33 +01:00
										 |  |  |       ofail "No changes to list of sponsors." | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |       puts "List of sponsors updated in the README." | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |