Merge pull request #17758 from Homebrew/shell-command-stubs

Add `ShellCommand` module for shell command stubs
This commit is contained in:
Ruoyu Zhong 2024-07-15 15:53:46 -04:00 committed by GitHub
commit 628d34b15f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 76 additions and 38 deletions

View File

@ -61,12 +61,5 @@ module Homebrew
sig { abstract.void } sig { abstract.void }
def run; end def run; end
sig { void }
def raise_sh_command_error!
raise StandardError,
"This command is just here for completions generation. " \
"It's actually defined in `cmd/#{self.class.command_name}.sh` instead."
end
end end
end end

View File

@ -2,10 +2,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module Cmd
class Repository < AbstractCommand class Repository < AbstractCommand
include ShellCommand
sig { override.returns(String) } sig { override.returns(String) }
def self.command_name = "--repository" def self.command_name = "--repository"
@ -18,9 +21,6 @@ module Homebrew
named_args :tap named_args :tap
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,10 +2,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module Cmd
class Version < AbstractCommand class Version < AbstractCommand
include ShellCommand
sig { override.returns(String) } sig { override.returns(String) }
def self.command_name = "--version" def self.command_name = "--version"
@ -15,9 +18,6 @@ module Homebrew
Homebrew/homebrew-cask (if tapped) to standard output. Homebrew/homebrew-cask (if tapped) to standard output.
EOS EOS
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,18 +2,18 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
# This Ruby command exists to allow generation of completions for the Bash # This Ruby command exists to allow generation of completions for the Bash
# version. It is not meant to be run. # version. It is not meant to be run.
module Homebrew module Homebrew
module Cmd module Cmd
class Casks < AbstractCommand class Casks < AbstractCommand
include ShellCommand
cmd_args do cmd_args do
description "List all locally installable casks including short names." description "List all locally installable casks including short names."
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,16 +2,16 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module Cmd
class Formulae < AbstractCommand class Formulae < AbstractCommand
include ShellCommand
cmd_args do cmd_args do
description "List all locally installable formulae including short names." description "List all locally installable formulae including short names."
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,10 +2,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module Cmd
class SetupRuby < AbstractCommand class SetupRuby < AbstractCommand
include ShellCommand
cmd_args do cmd_args do
description <<~EOS description <<~EOS
Installs and configures Homebrew's Ruby. If `command` is passed, it will only run Bundler if necessary for that command. Installs and configures Homebrew's Ruby. If `command` is passed, it will only run Bundler if necessary for that command.
@ -13,9 +16,6 @@ module Homebrew
named_args :command named_args :command
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,10 +2,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module Cmd
class Shellenv < AbstractCommand class Shellenv < AbstractCommand
include ShellCommand
cmd_args do cmd_args do
description <<~EOS description <<~EOS
Valid shells: bash|csh|fish|pwsh|sh|tcsh|zsh Valid shells: bash|csh|fish|pwsh|sh|tcsh|zsh
@ -21,9 +24,6 @@ module Homebrew
EOS EOS
named_args :shell named_args :shell
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,10 +2,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module Cmd
class UpdateReset < AbstractCommand class UpdateReset < AbstractCommand
include ShellCommand
cmd_args do cmd_args do
description <<~EOS description <<~EOS
Fetch and reset Homebrew and all tap repositories (or any specified <repository>) using `git`(1) to their latest `origin/HEAD`. Fetch and reset Homebrew and all tap repositories (or any specified <repository>) using `git`(1) to their latest `origin/HEAD`.
@ -15,9 +18,6 @@ module Homebrew
named_args :repository named_args :repository
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,10 +2,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module Cmd
class Update < AbstractCommand class Update < AbstractCommand
include ShellCommand
cmd_args do cmd_args do
description <<~EOS description <<~EOS
Fetch the newest version of Homebrew and all formulae from GitHub using `git`(1) and perform any necessary migrations. Fetch the newest version of Homebrew and all formulae from GitHub using `git`(1) and perform any necessary migrations.
@ -23,9 +26,6 @@ module Homebrew
switch "-d", "--debug", switch "-d", "--debug",
description: "Display a trace of all shell commands as they are executed." description: "Display a trace of all shell commands as they are executed."
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,10 +2,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module Cmd
class VendorInstall < AbstractCommand class VendorInstall < AbstractCommand
include ShellCommand
cmd_args do cmd_args do
description <<~EOS description <<~EOS
Install Homebrew's portable Ruby. Install Homebrew's portable Ruby.
@ -15,9 +18,6 @@ module Homebrew
hide_from_man_page! hide_from_man_page!
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -2,18 +2,18 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command" require "abstract_command"
require "shell_command"
module Homebrew module Homebrew
module Cmd module DevCmd
class Rubocop < AbstractCommand class Rubocop < AbstractCommand
include ShellCommand
cmd_args do cmd_args do
description <<~EOS description <<~EOS
Installs, configures and runs Homebrew's `rubocop`. Installs, configures and runs Homebrew's `rubocop`.
EOS EOS
end end
sig { override.void }
def run = raise_sh_command_error!
end end
end end
end end

View File

@ -11,6 +11,7 @@ require_relative "negate_include"
require_relative "presence" require_relative "presence"
require_relative "present" require_relative "present"
require_relative "safe_navigation_with_blank" require_relative "safe_navigation_with_blank"
require_relative "shell_command_stub"
require_relative "shell_commands" require_relative "shell_commands"
require_relative "install_bundler_gems" require_relative "install_bundler_gems"

View File

@ -0,0 +1,24 @@
# typed: strict
# frozen_string_literal: true
module RuboCop
module Cop
module Homebrew
class ShellCommandStub < Base
MSG = "Shell command stubs must have a `.sh` counterpart."
RESTRICT_ON_SEND = [:include].freeze
sig { params(node: AST::SendNode).void }
def on_send(node)
return if node.first_argument&.const_name != "ShellCommand"
stub_path = Pathname.new(processed_source.file_path)
sh_cmd_path = Pathname.new("#{stub_path.dirname}/#{stub_path.basename(".rb")}.sh")
return if sh_cmd_path.exist?
add_offense(node)
end
end
end
end
end

View File

@ -0,0 +1,20 @@
# typed: strict
# frozen_string_literal: true
module Homebrew
module ShellCommand
extend T::Helpers
requires_ancestor { AbstractCommand }
sig { void }
def run
T.bind(self, AbstractCommand)
sh_cmd_path = "#{self.class.dev_cmd? ? "dev-cmd" : "cmd"}/#{self.class.command_name}.sh"
raise StandardError,
"This command is just here for completions generation. " \
"It's actually defined in `#{sh_cmd_path}` instead."
end
end
end