pkgmgr-homebrew-formula-dev
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHomebrew Formula Development
Homebrew Formula开发
Guide for researching, creating, testing, and maintaining Homebrew formulas in a custom tap.
本指南介绍了在自定义tap中研究、创建、测试和维护Homebrew formula的方法。
When to Use This Skill
何时使用本技能
- Creating a new Homebrew formula for a project
- Debugging formula build or test failures
- Running local validation before CI
- Understanding Homebrew's Ruby DSL
- Setting up livecheck for automatic version detection
- 为项目创建新的Homebrew formula
- 调试formula构建或测试失败的问题
- 在CI前运行本地验证
- 理解Homebrew的Ruby DSL
- 设置livecheck以自动检测版本
Template Pipeline
模板流水线
This skill includes a JSON Schema → Mustache template pipeline for generating formulas from structured data.
本技能包含一条JSON Schema → Mustache模板流水线,用于从结构化数据生成formula。
Workflow
工作流程
- Create a JSON file conforming to
scripts/formula.schema.ts - Run to validate and render
just template-formula <path-to-json> - The pipeline validates with AJV, preprocesses (PascalCase, language dispatch, license rendering), then renders via Mustache
- 创建符合规范的JSON文件
scripts/formula.schema.ts - 运行命令进行验证和渲染
just template-formula <path-to-json> - 流水线会通过AJV进行验证,然后进行预处理(转换为PascalCase、语言分发、许可证渲染),最后通过Mustache完成渲染
Key Files
关键文件
| File | Purpose |
|---|---|
| JSON Schema (draft-2020-12) defining formula structure |
| Preprocessing: PascalCase, license rendering, install flattening, partials loading |
| Main template — renders all shared fields, dispatches to language partials |
| Language-specific install partials (go, rust, python, zig, cmake, autotools, meson) |
| Test fixtures covering each scenario |
| Test cases that validate rendered output |
| 文件 | 用途 |
|---|---|
| 定义formula结构的JSON Schema(draft-2020-12版本) |
| 预处理:转换为PascalCase、许可证渲染、安装信息扁平化、加载部分模板 |
| 主模板——渲染所有共享字段,分发到语言相关的部分模板 |
| 特定语言的安装部分模板(go、rust、python、zig、cmake、autotools、meson) |
| 覆盖各种场景的测试用例数据 |
| 验证渲染输出的测试用例 |
Running
运行方式
bash
undefinedbash
undefinedRender a formula from JSON
从JSON文件渲染formula
just template-formula path/to/formula.json
just template-formula path/to/formula.json
Run all tests
运行所有测试
just test
undefinedjust test
undefinedAdding a New Language
添加新语言
- Add definition to
install-<lang>scripts/formula.schema.ts - Add language dispatch entry in the
allOfdefinitionformula - Create partial
reference/templates/langs/<lang>.mustache - Add to the
"<lang>"enumlanguage - Add a test fixture in and test case in
test/data/test/cases/
- 在中添加
scripts/formula.schema.ts定义install-<lang> - 在定义中添加语言分发的
formula条目allOf - 创建部分模板
reference/templates/langs/<lang>.mustache - 在枚举中添加
language"<lang>" - 在中添加测试用例数据,在
test/data/中添加测试用例test/cases/
Research Phase
调研阶段
Before creating a formula, gather this information:
| Field | How to Find |
|---|---|
| Latest version | |
| License | Check LICENSE file or repo metadata (use SPDX identifier) |
| Build system | Look at Makefile, go.mod, Cargo.toml, pyproject.toml, etc. |
| Dependencies | Check build docs, CI files, or dependency manifests |
| Default branch | Check repo settings — may be |
| Binary name | May differ from formula name — check Cargo.toml |
在创建formula之前,需要收集以下信息:
| 字段 | 获取方式 |
|---|---|
| 最新版本 | |
| 许可证 | 查看LICENSE文件或仓库元数据(使用SPDX标识符) |
| 构建系统 | 查看Makefile、go.mod、Cargo.toml、pyproject.toml等文件 |
| 依赖项 | 查看构建文档、CI文件或依赖清单 |
| 默认分支 | 查看仓库设置——可能是 |
| 二进制文件名 | 可能与formula名称不同——查看Cargo.toml的 |
Determine Formula Type
确定Formula类型
| Scenario | Type | Has | Has |
|---|---|---|---|
| Tagged releases | Standard | Yes | Yes |
| No releases | HEAD-only | No | No |
| Monorepo subdirectory | Standard | Yes | Yes |
| 场景 | 类型 | 是否包含 | 是否包含 |
|---|---|---|---|
| 带标签的版本 | 标准类型 | 是 | 是 |
| 无正式版本 | 仅HEAD版本 | 否 | 否 |
| 单体仓库的子目录 | 标准类型 | 是 | 是 |
Calculate SHA256
计算SHA256值
bash
curl -sL "https://github.com/owner/repo/archive/refs/tags/vX.Y.Z.tar.gz" | shasum -a 256bash
curl -sL "https://github.com/owner/repo/archive/refs/tags/vX.Y.Z.tar.gz" | shasum -a 256Formula Naming
Formula命名规则
- Formula name: kebab-case (,
hex-patch)jwt-ui - Class name: PascalCase (,
HexPatch)JwtUi
- Formula名称:短横线分隔小写(kebab-case)(例如、
hex-patch)jwt-ui - 类名:大驼峰式(PascalCase)(例如、
HexPatch)JwtUi
Formula Structure
Formula结构
File Location
文件位置
Formulas are organized alphabetically:
Formula/<first-letter>/<name>.rbFormula按字母顺序组织:
Formula/<首字母>/<名称>.rbKey Elements
核心元素
| Element | Purpose |
|---|---|
| Short description (~80 chars) for |
| Project homepage URL |
| Source tarball URL (omit for HEAD-only) |
| Checksum (omit for HEAD-only) |
| SPDX identifier |
| Git URL for |
| Auto-detect new versions (omit for HEAD-only) |
| Build or runtime dependencies |
| Verification block |
| 元素 | 用途 |
|---|---|
| 简短描述(约80字符),用于 |
| 项目主页URL |
| 源码压缩包URL(仅HEAD版本可省略) |
| 校验和(仅HEAD版本可省略) |
| SPDX标识符 |
| 用于 |
| 自动检测新版本(仅HEAD版本可省略) |
| 构建或运行时依赖项 |
| 验证代码块 |
SPDX License Identifiers
SPDX许可证标识符
| License | SPDX |
|---|---|
| MIT | |
| Apache 2.0 | |
| GPL 3.0 (only) | |
| GPL 3.0 (or later) | |
| BSD 2-Clause | |
| BSD 3-Clause | |
Always specify or for GPL/LGPL/AGPL.
-only-or-later| 许可证 | SPDX标识符 |
|---|---|
| MIT | |
| Apache 2.0 | |
| GPL 3.0(仅) | |
| GPL 3.0(或更新版本) | |
| BSD 2-Clause | |
| BSD 3-Clause | |
对于GPL/LGPL/AGPL许可证,必须指定或。
-only-or-laterLanguage-Specific Patterns
特定语言的模式
Each language has a reference doc with install patterns, schema fields, and common issues:
- Go:
reference/langs/go.md - Rust:
reference/langs/rust.md - Python:
reference/langs/python.md
Additional languages supported by the template pipeline (cmake, autotools, meson, zig) — see their Mustache partials in .
reference/templates/langs/每种语言都有对应的参考文档,包含安装模式、Schema字段和常见问题:
- Go:
reference/langs/go.md - Rust:
reference/langs/rust.md - Python:
reference/langs/python.md
模板流水线还支持其他语言(cmake、autotools、meson、zig)——可查看中的Mustache部分模板。
reference/templates/langs/Reference Materials
参考资料
| Topic | Location |
|---|---|
| Local validation steps | |
| Common issues & FAQ | |
| Test block patterns | |
| Generated formula examples | |
| JSON Schema definition | |
| Bottle attestation & provenance | |
| 主题 | 位置 |
|---|---|
| 本地验证步骤 | |
| 常见问题与FAQ | |
| 测试代码块模式 | |
| 生成的formula示例 | |
| JSON Schema定义 | |
| Bottle认证与溯源 | |
Batch Formula Creation
批量创建Formula
When creating many formulas at once:
- Compute SHA256 hashes in parallel — launch multiple calls concurrently
curl | shasum - Research build details in parallel — check build manifests concurrently
- Write all formula files — no dependencies between them
- Create branches/PRs sequentially — one branch per formula, each from main
- Use to syntax-check all formulas before pushing
ruby -c *.rb
当需要批量创建多个formula时:
- 并行计算SHA256哈希值——同时启动多个命令
curl | shasum - 并行调研构建细节——同时检查多个构建清单
- 编写所有formula文件——文件之间无依赖关系
- 按顺序创建分支/PR——每个formula对应一个分支,均基于main分支创建
- **使用**在推送前语法检查所有formula
ruby -c *.rb
Architecture-Specific Binaries
特定架构的二进制文件
When a project provides pre-built binaries for different architectures:
Preferred: Build from source (avoids architecture complexity)
If pre-built binaries required: Use resource blocks, NOT url/sha256 in on_arm/on_intel:
ruby
on_arm do
resource "binary" do
url "https://github.com/org/repo/releases/download/vX.Y.Z/tool-darwin-arm64.tar.gz"
sha256 "..."
end
end
on_intel do
resource "binary" do
url "https://github.com/org/repo/releases/download/vX.Y.Z/tool-darwin-amd64.tar.gz"
sha256 "..."
end
end
def install
resource("binary").stage do
bin.install "tool"
end
endSee for details on deprecated patterns.
reference/faq/common.md当项目为不同架构提供预编译二进制文件时:
首选方式: 从源码构建(避免架构复杂性)
如果必须使用预编译二进制文件: 使用resource块,不要在on_arm/on_intel中使用url/sha256:
ruby
on_arm do
resource "binary" do
url "https://github.com/org/repo/releases/download/vX.Y.Z/tool-darwin-arm64.tar.gz"
sha256 "..."
end
end
on_intel do
resource "binary" do
url "https://github.com/org/repo/releases/download/vX.Y.Z/tool-darwin-amd64.tar.gz"
sha256 "..."
end
end
def install
resource("binary").stage do
bin.install "tool"
end
end关于已弃用的模式,可查看获取详情。
reference/faq/common.mdChecklist
检查清单
- Research complete (version, license, build system, deps, binary name, default branch)
- Formula type determined (standard vs HEAD-only)
- SHA256 calculated (if not HEAD-only)
- Formula file created at
Formula/<letter>/<name>.rb - passes (syntax check)
ruby -c - passes
brew audit --new - passes (or issues addressed)
brew style - succeeds
brew install --build-from-source - passes
brew test - Binary executes correctly
- PR created with CI passing
- 调研完成(版本、许可证、构建系统、依赖项、二进制文件名、默认分支)
- 确定Formula类型(标准类型 vs 仅HEAD版本)
- 计算SHA256值(非仅HEAD版本)
- 在路径下创建Formula文件
Formula/<首字母>/<名称>.rb - 语法检查通过
ruby -c - 检查通过
brew audit --new - 检查通过(或已处理相关问题)
brew style - 安装成功
brew install --build-from-source - 测试通过
brew test - 二进制文件可正常执行
- 创建PR且CI检查通过