Merge pull request #15020 from dtrodrigues/pluralize-includecount

Add include_count parameter to pluralize util
This commit is contained in:
Mike McQuaid 2023-03-20 13:53:31 +00:00 committed by GitHub
commit bd8341d422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 48 additions and 43 deletions

View File

@ -85,8 +85,7 @@ module Cask
if dry_run if dry_run
if (casks_to_install = casks.reject(&:installed?).presence) if (casks_to_install = casks.reject(&:installed?).presence)
plural = ::Utils.pluralize("cask", casks_to_install.count) ohai "Would install #{::Utils.pluralize("cask", casks_to_install.count, include_count: true)}:"
ohai "Would install #{casks_to_install.count} #{plural}:"
puts casks_to_install.map(&:full_name).join(" ") puts casks_to_install.map(&:full_name).join(" ")
end end
casks.each do |cask| casks.each do |cask|
@ -97,8 +96,8 @@ module Cask
.map(&:name) .map(&:name)
next if dep_names.blank? next if dep_names.blank?
plural = ::Utils.pluralize("dependenc", dep_names.count, plural: "ies", singular: "y") ohai "Would install #{::Utils.pluralize("dependenc", dep_names.count, plural: "ies", singular: "y",
ohai "Would install #{dep_names.count} #{plural} for #{cask.full_name}:" include_count: true)} for #{cask.full_name}:"
puts dep_names.join(" ") puts dep_names.join(" ")
end end
return return

View File

@ -454,7 +454,7 @@ module Cask
artifacts = @cask.artifacts artifacts = @cask.artifacts
odebug "Uninstalling artifacts" odebug "Uninstalling artifacts"
odebug "#{artifacts.length} #{::Utils.pluralize("artifact", artifacts.length)} defined", artifacts odebug "#{::Utils.pluralize("artifact", artifacts.length, include_count: true)} defined", artifacts
artifacts.each do |artifact| artifacts.each do |artifact|
if artifact.respond_to?(:uninstall_phase) if artifact.respond_to?(:uninstall_phase)

View File

@ -124,7 +124,7 @@ module Homebrew
return unless HOMEBREW_CELLAR.exist? return unless HOMEBREW_CELLAR.exist?
count = Formula.racks.length count = Formula.racks.length
puts "#{count} #{Utils.pluralize("keg", count)}, #{HOMEBREW_CELLAR.dup.abv}" puts "#{Utils.pluralize("keg", count, include_count: true)}, #{HOMEBREW_CELLAR.dup.abv}"
end end
sig { params(args: CLI::Args).void } sig { params(args: CLI::Args).void }

View File

@ -57,10 +57,10 @@ module Homebrew
command_count += tap.command_files.size command_count += tap.command_files.size
private_count += 1 if tap.private? private_count += 1 if tap.private?
end end
info = "#{tap_count} #{Utils.pluralize("tap", tap_count)}" info = Utils.pluralize("tap", tap_count, include_count: true)
info += ", #{private_count} private" info += ", #{private_count} private"
info += ", #{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}" info += ", #{Utils.pluralize("formula", formula_count, plural: "e", include_count: true)}"
info += ", #{command_count} #{Utils.pluralize("command", command_count)}" info += ", #{Utils.pluralize("command", command_count, include_count: true)}"
info += ", #{Tap::TAP_DIRECTORY.dup.abv}" if Tap::TAP_DIRECTORY.directory? info += ", #{Tap::TAP_DIRECTORY.dup.abv}" if Tap::TAP_DIRECTORY.directory?
puts info puts info
else else

View File

@ -233,8 +233,7 @@ module Homebrew
unless updated_taps.empty? unless updated_taps.empty?
auto_update_header args: args auto_update_header args: args
noun = Utils.pluralize("tap", updated_taps.count) puts "Updated #{Utils.pluralize("tap", updated_taps.count, include_count: true)} (#{updated_taps.to_sentence})."
puts "Updated #{updated_taps.count} #{noun} (#{updated_taps.to_sentence})."
updated = true updated = true
end end
@ -656,12 +655,12 @@ class ReporterHub
output_dump_formula_or_cask_report "Outdated Casks", outdated_casks output_dump_formula_or_cask_report "Outdated Casks", outdated_casks
elsif report_all elsif report_all
if (changed_formulae = select_formula_or_cask(:M).count) && changed_formulae.positive? if (changed_formulae = select_formula_or_cask(:M).count) && changed_formulae.positive?
noun = Utils.pluralize("formula", changed_formulae, plural: "e") ohai "Modified Formulae",
ohai "Modified Formulae", "Modified #{changed_formulae} #{noun}." "Modified #{Utils.pluralize("formula", changed_formulae, plural: "e", include_count: true)}."
end end
if (changed_casks = select_formula_or_cask(:MC).count) && changed_casks.positive? if (changed_casks = select_formula_or_cask(:MC).count) && changed_casks.positive?
ohai "Modified Casks", "Modified #{changed_casks} #{Utils.pluralize("cask", changed_casks)}." ohai "Modified Casks", "Modified #{Utils.pluralize("cask", changed_casks, include_count: true)}."
end end
else else
outdated_formulae = Formula.installed.select(&:outdated?).map(&:name) outdated_formulae = Formula.installed.select(&:outdated?).map(&:name)

View File

@ -275,22 +275,21 @@ module Homebrew
if total_problems_count.positive? if total_problems_count.positive?
puts new_formula_problem_lines.map { |s| " #{s}" } puts new_formula_problem_lines.map { |s| " #{s}" }
errors_summary = "#{total_problems_count} #{Utils.pluralize("problem", total_problems_count)}" errors_summary = Utils.pluralize("problem", total_problems_count, include_count: true)
error_sources = [] error_sources = []
if formula_count.positive? if formula_count.positive?
error_sources << "#{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}" error_sources << Utils.pluralize("formula", formula_count, plural: "e", include_count: true)
end end
error_sources << "#{cask_count} #{Utils.pluralize("cask", cask_count)}" if cask_count.positive? error_sources << Utils.pluralize("cask", cask_count, include_count: true) if cask_count.positive?
error_sources << "#{tap_count} #{Utils.pluralize("tap", tap_count)}" if tap_count.positive? error_sources << Utils.pluralize("tap", tap_count, include_count: true) if tap_count.positive?
errors_summary += " in #{error_sources.to_sentence}" if error_sources.any? errors_summary += " in #{error_sources.to_sentence}" if error_sources.any?
errors_summary += " detected" errors_summary += " detected"
if corrected_problem_count.positive? if corrected_problem_count.positive?
noun = Utils.pluralize("problem", corrected_problem_count) errors_summary += ", #{Utils.pluralize("problem", corrected_problem_count, include_count: true)} corrected"
errors_summary += ", #{corrected_problem_count} #{noun} corrected"
end end
ofail errors_summary ofail errors_summary

View File

@ -64,8 +64,8 @@ module Homebrew
results[user] = scan_repositories(repos, user, args) results[user] = scan_repositories(repos, user, args)
grand_totals[user] = total(results[user]) grand_totals[user] = total(results[user])
user_contrib = grand_totals[user].values.sum puts "#{user} contributed #{Utils.pluralize("time", grand_totals[user].values.sum,
puts "#{user} contributed #{user_contrib} #{Utils.pluralize("time", user_contrib)} #{time_period(args)}." include_count: true)} #{time_period(args)}."
puts generate_csv(T.must(user), results[user], grand_totals[user]) if args.csv? puts generate_csv(T.must(user), results[user], grand_totals[user]) if args.csv?
return return
end end
@ -80,9 +80,8 @@ module Homebrew
results[username] = scan_repositories(repos, username, args) results[username] = scan_repositories(repos, username, args)
grand_totals[username] = total(results[username]) grand_totals[username] = total(results[username])
username_contrib = grand_totals[username].values.sum puts "#{username} contributed #{Utils.pluralize("time", grand_totals[username].values.sum,
puts "#{username} contributed #{username_contrib} #{Utils.pluralize("time", include_count: true)} #{time_period(args)}."
username_contrib)} #{time_period(args)}."
end end
puts generate_maintainers_csv(grand_totals) if args.csv? puts generate_maintainers_csv(grand_totals) if args.csv?

View File

@ -908,7 +908,7 @@ module Homebrew
0 0
end end
"#{tap.path} (#{cask_count} #{Utils.pluralize("cask", cask_count)})" "#{tap.path} (#{Utils.pluralize("cask", cask_count, include_count: true)})"
end end
end) end)

View File

@ -199,13 +199,13 @@ module Kernel
if seconds > 59 if seconds > 59
minutes = seconds / 60 minutes = seconds / 60
seconds %= 60 seconds %= 60
res = +"#{minutes} #{Utils.pluralize("minute", minutes)}" res = +Utils.pluralize("minute", minutes, include_count: true)
return res.freeze if seconds.zero? return res.freeze if seconds.zero?
res << " " res << " "
end end
res << "#{seconds} #{Utils.pluralize("second", seconds)}" res << Utils.pluralize("second", seconds, include_count: true)
res.freeze res.freeze
end end

View File

@ -326,8 +326,8 @@ module Homebrew
if dry_run if dry_run
if (formulae_name_to_install = formulae_to_install.map(&:name)) if (formulae_name_to_install = formulae_to_install.map(&:name))
plural = Utils.pluralize("formula", formulae_name_to_install.count, plural: "e") ohai "Would install #{Utils.pluralize("formula", formulae_name_to_install.count,
ohai "Would install #{formulae_name_to_install.count} #{plural}:" plural: "e", include_count: true)}:"
puts formulae_name_to_install.join(" ") puts formulae_name_to_install.join(" ")
formula_installers.each do |fi| formula_installers.each do |fi|
@ -355,8 +355,8 @@ module Homebrew
def print_dry_run_dependencies(formula, dependencies, &block) def print_dry_run_dependencies(formula, dependencies, &block)
return if dependencies.empty? return if dependencies.empty?
plural = Utils.pluralize("dependenc", dependencies.count, plural: "ies", singular: "y") ohai "Would install #{Utils.pluralize("dependenc", dependencies.count, plural: "ies", singular: "y",
ohai "Would install #{dependencies.count} #{plural} for #{formula.name}:" include_count: true)} for #{formula.name}:"
formula_names = dependencies.map(&:first).map(&:to_formula).map(&block) formula_names = dependencies.map(&:first).map(&:to_formula).map(&block)
puts formula_names.join(" ") puts formula_names.join(" ")
end end

View File

@ -475,15 +475,15 @@ class Tap
contents = [] contents = []
if (command_count = command_files.count).positive? if (command_count = command_files.count).positive?
contents << "#{command_count} #{Utils.pluralize("command", command_count)}" contents << Utils.pluralize("command", command_count, include_count: true)
end end
if (cask_count = cask_files.count).positive? if (cask_count = cask_files.count).positive?
contents << "#{cask_count} #{Utils.pluralize("cask", cask_count)}" contents << Utils.pluralize("cask", cask_count, include_count: true)
end end
if (formula_count = formula_files.count).positive? if (formula_count = formula_files.count).positive?
contents << "#{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}" contents << Utils.pluralize("formula", formula_count, plural: "e", include_count: true)
end end
contents contents

View File

@ -70,6 +70,12 @@ describe Utils do
expect(described_class.pluralize("foo", 1, singular: "o", plural: "es")).to eq("fooo") expect(described_class.pluralize("foo", 1, singular: "o", plural: "es")).to eq("fooo")
expect(described_class.pluralize("foo", 2, singular: "o", plural: "es")).to eq("fooes") expect(described_class.pluralize("foo", 2, singular: "o", plural: "es")).to eq("fooes")
end end
it "includes the count when requested" do
expect(described_class.pluralize("foo", 0, include_count: true)).to eq("0 foos")
expect(described_class.pluralize("foo", 1, include_count: true)).to eq("1 foo")
expect(described_class.pluralize("foo", 2, include_count: true)).to eq("2 foos")
end
end end
describe ".underscore" do describe ".underscore" do

View File

@ -310,10 +310,10 @@ module Homebrew
if upgradeable_dependents.blank? if upgradeable_dependents.blank?
ohai "No outdated dependents to upgrade!" unless dry_run ohai "No outdated dependents to upgrade!" unless dry_run
else else
dependent_plural = Utils.pluralize("dependent", upgradeable_dependents.count)
formula_plural = Utils.pluralize("formula", installed_formulae.count, plural: "e") formula_plural = Utils.pluralize("formula", installed_formulae.count, plural: "e")
upgrade_verb = dry_run ? "Would upgrade" : "Upgrading" upgrade_verb = dry_run ? "Would upgrade" : "Upgrading"
ohai "#{upgrade_verb} #{upgradeable_dependents.count} #{dependent_plural} of upgraded #{formula_plural}:" ohai "#{upgrade_verb} #{Utils.pluralize("dependent", upgradeable_dependents.count,
include_count: true)} of upgraded #{formula_plural}:"
Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already! Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already!
formulae_upgrades = upgradeable_dependents.map do |f| formulae_upgrades = upgradeable_dependents.map do |f|
name = f.full_specified_name name = f.full_specified_name
@ -386,9 +386,8 @@ module Homebrew
if reinstallable_broken_dependents.blank? if reinstallable_broken_dependents.blank?
ohai "No broken dependents to reinstall!" ohai "No broken dependents to reinstall!"
else else
count = reinstallable_broken_dependents.count ohai "Reinstalling #{Utils.pluralize("dependent", reinstallable_broken_dependents.count,
plural = Utils.pluralize("dependent", reinstallable_broken_dependents.count) include_count: true)} with broken linkage from source:"
ohai "Reinstalling #{count} #{plural} with broken linkage from source:"
Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already! Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already!
puts reinstallable_broken_dependents.map(&:full_specified_name) puts reinstallable_broken_dependents.map(&:full_specified_name)
.join(", ") .join(", ")

View File

@ -126,10 +126,14 @@ module Utils
# A lightweight alternative to `ActiveSupport::Inflector.pluralize`: # A lightweight alternative to `ActiveSupport::Inflector.pluralize`:
# Combines `stem` with the `singular` or `plural` suffix based on `count`. # Combines `stem` with the `singular` or `plural` suffix based on `count`.
sig { params(stem: String, count: Integer, plural: String, singular: String).returns(String) } # Adds a prefix of the count value if `include_count` is set to true.
def self.pluralize(stem, count, plural: "s", singular: "") sig {
params(stem: String, count: Integer, plural: String, singular: String, include_count: T::Boolean).returns(String)
}
def self.pluralize(stem, count, plural: "s", singular: "", include_count: false)
prefix = include_count ? "#{count} " : ""
suffix = (count == 1) ? singular : plural suffix = (count == 1) ? singular : plural
"#{stem}#{suffix}" "#{prefix}#{stem}#{suffix}"
end end
sig { params(author: String).returns({ email: String, name: String }) } sig { params(author: String).returns({ email: String, name: String }) }