diff --git a/Library/Homebrew/dev-cmd/update-sponsors.rb b/Library/Homebrew/dev-cmd/update-sponsors.rb index 2c4c20d23c..8d213a503f 100644 --- a/Library/Homebrew/dev-cmd/update-sponsors.rb +++ b/Library/Homebrew/dev-cmd/update-sponsors.rb @@ -1,74 +1,79 @@ # typed: true # frozen_string_literal: true +require "abstract_command" require "cli/parser" require "utils/github" require "system_command" module Homebrew - extend SystemCommand::Mixin + module DevCmd + class UpdateSponsors < AbstractCommand + include SystemCommand::Mixin - NAMED_MONTHLY_AMOUNT = 100 - URL_MONTHLY_AMOUNT = 1000 + NAMED_MONTHLY_AMOUNT = 100 + URL_MONTHLY_AMOUNT = 1000 - sig { returns(CLI::Parser) } - def self.update_sponsors_args - Homebrew::CLI::Parser.new do - description <<~EOS - Update the list of GitHub Sponsors in the `Homebrew/brew` README. - EOS + cmd_args do + description <<~EOS + Update the list of GitHub Sponsors in the `Homebrew/brew` README. + EOS - named_args :none - end - end + named_args :none + end - def self.sponsor_name(sponsor) - sponsor[:name] || sponsor[:login] - end + sig { override.void } + def run + named_sponsors = [] + logo_sponsors = [] + # FIXME: This T.let should be unnecessary https://github.com/sorbet/sorbet/issues/6894 + largest_monthly_amount = T.let(0, T.untyped) - def self.sponsor_logo(sponsor) - "https://github.com/#{sponsor[:login]}.png?size=64" - end + 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 - def self.sponsor_url(sponsor) - "https://github.com/#{sponsor[:login]}" - end + next if largest_monthly_amount < URL_MONTHLY_AMOUNT - def self.update_sponsors - update_sponsors_args.parse + logo_sponsors << "[![#{sponsor_name(s)}](#{sponsor_logo(s)})](#{sponsor_url(s)})" + end - named_sponsors = [] - logo_sponsors = [] - # FIXME: This T.let should be unnecessary https://github.com/sorbet/sorbet/issues/6894 - largest_monthly_amount = T.let(0, T.untyped) + odie "No sponsorships amounts found! Ensure you have sufficient permissions!" if largest_monthly_amount.zero? - GitHub.sponsorships("Homebrew").each do |s| - largest_monthly_amount = [s[:monthly_amount], s[:closest_tier_monthly_amount]].max - named_sponsors << "[#{sponsor_name(s)}](#{sponsor_url(s)})" if largest_monthly_amount >= NAMED_MONTHLY_AMOUNT + named_sponsors << "many other users and organisations via [GitHub Sponsors](https://github.com/sponsors/Homebrew)" - next if largest_monthly_amount < URL_MONTHLY_AMOUNT + 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 - logo_sponsors << "[![#{sponsor_name(s)}](#{sponsor_logo(s)})](#{sponsor_url(s)})" - end + File.write(readme, content) - odie "No sponsorships amounts found! Ensure you have sufficient permissions!" if largest_monthly_amount.zero? + 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 - named_sponsors << "many other users and organisations via [GitHub Sponsors](https://github.com/sponsors/Homebrew)" + private - 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 + def sponsor_name(sponsor) + sponsor[:name] || sponsor[:login] + end - File.write(readme, content) + def sponsor_logo(sponsor) + "https://github.com/#{sponsor[:login]}.png?size=64" + end - 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." + def sponsor_url(sponsor) + "https://github.com/#{sponsor[:login]}" + end end end end diff --git a/Library/Homebrew/test/dev-cmd/update-sponsors_spec.rb b/Library/Homebrew/test/dev-cmd/update-sponsors_spec.rb index 092805a99f..a9354f7ee8 100644 --- a/Library/Homebrew/test/dev-cmd/update-sponsors_spec.rb +++ b/Library/Homebrew/test/dev-cmd/update-sponsors_spec.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require "cmd/shared_examples/args_parse" +require "dev-cmd/update-sponsors" -RSpec.describe "brew update-sponsors" do +RSpec.describe Homebrew::DevCmd::UpdateSponsors do it_behaves_like "parseable arguments" end