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

View File

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