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

View File

@ -1,10 +1,9 @@
# frozen_string_literal: true
require "cmd/info"
require "cmd/shared_examples/args_parse"
RSpec.describe "brew info" do
RSpec.describe Homebrew::Cmd::Info do
it_behaves_like "parseable arguments"
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
end
describe Homebrew do
describe "::github_remote_path" do
let(:remote) { "https://github.com/Homebrew/homebrew-core" }
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")
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")
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")
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")
end
end
end
end