Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some dev commands to use AbstractCommand #16921

Merged
merged 14 commits into from
Mar 20, 2024
15 changes: 6 additions & 9 deletions Library/Homebrew/abstract_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ class AbstractCommand
abstract!

class << self
sig { returns(T.nilable(CLI::Parser)) }
attr_reader :parser

sig { returns(String) }
def command_name = T.must(name).split("::").fetch(-1).downcase
def command_name = Utils.underscore(T.must(name).split("::").fetch(-1)).tr("_", "-")

# @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 }

sig { returns(CLI::Parser) }
def parser = CLI::Parser.new(self, &@parser_block)

private

sig { params(block: T.proc.bind(CLI::Parser).void).void }
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

Expand All @@ -39,10 +39,7 @@ def cmd_args(&block)

sig { params(argv: T::Array[String]).void }
def initialize(argv = ARGV.freeze)
parser = self.class.parser
raise "Commands must include a `cmd_args` block" if parser.nil?

@args = T.let(parser.parse(argv), CLI::Args)
@args = T.let(self.class.parser.parse(argv), CLI::Args)
end

sig { abstract.void }
Expand Down
36 changes: 27 additions & 9 deletions Library/Homebrew/cli/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "abstract_command"
require "env_config"
require "cask/config"
require "cli/args"
Expand All @@ -19,9 +20,18 @@ class Parser

def self.from_cmd_path(cmd_path)
cmd_args_method_name = Commands.args_method_name(cmd_path)
cmd_name = cmd_args_method_name.to_s.delete_suffix("_args").tr("_", "-")

begin
Homebrew.send(cmd_args_method_name) if require?(cmd_path)
if require?(cmd_path)
cmd = Homebrew::AbstractCommand.command(cmd_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be done in another PR: I think it would make sense to move AbstractCommand to Homebrew::CLI::AbstractCommand.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no strong feelings on their either way.

if cmd
cmd.parser
else
# FIXME: remove once commands are all subclasses of `AbstractCommand`:
Homebrew.send(cmd_args_method_name)
end
end
rescue NoMethodError => e
raise if e.name.to_sym != cmd_args_method_name

Expand Down Expand Up @@ -109,8 +119,10 @@ def self.global_options
]
end

sig { params(block: T.nilable(T.proc.bind(Parser).void)).void }
def initialize(&block)
sig {
params(cmd: T.nilable(T.class_of(Homebrew::AbstractCommand)), block: T.nilable(T.proc.bind(Parser).void)).void
}
def initialize(cmd = nil, &block)
@parser = OptionParser.new

@parser.summary_indent = " " * 2
Expand All @@ -123,12 +135,18 @@ def initialize(&block)

@args = Homebrew::CLI::Args.new

# 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)
if cmd
@command_name = cmd.command_name
@is_dev_cmd = cmd.name&.start_with?("Homebrew::DevCmd")
else
# FIXME: remove once commands are all subclasses of `AbstractCommand`:
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an especially nice change, IMO. No more inspecting the call stack to figure out the command!

# 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 = []
@conflicts = []
Expand Down