mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Search subclasses instead of using registry
This commit is contained in:
parent
96fc8bf66b
commit
7d34717ccd
@ -1,8 +1,6 @@
|
||||
# typed: strong
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "command_registry"
|
||||
|
||||
module Homebrew
|
||||
# Subclass this to implement a `brew` command. This is preferred to declaring a named function in the `Homebrew`
|
||||
# module, because:
|
||||
@ -22,19 +20,16 @@ module Homebrew
|
||||
sig { returns(String) }
|
||||
def command_name = T.must(name).split("::").fetch(-1).downcase
|
||||
|
||||
# @return the AbstractCommand subclass associated with the brew CLI command name.
|
||||
sig { params(name: String).returns(T.nilable(T.class_of(AbstractCommand))) }
|
||||
def command(name) = subclasses.find { _1.command_name == name }
|
||||
|
||||
private
|
||||
|
||||
sig { params(block: T.nilable(T.proc.bind(CLI::Parser).void)).void }
|
||||
def cmd_args(&block)
|
||||
@parser_block = T.let(block, T.nilable(T.proc.void))
|
||||
end
|
||||
|
||||
# registers subclasses for lookup by command name
|
||||
sig { params(subclass: T.class_of(AbstractCommand)).void }
|
||||
def inherited(subclass)
|
||||
super
|
||||
CommandRegistry.register(subclass)
|
||||
end
|
||||
end
|
||||
|
||||
# @note because `Args` makes use `OpenStruct`, subclasses may need to use a tapioca compiler,
|
||||
|
@ -84,7 +84,7 @@ begin
|
||||
end
|
||||
|
||||
if internal_cmd || Commands.external_ruby_v2_cmd_path(cmd)
|
||||
cmd_class = Homebrew::CommandRegistry.command(T.must(cmd))
|
||||
cmd_class = Homebrew::AbstractCommand.command(T.must(cmd))
|
||||
if cmd_class
|
||||
cmd_class.new.run
|
||||
else
|
||||
|
@ -1,19 +0,0 @@
|
||||
# typed: strong
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Homebrew
|
||||
module CommandRegistry
|
||||
extend T::Helpers
|
||||
|
||||
Cmd = T.type_alias { T.class_of(AbstractCommand) } # rubocop:disable Style/MutableConstant
|
||||
|
||||
sig { params(subclass: Cmd).void }
|
||||
def self.register(subclass)
|
||||
@cmds ||= T.let({}, T.nilable(T::Hash[String, Cmd]))
|
||||
@cmds[subclass.command_name] = subclass
|
||||
end
|
||||
|
||||
sig { params(name: String).returns(T.nilable(Cmd)) }
|
||||
def self.command(name) = @cmds&.[](name)
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user