| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "cli/parser" | 
					
						
							|  |  |  | require "utils/github" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 17:53:10 +00:00
										 |  |  |   NAMED_TIER_AMOUNT = 100
 | 
					
						
							|  |  |  |   URL_TIER_AMOUNT = 1000
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |   def sponsors_args | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |   def sponsor_name(s) | 
					
						
							|  |  |  |     s["name"] || s["login"] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def sponsor_logo(s) | 
					
						
							|  |  |  |     "https://github.com/#{s["login"]}.png?size=64" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def sponsor_url(s) | 
					
						
							|  |  |  |     "https://github.com/#{s["login"]}" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |   def sponsors | 
					
						
							|  |  |  |     sponsors_args.parse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |     named_sponsors = [] | 
					
						
							|  |  |  |     logo_sponsors = [] | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     GitHub.sponsors_by_tier("Homebrew").each do |tier| | 
					
						
							| 
									
										
										
										
											2021-02-17 17:53:10 +00:00
										 |  |  |       if tier["tier"] >= NAMED_TIER_AMOUNT | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |         named_sponsors += tier["sponsors"].map do |s| | 
					
						
							|  |  |  |           "[#{sponsor_name(s)}](#{sponsor_url(s)})" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 17:53:10 +00:00
										 |  |  |       next if tier["tier"] < URL_TIER_AMOUNT | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 13:31:56 +11:00
										 |  |  |       logo_sponsors += tier["sponsors"].map do |s| | 
					
						
							|  |  |  |         "[})](#{sponsor_url(s)})" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-04 15:03:17 +10:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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? | 
					
						
							|  |  |  |       puts "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 |