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,122 +1,128 @@
# 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
description <<~EOS
Display Homebrew's download cache. See also `HOMEBREW_CACHE`.
If <formula> is provided, display the file or directory used to cache <formula>. cmd_args do
EOS description <<~EOS
flag "--os=", Display Homebrew's download cache. See also `HOMEBREW_CACHE`.
description: "Show cache file for the given operating system. " \
"(Pass `all` to show cache files for all operating systems.)"
flag "--arch=",
description: "Show cache file for the given CPU architecture. " \
"(Pass `all` to show cache files for all architectures.)"
switch "-s", "--build-from-source",
description: "Show the cache file used when building from source."
switch "--force-bottle",
description: "Show the cache file used when pouring a bottle."
flag "--bottle-tag=",
description: "Show the cache file used when pouring a bottle for the given tag."
switch "--HEAD",
description: "Show the cache file used when building from HEAD."
switch "--formula", "--formulae",
description: "Only show cache files for formulae."
switch "--cask", "--casks",
description: "Only show cache files for casks."
conflicts "--build-from-source", "--force-bottle", "--bottle-tag", "--HEAD", "--cask" If <formula> is provided, display the file or directory used to cache <formula>.
conflicts "--formula", "--cask" EOS
conflicts "--os", "--bottle-tag" flag "--os=",
conflicts "--arch", "--bottle-tag" description: "Show cache file for the given operating system. " \
"(Pass `all` to show cache files for all operating systems.)"
flag "--arch=",
description: "Show cache file for the given CPU architecture. " \
"(Pass `all` to show cache files for all architectures.)"
switch "-s", "--build-from-source",
description: "Show the cache file used when building from source."
switch "--force-bottle",
description: "Show the cache file used when pouring a bottle."
flag "--bottle-tag=",
description: "Show the cache file used when pouring a bottle for the given tag."
switch "--HEAD",
description: "Show the cache file used when building from HEAD."
switch "--formula", "--formulae",
description: "Only show cache files for formulae."
switch "--cask", "--casks",
description: "Only show cache files for casks."
named_args [:formula, :cask] conflicts "--build-from-source", "--force-bottle", "--bottle-tag", "--HEAD", "--cask"
end conflicts "--formula", "--cask"
end conflicts "--os", "--bottle-tag"
conflicts "--arch", "--bottle-tag"
sig { void } named_args [:formula, :cask]
def self.__cache end
args = __cache_args.parse
if args.no_named? sig { override.void }
puts HOMEBREW_CACHE def run
return if args.no_named?
end puts HOMEBREW_CACHE
return
formulae_or_casks = args.named.to_formulae_and_casks
os_arch_combinations = args.os_arch_combinations
formulae_or_casks.each do |formula_or_cask|
case formula_or_cask
when Formula
formula = formula_or_cask
ref = formula.loaded_from_api? ? formula.full_name : formula.path
os_arch_combinations.each do |os, arch|
SimulateSystem.with(os:, arch:) do
formula = Formulary.factory(ref)
print_formula_cache(formula, os:, arch:, args:)
end
end end
else
cask = formula_or_cask
ref = cask.loaded_from_api? ? cask.full_name : cask.sourcefile_path
os_arch_combinations.each do |os, arch| formulae_or_casks = args.named.to_formulae_and_casks
next if os == :linux os_arch_combinations = args.os_arch_combinations
SimulateSystem.with(os:, arch:) do formulae_or_casks.each do |formula_or_cask|
cask = Cask::CaskLoader.load(ref) case formula_or_cask
print_cask_cache(cask) when Formula
formula = formula_or_cask
ref = formula.loaded_from_api? ? formula.full_name : formula.path
os_arch_combinations.each do |os, arch|
SimulateSystem.with(os:, arch:) do
formula = Formulary.factory(ref)
print_formula_cache(formula, os:, arch:)
end
end
when Cask::Cask
cask = formula_or_cask
ref = cask.loaded_from_api? ? cask.full_name : cask.sourcefile_path
os_arch_combinations.each do |os, arch|
next if os == :linux
SimulateSystem.with(os:, arch:) do
loaded_cask = Cask::CaskLoader.load(ref)
print_cask_cache(loaded_cask)
end
end
else
raise "Invalid type: #{formula_or_cask.class}"
end end
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:)
if fetch_bottle?( sig { params(formula: Formula, os: Symbol, arch: Symbol).void }
formula, def print_formula_cache(formula, os:, arch:)
force_bottle: args.force_bottle?, if fetch_bottle?(
bottle_tag: args.bottle_tag&.to_sym, formula,
build_from_source_formulae: args.build_from_source_formulae, force_bottle: args.force_bottle?,
os: args.os&.to_sym, bottle_tag: args.bottle_tag&.to_sym,
arch: args.arch&.to_sym, build_from_source_formulae: args.build_from_source_formulae,
) os: args.os&.to_sym,
bottle_tag = if (bottle_tag = args.bottle_tag&.to_sym) arch: args.arch&.to_sym,
Utils::Bottles::Tag.from_symbol(bottle_tag) )
else bottle_tag = if (bottle_tag = args.bottle_tag&.to_sym)
Utils::Bottles::Tag.new(system: os, arch:) Utils::Bottles::Tag.from_symbol(bottle_tag)
else
Utils::Bottles::Tag.new(system: os, arch:)
end
bottle = formula.bottle_for_tag(bottle_tag)
if bottle.nil?
opoo "Bottle for tag #{bottle_tag.to_sym.inspect} is unavailable."
return
end
puts bottle.cached_download
elsif args.HEAD?
puts T.must(formula.head).cached_download
else
puts formula.cached_download
end
end end
bottle = formula.bottle_for_tag(bottle_tag) sig { params(cask: Cask::Cask).void }
def print_cask_cache(cask)
if bottle.nil? puts Cask::Download.new(cask).downloader.cached_location
opoo "Bottle for tag #{bottle_tag.to_sym.inspect} is unavailable."
return
end end
puts bottle.cached_download
elsif args.HEAD?
puts T.must(formula.head).cached_download
else
puts formula.cached_download
end end
end end
sig { params(cask: Cask::Cask).void }
def self.print_cask_cache(cask)
puts Cask::Download.new(cask).downloader.cached_location
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