Use a command registry
This commit is contained in:
parent
cbcb221de6
commit
5364b6ef03
@ -1,6 +1,8 @@
|
||||
# typed: strong
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "command_registry"
|
||||
|
||||
module Homebrew
|
||||
class AbstractCommand
|
||||
extend T::Helpers
|
||||
@ -12,13 +14,9 @@ module Homebrew
|
||||
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
|
||||
CommandRegistry.register(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
|
||||
|
||||
@ -84,7 +84,7 @@ begin
|
||||
end
|
||||
|
||||
if internal_cmd || Commands.external_ruby_v2_cmd_path(cmd)
|
||||
cmd_class = Homebrew::AbstractCommand.command(T.must(cmd))
|
||||
cmd_class = Homebrew::CommandRegistry.command(T.must(cmd))
|
||||
if cmd_class
|
||||
cmd_class.new.run
|
||||
else
|
||||
|
||||
19
Library/Homebrew/command_registry.rb
Normal file
19
Library/Homebrew/command_registry.rb
Normal file
@ -0,0 +1,19 @@
|
||||
# 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