From 7bfa5527020fc345eb69e67f5528b47e27889721 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sun, 21 Apr 2024 16:15:57 -0700 Subject: [PATCH] Individually namespace args for each command --- Library/Homebrew/abstract_command.rb | 4 ++++ Library/Homebrew/cask/config.rb | 1 + Library/Homebrew/cli/args.rbi | 21 +++++++++++++++++++ Library/Homebrew/cli/parser.rb | 2 +- Library/Homebrew/dev-cmd/typecheck.rb | 3 ++- .../Homebrew/sorbet/tapioca/compilers/args.rb | 10 ++++++--- 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/abstract_command.rb b/Library/Homebrew/abstract_command.rb index c82eef77bd..9d11837cc1 100644 --- a/Library/Homebrew/abstract_command.rb +++ b/Library/Homebrew/abstract_command.rb @@ -19,6 +19,9 @@ module Homebrew 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") @@ -37,6 +40,7 @@ module Homebrew 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 diff --git a/Library/Homebrew/cask/config.rb b/Library/Homebrew/cask/config.rb index 37854d8ec5..5b69ddf79a 100644 --- a/Library/Homebrew/cask/config.rb +++ b/Library/Homebrew/cask/config.rb @@ -38,6 +38,7 @@ module Cask 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, diff --git a/Library/Homebrew/cli/args.rbi b/Library/Homebrew/cli/args.rbi index ad016c67da..11451fdb55 100644 --- a/Library/Homebrew/cli/args.rbi +++ b/Library/Homebrew/cli/args.rbi @@ -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 diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 72498a75fb..cbcd5d763c 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -152,7 +152,7 @@ module Homebrew # Disable default handling of `--help` switch. @parser.base.long.delete("help") - @args = T.let(Homebrew::CLI::Args.new, Homebrew::CLI::Args) + @args = T.let((cmd&.args_class || Args).new, Args) if cmd @command_name = T.let(cmd.command_name, String) diff --git a/Library/Homebrew/dev-cmd/typecheck.rb b/Library/Homebrew/dev-cmd/typecheck.rb index 71fc1f914e..46fa968a0a 100644 --- a/Library/Homebrew/dev-cmd/typecheck.rb +++ b/Library/Homebrew/dev-cmd/typecheck.rb @@ -48,7 +48,8 @@ module Homebrew HOMEBREW_LIBRARY_PATH.cd do if update - safe_system "bundle", "exec", "tapioca", "dsl" + workers = args.debug? ? ["--workers=1"] : [] + safe_system "bundle", "exec", "tapioca", "dsl", *workers # Prefer adding args here: Library/Homebrew/sorbet/tapioca/config.yml tapioca_args = args.update_all? ? ["--all"] : [] diff --git a/Library/Homebrew/sorbet/tapioca/compilers/args.rb b/Library/Homebrew/sorbet/tapioca/compilers/args.rb index d0644d70ab..474eeaac21 100644 --- a/Library/Homebrew/sorbet/tapioca/compilers/args.rb +++ b/Library/Homebrew/sorbet/tapioca/compilers/args.rb @@ -38,9 +38,13 @@ module Tapioca 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