Port Homebrew::Cmd::Cache

This commit is contained in:
Douglas Eichelberger 2024-04-01 15:44:50 -07:00
parent 6d716a7d69
commit 7bbf0a3206
2 changed files with 104 additions and 97 deletions

View File

@ -1,16 +1,19 @@
# typed: strict
# frozen_string_literal: true
require "abstract_command"
require "fetch"
require "cli/parser"
require "cask/download"
module Homebrew
extend Fetch
module Cmd
class Cache < AbstractCommand
include Fetch
sig { returns(CLI::Parser) }
def self.__cache_args
Homebrew::CLI::Parser.new do
sig { override.returns(String) }
def self.command_name = "--cache"
cmd_args do
description <<~EOS
Display Homebrew's download cache. See also `HOMEBREW_CACHE`.
@ -42,12 +45,9 @@ module Homebrew
named_args [:formula, :cask]
end
end
sig { void }
def self.__cache
args = __cache_args.parse
sig { override.void }
def run
if args.no_named?
puts HOMEBREW_CACHE
return
@ -65,10 +65,10 @@ module Homebrew
os_arch_combinations.each do |os, arch|
SimulateSystem.with(os:, arch:) do
formula = Formulary.factory(ref)
print_formula_cache(formula, os:, arch:, args:)
print_formula_cache(formula, os:, arch:)
end
end
else
when Cask::Cask
cask = formula_or_cask
ref = cask.loaded_from_api? ? cask.full_name : cask.sourcefile_path
@ -76,16 +76,20 @@ module Homebrew
next if os == :linux
SimulateSystem.with(os:, arch:) do
cask = Cask::CaskLoader.load(ref)
print_cask_cache(cask)
loaded_cask = Cask::CaskLoader.load(ref)
print_cask_cache(loaded_cask)
end
end
else
raise "Invalid type: #{formula_or_cask.class}"
end
end
end
sig { params(formula: Formula, os: Symbol, arch: Symbol, args: CLI::Args).void }
def self.print_formula_cache(formula, os:, arch:, args:)
private
sig { params(formula: Formula, os: Symbol, arch: Symbol).void }
def print_formula_cache(formula, os:, arch:)
if fetch_bottle?(
formula,
force_bottle: args.force_bottle?,
@ -116,7 +120,9 @@ module Homebrew
end
sig { params(cask: Cask::Cask).void }
def self.print_cask_cache(cask)
def print_cask_cache(cask)
puts Cask::Download.new(cask).downloader.cached_location
end
end
end
end

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true
require "cmd/--cache"
require "cmd/shared_examples/args_parse"
RSpec.describe "brew --cache" do
RSpec.describe Homebrew::Cmd::Cache do
it_behaves_like "parseable arguments"
it "prints all cache files for a given Formula", :integration_test do