brew/bin/brew

138 lines
4.1 KiB
Plaintext
Raw Normal View History

#!/bin/bash
set +o posix
# Fail fast with concise message when cwd does not exist
if ! [[ -d "${PWD}" ]]
then
echo "Error: The current working directory doesn't exist, cannot proceed." >&2
exit 1
fi
2021-09-15 17:10:05 +08:00
# Fail fast with concise message when not using bash
if [ -z "${BASH_VERSION:-}" ]
then
2021-09-15 17:10:05 +08:00
echo "Error: Bash is required to run brew." >&2
exit 1
fi
quiet_cd() {
cd "$@" &>/dev/null || return
}
symlink_target_directory() {
2018-10-04 09:31:37 +01:00
local target target_dirname
target="$(readlink "$1")"
2021-04-16 00:02:13 +09:00
target_dirname="$(dirname "${target}")"
local directory="$2"
2021-04-16 00:02:13 +09:00
quiet_cd "${directory}" && quiet_cd "${target_dirname}" && pwd -P
}
# Enable and use default Bash builtins rather than user-defined functions
builtin enable compgen unset
for cmd in $(builtin compgen -A builtin)
do
2021-04-16 00:02:13 +09:00
builtin unset -f "${cmd}"
builtin enable "${cmd}"
done
unset cmd
BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}/" && pwd -P)"
HOMEBREW_BREW_FILE="${BREW_FILE_DIRECTORY%/}/${0##*/}"
HOMEBREW_PREFIX="${HOMEBREW_BREW_FILE%/*/*}"
# Default to / prefix if unset or the bin/brew file.
2021-04-16 00:02:13 +09:00
if [[ -z "${HOMEBREW_PREFIX}" || "${HOMEBREW_PREFIX}" = "${HOMEBREW_BREW_FILE}" ]]
then
HOMEBREW_PREFIX="/"
fi
2021-04-16 00:02:13 +09:00
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}"
# Resolve the bin/brew symlink to find Homebrew's repository
2021-04-16 00:02:13 +09:00
if [[ -L "${HOMEBREW_BREW_FILE}" ]]
then
2021-04-16 00:02:13 +09:00
BREW_FILE_DIRECTORY="$(symlink_target_directory "${HOMEBREW_BREW_FILE}" "${BREW_FILE_DIRECTORY}")"
HOMEBREW_REPOSITORY="${BREW_FILE_DIRECTORY%/*}"
fi
# Try to find a /usr/local HOMEBREW_PREFIX where possible (for bottles)
2021-04-16 00:02:13 +09:00
if [[ -L "/usr/local/bin/brew" && ! -L "${HOMEBREW_PREFIX}/Cellar" ]]
then
USR_LOCAL_BREW_FILE_DIRECTORY="$(symlink_target_directory "/usr/local/bin/brew" "/usr/local/bin")"
USR_LOCAL_HOMEBREW_REPOSITORY="${USR_LOCAL_BREW_FILE_DIRECTORY%/*}"
2021-04-16 00:02:13 +09:00
if [[ "${HOMEBREW_REPOSITORY}" = "${USR_LOCAL_HOMEBREW_REPOSITORY}" ]]
then
HOMEBREW_PREFIX="/usr/local"
fi
fi
# If the location of HOMEBREW_LIBRARY changes
# keg_relocate.rb, formula_cellar_checks.rb, and test/global_spec.rb need to change.
2021-04-16 00:02:13 +09:00
HOMEBREW_LIBRARY="${HOMEBREW_REPOSITORY}/Library"
2020-09-09 11:49:26 -07:00
# Copy and export all HOMEBREW_* variables previously mentioned in
# manpage or used elsewhere by Homebrew.
for VAR in BROWSER DISPLAY EDITOR NO_COLOR PATH TMUX DBUS_SESSION_BUS_ADDRESS
do
# Skip if variable value is empty.
[[ -z "${!VAR}" ]] && continue
VAR_NEW="HOMEBREW_${VAR}"
# Skip if existing HOMEBREW_* variable is set.
[[ -n "${!VAR_NEW}" ]] && continue
2021-04-16 00:02:13 +09:00
export "${VAR_NEW}"="${!VAR}"
done
unset VAR VAR_NEW
2021-03-02 16:59:57 +00:00
export HOMEBREW_BREW_FILE
export HOMEBREW_PREFIX
export HOMEBREW_REPOSITORY
export HOMEBREW_LIBRARY
2021-04-15 17:24:17 +01:00
# set from user environment
2021-04-16 00:02:13 +09:00
# shellcheck disable=SC2154
2020-04-05 15:44:50 +01:00
# Use VISUAL if HOMEBREW_EDITOR and EDITOR are unset.
2021-04-16 00:02:13 +09:00
if [[ -z "${HOMEBREW_EDITOR}" && -n "${VISUAL}" ]]
2020-04-05 15:44:50 +01:00
then
2021-04-16 00:02:13 +09:00
export HOMEBREW_EDITOR="${VISUAL}"
2020-04-05 15:44:50 +01:00
fi
2021-04-15 17:24:17 +01:00
# set from user environment
2021-04-16 00:02:13 +09:00
# shellcheck disable=SC2154
# Set CI variable for Azure Pipelines and Jenkins
2021-04-12 22:47:33 +09:00
# (Set by default on GitHub Actions, Circle and Travis CI)
2021-04-16 00:02:13 +09:00
if [[ -z "${CI}" ]] && [[ -n "${TF_BUILD}" || -n "${JENKINS_HOME}" ]]
then
export CI="1"
fi
2021-04-15 17:24:17 +01:00
# set from user environment
2021-04-16 00:02:13 +09:00
# shellcheck disable=SC2154
if [[ -z "${HOMEBREW_NO_ENV_FILTERING}" ]]
2017-01-09 12:31:28 +00:00
then
2017-02-23 15:21:46 +00:00
PATH="/usr/bin:/bin:/usr/sbin:/sbin"
2017-02-26 20:42:24 +00:00
FILTERED_ENV=()
ENV_VAR_NAMES=(
HOME SHELL PATH TERM TERMINFO COLUMNS DISPLAY LOGNAME USER CI SSH_AUTH_SOCK SUDO_ASKPASS
http_proxy https_proxy ftp_proxy no_proxy all_proxy HTTPS_PROXY FTP_PROXY ALL_PROXY
GITHUB_ACTIONS GITHUB_WORKSPACE GITHUB_ACTIONS_HOMEBREW_MACOS_SELF_HOSTED
GITHUB_REPOSITORY GITHUB_RUN_ID GITHUB_SHA GITHUB_HEAD_REF GITHUB_BASE_REF GITHUB_REF
)
# Filter all but the specific variables.
for VAR in "${ENV_VAR_NAMES[@]}" "${!HOMEBREW_@}"
2017-02-23 15:21:46 +00:00
do
# Skip if variable value is empty.
[[ -z "${!VAR}" ]] && continue
FILTERED_ENV+=("${VAR}=${!VAR}")
2017-02-23 15:21:46 +00:00
done
unset VAR ENV_VAR_NAMES
2017-02-23 15:21:46 +00:00
2021-04-16 00:02:13 +09:00
exec /usr/bin/env -i "${FILTERED_ENV[@]}" /bin/bash "${HOMEBREW_LIBRARY}/Homebrew/brew.sh" "$@"
2017-01-09 12:31:28 +00:00
else
echo "Warning: HOMEBREW_NO_ENV_FILTERING is undocumented, deprecated and will be removed in a future Homebrew release (because it breaks many things)!" >&2
2021-04-16 00:02:13 +09:00
source "${HOMEBREW_LIBRARY}/Homebrew/brew.sh"
2017-01-09 12:31:28 +00:00
fi