From afaa48bd17775900b553f096d61741a3d0846b33 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Thu, 21 Mar 2024 10:39:08 -0700 Subject: [PATCH] Port Homebrew::DevCmd::Irb --- .../Homebrew/dev-cmd/install-bundler-gems.rb | 1 + Library/Homebrew/dev-cmd/irb.rb | 136 +++++++++--------- Library/Homebrew/test/dev-cmd/irb_spec.rb | 3 +- 3 files changed, 73 insertions(+), 67 deletions(-) diff --git a/Library/Homebrew/dev-cmd/install-bundler-gems.rb b/Library/Homebrew/dev-cmd/install-bundler-gems.rb index 3769be2fdf..6ac110609d 100644 --- a/Library/Homebrew/dev-cmd/install-bundler-gems.rb +++ b/Library/Homebrew/dev-cmd/install-bundler-gems.rb @@ -1,6 +1,7 @@ # typed: strict # frozen_string_literal: true +require "abstract_command" require "cli/parser" module Homebrew diff --git a/Library/Homebrew/dev-cmd/irb.rb b/Library/Homebrew/dev-cmd/irb.rb index 59b52cb667..9da9a012fa 100644 --- a/Library/Homebrew/dev-cmd/irb.rb +++ b/Library/Homebrew/dev-cmd/irb.rb @@ -1,6 +1,7 @@ # typed: true # frozen_string_literal: true +require "abstract_command" require "formulary" require "cask/cask_loader" require "cli/parser" @@ -27,73 +28,76 @@ class Symbol end module Homebrew - module_function + module DevCmd + class Irb < AbstractCommand + cmd_args do + description <<~EOS + Enter the interactive Homebrew Ruby shell. + EOS + switch "--examples", + description: "Show several examples." + switch "--pry", + env: :pry, + description: "Use Pry instead of IRB. Implied if `HOMEBREW_PRY` is set." + end - sig { returns(CLI::Parser) } - def irb_args - Homebrew::CLI::Parser.new do - description <<~EOS - Enter the interactive Homebrew Ruby shell. - EOS - switch "--examples", - description: "Show several examples." - switch "--pry", - env: :pry, - description: "Use Pry instead of IRB. Implied if `HOMEBREW_PRY` is set." + # work around IRB modifying ARGV. + sig { params(argv: T.nilable(T::Array[String])).void } + def initialize(argv = nil) = super(argv || ARGV.dup.freeze) + + sig { override.void } + def run + clean_argv + + if args.examples? + puts <<~EOS + 'v8'.f # => instance of the v8 formula + :hub.f.latest_version_installed? + :lua.f.methods - 1.methods + :mpd.f.recursive_dependencies.reject(&:installed?) + + 'vlc'.c # => instance of the vlc cask + :tsh.c.livecheckable? + EOS + return + end + + if args.pry? + Homebrew.install_bundler_gems!(groups: ["pry"]) + require "pry" + else + require "irb" + end + + require "formula" + require "keg" + require "cask" + + ohai "Interactive Homebrew Shell", "Example commands available with: `brew irb --examples`" + if args.pry? + Pry.config.should_load_rc = false # skip loading .pryrc + Pry.config.history_file = "#{Dir.home}/.brew_pry_history" + Pry.config.memory_size = 100 # max lines to save to history file + Pry.config.prompt_name = "brew" + + Pry.start + else + ENV["IRBRC"] = (HOMEBREW_LIBRARY_PATH/"brew_irbrc").to_s + + IRB.start + end + end + + private + + # Remove the `--debug`, `--verbose` and `--quiet` options which cause problems + # for IRB and have already been parsed by the CLI::Parser. + def clean_argv + global_options = Homebrew::CLI::Parser + .global_options + .flat_map { |options| options[0..1] } + ARGV.reject! { |arg| global_options.include?(arg) } + end end end - - def irb - # work around IRB modifying ARGV. - args = irb_args.parse(ARGV.dup.freeze) - - clean_argv - - if args.examples? - puts <<~EOS - 'v8'.f # => instance of the v8 formula - :hub.f.latest_version_installed? - :lua.f.methods - 1.methods - :mpd.f.recursive_dependencies.reject(&:installed?) - - 'vlc'.c # => instance of the vlc cask - :tsh.c.livecheckable? - EOS - return - end - - if args.pry? - Homebrew.install_bundler_gems!(groups: ["pry"]) - require "pry" - else - require "irb" - end - - require "formula" - require "keg" - require "cask" - - ohai "Interactive Homebrew Shell", "Example commands available with: `brew irb --examples`" - if args.pry? - Pry.config.should_load_rc = false # skip loading .pryrc - Pry.config.history_file = "#{Dir.home}/.brew_pry_history" - Pry.config.memory_size = 100 # max lines to save to history file - Pry.config.prompt_name = "brew" - - Pry.start - else - ENV["IRBRC"] = (HOMEBREW_LIBRARY_PATH/"brew_irbrc").to_s - - IRB.start - end - end - - # Remove the `--debug`, `--verbose` and `--quiet` options which cause problems - # for IRB and have already been parsed by the CLI::Parser. - def clean_argv - global_options = Homebrew::CLI::Parser - .global_options - .flat_map { |options| options[0..1] } - ARGV.reject! { |arg| global_options.include?(arg) } - end end diff --git a/Library/Homebrew/test/dev-cmd/irb_spec.rb b/Library/Homebrew/test/dev-cmd/irb_spec.rb index 5ede76a455..4ba95bc123 100644 --- a/Library/Homebrew/test/dev-cmd/irb_spec.rb +++ b/Library/Homebrew/test/dev-cmd/irb_spec.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true require "cmd/shared_examples/args_parse" +require "dev-cmd/irb" -RSpec.describe "brew irb" do +RSpec.describe Homebrew::DevCmd::Irb do it_behaves_like "parseable arguments" describe "integration test" do