logo
0
0
WeChat Login
✨ feat(seed): 生成 templates/.metadata.json 支撑 cnb-rs license list --featured

license-templates

开源许可证模板镜像,供 cnb-rs repo license 命令使用。

上游同步状态

上游 commited8cbf2
同步时间2026-05-17 23:17:19 +08:00 (Asia/Shanghai)
许可证数量47

内容来源

来自 github/choosealicense.com _licenses/ 目录的全部许可证文本。

  • 站点本身 License:MIT
  • 各许可证文本:归属对应组织(FSF、Apache Foundation、OSI 等),与许可证内容一同分发

仓库结构

.
├── README.md
├── seed.py             # 一键从上游同步许可证的脚本
├── .gitignore          # 忽略 seed.py 的临时目录
└── templates/          # 许可证数据全部在这里,与脚手架文件隔离
    ├── .metadata.json  # 元数据:spdx_id / title / featured / hidden(cnb-rs --featured 读这里)
    ├── 0BSD.txt
    ├── Apache-2.0.txt
    ├── ...
    └── Zlib.txt        # 共 47 个

cnb-rs repo license list/view 命令只读 templates/ 子目录,根目录的 README.md / seed.py / .gitignore 不会污染许可证列表。

.metadata.jsonseed.py 在解析上游 frontmatter 时一并生成,schema:

{
  "version": 1,
  "upstream_commit": "<sha>",
  "synced_at": "<北京时间>",
  "total": 47,
  "featured_count": 3,
  "hidden_count": 0,
  "licenses": [
    {"spdx_id": "MIT", "title": "MIT License", "featured": true, "hidden": false}
  ]
}

cnb-rs repo license list --featured 会拉这个文件 filter;如果文件缺失(手动维护的仓库 / 旧版 seed),会 fallback 列出所有 license 并给出 warn。

命名约定

每个许可证放在 templates/ 目录,文件名 <SPDX-ID>.txt,SPDX-ID 大小写遵循 SPDX 标准,取自上游 frontmatter 的 spdx-id 字段(如 MITApache-2.0BSD-3-Clause)。

内容处理

文件内容已剥除上游的 YAML frontmatter(站点用元数据,含 title / description / permissions 等字段),仅保留可直接复制到项目 LICENSE 文件的纯文本。

许可证文本中的 [year] / [fullname] / [email] 等占位符保留原样,由使用者按需替换。

使用方式

# 列出所有可用许可证(自动剥除 .txt 后缀)
cnb-rs repo license list

# 只列 featured 的(MIT / Apache-2.0 / GPL-3.0)
cnb-rs repo license list --featured

# 查看指定许可证内容(裸 SPDX-ID 即可)
cnb-rs repo license view MIT
cnb-rs repo license view Apache-2.0

# 直接写入到当前目录的 LICENSE
cnb-rs repo license view MIT > LICENSE

同步上游

需要 Python 3.8+ 和 git 在 PATH。脚本只用标准库,无需 pip install

# 默认从 GitHub 直连,跑完停在 staged 区让你 review
python3 seed.py

# 或指定镜像(国内场景)/ 已有的本地 clone
python3 seed.py --upstream https://kkgithub.com/github/choosealicense.com.git
python3 seed.py --upstream /path/to/local/github-choosealicense-clone

# 跑完自动 git commit(commit message 自动带 SHA + 北京时间 + 许可证数)
python3 seed.py --commit

# 跑完自动 commit + push(--push 隐含 --commit)
python3 seed.py --commit --push

seed.py 会:

  1. 克隆 upstream 到 _upstream/(远端 URL 走 --depth=1,本地路径全量 clone;目录被 .gitignore 忽略)
  2. templates/ 整个清空后重建
  3. 遍历 _upstream/_licenses/*.txt,剥 YAML frontmatter,按 spdx-id 字段重命名为 templates/<SPDX-ID>.txt
  4. title / featured / hidden / nickname 字段写到 templates/.metadata.json
  5. 用上游最新 commit SHA + 当前北京时间(UTC+8) + 许可证数量更新本 README 的 BEGIN_UPSTREAM_STATUS ... END_UPSTREAM_STATUS
  6. 清理 _upstream/
  7. 如果传了 --commit,自动 git add -A && git commit;如果传了 --push,再 git push

不带 --commit 时跑完会停留,请 git diff 检查改动后手工 commit & push。