From 8fde5826ad9b7b0c83dfa46196e4afdc46396558 Mon Sep 17 00:00:00 2001 From: neo Date: Mon, 19 Sep 2022 13:20:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install-origin.sh | 78 ++++++++++++++++++++++++++++++----------------- install.sh | 78 ++++++++++++++++++++++++++++++----------------- uninstall.sh | 30 +++++++++++++----- 3 files changed, 123 insertions(+), 63 deletions(-) diff --git a/install-origin.sh b/install-origin.sh index e3f23c0..4535f45 100755 --- a/install-origin.sh +++ b/install-origin.sh @@ -7,7 +7,7 @@ set -u abort() { - printf "%s\n" "$@" + printf "%s\n" "$@" >&2 exit 1 } @@ -33,6 +33,12 @@ then abort 'Both `$INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again.' 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 if [[ -t 1 ]] then @@ -94,6 +100,13 @@ else ohai 'Running in non-interactive mode because `$NONINTERACTIVE` is set.' 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. OS="$(uname)" if [[ "${OS}" == "Linux" ]] @@ -129,6 +142,7 @@ then CHGRP=("/usr/bin/chgrp") GROUP="admin" TOUCH=("/usr/bin/touch") + INSTALL=("/usr/bin/install" -d -o "root" -g "wheel" -m "0755") else UNAME_MACHINE="$(uname -m)" @@ -143,6 +157,7 @@ else CHGRP=("/bin/chgrp") GROUP="$(id -gn)" TOUCH=("/bin/touch") + INSTALL=("/usr/bin/install" -d -o "${USER}" -g "${GROUP}" -m "0755") fi CHMOD=("/bin/chmod") MKDIR=("/bin/mkdir" "-p") @@ -256,7 +271,7 @@ ring_bell() { wait_for_user() { local c 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 # we test for \r and \n because some stuff does \r instead if ! [[ "${c}" == $'\r' || "${c}" == $'\n' ]] @@ -282,6 +297,16 @@ version_lt() { [[ "${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() { if [[ -n "${HOMEBREW_ON_LINUX-}" ]] then @@ -409,13 +434,6 @@ EOABORT )" 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). if [[ -x /usr/bin/sudo ]] && ! /usr/bin/sudo -n -v 2>/dev/null then @@ -527,15 +545,7 @@ else fi HOMEBREW_CORE="${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core" -if [[ "${EUID:-${UID}}" == "0" ]] -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 +check_run_command_as_root if [[ -d "${HOMEBREW_PREFIX}" && ! -x "${HOMEBREW_PREFIX}" ]] then @@ -584,7 +594,7 @@ Your Mac OS X version is too old. See: ${tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset} EOABORT )" - elif version_lt "${macos_version}" "10.10" + elif version_lt "${macos_version}" "10.11" then abort "Your OS X version is too old." 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}\"") 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-}" ]] then ring_bell @@ -788,13 +804,7 @@ then execute_sudo "${CHGRP[@]}" "${GROUP}" "${chgrps[@]}" fi else - execute_sudo "${MKDIR[@]}" "${HOMEBREW_PREFIX}" - if [[ -z "${HOMEBREW_ON_LINUX-}" ]] - then - execute_sudo "${CHOWN[@]}" "root:wheel" "${HOMEBREW_PREFIX}" - else - execute_sudo "${CHOWN[@]}" "${USER}:${GROUP}" "${HOMEBREW_PREFIX}" - fi + execute_sudo "${INSTALL[@]}" "${HOMEBREW_PREFIX}" fi if [[ "${#mkdirs[@]}" -gt 0 ]] @@ -916,7 +926,16 @@ ohai "Downloading and installing Homebrew..." 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 ohai "Tapping homebrew/core" ( @@ -992,7 +1011,10 @@ case "${SHELL}" in shell_profile="${HOME}/.profile" ;; 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 cat <&2 exit 1 } @@ -33,6 +33,12 @@ then abort 'Both `$INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again.' 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 if [[ -t 1 ]] then @@ -94,6 +100,13 @@ else ohai 'Running in non-interactive mode because `$NONINTERACTIVE` is set.' 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. OS="$(uname)" if [[ "${OS}" == "Linux" ]] @@ -129,6 +142,7 @@ then CHGRP=("/usr/bin/chgrp") GROUP="admin" TOUCH=("/usr/bin/touch") + INSTALL=("/usr/bin/install" -d -o "root" -g "wheel" -m "0755") else UNAME_MACHINE="$(uname -m)" @@ -143,6 +157,7 @@ else CHGRP=("/bin/chgrp") GROUP="$(id -gn)" TOUCH=("/bin/touch") + INSTALL=("/usr/bin/install" -d -o "${USER}" -g "${GROUP}" -m "0755") fi CHMOD=("/bin/chmod") MKDIR=("/bin/mkdir" "-p") @@ -286,6 +301,16 @@ version_lt() { [[ "${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() { if [[ -n "${HOMEBREW_ON_LINUX-}" ]] then @@ -413,13 +438,6 @@ EOABORT )" 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). if [[ -x /usr/bin/sudo ]] && ! /usr/bin/sudo -n -v 2>/dev/null then @@ -531,15 +549,7 @@ else fi HOMEBREW_CORE="${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core" -if [[ "${EUID:-${UID}}" == "0" ]] -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 +check_run_command_as_root if [[ -d "${HOMEBREW_PREFIX}" && ! -x "${HOMEBREW_PREFIX}" ]] then @@ -588,7 +598,7 @@ Your Mac OS X version is too old. See: ${tty_underline}https://github.com/mistydemeo/tigerbrew${tty_reset} EOABORT )" - elif version_lt "${macos_version}" "10.10" + elif version_lt "${macos_version}" "10.11" then abort "Your OS X version is too old." 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}\"") 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 "安装提示" echo "中文安装教程(建议收藏):https://brew.idayer.com/" @@ -826,13 +842,7 @@ then execute_sudo "${CHGRP[@]}" "${GROUP}" "${chgrps[@]}" fi else - execute_sudo "${MKDIR[@]}" "${HOMEBREW_PREFIX}" - if [[ -z "${HOMEBREW_ON_LINUX-}" ]] - then - execute_sudo "${CHOWN[@]}" "root:wheel" "${HOMEBREW_PREFIX}" - else - execute_sudo "${CHOWN[@]}" "${USER}:${GROUP}" "${HOMEBREW_PREFIX}" - fi + execute_sudo "${INSTALL[@]}" "${HOMEBREW_PREFIX}" fi if [[ "${#mkdirs[@]}" -gt 0 ]] @@ -954,7 +964,16 @@ ohai "Downloading and installing Homebrew..." 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 ohai "Tapping homebrew/core" ( @@ -1030,10 +1049,13 @@ case "${SHELL}" in shell_profile="${HOME}/.profile" ;; 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 warn "!!!!!!!!!!! 重要 !!!!!!!!!!!!!!!" - echo "切记执行环境变量设置!,如已执行过请忽略。" + echo "切记在终端执行环境变量设置!,如已执行过请忽略。" cat <> ${shell_profile} diff --git a/uninstall.sh b/uninstall.sh index caabe53..373bdbe 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -7,7 +7,7 @@ set -u abort() { - printf "%s\n" "$@" + printf "%s\n" "$@" >&2 exit 1 } @@ -19,6 +19,12 @@ then abort "Bash is required to interpret this script." 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 strip_s() { @@ -200,7 +206,7 @@ homebrew_prefix_candidates=() usage() { cat <