brew/Library/Homebrew/debrew.rb

137 lines
3.2 KiB
Ruby
Raw Permalink Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2021-04-13 17:39:59 +01:00
require "ignorable"
2020-08-14 03:45:47 +02:00
# Helper module for debugging formulae.
module Debrew
2020-08-14 03:45:47 +02:00
# Module for allowing to debug formulae.
module Formula
def install
Debrew.debrew { super }
end
2014-09-18 14:16:07 -05:00
def patch
Debrew.debrew { super }
end
2014-09-18 14:16:07 -05:00
def test
Debrew.debrew { super }
end
end
2013-02-07 18:58:41 -06:00
2020-08-14 03:45:47 +02:00
# Module for displaying a debugging menu.
class Menu
Entry = Struct.new(:name, :action)
attr_accessor :prompt, :entries
2020-10-20 12:03:48 +02:00
sig { void }
def initialize
@entries = []
end
def choice(name, &action)
entries << Entry.new(name.to_s, action)
end
def self.choose
menu = new
yield menu
2023-03-31 08:34:33 -07:00
choice = T.let(nil, T.nilable(Entry))
while choice.nil?
2017-06-01 16:06:51 +02:00
menu.entries.each_with_index { |e, i| puts "#{i + 1}. #{e.name}" }
print menu.prompt unless menu.prompt.nil?
input = $stdin.gets || exit
input.chomp!
i = input.to_i
2017-09-24 20:12:58 +01:00
if i.positive?
2017-06-01 16:06:51 +02:00
choice = menu.entries[i - 1]
else
possible = menu.entries.select { |e| e.name.start_with?(input) }
case possible.size
when 0 then puts "No such option"
when 1 then choice = possible.first
else puts "Multiple options match: #{possible.map(&:name).join(" ")}"
end
end
end
choice[:action].call
end
end
2025-04-15 23:02:00 +01:00
@mutex = nil
@debugged_exceptions = Set.new
class << self
attr_reader :debugged_exceptions
2025-02-24 14:49:33 -08:00
sig { returns(T::Boolean) }
2025-04-15 23:02:00 +01:00
def active? = !@mutex.nil?
end
def self.debrew
2025-04-15 23:02:00 +01:00
@mutex = Mutex.new
2021-04-13 17:39:59 +01:00
Ignorable.hook_raise
begin
yield
rescue SystemExit
2021-04-13 17:39:59 +01:00
raise
2023-03-31 08:34:33 -07:00
rescue Ignorable::ExceptionMixin => e
e.ignore if debug(e) == :ignore # execution jumps back to where the exception was thrown
ensure
2021-04-13 17:39:59 +01:00
Ignorable.unhook_raise
2025-04-15 23:02:00 +01:00
@mutex = nil
end
end
def self.debug(exception)
2025-04-15 23:02:00 +01:00
raise(exception) if !active? || !debugged_exceptions.add?(exception) || !@mutex.try_lock
begin
2023-04-11 21:12:13 +01:00
puts exception.backtrace.first
puts Formatter.error(exception, label: exception.class.name)
loop do
Menu.choose do |menu|
menu.prompt = "Choose an action: "
2023-03-14 11:09:57 +01:00
menu.choice(:raise) { raise(exception) }
menu.choice(:ignore) { return :ignore } if exception.is_a?(Ignorable::ExceptionMixin)
menu.choice(:backtrace) { puts exception.backtrace }
if exception.is_a?(Ignorable::ExceptionMixin)
2016-09-20 22:03:08 +02:00
menu.choice(:irb) do
puts "When you exit this IRB session, execution will continue."
2018-07-01 01:43:04 +02:00
set_trace_func proc { |event, _, _, id, binding, klass|
2021-04-13 17:39:59 +01:00
if klass == Object && id == :raise && event == "return"
2016-09-20 22:03:08 +02:00
set_trace_func(nil)
2025-04-15 23:02:00 +01:00
@mutex.synchronize do
require "debrew/irb"
IRB.start_within(binding)
end
2016-09-20 22:03:08 +02:00
end
}
return :ignore
end
end
menu.choice(:shell) do
puts "When you exit this shell, you will return to the menu."
interactive_shell
end
end
end
ensure
2025-04-15 23:02:00 +01:00
@mutex.unlock
end
end
end