Revert Utils implementation

This commit is contained in:
Douglas Eichelberger 2023-03-02 14:07:10 -08:00
parent b8ddecf322
commit ecfceb3fee
20 changed files with 35 additions and 92 deletions

View File

@ -46,7 +46,7 @@ module Cask
pkgs = Pathname.glob(cask.staged_path/"**"/"*.pkg").map { |path| path.relative_path_from(cask.staged_path) }
message = "Could not find PKG source file '#{pkg}'"
message += ", found #{::Utils.to_sentence(pkgs.map { |path| "'#{path}'" })} instead" if pkgs.any?
message += ", found #{pkgs.map { |path| "'#{path}'" }.to_sentence} instead" if pkgs.any?
message += "."
raise CaskError, message

View File

@ -57,8 +57,7 @@ module Cask
if !language && language_blocks
sample_languages = if language_blocks.length > LANGUAGE_BLOCK_LIMIT && !@audit_new_cask
sample_keys = language_blocks.keys.sample(LANGUAGE_BLOCK_LIMIT)
sample_keys_sentence = ::Utils.to_sentence(sample_keys.map { |lang| lang[0].to_s })
ohai "Auditing a sample of available languages: #{sample_keys_sentence}"
ohai "Auditing a sample of available languages: #{sample_keys.map { |lang| lang[0].to_s }.to_sentence}"
language_blocks.select { |k| sample_keys.include?(k) }
else
language_blocks
@ -68,7 +67,7 @@ module Cask
audit = audit_languages(l)
summary = audit.summary(include_passed: output_passed?, include_warnings: output_warnings?)
if summary.present? && output_summary?(audit)
ohai "Auditing language: #{::Utils.to_sentence(l.map { |lang| "'#{lang}'" })}" if output_summary?
ohai "Auditing language: #{l.map { |lang| "'#{lang}'" }.to_sentence}" if output_summary?
puts summary
end
warnings += audit.warnings

View File

@ -46,7 +46,7 @@ module Cask
next if (versions = cask.versions).empty?
puts <<~EOS
#{cask} #{::Utils.to_sentence(versions)} #{(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

View File

@ -306,7 +306,7 @@ module Cask
rescue TSort::Cyclic
strongly_connected_components = graph.strongly_connected_components.sort_by(&:count)
cyclic_dependencies = strongly_connected_components.last - [@cask]
raise CaskCyclicDependencyError.new(@cask.token, ::Utils.to_sentence(cyclic_dependencies))
raise CaskCyclicDependencyError.new(@cask.token, cyclic_dependencies.to_sentence)
end
end

View File

@ -683,8 +683,8 @@ module Homebrew
"This command does not take named arguments."
else
types << :named if types.empty?
arg_types = Utils.to_sentence(types.map { |type| type.to_s.tr("_", " ") },
two_words_connector: " or ", last_word_connector: " or ")
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)}."
end
@ -697,8 +697,8 @@ module Homebrew
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
def initialize(minimum, types: [])
types << :named if types.empty?
arg_types = Utils.to_sentence(types.map { |type| type.to_s.tr("_", " ") },
two_words_connector: " or ", last_word_connector: " or ")
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)}."
end
@ -710,8 +710,8 @@ module Homebrew
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
def initialize(minimum, types: [])
types << :named if types.empty?
arg_types = Utils.to_sentence(types.map { |type| type.to_s.tr("_", " ") },
two_words_connector: " or ", last_word_connector: " or ")
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)}."
end

View File

@ -40,8 +40,7 @@ module Homebrew
case args.named.first
when nil, "state"
if env_vars.any?
env_vars_str = Utils.to_sentence(env_vars)
puts "Developer mode is enabled because #{env_vars_str} #{(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
@ -51,7 +50,7 @@ module Homebrew
Homebrew::Settings.write "devcmdrun", true
when "off"
Homebrew::Settings.delete "devcmdrun"
puts "To fully disable developer mode, you must unset #{Utils.to_sentence(env_vars)}." if env_vars.any?
puts "To fully disable developer mode, you must unset #{env_vars.to_sentence}." if env_vars.any?
else
raise UsageError, "unknown subcommand: #{args.named.first}"
end

View File

@ -234,7 +234,7 @@ module Homebrew
unless updated_taps.empty?
auto_update_header args: args
noun = Utils.pluralize("tap", updated_taps.count)
puts "Updated #{updated_taps.count} #{noun} (#{Utils.to_sentence(updated_taps)})."
puts "Updated #{updated_taps.count} #{noun} (#{updated_taps.to_sentence})."
updated = true
end

View File

@ -286,7 +286,7 @@ module Homebrew
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 #{Utils.to_sentence(error_sources)}" if error_sources.any?
errors_summary += " in #{error_sources.to_sentence}" if error_sources.any?
errors_summary += " detected"

View File

@ -18,7 +18,6 @@ module Homebrew
sig { returns(CLI::Parser) }
def contributions_args
supported_repos_sentence = Utils.to_sentence(SUPPORTED_REPOS.map { |t| "`#{t}`" })
Homebrew::CLI::Parser.new do
usage_banner "`contributions` [--user=<email|username>] [<--repositories>`=`] [<--csv>]"
description <<~EOS
@ -27,7 +26,7 @@ module Homebrew
comma_array "--repositories",
description: "Specify a comma-separated (no spaces) list of repositories to search. " \
"Supported repositories: #{supported_repos_sentence}. " \
"Supported repositories: #{SUPPORTED_REPOS.map { |t| "`#{t}`" }.to_sentence}. " \
"Omitting this flag, or specifying `--repositories=all`, searches all repositories. " \
"Use `--repositories=primary` to search only the main repositories: brew,core,cask."
flag "--from=",

View File

@ -37,7 +37,7 @@ module Homebrew
members.each do |group, hash|
hash.slice!(*public_members)
hash.each { |login, name| hash[login] = "[#{name}](https://github.com/#{login})" }
sentences[group] = Utils.to_sentence(hash.values.sort)
sentences[group] = hash.values.sort.to_sentence
end
readme = HOMEBREW_REPOSITORY/"README.md"

View File

@ -57,7 +57,7 @@ module Homebrew
readme = HOMEBREW_REPOSITORY/"README.md"
content = readme.read
content.gsub!(/(Homebrew is generously supported by) .*\Z/m, "\\1 #{Utils.to_sentence(named_sponsors)}.\n")
content.gsub!(/(Homebrew is generously supported by) .*\Z/m, "\\1 #{named_sponsors.to_sentence}.\n")
content << "\n#{logo_sponsors.join}\n" if logo_sponsors.presence
File.write(readme, content)

View File

@ -911,7 +911,7 @@ module Homebrew
end)
taps = Utils.pluralize("tap", error_tap_paths.count)
"Unable to read from cask #{taps}: #{Utils.to_sentence(error_tap_paths)}" if error_tap_paths.present?
"Unable to read from cask #{taps}: #{error_tap_paths.to_sentence}" if error_tap_paths.present?
end
def check_cask_load_path

View File

@ -100,8 +100,7 @@ class FormulaOrCaskUnavailableError < RuntimeError
similar_formula_names = Formula.fuzzy_search(name)
return "" if similar_formula_names.blank?
"Did you mean #{Utils.to_sentence(similar_formula_names, two_words_connector: " or ",
last_word_connector: " or ")}?"
"Did you mean #{similar_formula_names.to_sentence two_words_connector: " or ", last_word_connector: " or "}?"
end
sig { returns(String) }
@ -571,7 +570,7 @@ class UnbottledError < RuntimeError
msg = +<<~EOS
The following #{Utils.pluralize("formula", formulae.count, plural: "e")} cannot be installed from #{Utils.pluralize("bottle", formulae.count)} and must be
built from source.
#{Utils.to_sentence(formulae)}
#{formulae.to_sentence}
EOS
msg += "#{DevelopmentTools.installation_instructions}\n" unless DevelopmentTools.installed?
msg.freeze
@ -805,8 +804,7 @@ class CyclicDependencyError < RuntimeError
def initialize(strongly_connected_components)
super <<~EOS
The following packages contain cyclic dependencies:
#{strongly_connected_components.select { |packages| packages.count > 1 }
.map { |p| Utils.to_sentence(p) }.join("\n ")}
#{strongly_connected_components.select { |packages| packages.count > 1 }.map(&:to_sentence).join("\n ")}
EOS
end
end

View File

@ -652,7 +652,7 @@ class FormulaInstaller
puts "All dependencies for #{formula.full_name} are satisfied."
elsif !deps.empty?
oh1 "Installing dependencies for #{formula.full_name}: " \
"#{Utils.to_sentence(deps.map(&:first).map(&Formatter.method(:identifier)))}",
"#{deps.map(&:first).map(&Formatter.method(:identifier)).to_sentence}",
truncate: false
deps.each { |dep, options| install_dependency(dep, options) }
end
@ -1163,7 +1163,7 @@ class FormulaInstaller
return if deps.empty?
oh1 "Fetching dependencies for #{formula.full_name}: " \
"#{Utils.to_sentence(deps.map(&:first).map(&Formatter.method(:identifier)))}",
"#{deps.map(&:first).map(&Formatter.method(:identifier)).to_sentence}",
truncate: false
deps.each { |dep, _options| fetch_dependency(dep) }

View File

@ -1,6 +1,9 @@
# typed: strict
# frozen_string_literal: true
# TODO: remove this (and avoid further active support in rubocops)
require "active_support/core_ext/array/conversions"
require_relative "io_read"
require_relative "move_to_extend_os"
require_relative "shell_commands"

View File

@ -3,7 +3,6 @@
require "ast_constants"
require "rubocops/extend/formula_cop"
require "utils/array"
module RuboCop
module Cop
@ -76,8 +75,8 @@ module RuboCop
[:url, :version, :sha256],
[:url, :mirror, :version, :sha256],
]
minimum_methods = Utils.to_sentence(allowed_methods.first.map { |m| "`#{m}`" })
maximum_methods = Utils.to_sentence(allowed_methods.last.map { |m| "`#{m}`" })
minimum_methods = allowed_methods.first.map { |m| "`#{m}`" }.to_sentence
maximum_methods = allowed_methods.last.map { |m| "`#{m}`" }.to_sentence
on_system_bodies.each do |on_system_block, on_system_body|
method_name = on_system_block.method_name

View File

@ -326,7 +326,7 @@ class Tap
Commands.rebuild_commands_completion_list
link_completions_and_manpages
formatted_contents = contents.presence&.then { |c| Utils.to_sentence(c) }&.dup&.prepend(" ")
formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ")
$stderr.puts "Tapped#{formatted_contents} (#{path.abv})." unless quiet
CacheStoreDatabase.use(:descriptions) do |db|
DescriptionCacheStore.new(db)
@ -414,7 +414,7 @@ class Tap
$stderr.puts "Untapping #{name}..."
abv = path.abv
formatted_contents = contents.presence&.then { |c| Utils.to_sentence(c) }&.dup&.prepend(" ")
formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ")
CacheStoreDatabase.use(:descriptions) do |db|
DescriptionCacheStore.new(db)

View File

@ -52,7 +52,7 @@ module Homebrew
if rack.directory?
versions = rack.subdirs.map(&:basename)
puts <<~EOS
#{keg.name} #{Utils.to_sentence(versions)} #{(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,7 +136,7 @@ module Homebrew
end
def are_required_by_deps
"#{(reqs.count == 1) ? "is" : "are"} required by #{Utils.to_sentence(deps)}, " \
"#{(reqs.count == 1) ? "is" : "are"} required by #{deps.to_sentence}, " \
"which #{(deps.count == 1) ? "is" : "are"} currently installed"
end
end
@ -145,7 +145,7 @@ module Homebrew
class DeveloperDependentsMessage < DependentsMessage
def output
opoo <<~EOS
#{Utils.to_sentence(reqs)} #{are_required_by_deps}.
#{reqs.to_sentence} #{are_required_by_deps}.
You can silence this warning with:
#{sample_command}
EOS
@ -156,7 +156,7 @@ module Homebrew
class NondeveloperDependentsMessage < DependentsMessage
def output
ofail <<~EOS
Refusing to uninstall #{Utils.to_sentence(reqs)}
Refusing to uninstall #{reqs.to_sentence}
because #{(reqs.count == 1) ? "it" : "they"} #{are_required_by_deps}.
You can override this and force removal with:
#{sample_command}

View File

@ -4,7 +4,6 @@
require "time"
require "utils/analytics"
require "utils/array"
require "utils/curl"
require "utils/fork"
require "utils/formatter"

View File

@ -1,53 +0,0 @@
# typed: strict
# frozen_string_literal: true
module Utils
extend T::Sig
# Converts the array to a comma-separated sentence where the last element is
# joined by the connector word.
#
# You can pass the following options to change the default behavior. If you
# pass an option key that doesn't exist in the list below, it will raise an
# <tt>ArgumentError</tt>.
#
# ==== Options
#
# * <tt>:words_connector</tt> - The sign or word used to join all but the last
# element in arrays with three or more elements (default: ", ").
# * <tt>:last_word_connector</tt> - The sign or word used to join the last element
# in arrays with three or more elements (default: ", and ").
# * <tt>:two_words_connector</tt> - The sign or word used to join the elements
# in arrays with two elements (default: " and ").
#
# ==== Examples
#
# [].to_sentence # => ""
# ['one'].to_sentence # => "one"
# ['one', 'two'].to_sentence # => "one and two"
# ['one', 'two', 'three'].to_sentence # => "one, two, and three"
#
# ['one', 'two'].to_sentence(two_words_connector: '-')
# # => "one-two"
#
# ['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ')
# # => "one or two or at least three"
# @see https://github.com/rails/rails/blob/v6.1.7.2/activesupport/lib/active_support/core_ext/array/conversions.rb#L10-L85
# ActiveSupport implementation
sig {
params(array: T::Array[String], words_connector: String, two_words_connector: String, last_word_connector: String)
.returns(String)
}
def self.to_sentence(array, words_connector: ", ", two_words_connector: " and ", last_word_connector: ", and ")
case array.length
when 0
+""
when 1
+(array[0]).to_s
when 2
+"#{array[0]}#{two_words_connector}#{array[1]}"
else
+"#{T.must(array[0...-1]).join(words_connector)}#{last_word_connector}#{array[-1]}"
end
end
end