高效工作-自动化脚本-Homebrew脚本

167 阅读2分钟

本文内容

脚本内容

  1. 替换了homebrew安装地址为清华镜像地址
  2. 替换了homebrew软件依赖的相关地址为清华镜像地址
  3. 安装homebrew「已经安装则跳过」
  4. 配置homebrew环境变量
  5. 安装wget和dpkg

Homebrew迭代变更

注:自brew 3.3.0 (2021 年 10 月 25日) 起,Linuxbrew 核心仓库 linuxbrew-core 已被弃用。

注:自brew 4.0.0 (2023 年 2 月 16日) 起,HOMEBREW_INSTALL_FROM_API 会成为默认行为,无需设置。大部分用户无需再克隆 homebrew-core 仓库。

注:自brew 4.0.22 (2023 年 6 月 12 日) 起,homebrew-cask-drivers 已被弃用,所有 cask 合并至 homebrew-cask 仓库。

Chatgpt

在写这个脚本的过程中,chatgpt占了80%的功劳,我用的chatgpt见

ai.dooocs.com/

密码: www.dooocs.com/chatgpt/REA…

参考文章

mirrors.tuna.tsinghua.edu.cn/help/homebr… juejin.cn/post/726492… juejin.cn/post/726681…

安装脚本


# homebrew安装脚本
install_homebrew() {
    # 下载 Homebrew 安装脚本
    curl -v https://logan-download.oss-cn-hangzhou.aliyuncs.com/homebrew_install.sh > install-brew.sh

    
    # 设置镜像
    export HOMEBREW_INSTALL_FROM_API=1
    export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
    export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
    export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
    export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
    

    # 从本镜像下载安装脚本并安装 Homebrew / Linuxbrew
    git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
    /bin/bash brew-install/install.sh
    rm -rf brew-install
}

# 检查homebrew是否已经安装,如果未安装,则执行安装脚本
if ! command -v brew &> /dev/null; then
    echo "Homebrew not found. Installing..."
    install_homebrew
    echo "Homebrew is installed."
else
    echo "Homebrew is already installed."
fi

# 安装环境变量
echo "准备安装Homebrew对应的环境变量"
echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> ~/.zshrc
eval $(/opt/homebrew/bin/brew shellenv)
source ~/.zshrc
echo "环境变量安装成功!"

# 替换brew 源为清华的数据源
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"

# 注:自 brew 4.0 起,大部分 Homebrew 用户无需设置 homebrew/core 和 homebrew/cask 镜像,只需设置 HOMEBREW_API_DOMAIN 即可。
# 如果需要使用 Homebrew 的开发命令 (如 `brew cat <formula>`),则仍然需要设置 homebrew/core 和 homebrew/cask 镜像。
# 请按需执行如下两行命令:
brew tap --custom-remote --force-auto-update homebrew/core https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
brew tap --custom-remote --force-auto-update homebrew/cask https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

# 除 homebrew/core 和 homebrew/cask 仓库外的 tap 仓库仍然需要设置镜像
brew tap --custom-remote --force-auto-update homebrew/cask-fonts https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
brew tap --custom-remote --force-auto-update homebrew/cask-versions https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-versions.git
brew tap --custom-remote --force-auto-update homebrew/command-not-found https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-command-not-found.git
brew tap --custom-remote --force-auto-update homebrew/services https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-services.git
brew update

# 或使用下面的几行命令自动设置
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
for tap in core cask{,-fonts,-versions} command-not-found services; do
    brew tap --custom-remote --force-auto-update "homebrew/${tap}" "https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-${tap}.git"
done
brew update

echo "替换成功...."

# 使用brew安装基础的终端能力
check_and_install_dependencies() {
    dependency=$1
    if ! command -v $dependency &> /dev/null; then
        echo "$dependency is not installed, installing..."
        brew install $dependency
    else
        echo "$dependency is already installed"
    fi
}

echo "开始安装wget..."
check_and_install_dependencies wget
echo "wget 安装成功..."
echo "开始安装dpkg..."
check_and_install_dependencies dpkg
echo "dpkg 安装成功..."