| 
									
										
										
										
											2024-06-30 19:10:26 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 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-03-21 22:12:37 -07:00
										 |  |  |   module DevCmd | 
					
						
							|  |  |  |     class UpdateSponsors < AbstractCommand | 
					
						
							|  |  |  |       include SystemCommand::Mixin | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |       NAMED_MONTHLY_AMOUNT = 100
 | 
					
						
							|  |  |  |       URL_MONTHLY_AMOUNT = 1000
 | 
					
						
							| 
									
										
										
										
											2021-02-17 17:53:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           Update the list of GitHub Sponsors in the `Homebrew/brew` README. | 
					
						
							|  |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |         named_args :none | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         named_sponsors = [] | 
					
						
							|  |  |  |         logo_sponsors = [] | 
					
						
							| 
									
										
										
										
											2024-07-04 10:35:00 +01:00
										 |  |  |         largest_monthly_amount = T.let(0, Integer) | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |         GitHub.sponsorships("Homebrew").each do |s| | 
					
						
							|  |  |  |           largest_monthly_amount = [s[:monthly_amount], s[:closest_tier_monthly_amount]].max | 
					
						
							|  |  |  |           if largest_monthly_amount >= NAMED_MONTHLY_AMOUNT | 
					
						
							|  |  |  |             named_sponsors << "[#{sponsor_name(s)}](#{sponsor_url(s)})" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |           next if largest_monthly_amount < URL_MONTHLY_AMOUNT | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |           logo_sponsors << "[})](#{sponsor_url(s)})" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |         odie "No sponsorships amounts found! Ensure you have sufficient permissions!" if largest_monthly_amount.zero? | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |         named_sponsors << "many other users and organisations via [GitHub Sponsors](https://github.com/sponsors/Homebrew)" | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07: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 | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |         File.write(readme, content) | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |         diff = system_command "git", args: [ | 
					
						
							|  |  |  |           "-C", HOMEBREW_REPOSITORY, "diff", "--exit-code", "README.md" | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         if diff.status.success? | 
					
						
							|  |  |  |           ofail "No changes to list of sponsors." | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           puts "List of sponsors updated in the README." | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-09-08 12:19:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 10:35:00 +01:00
										 |  |  |       sig { params(sponsor: T::Hash[Symbol, String]).returns(T.nilable(String)) } | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |       def sponsor_name(sponsor) | 
					
						
							|  |  |  |         sponsor[:name] || sponsor[:login] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 10:35:00 +01:00
										 |  |  |       sig { params(sponsor: T::Hash[Symbol, String]).returns(String) } | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |       def sponsor_logo(sponsor) | 
					
						
							|  |  |  |         "https://github.com/#{sponsor[:login]}.png?size=64" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 10:35:00 +01:00
										 |  |  |       sig { params(sponsor: T::Hash[Symbol, String]).returns(String) } | 
					
						
							| 
									
										
										
										
											2024-03-21 22:12:37 -07:00
										 |  |  |       def sponsor_url(sponsor) | 
					
						
							|  |  |  |         "https://github.com/#{sponsor[:login]}" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |