From 4ffcd8a11063dca402524b005dc9c9f2c145466e Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Tue, 30 Apr 2024 11:09:42 +0100 Subject: [PATCH] Various improvements for `brew command` - Add a (large) speedup by moving some logic to Bash for the typical case of a normal or dev-cmd, Bash or Ruby command. - Make `brew command` a non-developer command, I don't think it makes sense to consider it something needed for developing Homebrew. - Update the manpage/tests/RBI accordingly. Co-authored-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> --- Library/Homebrew/brew.sh | 5 ++ Library/Homebrew/{dev-cmd => cmd}/command.rb | 2 +- Library/Homebrew/command_path.sh | 46 +++++++++++++++++++ .../sorbet/rbi/dsl/homebrew/cmd/command.rbi | 12 +++++ .../rbi/dsl/homebrew/dev_cmd/command.rbi | 12 ----- .../test/{dev-cmd => cmd}/command_spec.rb | 4 +- docs/Manpage.md | 8 ++-- manpages/brew.1 | 4 +- 8 files changed, 72 insertions(+), 21 deletions(-) rename Library/Homebrew/{dev-cmd => cmd}/command.rb (97%) create mode 100644 Library/Homebrew/command_path.sh create mode 100644 Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/command.rbi delete mode 100644 Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/command.rbi rename Library/Homebrew/test/{dev-cmd => cmd}/command_spec.rb (82%) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 90a5a3d333062..44bc124540e59 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -120,6 +120,11 @@ case "$*" in source "${HOMEBREW_LIBRARY}/Homebrew/formula_path.sh" homebrew-formula-path "$@" && exit 0 ;; + # falls back to cmd/command.rb on a non-zero return + command*) + source "${HOMEBREW_LIBRARY}/Homebrew/command_path.sh" + homebrew-command-path "$@" && exit 0 + ;; esac ##### diff --git a/Library/Homebrew/dev-cmd/command.rb b/Library/Homebrew/cmd/command.rb similarity index 97% rename from Library/Homebrew/dev-cmd/command.rb rename to Library/Homebrew/cmd/command.rb index 595e00e4b8601..4f89970fc1c47 100644 --- a/Library/Homebrew/dev-cmd/command.rb +++ b/Library/Homebrew/cmd/command.rb @@ -5,7 +5,7 @@ require "commands" module Homebrew - module DevCmd + module Cmd class Command < AbstractCommand cmd_args do description <<~EOS diff --git a/Library/Homebrew/command_path.sh b/Library/Homebrew/command_path.sh new file mode 100644 index 0000000000000..b3288a7adca90 --- /dev/null +++ b/Library/Homebrew/command_path.sh @@ -0,0 +1,46 @@ +# does the quickest output of brew command possible for the basic cases of an +# official Bash or Ruby normal or dev-cmd command. +# HOMEBREW_LIBRARY is set by brew.sh +# shellcheck disable=SC2154 +homebrew-command-path() { + case "$1" in + # check we actually have command and not e.g. commandsomething + command) ;; + *) return 1 ;; + esac + + local first_command found_command + for arg in "$@" + do + if [[ -z "${first_command}" && "${arg}" == "command" ]] + then + first_command=1 + continue + elif [[ -f "${HOMEBREW_LIBRARY}/Homebrew/cmd/${arg}.sh" ]] + then + echo "${HOMEBREW_LIBRARY}/Homebrew/cmd/${arg}.sh" + found_command=1 + elif [[ -f "${HOMEBREW_LIBRARY}/Homebrew/dev-cmd/${arg}.sh" ]] + then + echo "${HOMEBREW_LIBRARY}/Homebrew/dev-cmd/${arg}.sh" + found_command=1 + elif [[ -f "${HOMEBREW_LIBRARY}/Homebrew/cmd/${arg}.rb" ]] + then + echo "${HOMEBREW_LIBRARY}/Homebrew/cmd/${arg}.rb" + found_command=1 + elif [[ -f "${HOMEBREW_LIBRARY}/Homebrew/dev-cmd/${arg}.rb" ]] + then + echo "${HOMEBREW_LIBRARY}/Homebrew/dev-cmd/${arg}.rb" + found_command=1 + else + return 1 + fi + done + + if [[ -n "${found_command}" ]] + then + return 0 + else + return 1 + fi +} diff --git a/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/command.rbi b/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/command.rbi new file mode 100644 index 0000000000000..a4579cf33fb1f --- /dev/null +++ b/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/command.rbi @@ -0,0 +1,12 @@ +# typed: true + +# DO NOT EDIT MANUALLY +# This is an autogenerated file for dynamic methods in `Homebrew::Cmd::Command`. +# Please instead update this file by running `bin/tapioca dsl Homebrew::Cmd::Command`. + +class Homebrew::Cmd::Command + sig { returns(Homebrew::Cmd::Command::Args) } + def args; end +end + +class Homebrew::Cmd::Command::Args < Homebrew::CLI::Args; end diff --git a/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/command.rbi b/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/command.rbi deleted file mode 100644 index ce831184e6a98..0000000000000 --- a/Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/command.rbi +++ /dev/null @@ -1,12 +0,0 @@ -# typed: true - -# DO NOT EDIT MANUALLY -# This is an autogenerated file for dynamic methods in `Homebrew::DevCmd::Command`. -# Please instead update this file by running `bin/tapioca dsl Homebrew::DevCmd::Command`. - -class Homebrew::DevCmd::Command - sig { returns(Homebrew::DevCmd::Command::Args) } - def args; end -end - -class Homebrew::DevCmd::Command::Args < Homebrew::CLI::Args; end diff --git a/Library/Homebrew/test/dev-cmd/command_spec.rb b/Library/Homebrew/test/cmd/command_spec.rb similarity index 82% rename from Library/Homebrew/test/dev-cmd/command_spec.rb rename to Library/Homebrew/test/cmd/command_spec.rb index d5751e5dd4505..c756c3792a103 100644 --- a/Library/Homebrew/test/dev-cmd/command_spec.rb +++ b/Library/Homebrew/test/cmd/command_spec.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true require "cmd/shared_examples/args_parse" -require "dev-cmd/command" +require "cmd/command" -RSpec.describe Homebrew::DevCmd::Command do +RSpec.describe Homebrew::Cmd::Command do it_behaves_like "parseable arguments" it "returns the file for a given command", :integration_test do diff --git a/docs/Manpage.md b/docs/Manpage.md index c39a488633bb8..7556081cc01cc 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -153,6 +153,10 @@ old. This can be adjusted with `HOMEBREW_CLEANUP_MAX_AGE_DAYS`. : Only prune the symlinks and directories from the prefix and remove no other files. +### `command` *`command`* \[...\] + +Display the path to the file being used when invoking `brew` *`cmd`*. + ### `commands` \[`--quiet`\] \[`--include-aliases`\] Show lists of built-in and external commands. @@ -1985,10 +1989,6 @@ Display the source of a *`formula`* or *`cask`*. : Treat all named arguments as casks. -### `command` *`command`* \[...\] - -Display the path to the file being used when invoking `brew` *`cmd`*. - ### `contributions` \[--user=*`email|username`*\] \[*`--repositories`*`=`\] \[*`--csv`*\] Summarise contributions to Homebrew repositories. diff --git a/manpages/brew.1 b/manpages/brew.1 index 5aa403933a551..626cd2d6fbf77 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -93,6 +93,8 @@ Scrub the cache, including downloads for even the latest versions\. Note that do .TP \fB\-\-prune\-prefix\fP Only prune the symlinks and directories from the prefix and remove no other files\. +.SS "\fBcommand\fP \fIcommand\fP \fR[\.\.\.]" +Display the path to the file being used when invoking \fBbrew\fP \fIcmd\fP\&\. .SS "\fBcommands\fP \fR[\fB\-\-quiet\fP] \fR[\fB\-\-include\-aliases\fP]" Show lists of built\-in and external commands\. .TP @@ -1259,8 +1261,6 @@ Treat all named arguments as formulae\. .TP \fB\-\-cask\fP Treat all named arguments as casks\. -.SS "\fBcommand\fP \fIcommand\fP \fR[\.\.\.]" -Display the path to the file being used when invoking \fBbrew\fP \fIcmd\fP\&\. .SS "\fBcontributions\fP \fR[\-\-user=\fIemail|username\fP] \fR[\fI\-\-repositories\fP\fB=\fP] \fR[\fI\-\-csv\fP]" Summarise contributions to Homebrew repositories\. .TP