feat: 更新脚本

This commit is contained in:
neo 2022-09-19 13:20:12 +08:00
parent 815732c78d
commit 8fde5826ad
3 changed files with 123 additions and 63 deletions

View File

@ -7,7 +7,7 @@
set -u set -u
abort() { abort() {
printf "%s\n" "$@" printf "%s\n" "$@" >&2
exit 1 exit 1
} }
@ -33,6 +33,12 @@ then
abort 'Both `$INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again.' abort 'Both `$INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again.'
fi fi
# Check if script is run in POSIX mode
if [[ -n "${POSIXLY_CORRECT+1}" ]]
then
abort 'Bash must not run in POSIX mode. Please unset POSIXLY_CORRECT and try again.'
fi
# string formatters # string formatters
if [[ -t 1 ]] if [[ -t 1 ]]
then then
@ -94,6 +100,13 @@ else
ohai 'Running in non-interactive mode because `$NONINTERACTIVE` is set.' ohai 'Running in non-interactive mode because `$NONINTERACTIVE` is set.'
fi fi
# USER isn't always set so provide a fall back for the installer and subprocesses.
if [[ -z "${USER-}" ]]
then
USER="$(chomp "$(id -un)")"
export USER
fi
# First check OS. # First check OS.
OS="$(uname)" OS="$(uname)"
if [[ "${OS}" == "Linux" ]] if [[ "${OS}" == "Linux" ]]
@ -129,6 +142,7 @@ then
CHGRP=("/usr/bin/chgrp") CHGRP=("/usr/bin/chgrp")
GROUP="admin" GROUP="admin"
TOUCH=("/usr/bin/touch") TOUCH=("/usr/bin/touch")
INSTALL=("/usr/bin/install" -d -o "root" -g "wheel" -m "0755")
else else
UNAME_MACHINE="$(uname -m)" UNAME_MACHINE="$(uname -m)"
@ -143,6 +157,7 @@ else
CHGRP=("/bin/chgrp") CHGRP=("/bin/chgrp")
GROUP="$(id -gn)" GROUP="$(id -gn)"
TOUCH=("/bin/touch") TOUCH=("/bin/touch")
INSTALL=("/usr/bin/install" -d -o "${USER}" -g "${GROUP}" -m "0755")
fi fi
CHMOD=("/bin/chmod") CHMOD=("/bin/chmod")
MKDIR=("/bin/mkdir" "-p") MKDIR=("/bin/mkdir" "-p")
@ -256,7 +271,7 @@ ring_bell() {
wait_for_user() { wait_for_user() {
local c local c
echo echo
echo "Press ${tty_bold}RETURN${tty_reset} to continue or any other key to abort:" echo "Press ${tty_bold}RETURN${tty_reset}/${tty_bold}ENTER${tty_reset} to continue or any other key to abort:"
getc c getc c
# we test for \r and \n because some stuff does \r instead # we test for \r and \n because some stuff does \r instead
if ! [[ "${c}" == $'\r' || "${c}" == $'\n' ]] if ! [[ "${c}" == $'\r' || "${c}" == $'\n' ]]
@ -282,6 +297,16 @@ version_lt() {
[[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]] [[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]]
} }
check_run_command_as_root() {
[[ "${EUID:-${UID}}" == "0" ]] || return
# Allow Azure Pipelines/GitHub Actions/Docker/Concourse/Kubernetes to do everything as root (as it's normal there)
[[ -f /.dockerenv ]] && return
[[ -f /proc/1/cgroup ]] && grep -E "azpl_job|actions_job|docker|garden|kubepods" -q /proc/1/cgroup && return
abort "Don't run this as root!"
}
should_install_command_line_tools() { should_install_command_line_tools() {
if [[ -n "${HOMEBREW_ON_LINUX-}" ]] if [[ -n "${HOMEBREW_ON_LINUX-}" ]]
then then
@ -409,13 +434,6 @@ EOABORT
)" )"
fi fi
# USER isn't always set so provide a fall back for the installer and subprocesses.
if [[ -z "${USER-}" ]]
then
USER="$(chomp "$(id -un)")"
export USER
fi
# Invalidate sudo timestamp before exiting (if it wasn't active before). # Invalidate sudo timestamp before exiting (if it wasn't active before).
if [[ -x /usr/bin/sudo ]] && ! /usr/bin/sudo -n -v 2>/dev/null if [[ -x /usr/bin/sudo ]] && ! /usr/bin/sudo -n -v 2>/dev/null
then then
@ -527,15 +545,7 @@ else
fi fi
HOMEBREW_CORE="${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core" HOMEBREW_CORE="${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core"
if [[ "${EUID:-${UID}}" == "0" ]] check_run_command_as_root
then
# Allow Azure Pipelines/GitHub Actions/Docker/Concourse/Kubernetes to do everything as root (as it's normal there)
if ! [[ -f /proc/1/cgroup ]] ||
! grep -E "azpl_job|actions_job|docker|garden|kubepods" -q /proc/1/cgroup
then
abort "Don't run this as root!"
fi
fi
if [[ -d "${HOMEBREW_PREFIX}" && ! -x "${HOMEBREW_PREFIX}" ]] if [[ -d "${HOMEBREW_PREFIX}" && ! -x "${HOMEBREW_PREFIX}" ]]
then then
@ -584,7 +594,7 @@ Your Mac OS X version is too old. See:
${tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset} ${tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset}
EOABORT EOABORT
)" )"
elif version_lt "${macos_version}" "10.10" elif version_lt "${macos_version}" "10.11"
then then
abort "Your OS X version is too old." abort "Your OS X version is too old."
elif version_ge "${macos_version}" "${MACOS_NEWEST_UNSUPPORTED}" || elif version_ge "${macos_version}" "${MACOS_NEWEST_UNSUPPORTED}" ||
@ -759,6 +769,12 @@ then
additional_shellenv_commands+=("export HOMEBREW_CORE_GIT_REMOTE=\"${HOMEBREW_CORE_GIT_REMOTE}\"") additional_shellenv_commands+=("export HOMEBREW_CORE_GIT_REMOTE=\"${HOMEBREW_CORE_GIT_REMOTE}\"")
fi fi
if [[ -n "${HOMEBREW_INSTALL_FROM_API-}" ]]
then
ohai "HOMEBREW_INSTALL_FROM_API is set."
echo "Homebrew/homebrew-core will not be tapped during this ${tty_bold}install${tty_reset} run."
fi
if [[ -z "${NONINTERACTIVE-}" ]] if [[ -z "${NONINTERACTIVE-}" ]]
then then
ring_bell ring_bell
@ -788,13 +804,7 @@ then
execute_sudo "${CHGRP[@]}" "${GROUP}" "${chgrps[@]}" execute_sudo "${CHGRP[@]}" "${GROUP}" "${chgrps[@]}"
fi fi
else else
execute_sudo "${MKDIR[@]}" "${HOMEBREW_PREFIX}" execute_sudo "${INSTALL[@]}" "${HOMEBREW_PREFIX}"
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]
then
execute_sudo "${CHOWN[@]}" "root:wheel" "${HOMEBREW_PREFIX}"
else
execute_sudo "${CHOWN[@]}" "${USER}:${GROUP}" "${HOMEBREW_PREFIX}"
fi
fi fi
if [[ "${#mkdirs[@]}" -gt 0 ]] if [[ "${#mkdirs[@]}" -gt 0 ]]
@ -916,7 +926,16 @@ ohai "Downloading and installing Homebrew..."
fi fi
fi fi
if [[ ! -d "${HOMEBREW_CORE}" ]] if [[ -n "${HOMEBREW_INSTALL_FROM_API-}" ]]
then
# shellcheck disable=SC2016
ohai 'Skip tapping homebrew/core because `$HOMEBREW_INSTALL_FROM_API` is set.'
# Unset HOMEBREW_DEVELOPER since it is no longer needed and causes warnings during brew update below
if [[ -n "${HOMEBREW_ON_LINUX-}" && (-n "${HOMEBREW_CURL_PATH-}" || -n "${HOMEBREW_GIT_PATH-}") ]]
then
export -n HOMEBREW_DEVELOPER
fi
elif [[ ! -d "${HOMEBREW_CORE}" ]]
then then
ohai "Tapping homebrew/core" ohai "Tapping homebrew/core"
( (
@ -992,7 +1011,10 @@ case "${SHELL}" in
shell_profile="${HOME}/.profile" shell_profile="${HOME}/.profile"
;; ;;
esac esac
if [[ "${UNAME_MACHINE}" == "arm64" ]] || [[ -n "${HOMEBREW_ON_LINUX-}" ]]
# `which` is a shell function defined above.
# shellcheck disable=SC2230
if [[ "$(which brew)" != "${HOMEBREW_PREFIX}/bin/brew" ]]
then then
cat <<EOS cat <<EOS
- Run these two commands in your terminal to add Homebrew to your ${tty_bold}PATH${tty_reset}: - Run these two commands in your terminal to add Homebrew to your ${tty_bold}PATH${tty_reset}:

View File

@ -7,7 +7,7 @@
set -u set -u
abort() { abort() {
printf "%s\n" "$@" printf "%s\n" "$@" >&2
exit 1 exit 1
} }
@ -33,6 +33,12 @@ then
abort 'Both `$INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again.' abort 'Both `$INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again.'
fi fi
# Check if script is run in POSIX mode
if [[ -n "${POSIXLY_CORRECT+1}" ]]
then
abort 'Bash must not run in POSIX mode. Please unset POSIXLY_CORRECT and try again.'
fi
# string formatters # string formatters
if [[ -t 1 ]] if [[ -t 1 ]]
then then
@ -94,6 +100,13 @@ else
ohai 'Running in non-interactive mode because `$NONINTERACTIVE` is set.' ohai 'Running in non-interactive mode because `$NONINTERACTIVE` is set.'
fi fi
# USER isn't always set so provide a fall back for the installer and subprocesses.
if [[ -z "${USER-}" ]]
then
USER="$(chomp "$(id -un)")"
export USER
fi
# First check OS. # First check OS.
OS="$(uname)" OS="$(uname)"
if [[ "${OS}" == "Linux" ]] if [[ "${OS}" == "Linux" ]]
@ -129,6 +142,7 @@ then
CHGRP=("/usr/bin/chgrp") CHGRP=("/usr/bin/chgrp")
GROUP="admin" GROUP="admin"
TOUCH=("/usr/bin/touch") TOUCH=("/usr/bin/touch")
INSTALL=("/usr/bin/install" -d -o "root" -g "wheel" -m "0755")
else else
UNAME_MACHINE="$(uname -m)" UNAME_MACHINE="$(uname -m)"
@ -143,6 +157,7 @@ else
CHGRP=("/bin/chgrp") CHGRP=("/bin/chgrp")
GROUP="$(id -gn)" GROUP="$(id -gn)"
TOUCH=("/bin/touch") TOUCH=("/bin/touch")
INSTALL=("/usr/bin/install" -d -o "${USER}" -g "${GROUP}" -m "0755")
fi fi
CHMOD=("/bin/chmod") CHMOD=("/bin/chmod")
MKDIR=("/bin/mkdir" "-p") MKDIR=("/bin/mkdir" "-p")
@ -286,6 +301,16 @@ version_lt() {
[[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]] [[ "${1%.*}" -lt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -lt "${2#*.}" ]]
} }
check_run_command_as_root() {
[[ "${EUID:-${UID}}" == "0" ]] || return
# Allow Azure Pipelines/GitHub Actions/Docker/Concourse/Kubernetes to do everything as root (as it's normal there)
[[ -f /.dockerenv ]] && return
[[ -f /proc/1/cgroup ]] && grep -E "azpl_job|actions_job|docker|garden|kubepods" -q /proc/1/cgroup && return
abort "Don't run this as root!"
}
should_install_command_line_tools() { should_install_command_line_tools() {
if [[ -n "${HOMEBREW_ON_LINUX-}" ]] if [[ -n "${HOMEBREW_ON_LINUX-}" ]]
then then
@ -413,13 +438,6 @@ EOABORT
)" )"
fi fi
# USER isn't always set so provide a fall back for the installer and subprocesses.
if [[ -z "${USER-}" ]]
then
USER="$(chomp "$(id -un)")"
export USER
fi
# Invalidate sudo timestamp before exiting (if it wasn't active before). # Invalidate sudo timestamp before exiting (if it wasn't active before).
if [[ -x /usr/bin/sudo ]] && ! /usr/bin/sudo -n -v 2>/dev/null if [[ -x /usr/bin/sudo ]] && ! /usr/bin/sudo -n -v 2>/dev/null
then then
@ -531,15 +549,7 @@ else
fi fi
HOMEBREW_CORE="${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core" HOMEBREW_CORE="${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core"
if [[ "${EUID:-${UID}}" == "0" ]] check_run_command_as_root
then
# Allow Azure Pipelines/GitHub Actions/Docker/Concourse/Kubernetes to do everything as root (as it's normal there)
if ! [[ -f /proc/1/cgroup ]] ||
! grep -E "azpl_job|actions_job|docker|garden|kubepods" -q /proc/1/cgroup
then
abort "Don't run this as root!"
fi
fi
if [[ -d "${HOMEBREW_PREFIX}" && ! -x "${HOMEBREW_PREFIX}" ]] if [[ -d "${HOMEBREW_PREFIX}" && ! -x "${HOMEBREW_PREFIX}" ]]
then then
@ -588,7 +598,7 @@ Your Mac OS X version is too old. See:
${tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset} ${tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset}
EOABORT EOABORT
)" )"
elif version_lt "${macos_version}" "10.10" elif version_lt "${macos_version}" "10.11"
then then
abort "Your OS X version is too old." abort "Your OS X version is too old."
elif version_ge "${macos_version}" "${MACOS_NEWEST_UNSUPPORTED}" || elif version_ge "${macos_version}" "${MACOS_NEWEST_UNSUPPORTED}" ||
@ -763,6 +773,12 @@ then
additional_shellenv_commands+=("export HOMEBREW_CORE_GIT_REMOTE=\"${HOMEBREW_CORE_GIT_REMOTE}\"") additional_shellenv_commands+=("export HOMEBREW_CORE_GIT_REMOTE=\"${HOMEBREW_CORE_GIT_REMOTE}\"")
fi fi
if [[ -n "${HOMEBREW_INSTALL_FROM_API-}" ]]
then
ohai "HOMEBREW_INSTALL_FROM_API is set."
echo "Homebrew/homebrew-core will not be tapped during this ${tty_bold}install${tty_reset} run."
fi
ohai "安装提示" ohai "安装提示"
echo "中文安装教程建议收藏https://brew.idayer.com/" echo "中文安装教程建议收藏https://brew.idayer.com/"
@ -826,13 +842,7 @@ then
execute_sudo "${CHGRP[@]}" "${GROUP}" "${chgrps[@]}" execute_sudo "${CHGRP[@]}" "${GROUP}" "${chgrps[@]}"
fi fi
else else
execute_sudo "${MKDIR[@]}" "${HOMEBREW_PREFIX}" execute_sudo "${INSTALL[@]}" "${HOMEBREW_PREFIX}"
if [[ -z "${HOMEBREW_ON_LINUX-}" ]]
then
execute_sudo "${CHOWN[@]}" "root:wheel" "${HOMEBREW_PREFIX}"
else
execute_sudo "${CHOWN[@]}" "${USER}:${GROUP}" "${HOMEBREW_PREFIX}"
fi
fi fi
if [[ "${#mkdirs[@]}" -gt 0 ]] if [[ "${#mkdirs[@]}" -gt 0 ]]
@ -954,7 +964,16 @@ ohai "Downloading and installing Homebrew..."
fi fi
fi fi
if [[ ! -d "${HOMEBREW_CORE}" ]] if [[ -n "${HOMEBREW_INSTALL_FROM_API-}" ]]
then
# shellcheck disable=SC2016
ohai 'Skip tapping homebrew/core because `$HOMEBREW_INSTALL_FROM_API` is set.'
# Unset HOMEBREW_DEVELOPER since it is no longer needed and causes warnings during brew update below
if [[ -n "${HOMEBREW_ON_LINUX-}" && (-n "${HOMEBREW_CURL_PATH-}" || -n "${HOMEBREW_GIT_PATH-}") ]]
then
export -n HOMEBREW_DEVELOPER
fi
elif [[ ! -d "${HOMEBREW_CORE}" ]]
then then
ohai "Tapping homebrew/core" ohai "Tapping homebrew/core"
( (
@ -1030,10 +1049,13 @@ case "${SHELL}" in
shell_profile="${HOME}/.profile" shell_profile="${HOME}/.profile"
;; ;;
esac esac
if [[ "${UNAME_MACHINE}" == "arm64" ]] || [[ -n "${HOMEBREW_ON_LINUX-}" ]]
# `which` is a shell function defined above.
# shellcheck disable=SC2230
if [[ "$(which brew)" != "${HOMEBREW_PREFIX}/bin/brew" ]]
then then
warn " 重要 " warn " 重要 "
echo "切记执行环境变量设置!,如已执行过请忽略。" echo "切记在终端执行环境变量设置!,如已执行过请忽略。"
cat <<EOS cat <<EOS
- 执行下面命令将 Homebrew 到 ${tty_bold}PATH${tty_reset} 中: - 执行下面命令将 Homebrew 到 ${tty_bold}PATH${tty_reset} 中:
echo 'eval "\$(${HOMEBREW_PREFIX}/bin/brew shellenv)"' >> ${shell_profile} echo 'eval "\$(${HOMEBREW_PREFIX}/bin/brew shellenv)"' >> ${shell_profile}

View File

@ -7,7 +7,7 @@
set -u set -u
abort() { abort() {
printf "%s\n" "$@" printf "%s\n" "$@" >&2
exit 1 exit 1
} }
@ -19,6 +19,12 @@ then
abort "Bash is required to interpret this script." abort "Bash is required to interpret this script."
fi fi
# Check if script is run in POSIX mode
if [[ -n "${POSIXLY_CORRECT+1}" ]]
then
abort 'Bash must not run in POSIX mode. Please unset POSIXLY_CORRECT and try again.'
fi
shopt -s extglob shopt -s extglob
strip_s() { strip_s() {
@ -200,7 +206,7 @@ homebrew_prefix_candidates=()
usage() { usage() {
cat <<EOS cat <<EOS
Homebrew Uninstaller Homebrew Uninstaller
Usage: $0 [options] Usage: [NONINTERACTIVE=1] $0 [options]
-p, --path=PATH Sets Homebrew prefix. Defaults to ${homebrew_prefix_default}. -p, --path=PATH Sets Homebrew prefix. Defaults to ${homebrew_prefix_default}.
--skip-cache-and-logs --skip-cache-and-logs
Skips removal of HOMEBREW_CACHE and HOMEBREW_LOGS. Skips removal of HOMEBREW_CACHE and HOMEBREW_LOGS.
@ -208,6 +214,7 @@ Usage: $0 [options]
-q, --quiet Suppress all output. -q, --quiet Suppress all output.
-n, --dry-run Simulate uninstall but don't remove anything. -n, --dry-run Simulate uninstall but don't remove anything.
-h, --help Display this message. -h, --help Display this message.
NONINTERACTIVE Imply --force if NONINTERACTIVE is non-empty.
EOS EOS
exit "${1:-0}" exit "${1:-0}"
} }
@ -233,12 +240,13 @@ done
# Attempt to locate Homebrew unless `--path` is passed # Attempt to locate Homebrew unless `--path` is passed
if [[ "${#homebrew_prefix_candidates[@]}" -eq 0 ]] if [[ "${#homebrew_prefix_candidates[@]}" -eq 0 ]]
then then
prefix="$(brew --prefix)" prefix="$("${homebrew_prefix_default}"/bin/brew --prefix)"
[[ -n "${prefix}" ]] && homebrew_prefix_candidates+=("${prefix}") [[ -n "${prefix}" ]] && homebrew_prefix_candidates+=("${prefix}")
prefix="$(command -v brew)" || prefix="" prefix="$(command -v "${homebrew_prefix_default}"/bin/brew)" || prefix=""
[[ -n "${prefix}" ]] && homebrew_prefix_candidates+=("$(dirname "$(dirname "$(strip_s "${prefix}")")")") [[ -n "${prefix}" ]] && homebrew_prefix_candidates+=("$(dirname "$(dirname "$(strip_s "${prefix}")")")")
homebrew_prefix_candidates+=("${homebrew_prefix_default}") # Homebrew default path homebrew_prefix_candidates+=("${homebrew_prefix_default}") # Homebrew default path
homebrew_prefix_candidates+=("${HOME}/.linuxbrew") # Linuxbrew default path homebrew_prefix_candidates+=("${HOME}/.linuxbrew") # Linuxbrew default path
[[ "$(uname -m)" == "arm64" ]] && homebrew_prefix_candidates+=("/usr/local") # If migrated from Intel to ARM old path will remain
fi fi
HOMEBREW_PREFIX="$( HOMEBREW_PREFIX="$(
@ -272,7 +280,7 @@ if [[ -s "${HOMEBREW_REPOSITORY}/.gitignore" ]]
then then
gitignore="$(<"${HOMEBREW_REPOSITORY}/.gitignore")" gitignore="$(<"${HOMEBREW_REPOSITORY}/.gitignore")"
else else
gitignore=$(curl -fsSL https://cdn.jsdelivr.net/gh/Homebrew/brew/.gitignore) gitignore=$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/.gitignore)
fi fi
[[ -n "${gitignore}" ]] || abort "Failed to fetch Homebrew .gitignore!" [[ -n "${gitignore}" ]] || abort "Failed to fetch Homebrew .gitignore!"
@ -342,6 +350,14 @@ then
pretty_print_pathnames "${homebrew_files[@]}" pretty_print_pathnames "${homebrew_files[@]}"
fi fi
# Always use single-quoted strings with `exp` expressions
# shellcheck disable=SC2016
if [[ -n "${NONINTERACTIVE-}" ]]
then
ohai 'Running in non-interactive mode because `$NONINTERACTIVE` is set.'
opt_force=1
fi
if [[ -t 0 && -z "${opt_force}" && -z "${opt_dry_run}" ]] if [[ -t 0 && -z "${opt_force}" && -z "${opt_dry_run}" ]]
then then
read -rp "Are you sure you want to uninstall Homebrew? This will remove your installed packages! [y/N] " read -rp "Are you sure you want to uninstall Homebrew? This will remove your installed packages! [y/N] "