Skip to content

Commit

Permalink
Individually namespace args for each command
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Apr 21, 2024
1 parent 54bea63 commit 101f72f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Library/Homebrew/abstract_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class AbstractCommand
abstract!

class << self
sig { returns(T.nilable(T.class_of(CLI::Args))) }
attr_reader :args_class

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

Expand All @@ -30,13 +33,14 @@ def command(name) = subclasses.find { _1.command_name == name }
def dev_cmd? = T.must(name).start_with?("Homebrew::DevCmd")

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

private

sig { params(block: T.proc.bind(CLI::Parser).void).void }
def cmd_args(&block)
@parser_block = T.let(block, T.nilable(T.proc.void))
@args_class = T.let(const_set(:Args, Class.new(CLI::Args)), T.nilable(T.class_of(CLI::Args)))
end
end

Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/cask/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def self.defaults

sig { params(args: Homebrew::CLI::Args).returns(T.attached_class) }
def self.from_args(args)
args = T.unsafe(args)
new(explicit: {
appdir: args.appdir,
keyboard_layoutdir: args.keyboard_layoutdir,
Expand Down
21 changes: 21 additions & 0 deletions Library/Homebrew/cli/args.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,25 @@ class Homebrew::CLI::Args

sig { returns(T::Boolean) }
def verbose?; end

# FIXME: The methods below are not defined by Args, but are valid because Args inherits from OpenStruct
# We should instead be using type guards to check if the method is defined on the object before calling it

sig { returns(T.nilable(String)) }
def arch; end

sig { returns(T::Boolean) }
def build_from_source?; end

sig { returns(T::Boolean) }
def cask?; end

sig { returns(T::Boolean) }
def formula?; end

sig { returns(T::Boolean) }
def include_test?; end

sig { returns(T.nilable(String)) }
def os; end
end
10 changes: 7 additions & 3 deletions Library/Homebrew/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,21 @@ def self.option_to_name(option)
end

sig {
params(cmd: T.nilable(T.class_of(Homebrew::AbstractCommand)), block: T.nilable(T.proc.bind(Parser).void)).void
params(
cmd: T.nilable(T.class_of(AbstractCommand)),
args_class: T.nilable(T.class_of(Args)),
block: T.nilable(T.proc.bind(Parser).void),
).void
}
def initialize(cmd = nil, &block)
def initialize(cmd = nil, args_class = nil, &block)
@parser = T.let(OptionParser.new, OptionParser)
@parser.summary_indent = " "
# Disable default handling of `--version` switch.
@parser.base.long.delete("version")
# Disable default handling of `--help` switch.
@parser.base.long.delete("help")

@args = T.let(Homebrew::CLI::Args.new, Homebrew::CLI::Args)
@args = T.let((args_class || Args).new, Args)

if cmd
@command_name = T.let(cmd.command_name, String)
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/dev-cmd/typecheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def run

HOMEBREW_LIBRARY_PATH.cd do
if update
# FIXME: append "--workers=1" in debug mode
safe_system "bundle", "exec", "tapioca", "dsl"
# Prefer adding args here: Library/Homebrew/sorbet/tapioca/config.yml
tapioca_args = args.update_all? ? ["--all"] : []
Expand Down
10 changes: 7 additions & 3 deletions Library/Homebrew/sorbet/tapioca/compilers/args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ def decorate
end
end
else
root.create_path(Homebrew::CLI::Args) do |klass|
parser = T.cast(constant, T.class_of(Homebrew::AbstractCommand)).parser
create_args_methods(klass, parser)
cmd = T.cast(constant, T.class_of(Homebrew::AbstractCommand))
args_class_name = T.must(T.must(cmd.args_class).name)
root.create_class(args_class_name, superclass_name: "Homebrew::CLI::Args") do |klass|
create_args_methods(klass, cmd.parser)
end
root.create_path(constant) do |klass|
klass.create_method("args", return_type: args_class_name)
end
end
end
Expand Down

0 comments on commit 101f72f

Please sign in to comment.