From 0438a3a5385b8822e3e16e94c36dd185a3f0077e Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Wed, 22 Feb 2023 20:59:05 -0800 Subject: [PATCH 01/19] Draft implementation to replace ActiveSupport inflections --- Library/Homebrew/cask/cmd/install.rb | 4 ++-- Library/Homebrew/cask/cmd/uninstall.rb | 2 +- Library/Homebrew/cask/cmd/upgrade.rb | 4 ++-- Library/Homebrew/utils.rb | 1 + Library/Homebrew/utils/inflection.rb | 18 ++++++++++++++++++ 5 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 Library/Homebrew/utils/inflection.rb diff --git a/Library/Homebrew/cask/cmd/install.rb b/Library/Homebrew/cask/cmd/install.rb index 71d39f491ff65..f5dd9d57f024a 100644 --- a/Library/Homebrew/cask/cmd/install.rb +++ b/Library/Homebrew/cask/cmd/install.rb @@ -85,7 +85,7 @@ def self.install_casks( if dry_run if (casks_to_install = casks.reject(&:installed?).presence) - plural = "cask".pluralize(casks_to_install.count) + plural = ::Utils::Inflection.number(casks_to_install.count, "cask") ohai "Would install #{casks_to_install.count} #{plural}:" puts casks_to_install.map(&:full_name).join(" ") end @@ -97,7 +97,7 @@ def self.install_casks( .map(&:name) next if dep_names.blank? - plural = "dependency".pluralize(dep_names.count) + plural = ::Utils::Inflection.number(dep_names.count, "dependenc", "ies", "y") ohai "Would install #{dep_names.count} #{plural} for #{cask.full_name}:" puts dep_names.join(" ") end diff --git a/Library/Homebrew/cask/cmd/uninstall.rb b/Library/Homebrew/cask/cmd/uninstall.rb index 1ab2c8ce2d2aa..36a8c5d75825f 100644 --- a/Library/Homebrew/cask/cmd/uninstall.rb +++ b/Library/Homebrew/cask/cmd/uninstall.rb @@ -46,7 +46,7 @@ def self.uninstall_casks(*casks, binaries: nil, force: false, verbose: false) next if (versions = cask.versions).empty? puts <<~EOS - #{cask} #{versions.to_sentence} #{"is".pluralize(versions.count)} still installed. + #{cask} #{versions.to_sentence} #{::Utils::Inflection.number(versions.count, "", "are", "is")} still installed. Remove #{(versions.count == 1) ? "it" : "them all"} with `brew uninstall --cask --force #{cask}`. EOS end diff --git a/Library/Homebrew/cask/cmd/upgrade.rb b/Library/Homebrew/cask/cmd/upgrade.rb index b3c87a60d87ca..a3095464c9fb4 100644 --- a/Library/Homebrew/cask/cmd/upgrade.rb +++ b/Library/Homebrew/cask/cmd/upgrade.rb @@ -121,7 +121,7 @@ def self.upgrade_casks( if manual_installer_casks.present? count = manual_installer_casks.count - ofail "Not upgrading #{count} `installer manual` #{"cask".pluralize(count)}." + ofail "Not upgrading #{count} `installer manual` #{::Utils::Inflection.number(versions.count, "cask")}." puts manual_installer_casks.map(&:to_s) outdated_casks -= manual_installer_casks end @@ -142,7 +142,7 @@ def self.upgrade_casks( end verb = dry_run ? "Would upgrade" : "Upgrading" - oh1 "#{verb} #{outdated_casks.count} outdated #{"package".pluralize(outdated_casks.count)}:" + oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils::Inflection.number(outdated_casks.count, "package")}:" caught_exceptions = [] diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index d3edd13e4bf2a..70a9ebaeae53f 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -12,6 +12,7 @@ require "utils/git_repository" require "utils/github" require "utils/gzip" +require "utils/inflection" require "utils/inreplace" require "utils/link" require "utils/popen" diff --git a/Library/Homebrew/utils/inflection.rb b/Library/Homebrew/utils/inflection.rb new file mode 100644 index 0000000000000..2e2bca8f2577f --- /dev/null +++ b/Library/Homebrew/utils/inflection.rb @@ -0,0 +1,18 @@ +# typed: strict +# frozen_string_literal: true + +module Utils + # Inflection utility methods, as a lightweight alternative to `ActiveSupport::Inflector``. + # + # @api private + module Inflection + extend T::Sig + + # Combines `stem`` with the `singular`` or `plural` suffix based on `count`. + sig { params(count: Integer, stem: String, plural: String, singular: String).returns(String) } + def self.number(count, stem, plural = "s", singular = "") + suffix = (count == 1) ? singular : plural + "#{stem}#{suffix}" + end + end +end From 37015b6b08636d2c4393976c194cb57be9d72993 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Thu, 23 Feb 2023 12:14:37 -0800 Subject: [PATCH 02/19] Change to pluralize, port more call sites --- Library/Homebrew/cask/cmd/install.rb | 4 ++-- Library/Homebrew/cask/cmd/uninstall.rb | 2 +- Library/Homebrew/cask/cmd/upgrade.rb | 4 ++-- Library/Homebrew/cleanup.rb | 2 +- Library/Homebrew/cli/parser.rb | 6 +++--- Library/Homebrew/diagnostic.rb | 4 ++-- Library/Homebrew/utils/inflection.rb | 6 +++--- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Library/Homebrew/cask/cmd/install.rb b/Library/Homebrew/cask/cmd/install.rb index f5dd9d57f024a..acaa525997cac 100644 --- a/Library/Homebrew/cask/cmd/install.rb +++ b/Library/Homebrew/cask/cmd/install.rb @@ -85,7 +85,7 @@ def self.install_casks( if dry_run if (casks_to_install = casks.reject(&:installed?).presence) - plural = ::Utils::Inflection.number(casks_to_install.count, "cask") + plural = ::Utils::Inflection.pluralize("cask", casks_to_install.count) ohai "Would install #{casks_to_install.count} #{plural}:" puts casks_to_install.map(&:full_name).join(" ") end @@ -97,7 +97,7 @@ def self.install_casks( .map(&:name) next if dep_names.blank? - plural = ::Utils::Inflection.number(dep_names.count, "dependenc", "ies", "y") + plural = ::Utils::Inflection.pluralize("dependenc", dep_names.count, plural: "ies", singular: "y") ohai "Would install #{dep_names.count} #{plural} for #{cask.full_name}:" puts dep_names.join(" ") end diff --git a/Library/Homebrew/cask/cmd/uninstall.rb b/Library/Homebrew/cask/cmd/uninstall.rb index 36a8c5d75825f..e19739b8fddf4 100644 --- a/Library/Homebrew/cask/cmd/uninstall.rb +++ b/Library/Homebrew/cask/cmd/uninstall.rb @@ -46,7 +46,7 @@ def self.uninstall_casks(*casks, binaries: nil, force: false, verbose: false) next if (versions = cask.versions).empty? puts <<~EOS - #{cask} #{versions.to_sentence} #{::Utils::Inflection.number(versions.count, "", "are", "is")} still installed. + #{cask} #{versions.to_sentence} #{::Utils::Inflection.pluralize("", versions.count, plural: "are", singular: "is")} still installed. Remove #{(versions.count == 1) ? "it" : "them all"} with `brew uninstall --cask --force #{cask}`. EOS end diff --git a/Library/Homebrew/cask/cmd/upgrade.rb b/Library/Homebrew/cask/cmd/upgrade.rb index a3095464c9fb4..e805a6914bc87 100644 --- a/Library/Homebrew/cask/cmd/upgrade.rb +++ b/Library/Homebrew/cask/cmd/upgrade.rb @@ -121,7 +121,7 @@ def self.upgrade_casks( if manual_installer_casks.present? count = manual_installer_casks.count - ofail "Not upgrading #{count} `installer manual` #{::Utils::Inflection.number(versions.count, "cask")}." + ofail "Not upgrading #{count} `installer manual` #{::Utils::Inflection.pluralize("cask", versions.count)}." puts manual_installer_casks.map(&:to_s) outdated_casks -= manual_installer_casks end @@ -142,7 +142,7 @@ def self.upgrade_casks( end verb = dry_run ? "Would upgrade" : "Upgrading" - oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils::Inflection.number(outdated_casks.count, "package")}:" + oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils::Inflection.pluralize("package", outdated_casks.count)}:" caught_exceptions = [] diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 39b1484f38f4b..377a63c03539a 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -584,7 +584,7 @@ def self.autoremove(dry_run: false) formulae_names = removable_formulae.map(&:full_name).sort verb = dry_run ? "Would autoremove" : "Autoremoving" - oh1 "#{verb} #{formulae_names.count} unneeded #{"formula".pluralize(formulae_names.count)}:" + oh1 "#{verb} #{formulae_names.count} unneeded #{Utils::Inflection.pluralize("formula", formulae_names.count, plural: "e")}:" puts formulae_names.join("\n") return if dry_run diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 81ca29b28c610..86db8e223aa09 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -684,7 +684,7 @@ def initialize(maximum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - "This command does not take more than #{maximum} #{arg_types} #{"argument".pluralize(maximum)}." + "This command does not take more than #{maximum} #{arg_types} #{Utils::Inflection.pluralize("argument", maximum)}." end end end @@ -698,7 +698,7 @@ def initialize(minimum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires at least #{minimum} #{arg_types} #{"argument".pluralize(minimum)}." + super "This command requires at least #{minimum} #{arg_types} #{Utils::Inflection.pluralize("argument", minimum)}." end end @@ -711,7 +711,7 @@ def initialize(minimum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires exactly #{minimum} #{arg_types} #{"argument".pluralize(minimum)}." + super "This command requires exactly #{minimum} #{arg_types} #{Utils::Inflection.pluralize("argument", minimum)}." end end end diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 6b57247b20acb..4ce24c7376c91 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -906,11 +906,11 @@ def check_cask_taps 0 end - "#{tap.path} (#{cask_count} #{"cask".pluralize(cask_count)})" + "#{tap.path} (#{cask_count} #{Utils::Inflection.pluralize("cask", cask_count)})" end end) - taps = "tap".pluralize error_tap_paths.count + taps = Utils::Inflection.pluralize("tap", error_tap_paths.count) "Unable to read from cask #{taps}: #{error_tap_paths.to_sentence}" if error_tap_paths.present? end diff --git a/Library/Homebrew/utils/inflection.rb b/Library/Homebrew/utils/inflection.rb index 2e2bca8f2577f..a5cfa16a4664e 100644 --- a/Library/Homebrew/utils/inflection.rb +++ b/Library/Homebrew/utils/inflection.rb @@ -8,9 +8,9 @@ module Utils module Inflection extend T::Sig - # Combines `stem`` with the `singular`` or `plural` suffix based on `count`. - sig { params(count: Integer, stem: String, plural: String, singular: String).returns(String) } - def self.number(count, stem, plural = "s", singular = "") + # 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: "") suffix = (count == 1) ? singular : plural "#{stem}#{suffix}" end From d62211d3afa2e5880d01373b217b8d23f9ef2b34 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Thu, 23 Feb 2023 12:22:34 -0800 Subject: [PATCH 03/19] Replace demodulize inflection with util --- Library/Homebrew/formulary.rb | 2 +- Library/Homebrew/livecheck/livecheck.rb | 2 +- .../livecheck/strategy/electron_builder.rb | 2 +- .../livecheck/strategy/extract_plist.rb | 4 ++-- .../Homebrew/livecheck/strategy/page_match.rb | 2 +- Library/Homebrew/livecheck/strategy/sparkle.rb | 2 +- Library/Homebrew/utils/inflection.rb | 18 ++++++++++++++++++ 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 6d1b16708e892..4ac857835b5ec 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -55,7 +55,7 @@ def self.clear_cache namespace = klass.name.deconstantize next if namespace.deconstantize != name - remove_const(namespace.demodulize) + remove_const(Utils::Inflection.demodulize(namespace)) end end diff --git a/Library/Homebrew/livecheck/livecheck.rb b/Library/Homebrew/livecheck/livecheck.rb index 9e2920a77c23e..cef2357f69bad 100644 --- a/Library/Homebrew/livecheck/livecheck.rb +++ b/Library/Homebrew/livecheck/livecheck.rb @@ -61,7 +61,7 @@ def livecheck_strategy_names constant = Strategy.const_get(const_symbol) next unless constant.is_a?(Class) - @livecheck_strategy_names[constant] = T.must(constant.name).demodulize + @livecheck_strategy_names[constant] = Utils::Inflection.demodulize(T.must(constant.name)) end @livecheck_strategy_names.freeze end diff --git a/Library/Homebrew/livecheck/strategy/electron_builder.rb b/Library/Homebrew/livecheck/strategy/electron_builder.rb index 8bb862e1d4259..29ea5d1c2d570 100644 --- a/Library/Homebrew/livecheck/strategy/electron_builder.rb +++ b/Library/Homebrew/livecheck/strategy/electron_builder.rb @@ -73,7 +73,7 @@ def self.versions_from_content(content, regex = nil, &block) } def self.find_versions(url:, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{T.must(name).demodulize} only supports a regex when using a `strategy` block" + raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/extract_plist.rb b/Library/Homebrew/livecheck/strategy/extract_plist.rb index 2486a545bc82a..cde31a43c88c6 100644 --- a/Library/Homebrew/livecheck/strategy/extract_plist.rb +++ b/Library/Homebrew/livecheck/strategy/extract_plist.rb @@ -97,9 +97,9 @@ def self.versions_from_items(items, regex = nil, &block) } def self.find_versions(cask:, url: nil, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{T.must(name).demodulize} only supports a regex when using a `strategy` block" + raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end - raise ArgumentError, "The #{T.must(name).demodulize} strategy only supports casks." unless T.unsafe(cask) + raise ArgumentError, "The #{Utils::Inflection.demodulize(T.must(name))} strategy only supports casks." unless T.unsafe(cask) match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/page_match.rb b/Library/Homebrew/livecheck/strategy/page_match.rb index 9b16c8b8c081d..fc86e00654643 100644 --- a/Library/Homebrew/livecheck/strategy/page_match.rb +++ b/Library/Homebrew/livecheck/strategy/page_match.rb @@ -93,7 +93,7 @@ def self.versions_from_content(content, regex, &block) } def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **_unused, &block) if regex.blank? && block.blank? - raise ArgumentError, "#{T.must(name).demodulize} requires a regex or `strategy` block" + raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} requires a regex or `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/sparkle.rb b/Library/Homebrew/livecheck/strategy/sparkle.rb index 7cf8cd9acf456..bba267b1e07ea 100644 --- a/Library/Homebrew/livecheck/strategy/sparkle.rb +++ b/Library/Homebrew/livecheck/strategy/sparkle.rb @@ -203,7 +203,7 @@ def self.versions_from_content(content, regex = nil, &block) } def self.find_versions(url:, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{T.must(name).demodulize} only supports a regex when using a `strategy` block" + raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/utils/inflection.rb b/Library/Homebrew/utils/inflection.rb index a5cfa16a4664e..aaeb2ba665549 100644 --- a/Library/Homebrew/utils/inflection.rb +++ b/Library/Homebrew/utils/inflection.rb @@ -7,6 +7,24 @@ module Utils # @api private module Inflection extend T::Sig + # Removes the module part from the expression in the string. + # + # demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections" + # demodulize('Inflections') # => "Inflections" + # demodulize('::Inflections') # => "Inflections" + # demodulize('') # => "" + # + # See also #deconstantize. + # @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L230-L245 + # `ActiveSupport::Inflector.demodulize` + sig { params(path: String).returns(String) } + def self.demodulize(path) + if (i = path.rindex("::")) + T.must(path[(i + 2)..]) + else + path + end + end # Combines `stem` with the `singular` or `plural` suffix based on `count`. sig { params(stem: String, count: Integer, plural: String, singular: String).returns(String) } From 2400c6daed704b97725e10ad1bde7f85ceea9240 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Thu, 23 Feb 2023 12:23:44 -0800 Subject: [PATCH 04/19] brew style --fix --- Library/Homebrew/cask/cmd/upgrade.rb | 3 ++- Library/Homebrew/cleanup.rb | 3 ++- Library/Homebrew/cli/parser.rb | 9 ++++++--- Library/Homebrew/livecheck/strategy/electron_builder.rb | 3 ++- Library/Homebrew/livecheck/strategy/extract_plist.rb | 8 ++++++-- Library/Homebrew/livecheck/strategy/sparkle.rb | 3 ++- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/cask/cmd/upgrade.rb b/Library/Homebrew/cask/cmd/upgrade.rb index e805a6914bc87..1dc312d08beab 100644 --- a/Library/Homebrew/cask/cmd/upgrade.rb +++ b/Library/Homebrew/cask/cmd/upgrade.rb @@ -142,7 +142,8 @@ def self.upgrade_casks( end verb = dry_run ? "Would upgrade" : "Upgrading" - oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils::Inflection.pluralize("package", outdated_casks.count)}:" + oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils::Inflection.pluralize("package", + outdated_casks.count)}:" caught_exceptions = [] diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 377a63c03539a..57e5d3ef2741d 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -584,7 +584,8 @@ def self.autoremove(dry_run: false) formulae_names = removable_formulae.map(&:full_name).sort verb = dry_run ? "Would autoremove" : "Autoremoving" - oh1 "#{verb} #{formulae_names.count} unneeded #{Utils::Inflection.pluralize("formula", formulae_names.count, plural: "e")}:" + oh1 "#{verb} #{formulae_names.count} unneeded #{Utils::Inflection.pluralize("formula", formulae_names.count, + plural: "e")}:" puts formulae_names.join("\n") return if dry_run diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 86db8e223aa09..f3ec536c0ccd1 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -684,7 +684,8 @@ def initialize(maximum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - "This command does not take more than #{maximum} #{arg_types} #{Utils::Inflection.pluralize("argument", maximum)}." + "This command does not take more than #{maximum} #{arg_types} #{Utils::Inflection.pluralize("argument", + maximum)}." end end end @@ -698,7 +699,8 @@ def initialize(minimum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires at least #{minimum} #{arg_types} #{Utils::Inflection.pluralize("argument", minimum)}." + super "This command requires at least #{minimum} #{arg_types} #{Utils::Inflection.pluralize("argument", + minimum)}." end end @@ -711,7 +713,8 @@ def initialize(minimum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires exactly #{minimum} #{arg_types} #{Utils::Inflection.pluralize("argument", minimum)}." + super "This command requires exactly #{minimum} #{arg_types} #{Utils::Inflection.pluralize("argument", + minimum)}." end end end diff --git a/Library/Homebrew/livecheck/strategy/electron_builder.rb b/Library/Homebrew/livecheck/strategy/electron_builder.rb index 29ea5d1c2d570..542c00742d3e3 100644 --- a/Library/Homebrew/livecheck/strategy/electron_builder.rb +++ b/Library/Homebrew/livecheck/strategy/electron_builder.rb @@ -73,7 +73,8 @@ def self.versions_from_content(content, regex = nil, &block) } def self.find_versions(url:, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" + raise ArgumentError, + "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/extract_plist.rb b/Library/Homebrew/livecheck/strategy/extract_plist.rb index cde31a43c88c6..db7565dca6395 100644 --- a/Library/Homebrew/livecheck/strategy/extract_plist.rb +++ b/Library/Homebrew/livecheck/strategy/extract_plist.rb @@ -97,9 +97,13 @@ def self.versions_from_items(items, regex = nil, &block) } def self.find_versions(cask:, url: nil, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" + raise ArgumentError, + "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" + end + unless T.unsafe(cask) + raise ArgumentError, + "The #{Utils::Inflection.demodulize(T.must(name))} strategy only supports casks." end - raise ArgumentError, "The #{Utils::Inflection.demodulize(T.must(name))} strategy only supports casks." unless T.unsafe(cask) match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/sparkle.rb b/Library/Homebrew/livecheck/strategy/sparkle.rb index bba267b1e07ea..36685c15d493d 100644 --- a/Library/Homebrew/livecheck/strategy/sparkle.rb +++ b/Library/Homebrew/livecheck/strategy/sparkle.rb @@ -203,7 +203,8 @@ def self.versions_from_content(content, regex = nil, &block) } def self.find_versions(url:, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" + raise ArgumentError, + "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex: regex, url: url } From 2c73d4d9b7035bbccf58d672d24cd8681065bbf9 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Thu, 23 Feb 2023 22:20:35 -0800 Subject: [PATCH 05/19] Replace deconstantize inflection with util --- Library/Homebrew/formulary.rb | 4 ++-- Library/Homebrew/utils/inflection.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 4ac857835b5ec..b39ad638e2dfa 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -52,8 +52,8 @@ def self.clear_cache next if type == :formulary_factory cached_objects.each_value do |klass| - namespace = klass.name.deconstantize - next if namespace.deconstantize != name + namespace = Utils::Inflection.deconstantize(klass.name) + next if Utils::Inflection.deconstantize(namespace) != name remove_const(Utils::Inflection.demodulize(namespace)) end diff --git a/Library/Homebrew/utils/inflection.rb b/Library/Homebrew/utils/inflection.rb index aaeb2ba665549..5aebaac58f881 100644 --- a/Library/Homebrew/utils/inflection.rb +++ b/Library/Homebrew/utils/inflection.rb @@ -7,6 +7,22 @@ module Utils # @api private module Inflection extend T::Sig + # Removes the rightmost segment from the constant expression in the string. + # + # deconstantize('Net::HTTP') # => "Net" + # deconstantize('::Net::HTTP') # => "::Net" + # deconstantize('String') # => "" + # deconstantize('::String') # => "" + # deconstantize('') # => "" + # + # See also #demodulize. + # @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L247-L258 + # `ActiveSupport::Inflector.deconstantize` + sig { params(path: String).returns(String) } + def self.deconstantize(path) + T.must(path[0, path.rindex("::") || 0]) # implementation based on the one in facets' Module#spacename + end + # Removes the module part from the expression in the string. # # demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections" From eb2b990575daac744d5fb1948a4e2b7270fefad9 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 24 Feb 2023 08:42:45 -0800 Subject: [PATCH 06/19] Port more call sites --- Library/Homebrew/cask/installer.rb | 4 ++-- Library/Homebrew/cmd/developer.rb | 3 ++- Library/Homebrew/cmd/fetch.rb | 2 +- Library/Homebrew/cmd/info.rb | 2 +- Library/Homebrew/cmd/tap-info.rb | 6 +++--- Library/Homebrew/cmd/update-report.rb | 14 +++++++++----- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/cask/installer.rb b/Library/Homebrew/cask/installer.rb index ceb2c528e5e7a..64fa081e59585 100644 --- a/Library/Homebrew/cask/installer.rb +++ b/Library/Homebrew/cask/installer.rb @@ -231,7 +231,7 @@ def install_artifacts already_installed_artifacts = [] odebug "Installing artifacts" - odebug "#{artifacts.length} #{"artifact".pluralize(artifacts.length)} defined", artifacts + odebug "#{artifacts.length} #{::Utils::Inflection.pluralize("artifact", artifacts.length)} defined", artifacts artifacts.each do |artifact| next unless artifact.respond_to?(:install_phase) @@ -460,7 +460,7 @@ def uninstall_artifacts(clear: false) artifacts = @cask.artifacts odebug "Uninstalling artifacts" - odebug "#{artifacts.length} #{"artifact".pluralize(artifacts.length)} defined", artifacts + odebug "#{artifacts.length} #{::Utils::Inflection.pluralize("artifact", artifacts.length)} defined", artifacts artifacts.each do |artifact| if artifact.respond_to?(:uninstall_phase) diff --git a/Library/Homebrew/cmd/developer.rb b/Library/Homebrew/cmd/developer.rb index e967844b7a201..a142a77f7d99c 100755 --- a/Library/Homebrew/cmd/developer.rb +++ b/Library/Homebrew/cmd/developer.rb @@ -40,7 +40,8 @@ def developer case args.named.first when nil, "state" if env_vars.any? - puts "Developer mode is enabled because #{env_vars.to_sentence} #{"is".pluralize(env_vars.count)} set." + puts "Developer mode is enabled because #{env_vars.to_sentence} #{Utils::Inflection.pluralize("", + env_vars.count, plural: "are", singular: "is")} set." elsif Homebrew::Settings.read("devcmdrun") == "true" puts "Developer mode is enabled." else diff --git a/Library/Homebrew/cmd/fetch.rb b/Library/Homebrew/cmd/fetch.rb index 5bbe534ee7b2c..80af3a5200813 100644 --- a/Library/Homebrew/cmd/fetch.rb +++ b/Library/Homebrew/cmd/fetch.rb @@ -166,7 +166,7 @@ def retry_fetch?(f, args:) if args.retry? && (@fetch_tries[f] < FETCH_MAX_TRIES) wait = 2 ** @fetch_tries[f] remaining = FETCH_MAX_TRIES - @fetch_tries[f] - what = "try".pluralize(remaining) + what = Utils::Inflection.pluralize("tr", remaining, plural: "ies", singular: "y") ohai "Retrying download in #{wait}s... (#{remaining} #{what} left)" sleep wait diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index 22326122d3ca3..68982834d8ec4 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -124,7 +124,7 @@ def print_statistics return unless HOMEBREW_CELLAR.exist? count = Formula.racks.length - puts "#{count} #{"keg".pluralize(count)}, #{HOMEBREW_CELLAR.dup.abv}" + puts "#{count} #{Utils::Inflection.pluralize("keg", count)}, #{HOMEBREW_CELLAR.dup.abv}" end sig { params(args: CLI::Args).void } diff --git a/Library/Homebrew/cmd/tap-info.rb b/Library/Homebrew/cmd/tap-info.rb index f72c611abd8de..270892c7ecf26 100644 --- a/Library/Homebrew/cmd/tap-info.rb +++ b/Library/Homebrew/cmd/tap-info.rb @@ -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} #{"tap".pluralize(tap_count)}" + info = "#{tap_count} #{Utils::Inflection.pluralize("tap", tap_count)}" info += ", #{private_count} private" - info += ", #{formula_count} #{"formula".pluralize(formula_count)}" - info += ", #{command_count} #{"command".pluralize(command_count)}" + info += ", #{formula_count} #{Utils::Inflection.pluralize("formula", formula_count, plural: "e")}" + info += ", #{command_count} #{Utils::Inflection.pluralize("command", command_count)}" info += ", #{Tap::TAP_DIRECTORY.dup.abv}" if Tap::TAP_DIRECTORY.directory? puts info else diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index 69b2efeb5db5a..d4a8f4cbecc48 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -199,7 +199,8 @@ def output_update_report unless updated_taps.empty? auto_update_header args: args - puts "Updated #{updated_taps.count} #{"tap".pluralize(updated_taps.count)} (#{updated_taps.to_sentence})." + puts "Updated #{updated_taps.count} #{Utils::Inflection.pluralize("tap", + updated_taps.count)} (#{updated_taps.to_sentence})." updated = true end @@ -584,11 +585,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? - ohai "Modified Formulae", "Modified #{changed_formulae} #{"formula".pluralize(changed_formulae)}." + ohai "Modified Formulae", + "Modified #{changed_formulae} #{Utils::Inflection.pluralize("formula", changed_formulae, plural: "e")}." end if (changed_casks = select_formula_or_cask(:MC).count) && changed_casks.positive? - ohai "Modified Casks", "Modified #{changed_casks} #{"cask".pluralize(changed_casks)}." + ohai "Modified Casks", "Modified #{changed_casks} #{Utils::Inflection.pluralize("cask", changed_casks)}." end else outdated_formulae = Formula.installed.select(&:outdated?).map(&:name) @@ -609,12 +611,14 @@ def dump(updated_formula_report: true) msg = "" if outdated_formulae.positive? - msg += "#{Tty.bold}#{outdated_formulae}#{Tty.reset} outdated #{"formula".pluralize(outdated_formulae)}" + msg += "#{Tty.bold}#{outdated_formulae}#{Tty.reset} outdated #{Utils::Inflection.pluralize("formula", + outdated_formulae, plural: "e")}" end if outdated_casks.positive? msg += " and " if msg.present? - msg += "#{Tty.bold}#{outdated_casks}#{Tty.reset} outdated #{"cask".pluralize(outdated_casks)}" + msg += "#{Tty.bold}#{outdated_casks}#{Tty.reset} outdated #{Utils::Inflection.pluralize("cask", + outdated_casks)}" end return if msg.blank? From 3da68651e5ebef04c57237e5f831667df7d37d72 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 24 Feb 2023 08:53:04 -0800 Subject: [PATCH 07/19] Port more call sites --- Library/Homebrew/cmd/upgrade.rb | 5 +++-- Library/Homebrew/dev-cmd/audit.rb | 14 +++++++++----- Library/Homebrew/dev-cmd/pr-automerge.rb | 2 +- Library/Homebrew/exceptions.rb | 2 +- Library/Homebrew/install.rb | 4 ++-- Library/Homebrew/tap.rb | 6 +++--- Library/Homebrew/uninstall.rb | 9 +++++---- Library/Homebrew/upgrade.rb | 10 +++++----- 8 files changed, 29 insertions(+), 23 deletions(-) diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index f7012e316543f..5463636deb0cb 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -172,7 +172,7 @@ def upgrade_outdated_formulae(formulae, args:) end if !pinned.empty? && !args.ignore_pinned? - ofail "Not upgrading #{pinned.count} pinned #{"package".pluralize(pinned.count)}:" + ofail "Not upgrading #{pinned.count} pinned #{Utils::Inflection.pluralize("package", pinned.count)}:" puts pinned.map { |f| "#{f.full_specified_name} #{f.pkg_version}" } * ", " end @@ -180,7 +180,8 @@ def upgrade_outdated_formulae(formulae, args:) oh1 "No packages to upgrade" else verb = args.dry_run? ? "Would upgrade" : "Upgrading" - oh1 "#{verb} #{formulae_to_install.count} outdated #{"package".pluralize(formulae_to_install.count)}:" + oh1 "#{verb} #{formulae_to_install.count} outdated #{Utils::Inflection.pluralize("package", + formulae_to_install.count)}:" formulae_upgrades = formulae_to_install.map do |f| if f.optlinked? "#{f.full_specified_name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}" diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index ff0ebb3de8903..8feb3a6ad9c0e 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -277,19 +277,23 @@ def audit if total_problems_count.positive? puts new_formula_problem_lines.map { |s| " #{s}" } - errors_summary = "#{total_problems_count} #{"problem".pluralize(total_problems_count)}" + errors_summary = "#{total_problems_count} #{Utils::Inflection.pluralize("problem", total_problems_count)}" error_sources = [] - error_sources << "#{formula_count} #{"formula".pluralize(formula_count)}" if formula_count.positive? - error_sources << "#{cask_count} #{"cask".pluralize(cask_count)}" if cask_count.positive? - error_sources << "#{tap_count} #{"tap".pluralize(tap_count)}" if tap_count.positive? + if formula_count.positive? + error_sources << "#{formula_count} #{Utils::Inflection.pluralize("formula", formula_count, + plural: "e")}" + end + error_sources << "#{cask_count} #{Utils::Inflection.pluralize("cask", cask_count)}" if cask_count.positive? + error_sources << "#{tap_count} #{Utils::Inflection.pluralize("tap", tap_count)}" if tap_count.positive? errors_summary += " in #{error_sources.to_sentence}" if error_sources.any? errors_summary += " detected" if corrected_problem_count.positive? - errors_summary += ", #{corrected_problem_count} #{"problem".pluralize(corrected_problem_count)} corrected" + errors_summary += ", #{corrected_problem_count} #{Utils::Inflection.pluralize("problem", + corrected_problem_count)} corrected" end ofail errors_summary diff --git a/Library/Homebrew/dev-cmd/pr-automerge.rb b/Library/Homebrew/dev-cmd/pr-automerge.rb index 4014b9a656dd2..cec12b550d27c 100644 --- a/Library/Homebrew/dev-cmd/pr-automerge.rb +++ b/Library/Homebrew/dev-cmd/pr-automerge.rb @@ -61,7 +61,7 @@ def pr_automerge return end - ohai "#{prs.count} matching pull #{"request".pluralize(prs.count)}:" + ohai "#{prs.count} matching pull #{Utils::Inflection.pluralize("request", prs.count)}:" pr_urls = [] prs.each do |pr| puts "#{tap.full_name unless tap.core_tap?}##{pr["number"]}: #{pr["title"]}" diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index cda88f88c92ad..0237024ca2489 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -568,7 +568,7 @@ def dump(verbose: false) class UnbottledError < RuntimeError def initialize(formulae) msg = +<<~EOS - The following #{"formula".pluralize(formulae.count)} cannot be installed from #{"bottle".pluralize(formulae.count)} and must be + The following #{Utils::Inflection.pluralize("formula", formulae.count, plural: "e")} cannot be installed from #{Utils::Inflection.pluralize("bottle", formulae.count)} and must be built from source. #{formulae.to_sentence} EOS diff --git a/Library/Homebrew/install.rb b/Library/Homebrew/install.rb index 86e0f7c5b627d..8d989fbf30197 100644 --- a/Library/Homebrew/install.rb +++ b/Library/Homebrew/install.rb @@ -324,7 +324,7 @@ def install_formulae( if dry_run if (formulae_name_to_install = formulae_to_install.map(&:name)) - plural = "formula".pluralize(formulae_name_to_install.count) + plural = Utils::Inflection.pluralize("formula", formulae_name_to_install.count, plural: "e") ohai "Would install #{formulae_name_to_install.count} #{plural}:" puts formulae_name_to_install.join(" ") @@ -354,7 +354,7 @@ def install_formula(formula_installer) def print_dry_run_dependencies(formula, dependencies, &block) return if dependencies.empty? - plural = "dependency".pluralize(dependencies.count) + plural = Utils::Inflection.pluralize("dependenc", dependencies.count, plural: "ies", singular: "y") ohai "Would install #{dependencies.count} #{plural} for #{formula.name}:" formula_names = dependencies.map(&:first).map(&:to_formula).map(&block) puts formula_names.join(" ") diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index f165ffcc4aed0..7abd5ed5358a1 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -475,15 +475,15 @@ def contents contents = [] if (command_count = command_files.count).positive? - contents << "#{command_count} #{"command".pluralize(command_count)}" + contents << "#{command_count} #{Utils::Inflection.pluralize("command", command_count)}" end if (cask_count = cask_files.count).positive? - contents << "#{cask_count} #{"cask".pluralize(cask_count)}" + contents << "#{cask_count} #{Utils::Inflection.pluralize("cask", cask_count)}" end if (formula_count = formula_files.count).positive? - contents << "#{formula_count} #{"formula".pluralize(formula_count)}" + contents << "#{formula_count} #{Utils::Inflection.pluralize("formula", formula_count, plural: "e")}" end contents diff --git a/Library/Homebrew/uninstall.rb b/Library/Homebrew/uninstall.rb index 1d9a1585e4d17..42136bc14b9b9 100644 --- a/Library/Homebrew/uninstall.rb +++ b/Library/Homebrew/uninstall.rb @@ -52,7 +52,7 @@ def uninstall_kegs(kegs_by_rack, casks: [], force: false, ignore_dependencies: f if rack.directory? versions = rack.subdirs.map(&:basename) puts <<~EOS - #{keg.name} #{versions.to_sentence} #{"is".pluralize(versions.count)} still installed. + #{keg.name} #{versions.to_sentence} #{Utils::Inflection.pluralize("", versions.count, plural: "are", singular: "is")} still installed. To remove all versions, run: brew uninstall --force #{keg.name} EOS @@ -136,8 +136,9 @@ def sample_command end def are_required_by_deps - "#{"is".pluralize(reqs.count)} required by #{deps.to_sentence}, " \ - "which #{"is".pluralize(deps.count)} currently installed" + "#{Utils::Inflection.pluralize("", reqs.count, plural: "are", + singular: "is")} required by #{deps.to_sentence}, " \ + "which #{Utils::Inflection.pluralize("", deps.count, plural: "are", singular: "is")} currently installed" end end @@ -157,7 +158,7 @@ class NondeveloperDependentsMessage < DependentsMessage def output ofail <<~EOS Refusing to uninstall #{reqs.to_sentence} - because #{"it".pluralize(reqs.count)} #{are_required_by_deps}. + because #{Utils::Inflection.pluralize("", reqs.count, plural: "they", singular: "it")} #{are_required_by_deps}. You can override this and force removal with: #{sample_command} EOS diff --git a/Library/Homebrew/upgrade.rb b/Library/Homebrew/upgrade.rb index 6acb840881a19..1e65181932806 100644 --- a/Library/Homebrew/upgrade.rb +++ b/Library/Homebrew/upgrade.rb @@ -299,7 +299,7 @@ def check_installed_dependents( .sort { |a, b| depends_on(a, b) } if pinned_dependents.present? - plural = "dependent".pluralize(pinned_dependents.count) + plural = Utils::Inflection.pluralize("dependent", pinned_dependents.count) ohai "Not upgrading #{pinned_dependents.count} pinned #{plural}:" puts(pinned_dependents.map do |f| "#{f.full_specified_name} #{f.pkg_version}" @@ -310,8 +310,8 @@ def check_installed_dependents( if upgradeable_dependents.blank? ohai "No outdated dependents to upgrade!" unless dry_run else - dependent_plural = "dependent".pluralize(upgradeable_dependents.count) - formula_plural = "formula".pluralize(installed_formulae.count) + dependent_plural = Utils::Inflection.pluralize("dependent", upgradeable_dependents.count) + formula_plural = Utils::Inflection.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}:" Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already! @@ -375,7 +375,7 @@ def check_installed_dependents( # Print the pinned dependents. if outdated_pinned_broken_dependents.present? count = outdated_pinned_broken_dependents.count - plural = "dependent".pluralize(outdated_pinned_broken_dependents.count) + plural = Utils::Inflection.pluralize("dependent", outdated_pinned_broken_dependents.count) onoe "Not reinstalling #{count} broken and outdated, but pinned #{plural}:" $stderr.puts(outdated_pinned_broken_dependents.map do |f| "#{f.full_specified_name} #{f.pkg_version}" @@ -387,7 +387,7 @@ def check_installed_dependents( ohai "No broken dependents to reinstall!" else count = reinstallable_broken_dependents.count - plural = "dependent".pluralize(reinstallable_broken_dependents.count) + plural = Utils::Inflection.pluralize("dependent", reinstallable_broken_dependents.count) ohai "Reinstalling #{count} #{plural} with broken linkage from source:" Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already! puts reinstallable_broken_dependents.map(&:full_specified_name) From 2c5067ec7743a029508656636db23e0d51748f2d Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 24 Feb 2023 08:56:41 -0800 Subject: [PATCH 08/19] Port dynamic invocation --- Library/Homebrew/utils/repology.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/utils/repology.rb b/Library/Homebrew/utils/repology.rb index 0782cd1d49ad3..14396de368cbe 100644 --- a/Library/Homebrew/utils/repology.rb +++ b/Library/Homebrew/utils/repology.rb @@ -53,14 +53,14 @@ def single_package_query(name, repository:) def parse_api_response(limit = nil, last_package = "", repository:) package_term = case repository when HOMEBREW_CORE - "formula" + "formulae" when HOMEBREW_CASK - "cask" + "casks" else - "package" + "packages" end - ohai "Querying outdated #{package_term.pluralize} from Repology" + ohai "Querying outdated #{package_term} from Repology" page_no = 1 outdated_packages = {} @@ -76,7 +76,8 @@ def parse_api_response(limit = nil, last_package = "", repository:) break if (limit && outdated_packages.size >= limit) || response.size <= 1 end - puts "#{outdated_packages.size} outdated #{package_term.pluralize(outdated_packages.size)} found" + package_term = package_term.chop if outdated_packages.size == 1 + puts "#{outdated_packages.size} outdated #{package_term} found" puts outdated_packages.sort.to_h From 7f3593e12dbe7b801b2b1665edd66aa476f0a597 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 24 Feb 2023 09:09:30 -0800 Subject: [PATCH 09/19] inline titleize --- Library/Homebrew/utils/spdx.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/spdx.rb b/Library/Homebrew/utils/spdx.rb index 873cc8310e86d..8d793e6b01ff6 100644 --- a/Library/Homebrew/utils/spdx.rb +++ b/Library/Homebrew/utils/spdx.rb @@ -95,7 +95,7 @@ def license_expression_to_string(license_expression, bracket: false, hash_type: when String license_expression when Symbol - license_expression.to_s.tr("_", " ").titleize + license_expression.to_s.tr("_", " ").gsub(/\b(? Date: Fri, 24 Feb 2023 09:29:37 -0800 Subject: [PATCH 10/19] Fix type error --- Library/Homebrew/livecheck/strategy/git.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/livecheck/strategy/git.rb b/Library/Homebrew/livecheck/strategy/git.rb index 8b0d6c2d5ab14..1843b3ab9fa8d 100644 --- a/Library/Homebrew/livecheck/strategy/git.rb +++ b/Library/Homebrew/livecheck/strategy/git.rb @@ -103,7 +103,8 @@ def self.versions_from_tags(tags, regex = nil, &block) tags.map do |tag| if regex # Use the first capture group (the version) - tag.scan(regex).first&.first + # This code is not typesafe unless the regex includes a capture group + T.unsafe(tag.scan(regex).first)&.first else # Remove non-digits from the start of the tag and use that as the # version text From 8d788ec98ce518a92eb290b33c6efa323416da46 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 24 Feb 2023 09:48:48 -0800 Subject: [PATCH 11/19] Try removing String monkey-patched types --- .../sorbet/rbi/gems/activesupport@6.1.7.2.rbi | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/Library/Homebrew/sorbet/rbi/gems/activesupport@6.1.7.2.rbi b/Library/Homebrew/sorbet/rbi/gems/activesupport@6.1.7.2.rbi index 3515dbbea3785..5ba4ae60d86af 100644 --- a/Library/Homebrew/sorbet/rbi/gems/activesupport@6.1.7.2.rbi +++ b/Library/Homebrew/sorbet/rbi/gems/activesupport@6.1.7.2.rbi @@ -3415,55 +3415,55 @@ class Regexp::Token < ::Struct end end -class String - include ::Comparable - include ::JSON::Ext::Generator::GeneratorMethods::String - include ::MessagePack::CoreExt - extend ::JSON::Ext::Generator::GeneratorMethods::String::Extend - - def acts_like_string?; end - def as_json(options = T.unsafe(nil)); end - def at(position); end - def blank?; end - def camelcase(first_letter = T.unsafe(nil)); end - def camelize(first_letter = T.unsafe(nil)); end - def classify; end - def constantize; end - def dasherize; end - def deconstantize; end - def demodulize; end - def first(limit = T.unsafe(nil)); end - def foreign_key(separate_class_name_and_id_with_underscore = T.unsafe(nil)); end - def from(position); end - def html_safe; end - def humanize(capitalize: T.unsafe(nil), keep_id_suffix: T.unsafe(nil)); end - def is_utf8?; end - def last(limit = T.unsafe(nil)); end - def mb_chars; end - def parameterize(separator: T.unsafe(nil), preserve_case: T.unsafe(nil), locale: T.unsafe(nil)); end - def pluralize(count = T.unsafe(nil), locale = T.unsafe(nil)); end - def remove(*patterns); end - def remove!(*patterns); end - def safe_constantize; end - def singularize(locale = T.unsafe(nil)); end - def squish; end - def squish!; end - def tableize; end - def titlecase(keep_id_suffix: T.unsafe(nil)); end - def titleize(keep_id_suffix: T.unsafe(nil)); end - def to(position); end - def to_date; end - def to_datetime; end - def to_time(form = T.unsafe(nil)); end - def truncate(truncate_at, options = T.unsafe(nil)); end - def truncate_bytes(truncate_at, omission: T.unsafe(nil)); end - def truncate_words(words_count, options = T.unsafe(nil)); end - def underscore; end - def upcase_first; end -end - -String::BLANK_RE = T.let(T.unsafe(nil), Regexp) -String::ENCODED_BLANKS = T.let(T.unsafe(nil), Concurrent::Map) +# class String +# include ::Comparable +# include ::JSON::Ext::Generator::GeneratorMethods::String +# include ::MessagePack::CoreExt +# extend ::JSON::Ext::Generator::GeneratorMethods::String::Extend + +# def acts_like_string?; end +# def as_json(options = T.unsafe(nil)); end +# def at(position); end +# def blank?; end +# def camelcase(first_letter = T.unsafe(nil)); end +# def camelize(first_letter = T.unsafe(nil)); end +# def classify; end +# def constantize; end +# def dasherize; end +# def deconstantize; end +# def demodulize; end +# def first(limit = T.unsafe(nil)); end +# def foreign_key(separate_class_name_and_id_with_underscore = T.unsafe(nil)); end +# def from(position); end +# def html_safe; end +# def humanize(capitalize: T.unsafe(nil), keep_id_suffix: T.unsafe(nil)); end +# def is_utf8?; end +# def last(limit = T.unsafe(nil)); end +# def mb_chars; end +# def parameterize(separator: T.unsafe(nil), preserve_case: T.unsafe(nil), locale: T.unsafe(nil)); end +# def pluralize(count = T.unsafe(nil), locale = T.unsafe(nil)); end +# def remove(*patterns); end +# def remove!(*patterns); end +# def safe_constantize; end +# def singularize(locale = T.unsafe(nil)); end +# def squish; end +# def squish!; end +# def tableize; end +# def titlecase(keep_id_suffix: T.unsafe(nil)); end +# def titleize(keep_id_suffix: T.unsafe(nil)); end +# def to(position); end +# def to_date; end +# def to_datetime; end +# def to_time(form = T.unsafe(nil)); end +# def truncate(truncate_at, options = T.unsafe(nil)); end +# def truncate_bytes(truncate_at, omission: T.unsafe(nil)); end +# def truncate_words(words_count, options = T.unsafe(nil)); end +# def underscore; end +# def upcase_first; end +# end + +# String::BLANK_RE = T.let(T.unsafe(nil), Regexp) +# String::ENCODED_BLANKS = T.let(T.unsafe(nil), Concurrent::Map) class Struct include ::Enumerable From 86c500fdccefd3de5204ff8051b8c1c0344bd3b9 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 24 Feb 2023 09:49:28 -0800 Subject: [PATCH 12/19] Try removing inflections require --- Library/Homebrew/global.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index c773b8bb0f021..7712a783f7393 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -16,7 +16,7 @@ require "active_support/core_ext/string/filters" require "active_support/core_ext/object/try" require "active_support/core_ext/array/access" -require "active_support/core_ext/string/inflections" +require "i18n" require "active_support/core_ext/array/conversions" require "active_support/core_ext/hash/deep_merge" require "active_support/core_ext/file/atomic" @@ -27,12 +27,6 @@ I18n.backend.available_locales # Initialize locales so they can be overwritten. I18n.backend.store_translations :en, support: { array: { last_word_connector: " and " } } -ActiveSupport::Inflector.inflections(:en) do |inflect| - inflect.irregular "formula", "formulae" - inflect.irregular "is", "are" - inflect.irregular "it", "they" -end - HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_BOTTLE_DEFAULT_DOMAIN").freeze HOMEBREW_BREW_DEFAULT_GIT_REMOTE = ENV.fetch("HOMEBREW_BREW_DEFAULT_GIT_REMOTE").freeze From 0eccc0e98763aa14791a4e014dfe4c722de5a19d Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 27 Feb 2023 20:16:34 -0800 Subject: [PATCH 13/19] git grep -l Utils::Inflection | xargs gsed -i 's|Utils::Inflection|Utils|g' --- Library/Homebrew/cask/cmd/install.rb | 4 ++-- Library/Homebrew/cask/cmd/uninstall.rb | 2 +- Library/Homebrew/cask/cmd/upgrade.rb | 4 ++-- Library/Homebrew/cask/installer.rb | 4 ++-- Library/Homebrew/cleanup.rb | 2 +- Library/Homebrew/cli/parser.rb | 6 +++--- Library/Homebrew/cmd/developer.rb | 2 +- Library/Homebrew/cmd/fetch.rb | 2 +- Library/Homebrew/cmd/info.rb | 2 +- Library/Homebrew/cmd/tap-info.rb | 6 +++--- Library/Homebrew/cmd/update-report.rb | 10 +++++----- Library/Homebrew/cmd/upgrade.rb | 4 ++-- Library/Homebrew/dev-cmd/audit.rb | 10 +++++----- Library/Homebrew/dev-cmd/pr-automerge.rb | 2 +- Library/Homebrew/diagnostic.rb | 4 ++-- Library/Homebrew/exceptions.rb | 2 +- Library/Homebrew/formulary.rb | 6 +++--- Library/Homebrew/install.rb | 4 ++-- Library/Homebrew/livecheck/livecheck.rb | 2 +- .../Homebrew/livecheck/strategy/electron_builder.rb | 2 +- Library/Homebrew/livecheck/strategy/extract_plist.rb | 4 ++-- Library/Homebrew/livecheck/strategy/page_match.rb | 2 +- Library/Homebrew/livecheck/strategy/sparkle.rb | 2 +- Library/Homebrew/tap.rb | 6 +++--- Library/Homebrew/uninstall.rb | 8 ++++---- Library/Homebrew/upgrade.rb | 10 +++++----- 26 files changed, 56 insertions(+), 56 deletions(-) diff --git a/Library/Homebrew/cask/cmd/install.rb b/Library/Homebrew/cask/cmd/install.rb index acaa525997cac..efd84d93651f8 100644 --- a/Library/Homebrew/cask/cmd/install.rb +++ b/Library/Homebrew/cask/cmd/install.rb @@ -85,7 +85,7 @@ def self.install_casks( if dry_run if (casks_to_install = casks.reject(&:installed?).presence) - plural = ::Utils::Inflection.pluralize("cask", casks_to_install.count) + plural = ::Utils.pluralize("cask", casks_to_install.count) ohai "Would install #{casks_to_install.count} #{plural}:" puts casks_to_install.map(&:full_name).join(" ") end @@ -97,7 +97,7 @@ def self.install_casks( .map(&:name) next if dep_names.blank? - plural = ::Utils::Inflection.pluralize("dependenc", dep_names.count, plural: "ies", singular: "y") + plural = ::Utils.pluralize("dependenc", dep_names.count, plural: "ies", singular: "y") ohai "Would install #{dep_names.count} #{plural} for #{cask.full_name}:" puts dep_names.join(" ") end diff --git a/Library/Homebrew/cask/cmd/uninstall.rb b/Library/Homebrew/cask/cmd/uninstall.rb index e19739b8fddf4..f121cbb98c222 100644 --- a/Library/Homebrew/cask/cmd/uninstall.rb +++ b/Library/Homebrew/cask/cmd/uninstall.rb @@ -46,7 +46,7 @@ def self.uninstall_casks(*casks, binaries: nil, force: false, verbose: false) next if (versions = cask.versions).empty? puts <<~EOS - #{cask} #{versions.to_sentence} #{::Utils::Inflection.pluralize("", versions.count, plural: "are", singular: "is")} still installed. + #{cask} #{versions.to_sentence} #{::Utils.pluralize("", versions.count, plural: "are", singular: "is")} still installed. Remove #{(versions.count == 1) ? "it" : "them all"} with `brew uninstall --cask --force #{cask}`. EOS end diff --git a/Library/Homebrew/cask/cmd/upgrade.rb b/Library/Homebrew/cask/cmd/upgrade.rb index 1dc312d08beab..2c4b98691de77 100644 --- a/Library/Homebrew/cask/cmd/upgrade.rb +++ b/Library/Homebrew/cask/cmd/upgrade.rb @@ -121,7 +121,7 @@ def self.upgrade_casks( if manual_installer_casks.present? count = manual_installer_casks.count - ofail "Not upgrading #{count} `installer manual` #{::Utils::Inflection.pluralize("cask", versions.count)}." + ofail "Not upgrading #{count} `installer manual` #{::Utils.pluralize("cask", versions.count)}." puts manual_installer_casks.map(&:to_s) outdated_casks -= manual_installer_casks end @@ -142,7 +142,7 @@ def self.upgrade_casks( end verb = dry_run ? "Would upgrade" : "Upgrading" - oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils::Inflection.pluralize("package", + oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils.pluralize("package", outdated_casks.count)}:" caught_exceptions = [] diff --git a/Library/Homebrew/cask/installer.rb b/Library/Homebrew/cask/installer.rb index 64fa081e59585..91f6fc1401214 100644 --- a/Library/Homebrew/cask/installer.rb +++ b/Library/Homebrew/cask/installer.rb @@ -231,7 +231,7 @@ def install_artifacts already_installed_artifacts = [] odebug "Installing artifacts" - odebug "#{artifacts.length} #{::Utils::Inflection.pluralize("artifact", artifacts.length)} defined", artifacts + odebug "#{artifacts.length} #{::Utils.pluralize("artifact", artifacts.length)} defined", artifacts artifacts.each do |artifact| next unless artifact.respond_to?(:install_phase) @@ -460,7 +460,7 @@ def uninstall_artifacts(clear: false) artifacts = @cask.artifacts odebug "Uninstalling artifacts" - odebug "#{artifacts.length} #{::Utils::Inflection.pluralize("artifact", artifacts.length)} defined", artifacts + odebug "#{artifacts.length} #{::Utils.pluralize("artifact", artifacts.length)} defined", artifacts artifacts.each do |artifact| if artifact.respond_to?(:uninstall_phase) diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 57e5d3ef2741d..6c6e73f6874c7 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -584,7 +584,7 @@ def self.autoremove(dry_run: false) formulae_names = removable_formulae.map(&:full_name).sort verb = dry_run ? "Would autoremove" : "Autoremoving" - oh1 "#{verb} #{formulae_names.count} unneeded #{Utils::Inflection.pluralize("formula", formulae_names.count, + oh1 "#{verb} #{formulae_names.count} unneeded #{Utils.pluralize("formula", formulae_names.count, plural: "e")}:" puts formulae_names.join("\n") return if dry_run diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index f3ec536c0ccd1..fc6f15bd8f3f6 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -684,7 +684,7 @@ def initialize(maximum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - "This command does not take more than #{maximum} #{arg_types} #{Utils::Inflection.pluralize("argument", + "This command does not take more than #{maximum} #{arg_types} #{Utils.pluralize("argument", maximum)}." end end @@ -699,7 +699,7 @@ def initialize(minimum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires at least #{minimum} #{arg_types} #{Utils::Inflection.pluralize("argument", + super "This command requires at least #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}." end end @@ -713,7 +713,7 @@ def initialize(minimum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires exactly #{minimum} #{arg_types} #{Utils::Inflection.pluralize("argument", + super "This command requires exactly #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}." end end diff --git a/Library/Homebrew/cmd/developer.rb b/Library/Homebrew/cmd/developer.rb index a142a77f7d99c..aebe05654eb93 100755 --- a/Library/Homebrew/cmd/developer.rb +++ b/Library/Homebrew/cmd/developer.rb @@ -40,7 +40,7 @@ def developer case args.named.first when nil, "state" if env_vars.any? - puts "Developer mode is enabled because #{env_vars.to_sentence} #{Utils::Inflection.pluralize("", + puts "Developer mode is enabled because #{env_vars.to_sentence} #{Utils.pluralize("", env_vars.count, plural: "are", singular: "is")} set." elsif Homebrew::Settings.read("devcmdrun") == "true" puts "Developer mode is enabled." diff --git a/Library/Homebrew/cmd/fetch.rb b/Library/Homebrew/cmd/fetch.rb index 80af3a5200813..d348b69bf2135 100644 --- a/Library/Homebrew/cmd/fetch.rb +++ b/Library/Homebrew/cmd/fetch.rb @@ -166,7 +166,7 @@ def retry_fetch?(f, args:) if args.retry? && (@fetch_tries[f] < FETCH_MAX_TRIES) wait = 2 ** @fetch_tries[f] remaining = FETCH_MAX_TRIES - @fetch_tries[f] - what = Utils::Inflection.pluralize("tr", remaining, plural: "ies", singular: "y") + what = Utils.pluralize("tr", remaining, plural: "ies", singular: "y") ohai "Retrying download in #{wait}s... (#{remaining} #{what} left)" sleep wait diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index 68982834d8ec4..52cc4267175bf 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -124,7 +124,7 @@ def print_statistics return unless HOMEBREW_CELLAR.exist? count = Formula.racks.length - puts "#{count} #{Utils::Inflection.pluralize("keg", count)}, #{HOMEBREW_CELLAR.dup.abv}" + puts "#{count} #{Utils.pluralize("keg", count)}, #{HOMEBREW_CELLAR.dup.abv}" end sig { params(args: CLI::Args).void } diff --git a/Library/Homebrew/cmd/tap-info.rb b/Library/Homebrew/cmd/tap-info.rb index 270892c7ecf26..4a471a8e0193d 100644 --- a/Library/Homebrew/cmd/tap-info.rb +++ b/Library/Homebrew/cmd/tap-info.rb @@ -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::Inflection.pluralize("tap", tap_count)}" + info = "#{tap_count} #{Utils.pluralize("tap", tap_count)}" info += ", #{private_count} private" - info += ", #{formula_count} #{Utils::Inflection.pluralize("formula", formula_count, plural: "e")}" - info += ", #{command_count} #{Utils::Inflection.pluralize("command", command_count)}" + info += ", #{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}" + info += ", #{command_count} #{Utils.pluralize("command", command_count)}" info += ", #{Tap::TAP_DIRECTORY.dup.abv}" if Tap::TAP_DIRECTORY.directory? puts info else diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index d4a8f4cbecc48..fceb705f08b75 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -199,7 +199,7 @@ def output_update_report unless updated_taps.empty? auto_update_header args: args - puts "Updated #{updated_taps.count} #{Utils::Inflection.pluralize("tap", + puts "Updated #{updated_taps.count} #{Utils.pluralize("tap", updated_taps.count)} (#{updated_taps.to_sentence})." updated = true end @@ -586,11 +586,11 @@ def dump(updated_formula_report: true) elsif report_all if (changed_formulae = select_formula_or_cask(:M).count) && changed_formulae.positive? ohai "Modified Formulae", - "Modified #{changed_formulae} #{Utils::Inflection.pluralize("formula", changed_formulae, plural: "e")}." + "Modified #{changed_formulae} #{Utils.pluralize("formula", changed_formulae, plural: "e")}." end if (changed_casks = select_formula_or_cask(:MC).count) && changed_casks.positive? - ohai "Modified Casks", "Modified #{changed_casks} #{Utils::Inflection.pluralize("cask", changed_casks)}." + ohai "Modified Casks", "Modified #{changed_casks} #{Utils.pluralize("cask", changed_casks)}." end else outdated_formulae = Formula.installed.select(&:outdated?).map(&:name) @@ -611,13 +611,13 @@ def dump(updated_formula_report: true) msg = "" if outdated_formulae.positive? - msg += "#{Tty.bold}#{outdated_formulae}#{Tty.reset} outdated #{Utils::Inflection.pluralize("formula", + msg += "#{Tty.bold}#{outdated_formulae}#{Tty.reset} outdated #{Utils.pluralize("formula", outdated_formulae, plural: "e")}" end if outdated_casks.positive? msg += " and " if msg.present? - msg += "#{Tty.bold}#{outdated_casks}#{Tty.reset} outdated #{Utils::Inflection.pluralize("cask", + msg += "#{Tty.bold}#{outdated_casks}#{Tty.reset} outdated #{Utils.pluralize("cask", outdated_casks)}" end diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index 5463636deb0cb..9927c5ffcf0f0 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -172,7 +172,7 @@ def upgrade_outdated_formulae(formulae, args:) end if !pinned.empty? && !args.ignore_pinned? - ofail "Not upgrading #{pinned.count} pinned #{Utils::Inflection.pluralize("package", pinned.count)}:" + ofail "Not upgrading #{pinned.count} pinned #{Utils.pluralize("package", pinned.count)}:" puts pinned.map { |f| "#{f.full_specified_name} #{f.pkg_version}" } * ", " end @@ -180,7 +180,7 @@ def upgrade_outdated_formulae(formulae, args:) oh1 "No packages to upgrade" else verb = args.dry_run? ? "Would upgrade" : "Upgrading" - oh1 "#{verb} #{formulae_to_install.count} outdated #{Utils::Inflection.pluralize("package", + oh1 "#{verb} #{formulae_to_install.count} outdated #{Utils.pluralize("package", formulae_to_install.count)}:" formulae_upgrades = formulae_to_install.map do |f| if f.optlinked? diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 8feb3a6ad9c0e..356b8f13b1f44 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -277,22 +277,22 @@ def audit if total_problems_count.positive? puts new_formula_problem_lines.map { |s| " #{s}" } - errors_summary = "#{total_problems_count} #{Utils::Inflection.pluralize("problem", total_problems_count)}" + errors_summary = "#{total_problems_count} #{Utils.pluralize("problem", total_problems_count)}" error_sources = [] if formula_count.positive? - error_sources << "#{formula_count} #{Utils::Inflection.pluralize("formula", formula_count, + error_sources << "#{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}" end - error_sources << "#{cask_count} #{Utils::Inflection.pluralize("cask", cask_count)}" if cask_count.positive? - error_sources << "#{tap_count} #{Utils::Inflection.pluralize("tap", tap_count)}" if tap_count.positive? + 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? errors_summary += " in #{error_sources.to_sentence}" if error_sources.any? errors_summary += " detected" if corrected_problem_count.positive? - errors_summary += ", #{corrected_problem_count} #{Utils::Inflection.pluralize("problem", + errors_summary += ", #{corrected_problem_count} #{Utils.pluralize("problem", corrected_problem_count)} corrected" end diff --git a/Library/Homebrew/dev-cmd/pr-automerge.rb b/Library/Homebrew/dev-cmd/pr-automerge.rb index cec12b550d27c..e2e302b6c965e 100644 --- a/Library/Homebrew/dev-cmd/pr-automerge.rb +++ b/Library/Homebrew/dev-cmd/pr-automerge.rb @@ -61,7 +61,7 @@ def pr_automerge return end - ohai "#{prs.count} matching pull #{Utils::Inflection.pluralize("request", prs.count)}:" + ohai "#{prs.count} matching pull #{Utils.pluralize("request", prs.count)}:" pr_urls = [] prs.each do |pr| puts "#{tap.full_name unless tap.core_tap?}##{pr["number"]}: #{pr["title"]}" diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 4ce24c7376c91..3d1c512a22592 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -906,11 +906,11 @@ def check_cask_taps 0 end - "#{tap.path} (#{cask_count} #{Utils::Inflection.pluralize("cask", cask_count)})" + "#{tap.path} (#{cask_count} #{Utils.pluralize("cask", cask_count)})" end end) - taps = Utils::Inflection.pluralize("tap", error_tap_paths.count) + taps = Utils.pluralize("tap", error_tap_paths.count) "Unable to read from cask #{taps}: #{error_tap_paths.to_sentence}" if error_tap_paths.present? end diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index 0237024ca2489..c0b525e1cb607 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -568,7 +568,7 @@ def dump(verbose: false) class UnbottledError < RuntimeError def initialize(formulae) msg = +<<~EOS - The following #{Utils::Inflection.pluralize("formula", formulae.count, plural: "e")} cannot be installed from #{Utils::Inflection.pluralize("bottle", formulae.count)} and must be + The following #{Utils.pluralize("formula", formulae.count, plural: "e")} cannot be installed from #{Utils.pluralize("bottle", formulae.count)} and must be built from source. #{formulae.to_sentence} EOS diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index b39ad638e2dfa..aa179fbcfa80b 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -52,10 +52,10 @@ def self.clear_cache next if type == :formulary_factory cached_objects.each_value do |klass| - namespace = Utils::Inflection.deconstantize(klass.name) - next if Utils::Inflection.deconstantize(namespace) != name + namespace = Utils.deconstantize(klass.name) + next if Utils.deconstantize(namespace) != name - remove_const(Utils::Inflection.demodulize(namespace)) + remove_const(Utils.demodulize(namespace)) end end diff --git a/Library/Homebrew/install.rb b/Library/Homebrew/install.rb index 8d989fbf30197..aedd5723933dd 100644 --- a/Library/Homebrew/install.rb +++ b/Library/Homebrew/install.rb @@ -324,7 +324,7 @@ def install_formulae( if dry_run if (formulae_name_to_install = formulae_to_install.map(&:name)) - plural = Utils::Inflection.pluralize("formula", formulae_name_to_install.count, plural: "e") + plural = Utils.pluralize("formula", formulae_name_to_install.count, plural: "e") ohai "Would install #{formulae_name_to_install.count} #{plural}:" puts formulae_name_to_install.join(" ") @@ -354,7 +354,7 @@ def install_formula(formula_installer) def print_dry_run_dependencies(formula, dependencies, &block) return if dependencies.empty? - plural = Utils::Inflection.pluralize("dependenc", dependencies.count, plural: "ies", singular: "y") + plural = Utils.pluralize("dependenc", dependencies.count, plural: "ies", singular: "y") ohai "Would install #{dependencies.count} #{plural} for #{formula.name}:" formula_names = dependencies.map(&:first).map(&:to_formula).map(&block) puts formula_names.join(" ") diff --git a/Library/Homebrew/livecheck/livecheck.rb b/Library/Homebrew/livecheck/livecheck.rb index cef2357f69bad..07c436f3a5f7f 100644 --- a/Library/Homebrew/livecheck/livecheck.rb +++ b/Library/Homebrew/livecheck/livecheck.rb @@ -61,7 +61,7 @@ def livecheck_strategy_names constant = Strategy.const_get(const_symbol) next unless constant.is_a?(Class) - @livecheck_strategy_names[constant] = Utils::Inflection.demodulize(T.must(constant.name)) + @livecheck_strategy_names[constant] = Utils.demodulize(T.must(constant.name)) end @livecheck_strategy_names.freeze end diff --git a/Library/Homebrew/livecheck/strategy/electron_builder.rb b/Library/Homebrew/livecheck/strategy/electron_builder.rb index 542c00742d3e3..4be6c35f3e898 100644 --- a/Library/Homebrew/livecheck/strategy/electron_builder.rb +++ b/Library/Homebrew/livecheck/strategy/electron_builder.rb @@ -74,7 +74,7 @@ def self.versions_from_content(content, regex = nil, &block) def self.find_versions(url:, regex: nil, **_unused, &block) if regex.present? && block.blank? raise ArgumentError, - "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" + "#{Utils.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/extract_plist.rb b/Library/Homebrew/livecheck/strategy/extract_plist.rb index db7565dca6395..b9025dfbe7048 100644 --- a/Library/Homebrew/livecheck/strategy/extract_plist.rb +++ b/Library/Homebrew/livecheck/strategy/extract_plist.rb @@ -98,11 +98,11 @@ def self.versions_from_items(items, regex = nil, &block) def self.find_versions(cask:, url: nil, regex: nil, **_unused, &block) if regex.present? && block.blank? raise ArgumentError, - "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" + "#{Utils.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end unless T.unsafe(cask) raise ArgumentError, - "The #{Utils::Inflection.demodulize(T.must(name))} strategy only supports casks." + "The #{Utils.demodulize(T.must(name))} strategy only supports casks." end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/page_match.rb b/Library/Homebrew/livecheck/strategy/page_match.rb index fc86e00654643..a024bdb55200d 100644 --- a/Library/Homebrew/livecheck/strategy/page_match.rb +++ b/Library/Homebrew/livecheck/strategy/page_match.rb @@ -93,7 +93,7 @@ def self.versions_from_content(content, regex, &block) } def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **_unused, &block) if regex.blank? && block.blank? - raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} requires a regex or `strategy` block" + raise ArgumentError, "#{Utils.demodulize(T.must(name))} requires a regex or `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/sparkle.rb b/Library/Homebrew/livecheck/strategy/sparkle.rb index 36685c15d493d..5fbb74842b2f4 100644 --- a/Library/Homebrew/livecheck/strategy/sparkle.rb +++ b/Library/Homebrew/livecheck/strategy/sparkle.rb @@ -204,7 +204,7 @@ def self.versions_from_content(content, regex = nil, &block) def self.find_versions(url:, regex: nil, **_unused, &block) if regex.present? && block.blank? raise ArgumentError, - "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" + "#{Utils.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 7abd5ed5358a1..457fe6772613f 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -475,15 +475,15 @@ def contents contents = [] if (command_count = command_files.count).positive? - contents << "#{command_count} #{Utils::Inflection.pluralize("command", command_count)}" + contents << "#{command_count} #{Utils.pluralize("command", command_count)}" end if (cask_count = cask_files.count).positive? - contents << "#{cask_count} #{Utils::Inflection.pluralize("cask", cask_count)}" + contents << "#{cask_count} #{Utils.pluralize("cask", cask_count)}" end if (formula_count = formula_files.count).positive? - contents << "#{formula_count} #{Utils::Inflection.pluralize("formula", formula_count, plural: "e")}" + contents << "#{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}" end contents diff --git a/Library/Homebrew/uninstall.rb b/Library/Homebrew/uninstall.rb index 42136bc14b9b9..2a7fcafbf9765 100644 --- a/Library/Homebrew/uninstall.rb +++ b/Library/Homebrew/uninstall.rb @@ -52,7 +52,7 @@ def uninstall_kegs(kegs_by_rack, casks: [], force: false, ignore_dependencies: f if rack.directory? versions = rack.subdirs.map(&:basename) puts <<~EOS - #{keg.name} #{versions.to_sentence} #{Utils::Inflection.pluralize("", versions.count, plural: "are", singular: "is")} still installed. + #{keg.name} #{versions.to_sentence} #{Utils.pluralize("", versions.count, plural: "are", singular: "is")} still installed. To remove all versions, run: brew uninstall --force #{keg.name} EOS @@ -136,9 +136,9 @@ def sample_command end def are_required_by_deps - "#{Utils::Inflection.pluralize("", reqs.count, plural: "are", + "#{Utils.pluralize("", reqs.count, plural: "are", singular: "is")} required by #{deps.to_sentence}, " \ - "which #{Utils::Inflection.pluralize("", deps.count, plural: "are", singular: "is")} currently installed" + "which #{Utils.pluralize("", deps.count, plural: "are", singular: "is")} currently installed" end end @@ -158,7 +158,7 @@ class NondeveloperDependentsMessage < DependentsMessage def output ofail <<~EOS Refusing to uninstall #{reqs.to_sentence} - because #{Utils::Inflection.pluralize("", reqs.count, plural: "they", singular: "it")} #{are_required_by_deps}. + because #{Utils.pluralize("", reqs.count, plural: "they", singular: "it")} #{are_required_by_deps}. You can override this and force removal with: #{sample_command} EOS diff --git a/Library/Homebrew/upgrade.rb b/Library/Homebrew/upgrade.rb index 1e65181932806..98757792752a6 100644 --- a/Library/Homebrew/upgrade.rb +++ b/Library/Homebrew/upgrade.rb @@ -299,7 +299,7 @@ def check_installed_dependents( .sort { |a, b| depends_on(a, b) } if pinned_dependents.present? - plural = Utils::Inflection.pluralize("dependent", pinned_dependents.count) + plural = Utils.pluralize("dependent", pinned_dependents.count) ohai "Not upgrading #{pinned_dependents.count} pinned #{plural}:" puts(pinned_dependents.map do |f| "#{f.full_specified_name} #{f.pkg_version}" @@ -310,8 +310,8 @@ def check_installed_dependents( if upgradeable_dependents.blank? ohai "No outdated dependents to upgrade!" unless dry_run else - dependent_plural = Utils::Inflection.pluralize("dependent", upgradeable_dependents.count) - formula_plural = Utils::Inflection.pluralize("formula", installed_formulae.count, plural: "e") + 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}:" Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already! @@ -375,7 +375,7 @@ def check_installed_dependents( # Print the pinned dependents. if outdated_pinned_broken_dependents.present? count = outdated_pinned_broken_dependents.count - plural = Utils::Inflection.pluralize("dependent", outdated_pinned_broken_dependents.count) + plural = Utils.pluralize("dependent", outdated_pinned_broken_dependents.count) onoe "Not reinstalling #{count} broken and outdated, but pinned #{plural}:" $stderr.puts(outdated_pinned_broken_dependents.map do |f| "#{f.full_specified_name} #{f.pkg_version}" @@ -387,7 +387,7 @@ def check_installed_dependents( ohai "No broken dependents to reinstall!" else count = reinstallable_broken_dependents.count - plural = Utils::Inflection.pluralize("dependent", reinstallable_broken_dependents.count) + plural = Utils.pluralize("dependent", reinstallable_broken_dependents.count) ohai "Reinstalling #{count} #{plural} with broken linkage from source:" Upgrade.puts_no_installed_dependents_check_disable_message_if_not_already! puts reinstallable_broken_dependents.map(&:full_specified_name) From 325994a60c7cd20d1385a4fc036963aa7852fcb1 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 27 Feb 2023 20:20:56 -0800 Subject: [PATCH 14/19] Move inflection utils to Utils module --- Library/Homebrew/utils.rb | 48 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 70a9ebaeae53f..df758bafeb346 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -12,7 +12,6 @@ require "utils/git_repository" require "utils/github" require "utils/gzip" -require "utils/inflection" require "utils/inreplace" require "utils/link" require "utils/popen" @@ -86,3 +85,50 @@ def inject_dump_stats!(the_module, pattern) end # rubocop:enable Style/GlobalVars end + +module Utils + extend T::Sig + + # Removes the rightmost segment from the constant expression in the string. + # + # deconstantize('Net::HTTP') # => "Net" + # deconstantize('::Net::HTTP') # => "::Net" + # deconstantize('String') # => "" + # deconstantize('::String') # => "" + # deconstantize('') # => "" + # + # See also #demodulize. + # @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L247-L258 + # `ActiveSupport::Inflector.deconstantize` + sig { params(path: String).returns(String) } + def self.deconstantize(path) + T.must(path[0, path.rindex("::") || 0]) # implementation based on the one in facets' Module#spacename + end + + # Removes the module part from the expression in the string. + # + # demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections" + # demodulize('Inflections') # => "Inflections" + # demodulize('::Inflections') # => "Inflections" + # demodulize('') # => "" + # + # See also #deconstantize. + # @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L230-L245 + # `ActiveSupport::Inflector.demodulize` + sig { params(path: String).returns(String) } + def self.demodulize(path) + if (i = path.rindex("::")) + T.must(path[(i + 2)..]) + else + path + end + end + + # 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: "") + suffix = (count == 1) ? singular : plural + "#{stem}#{suffix}" + end +end From 6ea501b59f97ef8dc71a392b092e907c1b044fa6 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 27 Feb 2023 20:30:55 -0800 Subject: [PATCH 15/19] Add tests --- Library/Homebrew/test/utils_spec.rb | 61 +++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Library/Homebrew/test/utils_spec.rb diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb new file mode 100644 index 0000000000000..c75d21bed85e3 --- /dev/null +++ b/Library/Homebrew/test/utils_spec.rb @@ -0,0 +1,61 @@ +# typed: false +# frozen_string_literal: true + +require "utils" + +describe Utils do + describe ".deconstantize" do + it "removes the rightmost segment from the constant expression in the string" do + expect(described_class.deconstantize("Net::HTTP")).to eq("Net") + expect(described_class.deconstantize("::Net::HTTP")).to eq("::Net") + expect(described_class.deconstantize("String")).to eq("") + expect(described_class.deconstantize("::String")).to eq("") + end + + it "returns an empty string if the namespace is empty" do + expect(described_class.deconstantize("")).to eq("") + expect(described_class.deconstantize("::")).to eq("") + end + end + + describe ".demodulize" do + it "removes the module part from the expression in the string" do + expect(described_class.demodulize("Foo::Bar")).to eq("Bar") + end + + it "returns the string if it does not contain a module expression" do + expect(described_class.demodulize("FooBar")).to eq("FooBar") + end + + it "returns an empty string if the namespace is empty" do + expect(described_class.demodulize("")).to eq("") + expect(described_class.demodulize("::")).to eq("") + end + end + + describe ".pluralize" do + it "combines the stem with the default suffix based on the count" do + expect(described_class.pluralize("foo", 0)).to eq("foos") + expect(described_class.pluralize("foo", 1)).to eq("foo") + expect(described_class.pluralize("foo", 2)).to eq("foos") + end + + it "combines the stem with the singular suffix based on the count" do + expect(described_class.pluralize("foo", 0, singular: "o")).to eq("foos") + expect(described_class.pluralize("foo", 1, singular: "o")).to eq("fooo") + expect(described_class.pluralize("foo", 2, singular: "o")).to eq("foos") + end + + it "combines the stem with the plural suffix based on the count" do + expect(described_class.pluralize("foo", 0, plural: "es")).to eq("fooes") + expect(described_class.pluralize("foo", 1, plural: "es")).to eq("foo") + expect(described_class.pluralize("foo", 2, plural: "es")).to eq("fooes") + end + + it "combines the stem with the singular and plural suffix based on the count" do + expect(described_class.pluralize("foo", 0, singular: "o", plural: "es")).to eq("fooes") + 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 + end +end From 82dad857a808630c02aac6b7074eadfbcdbe32bb Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 27 Feb 2023 20:31:01 -0800 Subject: [PATCH 16/19] Restore global --- Library/Homebrew/global.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index 7712a783f7393..c773b8bb0f021 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -16,7 +16,7 @@ require "active_support/core_ext/string/filters" require "active_support/core_ext/object/try" require "active_support/core_ext/array/access" -require "i18n" +require "active_support/core_ext/string/inflections" require "active_support/core_ext/array/conversions" require "active_support/core_ext/hash/deep_merge" require "active_support/core_ext/file/atomic" @@ -27,6 +27,12 @@ I18n.backend.available_locales # Initialize locales so they can be overwritten. I18n.backend.store_translations :en, support: { array: { last_word_connector: " and " } } +ActiveSupport::Inflector.inflections(:en) do |inflect| + inflect.irregular "formula", "formulae" + inflect.irregular "is", "are" + inflect.irregular "it", "they" +end + HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_BOTTLE_DEFAULT_DOMAIN").freeze HOMEBREW_BREW_DEFAULT_GIT_REMOTE = ENV.fetch("HOMEBREW_BREW_DEFAULT_GIT_REMOTE").freeze From 1ab278f74c828126f1486f557f7069b8b9662f49 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 27 Feb 2023 20:34:07 -0800 Subject: [PATCH 17/19] Fix style/type violations --- Library/Homebrew/cask/cmd/upgrade.rb | 2 +- Library/Homebrew/cleanup.rb | 2 +- Library/Homebrew/cli/parser.rb | 6 +++--- Library/Homebrew/cmd/developer.rb | 4 ++-- Library/Homebrew/cmd/update-report.rb | 6 +++--- Library/Homebrew/cmd/upgrade.rb | 2 +- Library/Homebrew/dev-cmd/audit.rb | 4 ++-- Library/Homebrew/livecheck/strategy/json.rb | 2 +- Library/Homebrew/uninstall.rb | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Library/Homebrew/cask/cmd/upgrade.rb b/Library/Homebrew/cask/cmd/upgrade.rb index 2c4b98691de77..9200926973ca0 100644 --- a/Library/Homebrew/cask/cmd/upgrade.rb +++ b/Library/Homebrew/cask/cmd/upgrade.rb @@ -143,7 +143,7 @@ def self.upgrade_casks( verb = dry_run ? "Would upgrade" : "Upgrading" oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils.pluralize("package", - outdated_casks.count)}:" + outdated_casks.count)}:" caught_exceptions = [] diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 6c6e73f6874c7..3964dc5a4b95f 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -585,7 +585,7 @@ def self.autoremove(dry_run: false) verb = dry_run ? "Would autoremove" : "Autoremoving" oh1 "#{verb} #{formulae_names.count} unneeded #{Utils.pluralize("formula", formulae_names.count, - plural: "e")}:" + plural: "e")}:" puts formulae_names.join("\n") return if dry_run diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index fc6f15bd8f3f6..ea9a89820050a 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -685,7 +685,7 @@ def initialize(maximum, types: []) .to_sentence two_words_connector: " or ", last_word_connector: " or " "This command does not take more than #{maximum} #{arg_types} #{Utils.pluralize("argument", - maximum)}." + maximum)}." end end end @@ -700,7 +700,7 @@ def initialize(minimum, types: []) .to_sentence two_words_connector: " or ", last_word_connector: " or " super "This command requires at least #{minimum} #{arg_types} #{Utils.pluralize("argument", - minimum)}." + minimum)}." end end @@ -714,7 +714,7 @@ def initialize(minimum, types: []) .to_sentence two_words_connector: " or ", last_word_connector: " or " super "This command requires exactly #{minimum} #{arg_types} #{Utils.pluralize("argument", - minimum)}." + minimum)}." end end end diff --git a/Library/Homebrew/cmd/developer.rb b/Library/Homebrew/cmd/developer.rb index aebe05654eb93..03d0561573cdc 100755 --- a/Library/Homebrew/cmd/developer.rb +++ b/Library/Homebrew/cmd/developer.rb @@ -40,8 +40,8 @@ def developer case args.named.first when nil, "state" if env_vars.any? - puts "Developer mode is enabled because #{env_vars.to_sentence} #{Utils.pluralize("", - env_vars.count, plural: "are", singular: "is")} set." + verb = Utils.pluralize("", env_vars.count, plural: "are", singular: "is") + puts "Developer mode is enabled because #{env_vars.to_sentence} #{verb} set." elsif Homebrew::Settings.read("devcmdrun") == "true" puts "Developer mode is enabled." else diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index fceb705f08b75..69efca25f32e5 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -200,7 +200,7 @@ def output_update_report unless updated_taps.empty? auto_update_header args: args puts "Updated #{updated_taps.count} #{Utils.pluralize("tap", - updated_taps.count)} (#{updated_taps.to_sentence})." + updated_taps.count)} (#{updated_taps.to_sentence})." updated = true end @@ -612,13 +612,13 @@ def dump(updated_formula_report: true) if outdated_formulae.positive? msg += "#{Tty.bold}#{outdated_formulae}#{Tty.reset} outdated #{Utils.pluralize("formula", - outdated_formulae, plural: "e")}" + outdated_formulae, plural: "e")}" end if outdated_casks.positive? msg += " and " if msg.present? msg += "#{Tty.bold}#{outdated_casks}#{Tty.reset} outdated #{Utils.pluralize("cask", - outdated_casks)}" + outdated_casks)}" end return if msg.blank? diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index 9927c5ffcf0f0..9016bc9706b1a 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -181,7 +181,7 @@ def upgrade_outdated_formulae(formulae, args:) else verb = args.dry_run? ? "Would upgrade" : "Upgrading" oh1 "#{verb} #{formulae_to_install.count} outdated #{Utils.pluralize("package", - formulae_to_install.count)}:" + formulae_to_install.count)}:" formulae_upgrades = formulae_to_install.map do |f| if f.optlinked? "#{f.full_specified_name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}" diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 356b8f13b1f44..e37b5d7f23d53 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -282,7 +282,7 @@ def audit error_sources = [] if formula_count.positive? error_sources << "#{formula_count} #{Utils.pluralize("formula", formula_count, - plural: "e")}" + plural: "e")}" 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? @@ -293,7 +293,7 @@ def audit if corrected_problem_count.positive? errors_summary += ", #{corrected_problem_count} #{Utils.pluralize("problem", - corrected_problem_count)} corrected" + corrected_problem_count)} corrected" end ofail errors_summary diff --git a/Library/Homebrew/livecheck/strategy/json.rb b/Library/Homebrew/livecheck/strategy/json.rb index a6353ddd58084..91a7fbe30fd7b 100644 --- a/Library/Homebrew/livecheck/strategy/json.rb +++ b/Library/Homebrew/livecheck/strategy/json.rb @@ -100,7 +100,7 @@ def self.versions_from_content(content, regex = nil, &block) ).returns(T::Hash[Symbol, T.untyped]) } def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **_unused, &block) - raise ArgumentError, "#{T.must(name).demodulize} requires a `strategy` block" if block.blank? + raise ArgumentError, "#{Utils.demodulize(T.must(name))} requires a `strategy` block" if block.blank? match_data = { matches: {}, regex: regex, url: url } return match_data if url.blank? || block.blank? diff --git a/Library/Homebrew/uninstall.rb b/Library/Homebrew/uninstall.rb index 2a7fcafbf9765..4905e26e6e127 100644 --- a/Library/Homebrew/uninstall.rb +++ b/Library/Homebrew/uninstall.rb @@ -137,7 +137,7 @@ def sample_command def are_required_by_deps "#{Utils.pluralize("", reqs.count, plural: "are", - singular: "is")} required by #{deps.to_sentence}, " \ + singular: "is")} required by #{deps.to_sentence}, " \ "which #{Utils.pluralize("", deps.count, plural: "are", singular: "is")} currently installed" end end From f9f73f3ef60c0d79c0603361684a974484ec35c3 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 27 Feb 2023 20:49:02 -0800 Subject: [PATCH 18/19] Tidy up --- Library/Homebrew/cask/cmd/uninstall.rb | 2 +- Library/Homebrew/cask/cmd/upgrade.rb | 3 +- Library/Homebrew/cleanup.rb | 3 +- Library/Homebrew/cli/parser.rb | 9 ++-- Library/Homebrew/cmd/developer.rb | 3 +- Library/Homebrew/cmd/update-report.rb | 15 +++--- Library/Homebrew/cmd/upgrade.rb | 3 +- Library/Homebrew/dev-cmd/audit.rb | 7 ++- .../livecheck/strategy/extract_plist.rb | 3 +- .../sorbet/rbi/gems/activesupport@6.1.7.2.rbi | 50 ------------------ Library/Homebrew/uninstall.rb | 9 ++-- Library/Homebrew/utils/inflection.rb | 52 ------------------- 12 files changed, 23 insertions(+), 136 deletions(-) delete mode 100644 Library/Homebrew/utils/inflection.rb diff --git a/Library/Homebrew/cask/cmd/uninstall.rb b/Library/Homebrew/cask/cmd/uninstall.rb index f121cbb98c222..dbbdd328d51fc 100644 --- a/Library/Homebrew/cask/cmd/uninstall.rb +++ b/Library/Homebrew/cask/cmd/uninstall.rb @@ -46,7 +46,7 @@ def self.uninstall_casks(*casks, binaries: nil, force: false, verbose: false) next if (versions = cask.versions).empty? puts <<~EOS - #{cask} #{versions.to_sentence} #{::Utils.pluralize("", versions.count, plural: "are", singular: "is")} still installed. + #{cask} #{versions.to_sentence} #{versions.count == 1 ? "is" : "are"} still installed. Remove #{(versions.count == 1) ? "it" : "them all"} with `brew uninstall --cask --force #{cask}`. EOS end diff --git a/Library/Homebrew/cask/cmd/upgrade.rb b/Library/Homebrew/cask/cmd/upgrade.rb index 9200926973ca0..d4caa561e94ea 100644 --- a/Library/Homebrew/cask/cmd/upgrade.rb +++ b/Library/Homebrew/cask/cmd/upgrade.rb @@ -142,8 +142,7 @@ def self.upgrade_casks( end verb = dry_run ? "Would upgrade" : "Upgrading" - oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils.pluralize("package", - outdated_casks.count)}:" + oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils.pluralize("package", outdated_casks.count)}:" caught_exceptions = [] diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 3964dc5a4b95f..548dbf095a8a2 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -584,8 +584,7 @@ def self.autoremove(dry_run: false) formulae_names = removable_formulae.map(&:full_name).sort verb = dry_run ? "Would autoremove" : "Autoremoving" - oh1 "#{verb} #{formulae_names.count} unneeded #{Utils.pluralize("formula", formulae_names.count, - plural: "e")}:" + oh1 "#{verb} #{formulae_names.count} unneeded #{Utils.pluralize("formula", formulae_names.count, plural: "e")}:" puts formulae_names.join("\n") return if dry_run diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index ea9a89820050a..cd3c98f735626 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -684,8 +684,7 @@ def initialize(maximum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - "This command does not take more than #{maximum} #{arg_types} #{Utils.pluralize("argument", - maximum)}." + "This command does not take more than #{maximum} #{arg_types} #{Utils.pluralize("argument", maximum)}." end end end @@ -699,8 +698,7 @@ def initialize(minimum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires at least #{minimum} #{arg_types} #{Utils.pluralize("argument", - minimum)}." + super "This command requires at least #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}." end end @@ -713,8 +711,7 @@ def initialize(minimum, types: []) arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires exactly #{minimum} #{arg_types} #{Utils.pluralize("argument", - minimum)}." + super "This command requires exactly #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}." end end end diff --git a/Library/Homebrew/cmd/developer.rb b/Library/Homebrew/cmd/developer.rb index 03d0561573cdc..60e1ef744afae 100755 --- a/Library/Homebrew/cmd/developer.rb +++ b/Library/Homebrew/cmd/developer.rb @@ -40,8 +40,7 @@ def developer case args.named.first when nil, "state" if env_vars.any? - verb = Utils.pluralize("", env_vars.count, plural: "are", singular: "is") - puts "Developer mode is enabled because #{env_vars.to_sentence} #{verb} set." + puts "Developer mode is enabled because #{env_vars.to_sentence} #{env_vars.count == 1 ? "is" : "are"} set." elsif Homebrew::Settings.read("devcmdrun") == "true" puts "Developer mode is enabled." else diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index 69efca25f32e5..4d548e3d5e1f9 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -199,8 +199,8 @@ def output_update_report unless updated_taps.empty? auto_update_header args: args - puts "Updated #{updated_taps.count} #{Utils.pluralize("tap", - updated_taps.count)} (#{updated_taps.to_sentence})." + noun = Utils.pluralize("tap", updated_taps.count) + puts "Updated #{updated_taps.count} #{noun} (#{updated_taps.to_sentence})." updated = true end @@ -585,8 +585,8 @@ 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? - ohai "Modified Formulae", - "Modified #{changed_formulae} #{Utils.pluralize("formula", changed_formulae, plural: "e")}." + noun = Utils.pluralize("formula", changed_formulae, plural: "e") + ohai "Modified Formulae", "Modified #{changed_formulae} #{noun}." end if (changed_casks = select_formula_or_cask(:MC).count) && changed_casks.positive? @@ -611,14 +611,13 @@ def dump(updated_formula_report: true) msg = "" if outdated_formulae.positive? - msg += "#{Tty.bold}#{outdated_formulae}#{Tty.reset} outdated #{Utils.pluralize("formula", - outdated_formulae, plural: "e")}" + noun = Utils.pluralize("formula", outdated_formulae, plural: "e") + msg += "#{Tty.bold}#{outdated_formulae}#{Tty.reset} outdated #{noun}" end if outdated_casks.positive? msg += " and " if msg.present? - msg += "#{Tty.bold}#{outdated_casks}#{Tty.reset} outdated #{Utils.pluralize("cask", - outdated_casks)}" + msg += "#{Tty.bold}#{outdated_casks}#{Tty.reset} outdated #{Utils.pluralize("cask", outdated_casks)}" end return if msg.blank? diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index 9016bc9706b1a..94323b369fdca 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -180,8 +180,7 @@ def upgrade_outdated_formulae(formulae, args:) oh1 "No packages to upgrade" else verb = args.dry_run? ? "Would upgrade" : "Upgrading" - oh1 "#{verb} #{formulae_to_install.count} outdated #{Utils.pluralize("package", - formulae_to_install.count)}:" + oh1 "#{verb} #{formulae_to_install.count} outdated #{Utils.pluralize("package", formulae_to_install.count)}:" formulae_upgrades = formulae_to_install.map do |f| if f.optlinked? "#{f.full_specified_name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}" diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index e37b5d7f23d53..c5dac1cb53857 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -281,8 +281,7 @@ def audit error_sources = [] if formula_count.positive? - error_sources << "#{formula_count} #{Utils.pluralize("formula", formula_count, - plural: "e")}" + error_sources << "#{formula_count} #{Utils.pluralize("formula", formula_count, plural: "e")}" 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? @@ -292,8 +291,8 @@ def audit errors_summary += " detected" if corrected_problem_count.positive? - errors_summary += ", #{corrected_problem_count} #{Utils.pluralize("problem", - corrected_problem_count)} corrected" + noun = Utils.pluralize("problem", corrected_problem_count) + errors_summary += ", #{corrected_problem_count} #{noun} corrected" end ofail errors_summary diff --git a/Library/Homebrew/livecheck/strategy/extract_plist.rb b/Library/Homebrew/livecheck/strategy/extract_plist.rb index b9025dfbe7048..842e966571561 100644 --- a/Library/Homebrew/livecheck/strategy/extract_plist.rb +++ b/Library/Homebrew/livecheck/strategy/extract_plist.rb @@ -101,8 +101,7 @@ def self.find_versions(cask:, url: nil, regex: nil, **_unused, &block) "#{Utils.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end unless T.unsafe(cask) - raise ArgumentError, - "The #{Utils.demodulize(T.must(name))} strategy only supports casks." + raise ArgumentError, "The #{Utils.demodulize(T.must(name))} strategy only supports casks." end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/sorbet/rbi/gems/activesupport@6.1.7.2.rbi b/Library/Homebrew/sorbet/rbi/gems/activesupport@6.1.7.2.rbi index 5ba4ae60d86af..8840430c242d5 100644 --- a/Library/Homebrew/sorbet/rbi/gems/activesupport@6.1.7.2.rbi +++ b/Library/Homebrew/sorbet/rbi/gems/activesupport@6.1.7.2.rbi @@ -3415,56 +3415,6 @@ class Regexp::Token < ::Struct end end -# class String -# include ::Comparable -# include ::JSON::Ext::Generator::GeneratorMethods::String -# include ::MessagePack::CoreExt -# extend ::JSON::Ext::Generator::GeneratorMethods::String::Extend - -# def acts_like_string?; end -# def as_json(options = T.unsafe(nil)); end -# def at(position); end -# def blank?; end -# def camelcase(first_letter = T.unsafe(nil)); end -# def camelize(first_letter = T.unsafe(nil)); end -# def classify; end -# def constantize; end -# def dasherize; end -# def deconstantize; end -# def demodulize; end -# def first(limit = T.unsafe(nil)); end -# def foreign_key(separate_class_name_and_id_with_underscore = T.unsafe(nil)); end -# def from(position); end -# def html_safe; end -# def humanize(capitalize: T.unsafe(nil), keep_id_suffix: T.unsafe(nil)); end -# def is_utf8?; end -# def last(limit = T.unsafe(nil)); end -# def mb_chars; end -# def parameterize(separator: T.unsafe(nil), preserve_case: T.unsafe(nil), locale: T.unsafe(nil)); end -# def pluralize(count = T.unsafe(nil), locale = T.unsafe(nil)); end -# def remove(*patterns); end -# def remove!(*patterns); end -# def safe_constantize; end -# def singularize(locale = T.unsafe(nil)); end -# def squish; end -# def squish!; end -# def tableize; end -# def titlecase(keep_id_suffix: T.unsafe(nil)); end -# def titleize(keep_id_suffix: T.unsafe(nil)); end -# def to(position); end -# def to_date; end -# def to_datetime; end -# def to_time(form = T.unsafe(nil)); end -# def truncate(truncate_at, options = T.unsafe(nil)); end -# def truncate_bytes(truncate_at, omission: T.unsafe(nil)); end -# def truncate_words(words_count, options = T.unsafe(nil)); end -# def underscore; end -# def upcase_first; end -# end - -# String::BLANK_RE = T.let(T.unsafe(nil), Regexp) -# String::ENCODED_BLANKS = T.let(T.unsafe(nil), Concurrent::Map) - class Struct include ::Enumerable diff --git a/Library/Homebrew/uninstall.rb b/Library/Homebrew/uninstall.rb index 4905e26e6e127..3ecc965a6d020 100644 --- a/Library/Homebrew/uninstall.rb +++ b/Library/Homebrew/uninstall.rb @@ -52,7 +52,7 @@ def uninstall_kegs(kegs_by_rack, casks: [], force: false, ignore_dependencies: f if rack.directory? versions = rack.subdirs.map(&:basename) puts <<~EOS - #{keg.name} #{versions.to_sentence} #{Utils.pluralize("", versions.count, plural: "are", singular: "is")} still installed. + #{keg.name} #{versions.to_sentence} #{versions.count == 1 ? "is" : "are"} still installed. To remove all versions, run: brew uninstall --force #{keg.name} EOS @@ -136,9 +136,8 @@ def sample_command end def are_required_by_deps - "#{Utils.pluralize("", reqs.count, plural: "are", - singular: "is")} required by #{deps.to_sentence}, " \ - "which #{Utils.pluralize("", deps.count, plural: "are", singular: "is")} currently installed" + "#{reqs.count == 1 ? "is" : "are"} required by #{deps.to_sentence}, " \ + "which #{deps.count == 1 ? "is" : "are"} currently installed" end end @@ -158,7 +157,7 @@ class NondeveloperDependentsMessage < DependentsMessage def output ofail <<~EOS Refusing to uninstall #{reqs.to_sentence} - because #{Utils.pluralize("", reqs.count, plural: "they", singular: "it")} #{are_required_by_deps}. + because #{(reqs.count == 1) ? "it" : "they"} #{are_required_by_deps}. You can override this and force removal with: #{sample_command} EOS diff --git a/Library/Homebrew/utils/inflection.rb b/Library/Homebrew/utils/inflection.rb deleted file mode 100644 index 5aebaac58f881..0000000000000 --- a/Library/Homebrew/utils/inflection.rb +++ /dev/null @@ -1,52 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -module Utils - # Inflection utility methods, as a lightweight alternative to `ActiveSupport::Inflector``. - # - # @api private - module Inflection - extend T::Sig - # Removes the rightmost segment from the constant expression in the string. - # - # deconstantize('Net::HTTP') # => "Net" - # deconstantize('::Net::HTTP') # => "::Net" - # deconstantize('String') # => "" - # deconstantize('::String') # => "" - # deconstantize('') # => "" - # - # See also #demodulize. - # @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L247-L258 - # `ActiveSupport::Inflector.deconstantize` - sig { params(path: String).returns(String) } - def self.deconstantize(path) - T.must(path[0, path.rindex("::") || 0]) # implementation based on the one in facets' Module#spacename - end - - # Removes the module part from the expression in the string. - # - # demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections" - # demodulize('Inflections') # => "Inflections" - # demodulize('::Inflections') # => "Inflections" - # demodulize('') # => "" - # - # See also #deconstantize. - # @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L230-L245 - # `ActiveSupport::Inflector.demodulize` - sig { params(path: String).returns(String) } - def self.demodulize(path) - if (i = path.rindex("::")) - T.must(path[(i + 2)..]) - else - path - end - end - - # 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: "") - suffix = (count == 1) ? singular : plural - "#{stem}#{suffix}" - end - end -end From 2073e7069689490a5f68d71eecc24f13a5d2bf2c Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 27 Feb 2023 21:33:16 -0800 Subject: [PATCH 19/19] brew style --fix --- Library/Homebrew/cask/cmd/uninstall.rb | 2 +- Library/Homebrew/cmd/developer.rb | 2 +- Library/Homebrew/uninstall.rb | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/cask/cmd/uninstall.rb b/Library/Homebrew/cask/cmd/uninstall.rb index dbbdd328d51fc..1700482a65148 100644 --- a/Library/Homebrew/cask/cmd/uninstall.rb +++ b/Library/Homebrew/cask/cmd/uninstall.rb @@ -46,7 +46,7 @@ def self.uninstall_casks(*casks, binaries: nil, force: false, verbose: false) next if (versions = cask.versions).empty? puts <<~EOS - #{cask} #{versions.to_sentence} #{versions.count == 1 ? "is" : "are"} still installed. + #{cask} #{versions.to_sentence} #{(versions.count == 1) ? "is" : "are"} still installed. Remove #{(versions.count == 1) ? "it" : "them all"} with `brew uninstall --cask --force #{cask}`. EOS end diff --git a/Library/Homebrew/cmd/developer.rb b/Library/Homebrew/cmd/developer.rb index 60e1ef744afae..131f18cef56d8 100755 --- a/Library/Homebrew/cmd/developer.rb +++ b/Library/Homebrew/cmd/developer.rb @@ -40,7 +40,7 @@ def developer case args.named.first when nil, "state" if env_vars.any? - puts "Developer mode is enabled because #{env_vars.to_sentence} #{env_vars.count == 1 ? "is" : "are"} set." + puts "Developer mode is enabled because #{env_vars.to_sentence} #{(env_vars.count == 1) ? "is" : "are"} set." elsif Homebrew::Settings.read("devcmdrun") == "true" puts "Developer mode is enabled." else diff --git a/Library/Homebrew/uninstall.rb b/Library/Homebrew/uninstall.rb index 3ecc965a6d020..a6f366f448797 100644 --- a/Library/Homebrew/uninstall.rb +++ b/Library/Homebrew/uninstall.rb @@ -52,7 +52,7 @@ def uninstall_kegs(kegs_by_rack, casks: [], force: false, ignore_dependencies: f if rack.directory? versions = rack.subdirs.map(&:basename) puts <<~EOS - #{keg.name} #{versions.to_sentence} #{versions.count == 1 ? "is" : "are"} still installed. + #{keg.name} #{versions.to_sentence} #{(versions.count == 1) ? "is" : "are"} still installed. To remove all versions, run: brew uninstall --force #{keg.name} EOS @@ -136,8 +136,8 @@ def sample_command end def are_required_by_deps - "#{reqs.count == 1 ? "is" : "are"} required by #{deps.to_sentence}, " \ - "which #{deps.count == 1 ? "is" : "are"} currently installed" + "#{(reqs.count == 1) ? "is" : "are"} required by #{deps.to_sentence}, " \ + "which #{(deps.count == 1) ? "is" : "are"} currently installed" end end