Mac OS下用Homebrew安装自己写的开源工具

3,683 阅读2分钟
本文主要介绍了适用于Mac OS系统的一款软件包管理工具Homebrew,并详细说明了新建Formula的步骤。
上篇文章回顾:Nginx动态发现方案与实践

1、Homebrew 介绍

Homebrew[1]是一款 Mac OS 平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。Homebrew 可以说已经成为使用 Mac 电脑程序员的标配。做为开源软件的贡献者,本文将向大家介绍一下如何能够让自己的开源项目通过 Homebrew 进行软件包管理。

brew install foo

2、Formula VS. Cask

Homebrew 支持多种形式的软件包管理,常见的有 Formula 和 Cask 两种。Formula 一般用来源码编译安装开源的命令行工具,Cask 一般用来安装支持UI的应用程序。官方 Formula 对应的 Github 项目为 homebrew-core[2], Cask 对应的 Github 项目为homebrew-cask[3]。建议先 Fork 相应的 Git 项目到自己的账号下再添加新的工具。文章篇幅所限,本文只会介绍 Formula 的创建使用方法,Cask 的创建方法类似。

3、新建 Formula 步骤

1、本地 Git 仓库准备

cd `brew --repo`/Library/Taps/homebrew/homebrew-core
git remote add ${your_github_id} https://github.com/${your_github_id}/homebrew-core.git
git fetch ${your_github_id}
git checkout -b ${your_github_id} ${your_github_id}/master

2、新建 Formula 脚本文件

# 执行该命令后将自动在 Formula 目录下创建一个名为foo.rb的文件,文本编辑器也会自动打开该文件。
brew create foo

3、编辑 Formula 文件

4、测试安装

# 安装
brew install foo
# 下载源码安装而不是下载编译好的 bottle 版本,下面链接是对 bottle 的介绍
# https://docs.brew.sh/Bottle
brew reinstall --build-from-source foo
# 检查 Formula 脚本格式,超好用的 linter 工具
brew audit --strict foo

5、提交代码等待 PR 合并

Homebrew 的社区相当活跃,一般 PR 提交24小时内就能得到项目维护人的回复。为了让自已写的工具尽快被收录,建议提交 PR 前认真阅读 Issue Template,并按照建议修改。下面是官方提供的 PR 建议。

- [ ] Have you followed the [guidelines for contributing](https://github.com/Homebrew/homebrew-core/blob/master/CONTRIBUTING.md)?
- [ ] Have you checked that there aren't other open [pull requests](https://github.com/Homebrew/homebrew-core/pulls) for the same formula update/change?
- [ ] Have you built your formula locally with `brew install --build-from-source <formula>`, where `<formula>` is the name of the formula you're submitting?
- [ ] Does your build pass `brew audit --strict <formula>` (after doing `brew install <formula>`)?


参考链接

[1] https://brew.sh/

[2] https://github.com/Homebrew/homebrew-core

[3] https://github.com/Homebrew/homebrew-cask

[4] https://docs.brew.sh/Formula-Cookbook#homebrew-terminology

[5] https://docs.brew.sh/Formula-Cookbook

[6] https://github.com/Homebrew


本文首发于公众号“小米运维”,点击查看原文