mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Update Command API
This commit is contained in:
parent
91d670c3fa
commit
cae62e0175
@ -16,9 +16,6 @@ module Homebrew
|
|||||||
abstract!
|
abstract!
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
sig { returns(T.nilable(CLI::Parser)) }
|
|
||||||
attr_reader :parser
|
|
||||||
|
|
||||||
sig { returns(String) }
|
sig { returns(String) }
|
||||||
def command_name = T.must(name).split("::").fetch(-1).downcase
|
def command_name = T.must(name).split("::").fetch(-1).downcase
|
||||||
|
|
||||||
@ -26,11 +23,14 @@ module Homebrew
|
|||||||
sig { params(name: String).returns(T.nilable(T.class_of(AbstractCommand))) }
|
sig { params(name: String).returns(T.nilable(T.class_of(AbstractCommand))) }
|
||||||
def command(name) = subclasses.find { _1.command_name == name }
|
def command(name) = subclasses.find { _1.command_name == name }
|
||||||
|
|
||||||
|
sig { returns(CLI::Parser) }
|
||||||
|
def parser = CLI::Parser.new(self, &@parser_block)
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
sig { params(block: T.proc.bind(CLI::Parser).void).void }
|
sig { params(block: T.proc.bind(CLI::Parser).void).void }
|
||||||
def cmd_args(&block)
|
def cmd_args(&block)
|
||||||
@parser = T.let(CLI::Parser.new(&block), T.nilable(CLI::Parser))
|
@parser_block = T.let(block, T.nilable(T.proc.void))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -39,10 +39,7 @@ module Homebrew
|
|||||||
|
|
||||||
sig { params(argv: T::Array[String]).void }
|
sig { params(argv: T::Array[String]).void }
|
||||||
def initialize(argv = ARGV.freeze)
|
def initialize(argv = ARGV.freeze)
|
||||||
parser = self.class.parser
|
@args = T.let(self.class.parser.parse(argv), CLI::Args)
|
||||||
raise "Commands must include a `cmd_args` block" if parser.nil?
|
|
||||||
|
|
||||||
@args = T.let(parser.parse(argv), CLI::Args)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { abstract.void }
|
sig { abstract.void }
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# typed: true
|
# typed: true
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "abstract_command"
|
||||||
require "env_config"
|
require "env_config"
|
||||||
require "cask/config"
|
require "cask/config"
|
||||||
require "cli/args"
|
require "cli/args"
|
||||||
@ -19,9 +20,18 @@ module Homebrew
|
|||||||
|
|
||||||
def self.from_cmd_path(cmd_path)
|
def self.from_cmd_path(cmd_path)
|
||||||
cmd_args_method_name = Commands.args_method_name(cmd_path)
|
cmd_args_method_name = Commands.args_method_name(cmd_path)
|
||||||
|
cmd_name = cmd_args_method_name.to_s.delete_suffix("_args")
|
||||||
|
|
||||||
begin
|
begin
|
||||||
Homebrew.send(cmd_args_method_name) if require?(cmd_path)
|
if require?(cmd_path)
|
||||||
|
cmd = Homebrew::AbstractCommand.command(cmd_name)
|
||||||
|
if cmd
|
||||||
|
cmd.parser
|
||||||
|
else
|
||||||
|
# TODO: remove once all commands inherit AbstractCommand
|
||||||
|
Homebrew.send(cmd_args_method_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
rescue NoMethodError => e
|
rescue NoMethodError => e
|
||||||
raise if e.name.to_sym != cmd_args_method_name
|
raise if e.name.to_sym != cmd_args_method_name
|
||||||
|
|
||||||
@ -109,8 +119,8 @@ module Homebrew
|
|||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { params(block: T.nilable(T.proc.bind(Parser).void)).void }
|
sig { params(cmd: T.nilable(T.class_of(Homebrew::AbstractCommand)), block: T.nilable(T.proc.bind(Parser).void)).void }
|
||||||
def initialize(&block)
|
def initialize(cmd = nil, &block)
|
||||||
@parser = OptionParser.new
|
@parser = OptionParser.new
|
||||||
|
|
||||||
@parser.summary_indent = " " * 2
|
@parser.summary_indent = " " * 2
|
||||||
@ -123,12 +133,18 @@ module Homebrew
|
|||||||
|
|
||||||
@args = Homebrew::CLI::Args.new
|
@args = Homebrew::CLI::Args.new
|
||||||
|
|
||||||
# Filter out Sorbet runtime type checking method calls.
|
if cmd
|
||||||
cmd_location = T.must(caller_locations).select do |location|
|
@command_name = cmd.command_name
|
||||||
T.must(location.path).exclude?("/gems/sorbet-runtime-")
|
@is_dev_cmd = cmd.name&.start_with?("Homebrew::DevCmd")
|
||||||
end.fetch(1)
|
else
|
||||||
@command_name = T.must(cmd_location.label).chomp("_args").tr("_", "-")
|
# FIXME: remove once commands are all subclasses of `AbstractCommand`:
|
||||||
@is_dev_cmd = T.must(cmd_location.absolute_path).start_with?(Commands::HOMEBREW_DEV_CMD_PATH)
|
# Filter out Sorbet runtime type checking method calls.
|
||||||
|
cmd_location = T.must(caller_locations).select do |location|
|
||||||
|
T.must(location.path).exclude?("/gems/sorbet-runtime-")
|
||||||
|
end.fetch(1)
|
||||||
|
@command_name = T.must(cmd_location.label).chomp("_args").tr("_", "-")
|
||||||
|
@is_dev_cmd = T.must(cmd_location.absolute_path).start_with?(Commands::HOMEBREW_DEV_CMD_PATH)
|
||||||
|
end
|
||||||
|
|
||||||
@constraints = []
|
@constraints = []
|
||||||
@conflicts = []
|
@conflicts = []
|
||||||
|
@ -49,7 +49,8 @@ module Tapioca
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
root.create_path(Homebrew::CLI::Args) do |klass|
|
root.create_path(Homebrew::CLI::Args) do |klass|
|
||||||
create_args_methods(klass, T.must(T.cast(constant, T.class_of(Homebrew::AbstractCommand)).parser))
|
parser = T.cast(constant, T.class_of(Homebrew::AbstractCommand)).parser
|
||||||
|
create_args_methods(klass, parser)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user