brew/Library/Homebrew/cmd/setup-ruby.sh
Mike McQuaid 97ad3567d1
Optimise more command handling/speed
- in `brew.sh` split the `case` into those cases that take a single or
  no arguments and those that take multiple arguments or handle
  multiple commands. This considerably speeds up the
  `brew shellenv bash` case that wasn't being handled here before.
- add `setup-ruby` to the list of commands that can be called quickly
  by `brew.sh` without additional setup. This speeds up the
  `brew setup-ruby` no-op case by ~10x.
- add a parameter to `setup-ruby` to avoid running Bundler if the
  command doesn't need it. This makes many more cases for
  `brew setup-ruby` to be no-op cases.
- Remove the (now) unused `HOMEBREW_RUBY3` check in `setup-ruby`.
- Improve argument handling in `command_path.sh` to allow it to be
  used as a function in `setup-ruby.sh`.
- Add a new RuboCop to check usage of `install_bundler_gems!` is only
  inside `dev-cmd` (or a few other acceptable places).
- Use new `processed_source.file_path` API in `formula_cop.rb`
2024-04-30 12:48:29 +01:00

53 lines
1.4 KiB
Bash

#: * `setup-ruby [command]`
#:
#: 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_RUBY_PATH is set by utils/ruby.sh
# RUBY_DISABLE_OPTIONS is set by brew.sh
# HOMEBREW_BREW_FILE is set by extend/ENV/super.rb
# shellcheck disable=SC2154
homebrew-setup-ruby() {
source "${HOMEBREW_LIBRARY}/Homebrew/utils/ruby.sh"
setup-ruby-path
if [[ -z "${HOMEBREW_DEVELOPER}" ]]
then
return
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"]')"
echo "${GEM_VERSION}"
GEM_HOME="${HOMEBREW_LIBRARY}/Homebrew/vendor/bundle/ruby/${GEM_VERSION}"
BUNDLE_GEMFILE="${HOMEBREW_LIBRARY}/Homebrew/Gemfile"
export GEM_HOME
export BUNDLE_GEMFILE
if ! bundle check &>/dev/null
then
"${HOMEBREW_BREW_FILE}" install-bundler-gems
fi
}