Merge pull request #17188 from Homebrew/more_command_optimisation

Optimise more command handling/speed
This commit is contained in:
Mike McQuaid 2024-04-30 15:32:46 +01:00 committed by GitHub
commit bfb8bb7bb1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 80 additions and 14 deletions

View File

@ -82,7 +82,9 @@ HOMEBREW_TEMP="${HOMEBREW_TEMP:-${HOMEBREW_DEFAULT_TEMP}}"
# Don't need to handle a default case. # Don't need to handle a default case.
# HOMEBREW_LIBRARY set by bin/brew # HOMEBREW_LIBRARY set by bin/brew
# shellcheck disable=SC2249,SC2154 # shellcheck disable=SC2249,SC2154
case "$*" in #
# commands that take a single or no arguments.
case "$1" in
--cellar) --cellar)
echo "${HOMEBREW_CELLAR}" echo "${HOMEBREW_CELLAR}"
exit 0 exit 0
@ -99,12 +101,6 @@ case "$*" in
echo "${HOMEBREW_CACHE}" echo "${HOMEBREW_CACHE}"
exit 0 exit 0
;; ;;
shellenv)
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"
shift
homebrew-shellenv "$1"
exit 0
;;
formulae) formulae)
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/formulae.sh" source "${HOMEBREW_LIBRARY}/Homebrew/cmd/formulae.sh"
homebrew-formulae homebrew-formulae
@ -115,6 +111,21 @@ case "$*" in
homebrew-casks homebrew-casks
exit 0 exit 0
;; ;;
shellenv)
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"
shift
homebrew-shellenv "$1"
exit 0
;;
setup-ruby)
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/setup-ruby.sh"
shift
homebrew-setup-ruby "$1"
exit 0
;;
esac
# functions that take multiple arguments or handle multiple commands.
case "$@" in
# falls back to cmd/--prefix.rb and cmd/--cellar.rb on a non-zero return # falls back to cmd/--prefix.rb and cmd/--cellar.rb on a non-zero return
--prefix* | --cellar*) --prefix* | --cellar*)
source "${HOMEBREW_LIBRARY}/Homebrew/formula_path.sh" source "${HOMEBREW_LIBRARY}/Homebrew/formula_path.sh"
@ -497,7 +508,8 @@ HOMEBREW_CORE_REPOSITORY="${HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-core"
# shellcheck disable=SC2034 # shellcheck disable=SC2034
HOMEBREW_CASK_REPOSITORY="${HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-cask" HOMEBREW_CASK_REPOSITORY="${HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-cask"
case "$*" in # commands that take a single or no arguments.
case "$1" in
--version | -v) --version | -v)
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/--version.sh" source "${HOMEBREW_LIBRARY}/Homebrew/cmd/--version.sh"
homebrew-version homebrew-version

View File

@ -1,6 +1,8 @@
#: * `setup-ruby` #: * `setup-ruby [command]`
#: #:
#: Installs and configures Homebrew's Ruby. #: Installs and configures Homebrew's Ruby.
#: If `command` is passed, it will only run Bundler if necessary for that
#: command.
#: #:
# HOMEBREW_LIBRARY is from the user environment. # HOMEBREW_LIBRARY is from the user environment.
@ -12,11 +14,29 @@ homebrew-setup-ruby() {
source "${HOMEBREW_LIBRARY}/Homebrew/utils/ruby.sh" source "${HOMEBREW_LIBRARY}/Homebrew/utils/ruby.sh"
setup-ruby-path setup-ruby-path
if [[ -z "${HOMEBREW_DEVELOPER}" && -z "${HOMEBREW_RUBY3}" ]] if [[ -z "${HOMEBREW_DEVELOPER}" ]]
then then
return return
fi fi
# Avoid running Bundler if the command doesn't need it.
local command="$1"
if [[ -n "${command}" ]]
then
source "${HOMEBREW_LIBRARY}/Homebrew/command_path.sh"
command_path="$(homebrew-command-path "${command}")"
if [[ -n "${command_path}" && "${command_path}" != *"/dev-cmd/"* ]]
then
return
fi
if ! grep -q "Homebrew.install_bundler_gems\!" "${command_path}"
then
return
fi
fi
GEM_VERSION="$("${HOMEBREW_RUBY_PATH}" "${HOMEBREW_RUBY_DISABLE_OPTIONS}" /dev/stdin <<<'require "rbconfig"; puts RbConfig::CONFIG["ruby_version"]')" GEM_VERSION="$("${HOMEBREW_RUBY_PATH}" "${HOMEBREW_RUBY_DISABLE_OPTIONS}" /dev/stdin <<<'require "rbconfig"; puts RbConfig::CONFIG["ruby_version"]')"
echo "${GEM_VERSION}" echo "${GEM_VERSION}"
GEM_HOME="${HOMEBREW_LIBRARY}/Homebrew/vendor/bundle/ruby/${GEM_VERSION}" GEM_HOME="${HOMEBREW_LIBRARY}/Homebrew/vendor/bundle/ruby/${GEM_VERSION}"

View File

@ -6,7 +6,8 @@ homebrew-command-path() {
case "$1" in case "$1" in
# check we actually have command and not e.g. commandsomething # check we actually have command and not e.g. commandsomething
command) ;; command) ;;
*) return 1 ;; command*) return 1 ;;
*) ;;
esac esac
local first_command found_command local first_command found_command

View File

@ -12,6 +12,7 @@ require_relative "presence"
require_relative "present" require_relative "present"
require_relative "safe_navigation_with_blank" require_relative "safe_navigation_with_blank"
require_relative "shell_commands" require_relative "shell_commands"
require_relative "install_bundler_gems"
# formula audit cops # formula audit cops
require_relative "bottle" require_relative "bottle"

View File

@ -20,7 +20,7 @@ module RuboCop
# This method is called by RuboCop and is the main entry point. # This method is called by RuboCop and is the main entry point.
def on_class(node) def on_class(node)
@file_path = processed_source.buffer.name @file_path = processed_source.file_path
return unless file_path_allowed? return unless file_path_allowed?
return unless formula_class?(node) return unless formula_class?(node)
@ -228,10 +228,9 @@ module RuboCop
end end
def file_path_allowed? def file_path_allowed?
paths_to_exclude = [%r{/Library/Homebrew/test/}]
return true if @file_path.nil? # file_path is nil when source is directly passed to the cop, e.g. in specs return true if @file_path.nil? # file_path is nil when source is directly passed to the cop, e.g. in specs
!@file_path.match?(Regexp.union(paths_to_exclude)) !@file_path.include?("/Library/Homebrew/test/")
end end
def on_system_methods def on_system_methods

View File

@ -0,0 +1,21 @@
# typed: true
# frozen_string_literal: true
module RuboCop
module Cop
module Homebrew
# Enforces the use of `Homebrew.install_bundler_gems!` in dev-cmd.
class InstallBundlerGems < Base
MSG = "Only use `Homebrew.install_bundler_gems!` in dev-cmd."
RESTRICT_ON_SEND = [:install_bundler_gems!].freeze
def on_send(node)
file_path = processed_source.file_path
return if file_path.match?(%r{/(dev-cmd/.+|standalone/init|startup/bootsnap)\.rb\z})
add_offense(node)
end
end
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require "rubocops/install_bundler_gems"
RSpec.describe RuboCop::Cop::Homebrew::InstallBundlerGems, :config do
it "registers an offense and corrects when using `Homebrew.install_bundler_gems!`" do
expect_offense(<<~RUBY)
Homebrew.install_bundler_gems!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Only use `Homebrew.install_bundler_gems!` in dev-cmd.
RUBY
end
end