Skip to content

Commit

Permalink
Merge pull request #16998 from Homebrew/ported-cmds
Browse files Browse the repository at this point in the history
Port remaining commands to use AbstractCommand
  • Loading branch information
MikeMcQuaid authored Apr 2, 2024
2 parents bf4039e + 3f856f6 commit 0b98c50
Show file tree
Hide file tree
Showing 61 changed files with 2,418 additions and 2,354 deletions.
208 changes: 107 additions & 101 deletions Library/Homebrew/cmd/--cache.rb
Original file line number Diff line number Diff line change
@@ -1,122 +1,128 @@
# typed: strict
# frozen_string_literal: true

require "abstract_command"
require "fetch"
require "cli/parser"
require "cask/download"

module Homebrew
extend Fetch

sig { returns(CLI::Parser) }
def self.__cache_args
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>.
EOS
flag "--os=",
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"
conflicts "--formula", "--cask"
conflicts "--os", "--bottle-tag"
conflicts "--arch", "--bottle-tag"

named_args [:formula, :cask]
end
end

sig { void }
def self.__cache
args = __cache_args.parse

if args.no_named?
puts HOMEBREW_CACHE
return
end

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
module Cmd
class Cache < AbstractCommand
include Fetch

sig { override.returns(String) }
def self.command_name = "--cache"

cmd_args 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>.
EOS
flag "--os=",
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"
conflicts "--formula", "--cask"
conflicts "--os", "--bottle-tag"
conflicts "--arch", "--bottle-tag"

named_args [:formula, :cask]
end

os_arch_combinations.each do |os, arch|
SimulateSystem.with(os:, arch:) do
formula = Formulary.factory(ref)
print_formula_cache(formula, os:, arch:, args:)
end
sig { override.void }
def run
if args.no_named?
puts HOMEBREW_CACHE
return
end
else
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
cask = Cask::CaskLoader.load(ref)
print_cask_cache(cask)
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:)
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

sig { params(formula: Formula, os: Symbol, arch: Symbol, args: CLI::Args).void }
def self.print_formula_cache(formula, os:, arch:, args:)
if fetch_bottle?(
formula,
force_bottle: args.force_bottle?,
bottle_tag: args.bottle_tag&.to_sym,
build_from_source_formulae: args.build_from_source_formulae,
os: args.os&.to_sym,
arch: args.arch&.to_sym,
)
bottle_tag = if (bottle_tag = args.bottle_tag&.to_sym)
Utils::Bottles::Tag.from_symbol(bottle_tag)
else
Utils::Bottles::Tag.new(system: os, arch:)
end
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?,
bottle_tag: args.bottle_tag&.to_sym,
build_from_source_formulae: args.build_from_source_formulae,
os: args.os&.to_sym,
arch: args.arch&.to_sym,
)
bottle_tag = if (bottle_tag = args.bottle_tag&.to_sym)
Utils::Bottles::Tag.from_symbol(bottle_tag)
else
Utils::Bottles::Tag.new(system: os, arch:)
end

bottle = formula.bottle_for_tag(bottle_tag)

bottle = formula.bottle_for_tag(bottle_tag)
if bottle.nil?
opoo "Bottle for tag #{bottle_tag.to_sym.inspect} is unavailable."
return
end

if bottle.nil?
opoo "Bottle for tag #{bottle_tag.to_sym.inspect} is unavailable."
return
puts bottle.cached_download
elsif args.HEAD?
puts T.must(formula.head).cached_download
else
puts formula.cached_download
end
end

puts bottle.cached_download
elsif args.HEAD?
puts T.must(formula.head).cached_download
else
puts formula.cached_download
sig { params(cask: Cask::Cask).void }
def print_cask_cache(cask)
puts Cask::Download.new(cask).downloader.cached_location
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
44 changes: 23 additions & 21 deletions Library/Homebrew/cmd/--caskroom.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# typed: strict
# frozen_string_literal: true

require "abstract_command"

module Homebrew
module_function
module Cmd
class Caskroom < AbstractCommand
sig { override.returns(String) }
def self.command_name = "--caskroom"

sig { returns(CLI::Parser) }
def __caskroom_args
Homebrew::CLI::Parser.new do
description <<~EOS
Display Homebrew's Caskroom path.
cmd_args do
description <<~EOS
Display Homebrew's Caskroom path.
If <cask> is provided, display the location in the Caskroom where <cask>
would be installed, without any sort of versioned directory as the last path.
EOS
If <cask> is provided, display the location in the Caskroom where <cask>
would be installed, without any sort of versioned directory as the last path.
EOS

named_args :cask
end
end

sig { void }
def __caskroom
args = __caskroom_args.parse
named_args :cask
end

if args.named.to_casks.blank?
puts Cask::Caskroom.path
else
args.named.to_casks.each do |cask|
puts "#{Cask::Caskroom.path}/#{cask.token}"
sig { override.void }
def run
if args.named.to_casks.blank?
puts Cask::Caskroom.path
else
args.named.to_casks.each do |cask|
puts "#{Cask::Caskroom.path}/#{cask.token}"
end
end
end
end
end
Expand Down
44 changes: 23 additions & 21 deletions Library/Homebrew/cmd/--cellar.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "cli/parser"
require "abstract_command"

module Homebrew
module_function
module Cmd
class Cellar < AbstractCommand
sig { override.returns(String) }
def self.command_name = "--cellar"

def __cellar_args
Homebrew::CLI::Parser.new do
description <<~EOS
Display Homebrew's Cellar path. *Default:* `$(brew --prefix)/Cellar`, or if
that directory doesn't exist, `$(brew --repository)/Cellar`.
cmd_args do
description <<~EOS
Display Homebrew's Cellar path. *Default:* `$(brew --prefix)/Cellar`, or if
that directory doesn't exist, `$(brew --repository)/Cellar`.
If <formula> is provided, display the location in the Cellar where <formula>
would be installed, without any sort of versioned directory as the last path.
EOS
If <formula> is provided, display the location in the Cellar where <formula>
would be installed, without any sort of versioned directory as the last path.
EOS

named_args :formula
end
end

def __cellar
args = __cellar_args.parse
named_args :formula
end

if args.no_named?
puts HOMEBREW_CELLAR
else
puts args.named.to_resolved_formulae.map(&:rack)
sig { override.void }
def run
if args.no_named?
puts HOMEBREW_CELLAR
else
puts args.named.to_resolved_formulae.map(&:rack)
end
end
end
end
end
Loading

0 comments on commit 0b98c50

Please sign in to comment.