diff --git a/Library/Homebrew/cmd/tap-info.rb b/Library/Homebrew/cmd/tap-info.rb index b2d9ee5088..0f63082a10 100644 --- a/Library/Homebrew/cmd/tap-info.rb +++ b/Library/Homebrew/cmd/tap-info.rb @@ -62,13 +62,11 @@ module Homebrew if tap.installed? info += tap.pinned? ? "pinned" : "unpinned" info += ", private" if tap.private? - if (formula_count = tap.formula_files.size).positive? - info += ", #{Formatter.pluralize(formula_count, "formula")}" + info += if (contents = tap.contents).empty? + ", no commands/casks/formulae" + else + ", #{contents.join(", ")}" end - if (command_count = tap.command_files.size).positive? - info += ", #{Formatter.pluralize(command_count, "command")}" - end - info += ", no formulae/commands" if (formula_count + command_count).zero? info += "\n#{tap.path} (#{tap.path.abv})" info += "\nFrom: #{tap.remote.nil? ? "N/A" : tap.remote}" else diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index a6f7c4044f..5f7d691bff 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -284,8 +284,8 @@ class Tap link_completions_and_manpages - formula_count = formula_files.size - puts "Tapped #{Formatter.pluralize(formula_count, "formula")} (#{path.abv})" unless quiet + formatted_contents = Formatter.comma_and(*contents)&.prepend(" ") + puts "Tapped#{formatted_contents} (#{path.abv})." unless quiet Descriptions.cache_formulae(formula_names) return if options[:clone_target] @@ -311,15 +311,18 @@ class Tap require "descriptions" raise TapUnavailableError, name unless installed? - puts "Untapping #{name}... (#{path.abv})" + puts "Untapping #{name}..." + + abv = path.abv + formatted_contents = Formatter.comma_and(*contents)&.prepend(" ") + unpin if pinned? - formula_count = formula_files.size Descriptions.uncache_formulae(formula_names) Utils::Link.unlink_manpages(path) Utils::Link.unlink_completions(path) path.rmtree path.parent.rmdir_if_possible - puts "Untapped #{Formatter.pluralize(formula_count, "formula")}" + puts "Untapped#{formatted_contents} (#{abv})." clear_cache end @@ -343,6 +346,24 @@ class Tap @cask_dir ||= path/"Casks" end + def contents + contents = [] + + if (command_count = command_files.count).positive? + contents << Formatter.pluralize(command_count, "command") + end + + if (cask_count = cask_files.count).positive? + contents << Formatter.pluralize(cask_count, "cask") + end + + if (formula_count = formula_files.count).positive? + contents << Formatter.pluralize(formula_count, "formula") + end + + contents + end + # an array of all {Formula} files of this {Tap}. def formula_files @formula_files ||= if formula_dir.directory? @@ -427,7 +448,8 @@ class Tap # an array of all commands files of this {Tap}. def command_files - @command_files ||= Pathname.glob("#{path}/cmd/brew-*").select(&:executable?) + @command_files ||= Pathname.glob("#{path}/cmd/brew{,cask}-*") + .select { |file| file.executable? || file.extname == ".rb" } end # path to the pin record for this {Tap}. diff --git a/Library/Homebrew/test/formatter_spec.rb b/Library/Homebrew/test/formatter_spec.rb index 14c63b8c3a..1c97bf6f8f 100644 --- a/Library/Homebrew/test/formatter_spec.rb +++ b/Library/Homebrew/test/formatter_spec.rb @@ -76,4 +76,22 @@ describe Formatter do expect(described_class.pluralize(2, "new formula")).to eq("2 new formulae") end end + + describe "::comma_and" do + it "returns nil if given no arguments" do + expect(described_class.comma_and).to be nil + end + + it "returns the input as string if there is only one argument" do + expect(described_class.comma_and(1)).to eq("1") + end + + it "concatenates two items with “and”" do + expect(described_class.comma_and(1, 2)).to eq("1 and 2") + end + + it "concatenates all items with a comma and appends the last with “and”" do + expect(described_class.comma_and(1, 2, 3)).to eq("1, 2 and 3") + end + end end diff --git a/Library/Homebrew/utils/formatter.rb b/Library/Homebrew/utils/formatter.rb index ec144bf2f8..36542d7389 100644 --- a/Library/Homebrew/utils/formatter.rb +++ b/Library/Homebrew/utils/formatter.rb @@ -108,4 +108,14 @@ module Formatter show_count ? "#{count} #{words}" : words end + + def comma_and(*items) + # TODO: Remove when RuboCop 0.57.3 is released. + # False positive has been fixed and merged, but is not yet in a + # stable release: https://github.com/rubocop-hq/rubocop/pull/6038 + *items, last = items.map(&:to_s) # rubocop:disable Lint/ShadowedArgument + return last if items.empty? + + "#{items.join(", ")} and #{last}" + end end