Port Homebrew::Cmd::Info

This commit is contained in:
Douglas Eichelberger 2024-03-29 18:39:39 -07:00
parent 74218c0483
commit d6a6742a4d
2 changed files with 340 additions and 346 deletions

View File

@ -1,9 +1,9 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "missing_formula" require "missing_formula"
require "caveats" require "caveats"
require "cli/parser"
require "options" require "options"
require "formula" require "formula"
require "keg" require "keg"
@ -14,15 +14,13 @@ require "deprecate_disable"
require "api" require "api"
module Homebrew module Homebrew
module_function module Cmd
class Info < AbstractCommand
VALID_DAYS = %w[30 90 365].freeze VALID_DAYS = %w[30 90 365].freeze
VALID_FORMULA_CATEGORIES = %w[install install-on-request build-error].freeze VALID_FORMULA_CATEGORIES = %w[install install-on-request build-error].freeze
VALID_CATEGORIES = (VALID_FORMULA_CATEGORIES + %w[cask-install os-version]).freeze VALID_CATEGORIES = (VALID_FORMULA_CATEGORIES + %w[cask-install os-version]).freeze
sig { returns(CLI::Parser) } cmd_args do
def info_args
Homebrew::CLI::Parser.new do
description <<~EOS description <<~EOS
Display brief statistics for your Homebrew installation. Display brief statistics for your Homebrew installation.
If a <formula> or <cask> is provided, show summary of information about it. If a <formula> or <cask> is provided, show summary of information about it.
@ -74,12 +72,9 @@ module Homebrew
named_args [:formula, :cask] named_args [:formula, :cask]
end end
end
sig { void }
def info
args = info_args.parse
sig { override.void }
def run
if args.analytics? if args.analytics?
if args.days.present? && VALID_DAYS.exclude?(args.days) if args.days.present? && VALID_DAYS.exclude?(args.days)
raise UsageError, "`--days` must be one of #{VALID_DAYS.join(", ")}." raise UsageError, "`--days` must be one of #{VALID_DAYS.join(", ")}."
@ -96,11 +91,11 @@ module Homebrew
end end
end end
print_analytics(args:) print_analytics
elsif args.json elsif args.json
all = args.eval_all? all = args.eval_all?
print_json(all, args:) print_json(all)
elsif args.github? elsif args.github?
raise FormulaOrCaskUnspecifiedError if args.no_named? raise FormulaOrCaskUnspecifiedError if args.no_named?
@ -108,7 +103,7 @@ module Homebrew
elsif args.no_named? elsif args.no_named?
print_statistics print_statistics
else else
print_info(args:) print_info
end end
end end
@ -120,8 +115,8 @@ module Homebrew
puts "#{Utils.pluralize("keg", count, include_count: true)}, #{HOMEBREW_CELLAR.dup.abv}" puts "#{Utils.pluralize("keg", count, include_count: true)}, #{HOMEBREW_CELLAR.dup.abv}"
end end
sig { params(args: CLI::Args).void } sig { void }
def print_analytics(args:) def print_analytics
if args.no_named? if args.no_named?
Utils::Analytics.output(args:) Utils::Analytics.output(args:)
return return
@ -143,16 +138,16 @@ module Homebrew
end end
end end
sig { params(args: CLI::Args).void } sig { void }
def print_info(args:) def print_info
args.named.to_formulae_and_casks_and_unavailable.each_with_index do |obj, i| args.named.to_formulae_and_casks_and_unavailable.each_with_index do |obj, i|
puts unless i.zero? puts unless i.zero?
case obj case obj
when Formula when Formula
info_formula(obj, args:) info_formula(obj)
when Cask::Cask when Cask::Cask
info_cask(obj, args:) info_cask(obj)
when FormulaUnreadableError, FormulaClassUnavailableError, when FormulaUnreadableError, FormulaClassUnavailableError,
TapFormulaUnreadableError, TapFormulaClassUnavailableError, TapFormulaUnreadableError, TapFormulaClassUnavailableError,
Cask::CaskUnreadableError Cask::CaskUnreadableError
@ -184,8 +179,8 @@ module Homebrew
version_hash[version] version_hash[version]
end end
sig { params(all: T::Boolean, args: T.untyped).void } sig { params(all: T::Boolean).void }
def print_json(all, args:) def print_json(all)
raise FormulaOrCaskUnspecifiedError if !(all || args.installed?) && args.no_named? raise FormulaOrCaskUnspecifiedError if !(all || args.installed?) && args.no_named?
json = case json_version(args.json) json = case json_version(args.json)
@ -262,7 +257,7 @@ module Homebrew
github_remote_path(formula_or_cask.tap.remote, path) github_remote_path(formula_or_cask.tap.remote, path)
end end
def info_formula(formula, args:) def info_formula(formula)
specs = [] specs = []
if (stable = formula.stable) if (stable = formula.stable)
@ -369,9 +364,11 @@ module Homebrew
"#{dep.name} #{dep.option_tags.map { |o| "--#{o}" }.join(" ")}" "#{dep.name} #{dep.option_tags.map { |o| "--#{o}" }.join(" ")}"
end end
def info_cask(cask, args:) def info_cask(cask)
require "cask/info" require "cask/info"
Cask::Info.info(cask) Cask::Info.info(cask)
end end
end
end
end end

View File

@ -1,10 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/info" require "cmd/info"
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
RSpec.describe "brew info" do RSpec.describe Homebrew::Cmd::Info do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
it "prints as json with the --json=v1 flag", :integration_test do it "prints as json with the --json=v1 flag", :integration_test do
@ -25,23 +24,21 @@ RSpec.describe "brew info" do
.and be_a_success .and be_a_success
end end
describe Homebrew do
describe "::github_remote_path" do describe "::github_remote_path" do
let(:remote) { "https://github.com/Homebrew/homebrew-core" } let(:remote) { "https://github.com/Homebrew/homebrew-core" }
specify "returns correct URLs" do specify "returns correct URLs" do
expect(described_class.github_remote_path(remote, "Formula/git.rb")) expect(described_class.new([]).github_remote_path(remote, "Formula/git.rb"))
.to eq("https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/git.rb") .to eq("https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/git.rb")
expect(described_class.github_remote_path("#{remote}.git", "Formula/git.rb")) expect(described_class.new([]).github_remote_path("#{remote}.git", "Formula/git.rb"))
.to eq("https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/git.rb") .to eq("https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/git.rb")
expect(described_class.github_remote_path("git@github.com:user/repo", "foo.rb")) expect(described_class.new([]).github_remote_path("git@github.com:user/repo", "foo.rb"))
.to eq("https://github.com/user/repo/blob/HEAD/foo.rb") .to eq("https://github.com/user/repo/blob/HEAD/foo.rb")
expect(described_class.github_remote_path("https://mywebsite.com", "foo/bar.rb")) expect(described_class.new([]).github_remote_path("https://mywebsite.com", "foo/bar.rb"))
.to eq("https://mywebsite.com/foo/bar.rb") .to eq("https://mywebsite.com/foo/bar.rb")
end end
end end
end
end end