96 lines
3.0 KiB
Ruby
Raw 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
2019-04-17 18:25:08 +09:00
require "cli/parser"
require "commands"
module Homebrew
2020-08-17 18:31:37 +02:00
# Helper module for printing help output.
module Help
def self.help(cmd = nil, empty_argv: false, usage_error: nil, remaining_args: [])
2020-07-30 18:40:10 +02:00
if cmd.nil?
# Handle `brew` (no arguments).
if empty_argv
$stderr.puts HOMEBREW_HELP_MESSAGE
2020-07-30 18:40:10 +02:00
exit 1
end
# Handle `brew (-h|--help|--usage|-?|help)` (no other arguments).
puts HOMEBREW_HELP_MESSAGE
2020-07-30 18:40:10 +02:00
exit 0
end
# Resolve command aliases and find file containing the implementation.
2020-07-30 18:40:10 +02:00
path = Commands.path(cmd)
# Display command-specific (or generic) help in response to `UsageError`.
2020-07-30 18:40:10 +02:00
if usage_error
$stderr.puts path ? command_help(cmd, path, remaining_args:) : HOMEBREW_HELP_MESSAGE
$stderr.puts
2020-07-30 18:40:10 +02:00
onoe usage_error
exit 1
end
# Resume execution in `brew.rb` for unknown commands.
return if path.nil?
# Display help for internal command (or generic help if undocumented).
2024-03-07 16:20:20 +00:00
puts command_help(cmd, path, remaining_args:)
exit 0
end
def self.command_help(cmd, path, remaining_args:)
# Only some types of commands can have a parser.
output = if Commands.valid_internal_cmd?(cmd) ||
Commands.valid_internal_dev_cmd?(cmd) ||
Commands.external_ruby_v2_cmd_path(cmd)
2024-03-07 16:20:20 +00:00
parser_help(path, remaining_args:)
end
output ||= comment_help(path)
output ||= if output.blank?
2020-04-05 15:44:50 +01:00
opoo "No help text in: #{path}" if Homebrew::EnvConfig.developer?
HOMEBREW_HELP_MESSAGE
end
output
end
2020-08-17 18:31:37 +02:00
private_class_method :command_help
def self.parser_help(path, remaining_args:)
# Let OptionParser generate help text for commands which have a parser.
cmd_parser = CLI::Parser.from_cmd_path(path)
return unless cmd_parser
# Try parsing arguments here in order to show formula options in help output.
2020-08-02 15:36:05 +02:00
cmd_parser.parse(remaining_args, ignore_invalid_options: true)
cmd_parser.generate_help_text
end
2020-08-17 18:31:37 +02:00
private_class_method :parser_help
def self.command_help_lines(path)
path.read
.lines
.grep(/^#:/)
.map { |line| line.slice(2..-1).delete_prefix(" ") }
end
private_class_method :command_help_lines
def self.comment_help(path)
# Otherwise read #: lines from the file.
help_lines = command_help_lines(path)
return if help_lines.blank?
2024-04-03 20:17:02 -07:00
Formatter.format_help_text(help_lines.join, width: Formatter::COMMAND_DESC_WIDTH)
.sub("@hide_from_man_page ", "")
.sub(/^\* /, "#{Tty.bold}Usage: brew#{Tty.reset} ")
.gsub(/`(.*?)`/m, "#{Tty.bold}\\1#{Tty.reset}")
.gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) }
.gsub(/<(.*?)>/m, "#{Tty.underline}\\1#{Tty.reset}")
.gsub(/\*(.*?)\*/m, "#{Tty.underline}\\1#{Tty.reset}")
end
2020-08-17 18:31:37 +02:00
private_class_method :comment_help
end
end