Create AbstractCommand class
This commit is contained in:
parent
a3e5e3f7a0
commit
cbcb221de6
42
Library/Homebrew/abstract_command.rb
Normal file
42
Library/Homebrew/abstract_command.rb
Normal file
@ -0,0 +1,42 @@
|
||||
# typed: strong
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Homebrew
|
||||
class AbstractCommand
|
||||
extend T::Helpers
|
||||
|
||||
abstract!
|
||||
|
||||
class << self
|
||||
# registers subclasses for lookup by command name
|
||||
sig { params(subclass: T.class_of(AbstractCommand)).void }
|
||||
def inherited(subclass)
|
||||
super
|
||||
@cmds ||= T.let({}, T.nilable(T::Hash[String, T.class_of(AbstractCommand)]))
|
||||
@cmds[subclass.command_name] = subclass
|
||||
end
|
||||
|
||||
sig { params(name: String).returns(T.nilable(T.class_of(AbstractCommand))) }
|
||||
def command(name) = @cmds&.[](name)
|
||||
|
||||
sig { returns(String) }
|
||||
def command_name = T.must(name).split("::").fetch(-1).downcase
|
||||
end
|
||||
|
||||
# @note because `Args` makes use `OpenStruct`, subclasses may need to use a tapioca compiler,
|
||||
# hash accessors, args.rbi, or other means to make this work with legacy commands:
|
||||
sig { returns(Homebrew::CLI::Args) }
|
||||
attr_reader :args
|
||||
|
||||
sig { void }
|
||||
def initialize
|
||||
@args = T.let(raw_args.parse, Homebrew::CLI::Args)
|
||||
end
|
||||
|
||||
sig { abstract.returns(CLI::Parser) }
|
||||
def raw_args; end
|
||||
|
||||
sig { abstract.void }
|
||||
def run; end
|
||||
end
|
||||
end
|
||||
@ -59,6 +59,7 @@ begin
|
||||
|
||||
ENV["PATH"] = path.to_s
|
||||
|
||||
require "abstract_command"
|
||||
require "commands"
|
||||
require "settings"
|
||||
|
||||
@ -83,7 +84,12 @@ begin
|
||||
end
|
||||
|
||||
if internal_cmd || Commands.external_ruby_v2_cmd_path(cmd)
|
||||
Homebrew.send Commands.method_name(cmd)
|
||||
cmd_class = Homebrew::AbstractCommand.command(T.must(cmd))
|
||||
if cmd_class
|
||||
cmd_class.new.run
|
||||
else
|
||||
Homebrew.public_send Commands.method_name(cmd)
|
||||
end
|
||||
elsif (path = Commands.external_ruby_cmd_path(cmd))
|
||||
require?(path)
|
||||
exit Homebrew.failed? ? 1 : 0
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
# typed: true
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_command"
|
||||
require "metafiles"
|
||||
require "formula"
|
||||
require "cli/parser"
|
||||
@ -8,10 +9,12 @@ require "cask/list"
|
||||
require "system_command"
|
||||
|
||||
module Homebrew
|
||||
extend SystemCommand::Mixin
|
||||
module Cmd
|
||||
class List < AbstractCommand
|
||||
include SystemCommand::Mixin
|
||||
|
||||
sig { returns(CLI::Parser) }
|
||||
def self.list_args
|
||||
sig { override.returns(CLI::Parser) }
|
||||
def raw_args
|
||||
Homebrew::CLI::Parser.new do
|
||||
description <<~EOS
|
||||
List all installed formulae and casks.
|
||||
@ -43,8 +46,8 @@ module Homebrew
|
||||
description: "List formulae and/or casks in long format. " \
|
||||
"Has no effect when a formula or cask name is passed as an argument."
|
||||
switch "-r",
|
||||
description: "Reverse the order of the formulae and/or casks sort to list the oldest entries first. " \
|
||||
"Has no effect when a formula or cask name is passed as an argument."
|
||||
description: "Reverse the order of the formulae and/or casks sort to list the oldest entries " \
|
||||
"first. Has no effect when a formula or cask name is passed as an argument."
|
||||
switch "-t",
|
||||
description: "Sort formulae and/or casks by time modified, listing most recently modified first. " \
|
||||
"Has no effect when a formula or cask name is passed as an argument."
|
||||
@ -65,9 +68,8 @@ module Homebrew
|
||||
end
|
||||
end
|
||||
|
||||
def self.list
|
||||
args = list_args.parse
|
||||
|
||||
sig { override.void }
|
||||
def run
|
||||
if args.full_name?
|
||||
unless args.cask?
|
||||
formula_names = args.no_named? ? Formula.installed : args.named.to_resolved_formulae
|
||||
@ -81,23 +83,25 @@ module Homebrew
|
||||
else
|
||||
args.named.to_formulae_and_casks(only: :cask, method: :resolve)
|
||||
end
|
||||
full_cask_names = cask_names.map(&:full_name).sort(&tap_and_name_comparison)
|
||||
# The cast is because `Keg`` does not define `full_name`
|
||||
full_cask_names = T.cast(cask_names, T::Array[T.any(Formula, Cask::Cask)])
|
||||
.map(&:full_name).sort(&tap_and_name_comparison)
|
||||
full_cask_names = Formatter.columns(full_cask_names) unless args.public_send(:"1?")
|
||||
puts full_cask_names if full_cask_names.present?
|
||||
end
|
||||
elsif args.pinned?
|
||||
elsif args[:pinned?]
|
||||
filtered_list(args:)
|
||||
elsif args.versions?
|
||||
elsif args[:versions?]
|
||||
filtered_list(args:) unless args.cask?
|
||||
list_casks(args:) if args.cask? || (!args.formula? && !args.multiple? && args.no_named?)
|
||||
list_casks(args:) if args.cask? || (!args.formula? && !args[:multiple?] && args.no_named?)
|
||||
elsif args.no_named?
|
||||
ENV["CLICOLOR"] = nil
|
||||
|
||||
ls_args = []
|
||||
ls_args << "-1" if args.public_send(:"1?")
|
||||
ls_args << "-l" if args.l?
|
||||
ls_args << "-r" if args.r?
|
||||
ls_args << "-t" if args.t?
|
||||
ls_args << "-1" if args[:"1?"]
|
||||
ls_args << "-l" if args[:l?]
|
||||
ls_args << "-r" if args[:r?]
|
||||
ls_args << "-t" if args[:t?]
|
||||
|
||||
if !args.cask? && HOMEBREW_CELLAR.exist? && HOMEBREW_CELLAR.children.any?
|
||||
ohai "Formulae" if $stdout.tty? && !args.formula?
|
||||
@ -122,7 +126,9 @@ module Homebrew
|
||||
end
|
||||
end
|
||||
|
||||
def self.filtered_list(args:)
|
||||
private
|
||||
|
||||
def filtered_list(args:)
|
||||
names = if args.no_named?
|
||||
Formula.racks
|
||||
else
|
||||
@ -151,7 +157,7 @@ module Homebrew
|
||||
end
|
||||
end
|
||||
|
||||
def self.list_casks(args:)
|
||||
def list_casks(args:)
|
||||
casks = if args.no_named?
|
||||
Cask::Caskroom.casks
|
||||
else
|
||||
@ -231,3 +237,5 @@ class PrettyListing
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "cmd/shared_examples/args_parse"
|
||||
require "cmd/list"
|
||||
|
||||
RSpec.describe "brew list" do
|
||||
RSpec.describe Homebrew::Cmd::List do
|
||||
let(:formulae) { %w[bar foo qux] }
|
||||
|
||||
it_behaves_like "parseable arguments"
|
||||
|
||||
it "prints all installed Formulae", :integration_test do
|
||||
formulae.each do |f|
|
||||
(HOMEBREW_CELLAR/f/"1.0/somedir").mkpath
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user