Skip to content

Commit

Permalink
Various improvements for brew command
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
MikeMcQuaid and carlocab committed Apr 30, 2024
1 parent c9c8806 commit 4ffcd8a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 21 deletions.
5 changes: 5 additions & 0 deletions Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

#####
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require "commands"

module Homebrew
module DevCmd
module Cmd
class Command < AbstractCommand
cmd_args do
description <<~EOS
Expand Down
46 changes: 46 additions & 0 deletions Library/Homebrew/command_path.sh
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/command.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/command.rbi

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions docs/Manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions manpages/brew.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4ffcd8a

Please sign in to comment.