使用 Bash 管理 Homebrew 内置及自定义镜像配置,支持 Zsh 命令补全。
- 内置清华 TUNA、阿里云和 Homebrew 官方源。
- 查看、切换和管理镜像配置。
- 新增、导入和删除本机自定义镜像。
- 使用
XDG_CONFIG_HOME管理用户配置。 - 通过白名单解析配置文件,不执行用户配置中的 Shell 代码。
- 提供 Zsh 命令补全。
- macOS。
- Bash 3.2 及以上版本。
- Homebrew。
- Zsh,仅在需要命令补全时使用。
克隆仓库后执行:
./install.sh安装脚本会创建以下软链接:
~/.local/bin/brew-mirror
${XDG_DATA_HOME:-$HOME/.local/share}/zsh/site-functions/_brew-mirror
脚本不会自动修改 .zshrc。请确认 ~/.local/bin 已加入 PATH:
export PATH="$HOME/.local/bin:$PATH"在 Oh My Zsh 初始化之前加入补全目录:
fpath=("${XDG_DATA_HOME:-$HOME/.local/share}/zsh/site-functions" $fpath)然后加入当前镜像配置的加载逻辑:
if [ -r "${XDG_CONFIG_HOME:-$HOME/.config}/homebrew-mirror/current.zsh" ]; then
source "${XDG_CONFIG_HOME:-$HOME/.config}/homebrew-mirror/current.zsh"
fi重新打开终端或执行:
source ~/.zshrc| 命令 | 说明 |
|---|---|
list |
列出全部镜像 |
current |
显示当前镜像配置 |
show <镜像> |
显示指定镜像配置 |
use <镜像> |
持久化切换镜像 |
env <镜像> |
查看镜像对应的环境变量 |
add <镜像> [选项] |
新增自定义镜像 |
import <配置文件> |
导入自定义镜像 |
remove <镜像> |
删除自定义镜像 |
自定义镜像默认保存在:
~/.config/homebrew-mirror/profiles/<镜像名称>.conf
例如,执行 brew-mirror add example ... 后会生成:
~/.config/homebrew-mirror/profiles/example.conf
如果设置了 XDG_CONFIG_HOME,则配置保存在:
$XDG_CONFIG_HOME/homebrew-mirror/profiles/<镜像名称>.conf
当前正在使用的镜像会记录在:
${XDG_CONFIG_HOME:-$HOME/.config}/homebrew-mirror/current.zsh
自定义镜像配置和 current.zsh 的文件权限均为 600。这些文件位于仓库之外,不会被提交到 Git。
brew-mirror add example \
--label "示例镜像" \
--brew-git-remote "https://example.com/homebrew/brew.git" \
--core-git-remote "https://example.com/homebrew/homebrew-core.git" \
--api-domain "https://example.com/homebrew-bottles/api" \
--bottle-domain "https://example.com/homebrew-bottles"自定义镜像名称只能包含小写字母、数字、短横线和下划线。至少需要提供一个 Git、API 或 Bottles 地址。
配置文件格式如下:
MIRROR_NAME=example
MIRROR_LABEL=示例镜像
HOMEBREW_BREW_GIT_REMOTE=https://example.com/homebrew/brew.git
HOMEBREW_CORE_GIT_REMOTE=https://example.com/homebrew/homebrew-core.git
HOMEBREW_API_DOMAIN=https://example.com/homebrew-bottles/api
HOMEBREW_BOTTLE_DOMAIN=https://example.com/homebrew-bottles
HOMEBREW_INSTALL_FROM_API=1
导入并使用:
brew-mirror import ./example.conf
brew-mirror use example
source ~/.zshrc删除自定义镜像:
brew-mirror remove example内置镜像不能删除,当前正在使用的自定义镜像需要先切换后才能删除。
./uninstall.sh卸载脚本只删除由当前仓库创建的命令和补全软链接,不会删除用户镜像配置。卸载后可以按需手动清理 .zshrc 中的接入代码。
执行隔离测试:
./tests/test.sh测试使用临时 XDG_CONFIG_HOME,不会修改真实的 Homebrew 镜像配置。GitHub Actions 会在 macOS 和 Ubuntu 上运行同一套测试。