Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add include_count parameter to pluralize util #15020

Merged
merged 3 commits into from Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions Library/Homebrew/cask/cmd/install.rb
Expand Up @@ -85,8 +85,7 @@ def self.install_casks(

if dry_run
if (casks_to_install = casks.reject(&:installed?).presence)
plural = ::Utils.pluralize("cask", casks_to_install.count)
ohai "Would install #{casks_to_install.count} #{plural}:"
ohai "Would install #{::Utils.pluralize("cask", casks_to_install.count, include_count: true)}:"
puts casks_to_install.map(&:full_name).join(" ")
end
casks.each do |cask|
Expand All @@ -97,8 +96,8 @@ def self.install_casks(
.map(&:name)
next if dep_names.blank?

plural = ::Utils.pluralize("dependenc", dep_names.count, plural: "ies", singular: "y")
ohai "Would install #{dep_names.count} #{plural} for #{cask.full_name}:"
ohai "Would install #{::Utils.pluralize("dependenc", dep_names.count, plural: "ies", singular: "y",
include_count: true)} for #{cask.full_name}:"
puts dep_names.join(" ")
end
return
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/installer.rb
Expand Up @@ -454,7 +454,7 @@ def uninstall_artifacts(clear: false)
artifacts = @cask.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|
if artifact.respond_to?(:uninstall_phase)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/info.rb
Expand Up @@ -124,7 +124,7 @@ def print_statistics
return unless HOMEBREW_CELLAR.exist?

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

sig { params(args: CLI::Args).void }
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/cmd/tap-info.rb
Expand Up @@ -57,10 +57,10 @@ def print_tap_info(taps)
command_count += tap.command_files.size
private_count += 1 if tap.private?
end
info = "#{tap_count} #{Utils.pluralize("tap", tap_count)}"
info = Utils.pluralize("tap", tap_count, include_count: true)
info += ", #{private_count} private"
info += ", #{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}"
info += ", #{command_count} #{Utils.pluralize("command", command_count)}"
info += ", #{Utils.pluralize("formula", formula_count, plural: "e", include_count: true)}"
info += ", #{Utils.pluralize("command", command_count, include_count: true)}"
info += ", #{Tap::TAP_DIRECTORY.dup.abv}" if Tap::TAP_DIRECTORY.directory?
puts info
else
Expand Down
9 changes: 4 additions & 5 deletions Library/Homebrew/cmd/update-report.rb
Expand Up @@ -233,8 +233,7 @@ def output_update_report

unless updated_taps.empty?
auto_update_header args: args
noun = Utils.pluralize("tap", updated_taps.count)
puts "Updated #{updated_taps.count} #{noun} (#{updated_taps.to_sentence})."
puts "Updated #{Utils.pluralize("tap", updated_taps.count, include_count: true)} (#{updated_taps.to_sentence})."
updated = true
end

Expand Down Expand Up @@ -656,12 +655,12 @@ def dump(updated_formula_report: true)
output_dump_formula_or_cask_report "Outdated Casks", outdated_casks
elsif report_all
if (changed_formulae = select_formula_or_cask(:M).count) && changed_formulae.positive?
noun = Utils.pluralize("formula", changed_formulae, plural: "e")
ohai "Modified Formulae", "Modified #{changed_formulae} #{noun}."
ohai "Modified Formulae",
"Modified #{Utils.pluralize("formula", changed_formulae, plural: "e", include_count: true)}."
end

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
else
outdated_formulae = Formula.installed.select(&:outdated?).map(&:name)
Expand Down
11 changes: 5 additions & 6 deletions Library/Homebrew/dev-cmd/audit.rb
Expand Up @@ -275,22 +275,21 @@ def self.audit
if total_problems_count.positive?
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 = []
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
error_sources << "#{cask_count} #{Utils.pluralize("cask", cask_count)}" if cask_count.positive?
error_sources << "#{tap_count} #{Utils.pluralize("tap", tap_count)}" if tap_count.positive?
error_sources << Utils.pluralize("cask", cask_count, include_count: true) if cask_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 += " detected"

if corrected_problem_count.positive?
noun = Utils.pluralize("problem", corrected_problem_count)
errors_summary += ", #{corrected_problem_count} #{noun} corrected"
errors_summary += ", #{Utils.pluralize("problem", corrected_problem_count, include_count: true)} corrected"
end

ofail errors_summary
Expand Down
9 changes: 4 additions & 5 deletions Library/Homebrew/dev-cmd/contributions.rb
Expand Up @@ -64,8 +64,8 @@ def contributions
results[user] = scan_repositories(repos, user, args)
grand_totals[user] = total(results[user])

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

username_contrib = grand_totals[username].values.sum
puts "#{username} contributed #{username_contrib} #{Utils.pluralize("time",
username_contrib)} #{time_period(args)}."
puts "#{username} contributed #{Utils.pluralize("time", grand_totals[username].values.sum,
include_count: true)} #{time_period(args)}."
end

puts generate_maintainers_csv(grand_totals) if args.csv?
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/diagnostic.rb
Expand Up @@ -908,7 +908,7 @@ def check_cask_taps
0
end

"#{tap.path} (#{cask_count} #{Utils.pluralize("cask", cask_count)})"
"#{tap.path} (#{Utils.pluralize("cask", cask_count, include_count: true)})"
end
end)

Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/extend/kernel.rb
Expand Up @@ -199,13 +199,13 @@ def pretty_duration(seconds)
if seconds > 59
minutes = seconds / 60
seconds %= 60
res = +"#{minutes} #{Utils.pluralize("minute", minutes)}"
res = +Utils.pluralize("minute", minutes, include_count: true)
return res.freeze if seconds.zero?

res << " "
end

res << "#{seconds} #{Utils.pluralize("second", seconds)}"
res << Utils.pluralize("second", seconds, include_count: true)
res.freeze
end

Expand Down
8 changes: 4 additions & 4 deletions Library/Homebrew/install.rb
Expand Up @@ -326,8 +326,8 @@ def install_formulae(

if dry_run
if (formulae_name_to_install = formulae_to_install.map(&:name))
plural = Utils.pluralize("formula", formulae_name_to_install.count, plural: "e")
ohai "Would install #{formulae_name_to_install.count} #{plural}:"
ohai "Would install #{Utils.pluralize("formula", formulae_name_to_install.count,
plural: "e", include_count: true)}:"
puts formulae_name_to_install.join(" ")

formula_installers.each do |fi|
Expand Down Expand Up @@ -355,8 +355,8 @@ def install_formula(formula_installer)
def print_dry_run_dependencies(formula, dependencies, &block)
return if dependencies.empty?

plural = Utils.pluralize("dependenc", dependencies.count, plural: "ies", singular: "y")
ohai "Would install #{dependencies.count} #{plural} for #{formula.name}:"
ohai "Would install #{Utils.pluralize("dependenc", dependencies.count, plural: "ies", singular: "y",
include_count: true)} for #{formula.name}:"
formula_names = dependencies.map(&:first).map(&:to_formula).map(&block)
puts formula_names.join(" ")
end
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/tap.rb
Expand Up @@ -475,15 +475,15 @@ def contents
contents = []

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

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

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

contents
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/test/utils_spec.rb
Expand Up @@ -70,6 +70,12 @@
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")
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

describe ".underscore" do
Expand Down
9 changes: 4 additions & 5 deletions Library/Homebrew/upgrade.rb
Expand Up @@ -310,10 +310,10 @@ def check_installed_dependents(
if upgradeable_dependents.blank?
ohai "No outdated dependents to upgrade!" unless dry_run
else
dependent_plural = Utils.pluralize("dependent", upgradeable_dependents.count)
formula_plural = Utils.pluralize("formula", installed_formulae.count, plural: "e")
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!
formulae_upgrades = upgradeable_dependents.map do |f|
name = f.full_specified_name
Expand Down Expand Up @@ -386,9 +386,8 @@ def check_installed_dependents(
if reinstallable_broken_dependents.blank?
ohai "No broken dependents to reinstall!"
else
count = reinstallable_broken_dependents.count
plural = Utils.pluralize("dependent", reinstallable_broken_dependents.count)
ohai "Reinstalling #{count} #{plural} with broken linkage from source:"
ohai "Reinstalling #{Utils.pluralize("dependent", reinstallable_broken_dependents.count,
include_count: true)} with broken linkage from source:"
Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already!
puts reinstallable_broken_dependents.map(&:full_specified_name)
.join(", ")
Expand Down
10 changes: 7 additions & 3 deletions Library/Homebrew/utils.rb
Expand Up @@ -126,10 +126,14 @@ def self.demodulize(path)

# A lightweight alternative to `ActiveSupport::Inflector.pluralize`:
# Combines `stem` with the `singular` or `plural` suffix based on `count`.
sig { params(stem: String, count: Integer, plural: String, singular: String).returns(String) }
def self.pluralize(stem, count, plural: "s", singular: "")
# Adds a prefix of the count value if `include_count` is set to true.
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
"#{stem}#{suffix}"
"#{prefix}#{stem}#{suffix}"
end

sig { params(author: String).returns({ email: String, name: String }) }
Expand Down