diff --git a/install-origin.sh b/install-origin.sh index 4535f45..928c80f 100755 --- a/install-origin.sh +++ b/install-origin.sh @@ -112,15 +112,17 @@ OS="$(uname)" if [[ "${OS}" == "Linux" ]] then HOMEBREW_ON_LINUX=1 -elif [[ "${OS}" != "Darwin" ]] +elif [[ "${OS}" == "Darwin" ]] then + HOMEBREW_ON_MACOS=1 +else abort "Homebrew is only supported on macOS and Linux." fi # Required installation paths. To install elsewhere (which is unsupported) # you can untar https://github.com/Homebrew/brew/tarball/master # anywhere you like. -if [[ -z "${HOMEBREW_ON_LINUX-}" ]] +if [[ -n "${HOMEBREW_ON_MACOS-}" ]] then UNAME_MACHINE="$(/usr/bin/uname -m)" @@ -146,9 +148,9 @@ then else UNAME_MACHINE="$(uname -m)" - # On Linux, it installs to /home/linuxbrew/.linuxbrew if you have sudo access - # and ~/.linuxbrew (which is unsupported) if run interactively. - HOMEBREW_PREFIX_DEFAULT="/home/linuxbrew/.linuxbrew" + # On Linux, this script installs to /home/linuxbrew/.linuxbrew only + HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew" + HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew" HOMEBREW_CACHE="${HOME}/.cache/Homebrew" STAT_PRINTF=("stat" "--printf") @@ -179,9 +181,9 @@ fi export HOMEBREW_{BREW,CORE}_GIT_REMOTE # TODO: bump version when new macOS is released or announced -MACOS_NEWEST_UNSUPPORTED="13.0" +MACOS_NEWEST_UNSUPPORTED="14.0" # TODO: bump version when new macOS is released -MACOS_OLDEST_SUPPORTED="10.15" +MACOS_OLDEST_SUPPORTED="11.0" # For Homebrew on Linux REQUIRED_RUBY_VERSION=2.6 # https://github.com/Homebrew/brew/pull/6556 @@ -221,7 +223,7 @@ have_sudo_access() { HAVE_SUDO_ACCESS="$?" fi - if [[ -z "${HOMEBREW_ON_LINUX-}" ]] && [[ "${HAVE_SUDO_ACCESS}" -ne 0 ]] + if [[ -n "${HOMEBREW_ON_MACOS-}" ]] && [[ "${HAVE_SUDO_ACCESS}" -ne 0 ]] then abort "Need sudo access on macOS (e.g. the user ${USER} needs to be an Administrator)!" fi @@ -507,41 +509,25 @@ fi # shellcheck disable=SC2016 ohai 'Checking for `sudo` access (which may request your password)...' -if [[ -z "${HOMEBREW_ON_LINUX-}" ]] +if [[ -n "${HOMEBREW_ON_MACOS-}" ]] then have_sudo_access -else - if [[ -w "${HOMEBREW_PREFIX_DEFAULT}" ]] || - [[ -w "/home/linuxbrew" ]] || - [[ -w "/home" ]] - then - HOMEBREW_PREFIX="${HOMEBREW_PREFIX_DEFAULT}" - elif [[ -n "${NONINTERACTIVE-}" ]] - then - if have_sudo_access - then - HOMEBREW_PREFIX="${HOMEBREW_PREFIX_DEFAULT}" - else - abort "Insufficient permissions to install Homebrew to \"${HOMEBREW_PREFIX_DEFAULT}\"." - fi - else - trap exit SIGINT - if ! /usr/bin/sudo -n -v &>/dev/null - then - ohai "Select a Homebrew installation directory:" - echo "- ${tty_bold}Enter your password${tty_reset} to install to ${tty_underline}${HOMEBREW_PREFIX_DEFAULT}${tty_reset} (${tty_bold}recommended${tty_reset})" - echo "- ${tty_bold}Press Control-D${tty_reset} to install to ${tty_underline}${HOME}/.linuxbrew${tty_reset}" - echo "- ${tty_bold}Press Control-C${tty_reset} to cancel installation" - fi - if have_sudo_access - then - HOMEBREW_PREFIX="${HOMEBREW_PREFIX_DEFAULT}" - else - HOMEBREW_PREFIX="${HOME}/.linuxbrew" - fi - trap - SIGINT - fi - HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew" +elif ! [[ -w "${HOMEBREW_PREFIX}" ]] && + ! [[ -w "/home/linuxbrew" ]] && + ! [[ -w "/home" ]] && + ! have_sudo_access +then + abort "$( + cat </dev/null || return # we do it in four steps to avoid merge errors when reinstalling - execute "git" "init" "-q" + execute "git" "-c" "init.defaultBranch=master" "init" "--quiet" # "git remote add" will fail if the remote is defined in the global config execute "git" "config" "remote.origin.url" "${HOMEBREW_BREW_GIT_REMOTE}" execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*" # ensure we don't munge line endings on checkout - execute "git" "config" "core.autocrlf" "false" + execute "git" "config" "--bool" "core.autocrlf" "false" + + # make sure symlinks are saved as-is + execute "git" "config" "--bool" "core.symlinks" "true" execute "git" "fetch" "--force" "origin" execute "git" "fetch" "--force" "--tags" "origin" @@ -926,26 +914,20 @@ ohai "Downloading and installing Homebrew..." fi fi - if [[ -n "${HOMEBREW_INSTALL_FROM_API-}" ]] + if [[ -n "${HOMEBREW_NO_INSTALL_FROM_API-}" && ! -d "${HOMEBREW_CORE}" ]] then + # Always use single-quoted strings with `exp` expressions # 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" + ohai 'Tapping homebrew/core because `$HOMEBREW_NO_INSTALL_FROM_API` is set.' ( execute "${MKDIR[@]}" "${HOMEBREW_CORE}" cd "${HOMEBREW_CORE}" >/dev/null || return - execute "git" "init" "-q" + execute "git" "-c" "init.defaultBranch=master" "init" "--quiet" execute "git" "config" "remote.origin.url" "${HOMEBREW_CORE_GIT_REMOTE}" execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*" - execute "git" "config" "core.autocrlf" "false" + execute "git" "config" "--bool" "core.autocrlf" "false" + execute "git" "config" "--bool" "core.symlinks" "true" execute "git" "fetch" "--force" "origin" "refs/heads/master:refs/remotes/origin/master" execute "git" "remote" "set-head" "origin" "--auto" >/dev/null execute "git" "reset" "--hard" "origin/master" @@ -1012,16 +994,11 @@ case "${SHELL}" in ;; esac -# `which` is a shell function defined above. -# shellcheck disable=SC2230 -if [[ "$(which brew)" != "${HOMEBREW_PREFIX}/bin/brew" ]] -then - cat <> ${shell_profile} + (echo; echo 'eval "\$(${HOMEBREW_PREFIX}/bin/brew shellenv)"') >> ${shell_profile} eval "\$(${HOMEBREW_PREFIX}/bin/brew shellenv)" EOS -fi if [[ -n "${non_default_repos}" ]] then plural="" @@ -1029,7 +1006,8 @@ then then plural="s" fi - echo "- Run these commands in your terminal to add the non-default Git remote${plural} for ${non_default_repos}:" + printf -- "- Run these commands in your terminal to add the non-default Git remote%s for %s:\n" "${plural}" "${non_default_repos}" + printf " echo '# Set PATH, MANPATH, etc., for Homebrew.' >> %s\n" "${shell_profile}" printf " echo '%s' >> ${shell_profile}\n" "${additional_shellenv_commands[@]}" printf " %s\n" "${additional_shellenv_commands[@]}" fi diff --git a/install.sh b/install.sh index d7606e5..ad9da5e 100755 --- a/install.sh +++ b/install.sh @@ -50,6 +50,7 @@ tty_mkbold() { tty_escape "1;$1"; } tty_underline="$(tty_escape "4;39")" tty_blue="$(tty_mkbold 34)" tty_red="$(tty_mkbold 31)" +tty_green="$(tty_mkbold 32)" tty_bold="$(tty_mkbold 39)" tty_reset="$(tty_escape 0)" @@ -76,6 +77,18 @@ warn() { printf "${tty_red}Warning${tty_reset}: %s\n" "$(chomp "$1")" } +checkExecute() { + if [ $? -ne 0 ];then + echo "${tty_red}执行成功 '$1'${tty_reset}" + if [[ "$2" == 'out' ]]; then + exit 0 + fi + else + echo "${tty_green}执行成功${tty_reset}" + + fi +} + # Check if script is run non-interactively (e.g. CI) # If it is run non-interactively we should not prompt for passwords. # Always use single-quoted strings with `exp` expressions @@ -163,6 +176,7 @@ else fi CHMOD=("/bin/chmod") MKDIR=("/bin/mkdir" "-p") + #changed #HOMEBREW_BREW_DEFAULT_GIT_REMOTE="https://github.com/Homebrew/brew" HOMEBREW_BREW_DEFAULT_GIT_REMOTE="https://mirrors.ustc.edu.cn/brew.git" @@ -173,6 +187,8 @@ HOMEBREW_CORE_DEFAULT_GIT_REMOTE="https://mirrors.ustc.edu.cn/homebrew-core.git" HOMEBREW_CASK_DEFAULT_GIT_REMOTE="https://mirrors.ustc.edu.cn/homebrew-cask.git" HOMEBREW_SERVICES_DEFAULT_GIT_REMOTE="https://gitlab.com/mirrorx/homebrew-services.git" +HOMEBREW_API_DEFAULT_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles/api" + # Use remote URLs of Homebrew repositories from environment if set. HOMEBREW_BREW_GIT_REMOTE="${HOMEBREW_BREW_GIT_REMOTE:-"${HOMEBREW_BREW_DEFAULT_GIT_REMOTE}"}" HOMEBREW_CORE_GIT_REMOTE="${HOMEBREW_CORE_GIT_REMOTE:-"${HOMEBREW_CORE_DEFAULT_GIT_REMOTE}"}" @@ -191,6 +207,7 @@ then HOMEBREW_CASK_GIT_REMOTE="${HOMEBREW_CASK_DEFAULT_GIT_REMOTE}" fi export HOMEBREW_{BREW,CORE}_GIT_REMOTE +export HOMEBREW_API_DEFAULT_DOMAIN # TODO: bump version when new macOS is released or announced MACOS_NEWEST_UNSUPPORTED="14.0" @@ -624,14 +641,6 @@ EOS fi fi -ohai "This script will install:" -echo "${HOMEBREW_PREFIX}/bin/brew" -echo "${HOMEBREW_PREFIX}/share/doc/homebrew" -echo "${HOMEBREW_PREFIX}/share/man/man1/brew.1" -echo "${HOMEBREW_PREFIX}/share/zsh/site-functions/_brew" -echo "${HOMEBREW_PREFIX}/etc/bash_completion.d/brew" -echo "${HOMEBREW_REPOSITORY}" - # Keep relatively in sync with # https://github.com/Homebrew/brew/blob/master/Library/Homebrew/keg.rb directories=( @@ -786,7 +795,7 @@ ohai "安装提示" echo "中文安装教程(建议收藏):https://brew.idayer.com/" -echo "也可以查阅,Mac下镜像飞速安装Homebrew教程 :https://zhuanlan.zhihu.com/p/90508170" +echo "Mac下镜像飞速安装Homebrew教程(备用) :https://zhuanlan.zhihu.com/p/90508170" echo "如果你想换源,可以使用镜像助手:https://brew.idayer.com/guide/change-source/" @@ -981,7 +990,7 @@ ohai "Downloading and installing Homebrew..." execute "${MKDIR[@]}" "${HOMEBREW_CORE}" cd "${HOMEBREW_CORE}" >/dev/null || return - execute "git" "init" "-q" + execute "git" "-c" "init.defaultBranch=master" "init" "--quiet" execute "git" "config" "remote.origin.url" "${HOMEBREW_CORE_GIT_REMOTE}" execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*" execute "git" "config" "--bool" "core.autocrlf" "false" @@ -1003,7 +1012,7 @@ ohai "Downloading and installing Homebrew..." execute "${MKDIR[@]}" "${HOMEBREW_CASK}" cd "${HOMEBREW_CASK}" >/dev/null || return - execute "git" "init" "-q" + execute "git" "-c" "init.defaultBranch=master" "init" "--quiet" execute "git" "config" "remote.origin.url" "${HOMEBREW_CASK_GIT_REMOTE}" execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*" execute "git" "config" "--bool" "core.autocrlf" "false" @@ -1016,16 +1025,16 @@ ohai "Downloading and installing Homebrew..." ) || exit 1 fi - if [[ -n "${HOMEBREW_NO_INSTALL_FROM_API-}" && ! -d "${HOMEBREW_SERVICES}" ]] + if [[ ! -d "${HOMEBREW_SERVICES}" ]] then # Always use single-quoted strings with `exp` expressions # shellcheck disable=SC2016 - ohai 'Tapping homebrew/services because `$HOMEBREW_NO_INSTALL_FROM_API` is set.' + ohai 'Tapping homebrew/services' ( execute "${MKDIR[@]}" "${HOMEBREW_SERVICES}" cd "${HOMEBREW_SERVICES}" >/dev/null || return - execute "git" "init" "-q" + execute "git" "-c" "init.defaultBranch=master" "init" "--quiet" execute "git" "config" "remote.origin.url" "${HOMEBREW_SERVICES_DEFAULT_GIT_REMOTE}" execute "git" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*" execute "git" "config" "--bool" "core.autocrlf" "false" @@ -1047,23 +1056,12 @@ then warn "${HOMEBREW_PREFIX}/bin is not in your PATH." fi -ohai "Installation successful!" +ohai "🎉 恭喜,安装成功!" echo ring_bell -# Use an extra newline and bold to avoid this being missed. -ohai "Homebrew has enabled anonymous aggregate formulae and cask analytics." -echo "$( - cat <> ${shell_profile} - eval "\$(${HOMEBREW_PREFIX}/bin/brew shellenv)" + cat >> ${shell_profile} <> ${shell_profile} <> %s\n" "${shell_profile}" - printf " echo '%s' >> ${shell_profile}\n" "${additional_shellenv_commands[@]}" - printf " %s\n" "${additional_shellenv_commands[@]}" + +checkExecute +source "${shell_profile}" +if [ $? -ne 0 ];then + echo "$( + cat <