Repository: RubyMetric/chsrc Branch: main Commit: a56f330d03ce Files: 178 Total size: 569.3 KB Directory structure: gitextract_0jn8y55u/ ├── .editorconfig ├── .github/ │ ├── FUNDING.yml │ ├── ISSUE_TEMPLATE/ │ │ ├── 01-Report_Bug.yml │ │ ├── 02-Request_Recipe.yml │ │ ├── 03-Share.yml │ │ ├── 04-Deprecate.yml │ │ └── config.yml │ ├── PULL_REQUEST_TEMPLATE/ │ │ ├── Add_Feature.md │ │ ├── Clean.md │ │ ├── Enhance.md │ │ ├── Fix_Bug.md │ │ ├── Implement.md │ │ ├── Refactor.md │ │ └── Relate_Mirror_Source.md │ ├── READIT.md │ ├── copilot-instructions.md │ ├── dependabot.yml │ ├── pull_request_template.md │ └── workflows/ │ ├── PR-notify.yml │ ├── PR-test.yml │ ├── build-on-Linux-AArch64.yml │ ├── build-on-Linux-ARMv7.yml │ ├── build-on-Linux-riscv64.yml │ ├── build-on-Linux-x64.yml │ ├── build-on-Windows.yml │ ├── build-on-macOS.yml │ ├── pkg-deb.yml │ ├── pub-AUR-chsrc-and-chsrc-bin.yml │ ├── pub-AUR-chsrc-git.yml │ └── pub-WinGet.yml ├── .gitignore ├── .vscode/ │ ├── README.md │ ├── c_cpp_properties.json │ ├── extensions.json │ ├── launch.json │ ├── settings.json │ └── tasks.json ├── COPYING ├── LICENSE-MIT.txt ├── Makefile ├── README.md ├── bootstrap/ │ ├── README.md │ └── Termux.bash ├── doc/ │ ├── 01-开发与构建.md │ ├── 02-提交与贡献.md │ ├── 03-为什么拒绝使用代码格式化工具.md │ ├── 10-如何编写recipe.md │ ├── 11-如何设置换源链接与测速链接.md │ ├── 50-协作者与维护者.md │ ├── README.md │ ├── chsrc.1 │ └── chsrc.texi ├── include/ │ └── .keep ├── justfile ├── lefthook.yml ├── lib/ │ └── xy.h ├── pkg/ │ ├── AUR/ │ │ └── README.md │ ├── README.md │ └── deb/ │ ├── BUILD.md │ ├── CI.md │ ├── Makefile │ ├── README.md │ ├── deb-installation-test.sh │ └── debian/ │ ├── changelog │ ├── control │ ├── copyright │ ├── postinst │ ├── prerm │ └── rules ├── src/ │ ├── chsrc-main.c │ ├── framework/ │ │ ├── chef.c │ │ ├── core.c │ │ ├── helper.c │ │ ├── mirror.c │ │ ├── struct.h │ │ └── version.h │ ├── rawstr4c.h │ ├── rawstr4c.md │ ├── recipe/ │ │ ├── lang/ │ │ │ ├── Clojure.c │ │ │ ├── Dart/ │ │ │ │ ├── Flutter.c │ │ │ │ ├── Pub.c │ │ │ │ └── common.h │ │ │ ├── Go.c │ │ │ ├── Haskell.c │ │ │ ├── Java.c │ │ │ ├── JavaScript/ │ │ │ │ ├── Bun.c │ │ │ │ ├── JavaScript.c │ │ │ │ ├── Yarn.c │ │ │ │ ├── common.h │ │ │ │ ├── npm.c │ │ │ │ ├── nvm.c │ │ │ │ └── pnpm.c │ │ │ ├── Julia.c │ │ │ ├── Lua.c │ │ │ ├── NuGet.c │ │ │ ├── OCaml.c │ │ │ ├── PHP.c │ │ │ ├── Perl.c │ │ │ ├── Python/ │ │ │ │ ├── PDM.c │ │ │ │ ├── Poetry.c │ │ │ │ ├── Python.c │ │ │ │ ├── Rye.c │ │ │ │ ├── common.h │ │ │ │ ├── pip.c │ │ │ │ ├── rawstr4c.h │ │ │ │ ├── rawstr4c.md │ │ │ │ └── uv.c │ │ │ ├── R.c │ │ │ ├── Ruby/ │ │ │ │ ├── README.md │ │ │ │ └── Ruby.c │ │ │ ├── Rust/ │ │ │ │ ├── Cargo.c │ │ │ │ ├── common.h │ │ │ │ └── rustup.c │ │ │ ├── rawstr4c.h │ │ │ └── rawstr4c.md │ │ ├── menu.c │ │ ├── os/ │ │ │ ├── APT/ │ │ │ │ ├── Armbian.c │ │ │ │ ├── Debian.c │ │ │ │ ├── Kali-Linux.c │ │ │ │ ├── Linux-Lite.c │ │ │ │ ├── Linux-Mint.c │ │ │ │ ├── ROS.c │ │ │ │ ├── Raspberry-Pi-OS.c │ │ │ │ ├── Termux.c │ │ │ │ ├── Trisquel.c │ │ │ │ ├── Ubuntu.c │ │ │ │ ├── common.h │ │ │ │ ├── deepin.c │ │ │ │ ├── openKylin.c │ │ │ │ ├── rawstr4c.h │ │ │ │ └── rawstr4c.md │ │ │ ├── Alpine-Linux.c │ │ │ ├── BSD/ │ │ │ │ ├── FreeBSD.c │ │ │ │ ├── NetBSD.c │ │ │ │ └── OpenBSD.c │ │ │ ├── Gentoo-Linux.c │ │ │ ├── OpenWrt.c │ │ │ ├── Solus.c │ │ │ ├── Void-Linux.c │ │ │ ├── YUM/ │ │ │ │ ├── AlmaLinux.c │ │ │ │ ├── Anolis-OS.c │ │ │ │ ├── Fedora-Linux.c │ │ │ │ ├── Rocky-Linux.c │ │ │ │ ├── common.h │ │ │ │ └── openEuler.c │ │ │ ├── openSUSE.c │ │ │ ├── pacman/ │ │ │ │ ├── Arch-Linux.c │ │ │ │ ├── MSYS2.c │ │ │ │ └── Manjaro-Linux.c │ │ │ ├── rawstr4c.h │ │ │ └── rawstr4c.md │ │ ├── recipe-template.c │ │ └── ware/ │ │ ├── Anaconda/ │ │ │ ├── Anaconda.c │ │ │ ├── rawstr4c.h │ │ │ └── rawstr4c.md │ │ ├── CocoaPods.c │ │ ├── Docker/ │ │ │ ├── Docker.c │ │ │ ├── README.md │ │ │ ├── rawstr4c.h │ │ │ └── rawstr4c.md │ │ ├── Emacs.c │ │ ├── Flatpak.c │ │ ├── Guix.c │ │ ├── Homebrew/ │ │ │ ├── Homebrew.c │ │ │ ├── rawstr4c.h │ │ │ └── rawstr4c.md │ │ ├── Nix.c │ │ ├── TeX-Live.c │ │ └── WinGet.c │ └── resource/ │ └── chsrc.rc ├── test/ │ ├── cli.pl │ ├── fw.c │ └── xy.c └── tool/ ├── README.md ├── download-pre-on-GitHub.ps1 ├── git-ignore-vscode-settings.ps1 ├── installer.ps1 └── installer.sh ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ # -------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # -------------------------------------------------------------- # Config Type : EditorConfig # Config Authors: 曾奥然 # Contributors : Nil Null # Created On : <2023-09-06> # Last Modified : <2025-08-27> # # 请参考 ./doc/03-为什么拒绝使用代码格式化工具.md # # http://editorconfig.org # -------------------------------------------------------------- root = true [*] indent_style = space indent_size = 2 charset = utf-8 trim_trailing_whitespace = true # VS Code 对该配置(为 false)的实现有问题,这是确定的, # 然而 CLion 似乎对该配置(为 false)的实现是正确的,这导致不同贡献者反而产生了冲突 # 所以我们现在改成 true insert_final_newline = true [*.{c,C,cpp,cxx,cc,h,hpp}] indent_style = space indent_size = 2 [*.pl] indent_size = 4 [*.{raku,rakumod,rakutest}] indent_size = 2 [*.ps1] indent_size = 4 [*.{sh,bash}] indent_size = 2 [Makefile,makefile,*.{mk,make,makefile}] indent_style = tab # 使用 VS Code 生成文件的默认格式 [*.json] indent_size = 4 [*.{yaml,yml}] indent_size = 2 ================================================ FILE: .github/FUNDING.yml ================================================ custom: [ 'https://afdian.com/a/ccmywish', 'https://github.com/ccmywish/support-my-oss-work' ] ================================================ FILE: .github/ISSUE_TEMPLATE/01-Report_Bug.yml ================================================ name: 🐞 Bug 报告 description: 有 Bug 了吗? title: "详细报告BUG是chsrc用户的一大美德" # labels: [ ] type: Bug body: - type: markdown attributes: value: | 感谢花时间填写此 Bug 报告! 1. 你可能是通过包管理器安装的 `chsrc`,其版本往往稍旧,你可先尝试使用REAME中提供的安装命令来获取最新版本 2. 有时 Bug 是在最新版本中引入的,此时你可在安装命令时指定 `-v` 参数临时使用旧版本解决燃眉之急,详情查看README - type: textarea id: what-did-you-do attributes: label: 你操作了什么? # description: 告诉我们,在问题出现之前你使用 chsrc 做了什么? placeholder: 我运行了 chsrc set ... validations: required: true - type: textarea id: what-happened attributes: label: 发生了什么? # description: 告诉我们,发生了什么你认为不该出现的事?屏幕截图或视频记录都很有帮助 placeholder: 换源没有成功... validations: required: true - type: textarea id: what-should-happen attributes: label: 本应该怎么样? # description: 告诉我们,正常的或你期望看到的 运行结果和状态 placeholder: 我希望 chsrc 成功帮我换源... validations: required: true - type: input id: version attributes: label: chsrc 版本 # description: 你正在使用 chsrc 哪个版本? placeholder: 请使用 chsrc -h 或 chsrc -v 查看,并*复制发布日期* validations: required: true - type: dropdown id: os attributes: label: 你使用的是哪个操作系统? multiple: true options: - Windows - Linux - macOS - FreeBSD - NetBSD - OpenBSD - 其他 validations: required: true - type: input id: os-version attributes: label: OS 版本 / OS 发行版 # description: "" placeholder: "请告诉我们具体的OS版本或发行版,如 Windows 11, Ubuntu Linux 24.04... 等等" validations: required: false - type: textarea id: logs attributes: label: Log 输出 description: 请复制粘贴任何相关的 Log 输出。此内容将自动格式化为代码块,因此不需要反引号 render: shell - type: checkboxes id: terms attributes: label: 防止重复问题 description: 请确认在打开这个新的 issue 之前已经搜索过类似的issue。你可以评论或订阅已经存在的相关 issue options: - label: 我已在项目中搜索过类似的 issue required: true ================================================ FILE: .github/ISSUE_TEMPLATE/02-Request_Recipe.yml ================================================ name: 🫡 我想要对 target 换源! description: 想要对尚未支持的 target 进行换源? title: "提前找好方案怎么换源是chsrc用户的一大美德" # labels: [ ] type: Request body: - type: markdown attributes: value: | 请先回答这些问题 - type: textarea id: description attributes: label: 描述新的换源目标 placeholder: 它是编程语言、操作系统还是一个普通软件? validations: required: true - type: textarea id: reference-solution attributes: label: 请尽可能提供可参考的换源方法 placeholder: 往往提供该源的镜像站会附带换源方法,可以提供给我们参考 validations: required: true - type: textarea id: mirrors attributes: label: 请尽可能提供已有镜像站 placeholder: 当你提出该请求的时候,你可能已有心仪的镜像站,请告诉我们以避免重复查找劳动 validations: required: true ================================================ FILE: .github/ISSUE_TEMPLATE/03-Share.yml ================================================ name: 🎉 我找到了新的镜像站或源! description: 为大家分享新的镜像站或可用源! title: "分享是chsrc用户的一大美德" # labels: [ ] type: Contribute body: - type: markdown attributes: value: | 感谢你的分享!这将使你与更广大的用户得到更多可选的服务!同时也让镜像站开发维护人员更加有使命感! - type: textarea id: description attributes: label: 描述该镜像站或该源 description: | 1. 请告诉我们该镜像站的主体URL 2. 你是否已经测试过该镜像站的可用性? 3. 它大概提供哪些源? 4. 你想要我们为你添加该镜像站的具体哪个源? validations: required: true ================================================ FILE: .github/ISSUE_TEMPLATE/04-Deprecate.yml ================================================ name: ⛓️‍💥 镜像源已失效 description: 该镜像站已关停/该源已不再被支持 title: "报告镜像源情况是chsrc用户的一大美德" # labels: [ ] type: Deprecate body: - type: markdown attributes: value: | 感谢你的一手消息!让用户不再疑惑是哪里出了错! - type: textarea id: description attributes: label: 什么被弃用了? description: | 1. 请告诉我们是镜像站还是某个源失效 2. 如果可能,你可以开启该issue后参与维护!在代码中删除掉该源并不复杂! validations: required: true ================================================ FILE: .github/ISSUE_TEMPLATE/config.yml ================================================ blank_issues_enabled: true contact_links: - name: ❤️ 赞赏支持 chsrc url: https://afdian.com/a/ccmywish # about 不支持 Markdown 语法 about: 你是否因为 chsrc 而受到启发、节省了时间精力 or whatever? ================================================ FILE: .github/PULL_REQUEST_TEMPLATE/Add_Feature.md ================================================ ## 新功能描述 请描述该新功能,为什么要增加这个功能,以及具体的用例 --- ## 方案 请介绍你新增加该功能的方案 --- ## 实现 请介绍你的实现(若实现相当直接则不需要描述) ================================================ FILE: .github/PULL_REQUEST_TEMPLATE/Clean.md ================================================ ## 描述 注意: 小改动请使用该PR模板。而中等、大型规模的变化,或者引起巨大联动变化的改动,需要使用 `Refactor` PR模板。 请介绍你进行了什么清理,如某部分代码,如某部分文档 --- ## 方案 请介绍你进行清理的方案(如果你认为有必要的话) --- ## 实现 请介绍你的实现(若实现相当直接则不需要描述) ================================================ FILE: .github/PULL_REQUEST_TEMPLATE/Enhance.md ================================================ ## 改进描述 请描述要改进的具体内容,为什么要增强/改善它,以及具体的用例 --- ## 方案 请介绍你的改进方案 --- ## 实现 请介绍你的实现(若实现相当直接则不需要描述) ================================================ FILE: .github/PULL_REQUEST_TEMPLATE/Fix_Bug.md ================================================ ## Bug 背景 请介绍 Bug 的背景以及相关 issue --- ## Bug 原因 请描述导致 Bug 的具体原因 --- ## 方案 请介绍你的修复方案 --- ## 实现 请介绍你的实现(若实现相当直接则不需要描述) ================================================ FILE: .github/PULL_REQUEST_TEMPLATE/Implement.md ================================================ ## 问题描述 (此内容必填) 1. 简要说明此 PR 修复的具体问题或改进的功能背景 2. 列出与此 PR 相关的 issue 或任务,若没有填 `N/A`
## 方案 (此内容必填) 详细描述针对该问题或功能改进的解决方案
## 实现 (此内容可选填) 在按照上述方案实现时,若遇到需记录和提醒他人的细节时,务必在此描述
## 测试 (此内容可选填) 描述如何验证本 PR,列出具体的测试步骤
## 备注 (此内容可选填) 列出需要特别注意或额外注意的事项
================================================ FILE: .github/PULL_REQUEST_TEMPLATE/Refactor.md ================================================ ## 背景 注意: 中等、大型规模的变化,或者引起巨大联动变化的改动,才需要使用该模板。中小改动请使用 `Clean` PR模板。 请介绍该重构的背景,可以带来哪些优势? --- ## 方案 请介绍你进行重构的方案 --- ## 实现 请介绍你的实现(若实现相当直接则不需要描述) ================================================ FILE: .github/PULL_REQUEST_TEMPLATE/Relate_Mirror_Source.md ================================================ ## 可用性确认 1. 请你本人确认这些源真的可用或已经失效,不要等待其它用户帮你测试 2. 请确保修改后代码依然可以编译及运行 3. 请在你修改的各个文件的标头部分增加你自己的贡献信息,名字栏需要为中文拼音、或者英文名、或者账号名 ================================================ FILE: .github/READIT.md ================================================ [`pull_request_template.md`](./pull_request_template.md) 是 [`PULL_REQUEST_TEMPLATE/Implement.md`](./PULL_REQUEST_TEMPLATE/Implement.md) 的最简化版 ================================================ FILE: .github/copilot-instructions.md ================================================ # chsrc Project Rules for AI Assistants ## 项目概述 这是 chsrc 项目,一个用 C 语言编写的跨平台命令行换源工具,帮助用户在不同的镜像之间切换,适用于编程语言、操作系统、其他软件。它的最强大之处在于它是一个框架,能够帮助用户轻松地为不同的目标换源。 ## 架构 - **Framework**: 在目录 `src/framework/` 中,包含了核心实现,支持 recipe - `struct.h` 里定义了各种数据结构和宏,这是整个 chsrc 的核心,也是 chef DSL 的核心 - `chef.c` 里实现了 chef DSL,你可以使用它来确定正确的使用方法 - **Recipes**: 在目录 `src/recipe/` 中,包含了针对不同目标的具体实现 - `lang/` - 编程语言 (Ruby, JavaScript 等等) - `os/` - 操作系统 (Ubuntu, Arch Linux 等等) - `ware/` - 软件工具和应用 (Docker, Homebrew 等等) ## Coding Guidelines ### C Coding Style: 请阅读 `doc/03-为什么拒绝使用代码格式化工具.md` ### Important Project Concepts: 请阅读 `doc/10-如何编写recipe.md` ## Important: 一定要保持注释,因为它记录了重要的维护信息 ================================================ FILE: .github/dependabot.yml ================================================ version: 2 updates: - package-ecosystem: github-actions directory: "/" schedule: interval: weekly ================================================ FILE: .github/pull_request_template.md ================================================ ## 问题描述 1. 简要说明此 PR 修复的具体问题或改进的功能背景 2. 列出与此 PR 相关的 issue 或任务,若没有填 `N/A`
## 方案与实现 详细描述针对该问题或功能改进的解决方案
================================================ FILE: .github/workflows/PR-notify.yml ================================================ # --------------------------------------------------------------- # Workflow File : PR-notify.yml # File Authors : 曾奥然 # Contributors : Nul None # | # Created On : <2025-06-19> # Last Modified : <2025-08-07> # # Notify PR # --------------------------------------------------------------- name: 告知PR者 on: pull_request_target: types: [opened, ready_for_review, # draft PR 转为正式 PR review_requested, reopened] jobs: enforce-dev-branch: name: 强制使用dev分支 runs-on: ubuntu-latest # github.event.pull_request_target 内容为空,转而用 pull_request if: github.event.pull_request.base.ref != 'dev' steps: - name: 评论 uses: thollander/actions-comment-pull-request@v3 with: message: | Hi @${{github.event.pull_request.user.login}}, ❤️ 感谢你的贡献!你的 PR 当前基于 `${{github.base_ref}}` 分支,请修改使用 `dev` 分支 comment-tag: "❤️ 感谢你的贡献!" - run: | echo "::error::❌ PR 必须以 dev 分支为目标!当前是 ${GITHUB_BASE_REF}" exit 1 welcome: name: 欢迎PR者 runs-on: ubuntu-latest # 仅在 opened 时欢迎,其他情况都不再重复欢迎了 if: github.event.pull_request.base.ref == 'dev' && github.event.action == 'opened' steps: - name: 查看 GitHub Actions 环境 run: | echo "Event 类型: ${{ github.event.action }}" echo "Event 名: ${{ github.event_name }}" # 2025-10-06 移除点赞的步骤 # 因原 peter-evans/create-or-update-comment@v5 已不再支持空body,而如果使用 Github Token 之类又过于大材小用因此移除 - name: 添加评论欢迎 PRer uses: peter-evans/create-or-update-comment@v5 with: issue-number: ${{ github.event.pull_request.number }} body: | Hi @${{github.event.pull_request.user.login}} ❤️ 感谢你的贡献!我们将在最少半小时,最多5天内阅读此 PR 并回复你 edit-mode: replace ================================================ FILE: .github/workflows/PR-test.yml ================================================ # --------------------------------------------------------------- # Workflow File : PR-test.yml # File Authors : 曾奥然 # Contributors : Mikachu2333 # | # Created On : <2025-06-19> # Last Modified : <2025-08-17> # # Test PR # --------------------------------------------------------------- name: 测试PR on: pull_request: # 仅在开 pr、草稿转正式、手动要求 review、reopen的时候运行测试 types: [ opened, # 因 synchronize 将导致 pr 的构建过于频繁而禁用 # synchronize, # 在 pr 者 push commit 时每次构建 ready_for_review, # draft PR 转为正式 PR review_requested, reopened, ] paths: - "src/**" - "lib/**" jobs: test-on-ubuntu: name: 在Ubuntu上测试 runs-on: ubuntu-latest if: github.event.pull_request.base.ref == 'dev' steps: - name: 检出代码 uses: actions/checkout@v6 - name: 测试构建情况 run: | make - name: 测试test情况 run: | make test test-on-windows: name: 在Windows上测试 runs-on: windows-latest if: github.event.pull_request.base.ref == 'dev' steps: - name: 检出代码 uses: actions/checkout@v6 - name: 创建测试文件 shell: powershell run: | New-Item -Path "$env:USERPROFILE\Documents\Powershell\Microsoft.PowerShell_profile.ps1" -ItemType File -Force New-Item -Path "$env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1" -ItemType File -Force - name: 安装依赖 run: | choco install just - name: 测试构建情况 run: | just - name: 测试test情况 run: | just test ================================================ FILE: .github/workflows/build-on-Linux-AArch64.yml ================================================ # --------------------------------------------------------------- # Workflow File : build-on-Linux-AArch64.yml # File Authors : 曾奥然 # Contributors : Nul None # | # Created On : <2023-09-14> # Last Modified : <2025-09-12> # # Build chsrc on Linux (AArch64) and upload it to GitHub: the 'pre' release # --------------------------------------------------------------- name: 构建于 Linux AArch64 on: push: branches: [ "gh-build" ] jobs: build-and-upload: runs-on: ubuntu-latest steps: - name: 构建 uses: uraimo/run-on-arch-action@v3 with: arch: aarch64 distro: ubuntu_latest dockerRunArgs: | --volume "${PWD}/artifacts:/artifacts" run: | apt-get update -qq apt-get install build-essential -y -q apt-get install git -y -q pwd ; ls -al git clone https://github.com/RubyMetric/chsrc -b gh-build -q cd chsrc make build-in-ci-release-mode mv chsrc-ci-release chsrc-aarch64-linux cp ./chsrc-aarch64-linux /artifacts - name: 上传至 'pre' release uses: softprops/action-gh-release@v2 with: tag_name: pre files: | ./artifacts/chsrc-aarch64-linux ================================================ FILE: .github/workflows/build-on-Linux-ARMv7.yml ================================================ # --------------------------------------------------------------- # Workflow File : build-on-Linux-ARMv7.yml # File Authors : 曾奥然 # Contributors : Nul None # | # Created On : <2023-09-14> # Last Modified : <2025-09-12> # # Build chsrc on Linux (ARMv7) and upload it to GitHub: the 'pre' release # --------------------------------------------------------------- name: 构建于 Linux ARMv7 on: push: branches: [ "gh-build" ] jobs: build-and-upload: runs-on: ubuntu-latest steps: - name: 构建 uses: uraimo/run-on-arch-action@v3 with: arch: armv7 distro: ubuntu_latest dockerRunArgs: | --volume "${PWD}/artifacts:/artifacts" run: | apt-get update -qq apt-get install build-essential -y -q apt-get install git -y -q pwd ; ls -al git clone https://github.com/RubyMetric/chsrc -b gh-build -q cd chsrc make build-in-ci-release-mode mv chsrc-ci-release chsrc-armv7-linux cp ./chsrc-armv7-linux /artifacts - name: 上传至 'pre' release uses: softprops/action-gh-release@v2 with: tag_name: pre files: | ./artifacts/chsrc-armv7-linux ================================================ FILE: .github/workflows/build-on-Linux-riscv64.yml ================================================ # --------------------------------------------------------------- # Workflow File : build-on-Linux-riscv64.yml # File Authors : 曾奥然 # Contributors : Nul None # | # Created On : <2023-09-14> # Last Modified : <2025-09-12> # # Build chsrc on Linux (riscv64) and upload it to GitHub: the 'pre' release # --------------------------------------------------------------- name: 构建于 Linux riscv64 on: push: branches: [ "gh-build" ] jobs: build-and-upload: runs-on: ubuntu-latest steps: - name: 构建 uses: uraimo/run-on-arch-action@v3 with: arch: riscv64 distro: ubuntu_latest dockerRunArgs: | --volume "${PWD}/artifacts:/artifacts" run: | apt-get update -qq apt-get install build-essential -y -q apt-get install git -y -q pwd ; ls -al git clone https://github.com/RubyMetric/chsrc -b gh-build -q cd chsrc make build-in-ci-release-mode mv chsrc-ci-release chsrc-riscv64-linux cp ./chsrc-riscv64-linux /artifacts - name: 上传至 'pre' release uses: softprops/action-gh-release@v2 with: tag_name: pre files: | ./artifacts/chsrc-riscv64-linux ================================================ FILE: .github/workflows/build-on-Linux-x64.yml ================================================ # --------------------------------------------------------------- # Workflow File : build-on-Linux-x64.yml # File Authors : 曾奥然 # Contributors : Nul None # | # Created On : <2023-09-14> # Last Modified : <2025-09-12> # # Build chsrc on Linux (x64) and upload it to GitHub: the 'pre' release # --------------------------------------------------------------- name: 构建于 Linux x64 on: push: branches: [ "gh-build" ] jobs: build-and-upload: runs-on: ubuntu-latest steps: - name: 检出代码 uses: actions/checkout@v6 - name: 构建 run: | make build-in-ci-release-mode mv chsrc-ci-release chsrc-x64-linux - name: List files run: ls *-linux - name: 上传至 'pre' release uses: softprops/action-gh-release@v2 # if: startsWith(github.ref, 'refs/tags/') with: tag_name: pre files: | chsrc-x64-linux ================================================ FILE: .github/workflows/build-on-Windows.yml ================================================ # --------------------------------------------------------------- # Workflow File : build-on-Windows.yml # File Authors : 曾奥然 # Contributors : Nul None # | # Created On : <2023-09-14> # Last Modified : <2025-09-12> # # Build chsrc on Windows and upload it to GitHub: the 'pre' release # --------------------------------------------------------------- name: 构建于 Windows on: push: branches: [ "gh-build" ] jobs: build-and-upload: runs-on: windows-latest defaults: run: shell: msys2 {0} steps: - name: 检出代码 uses: actions/checkout@v6 - name: 安装 GCC uses: msys2/setup-msys2@v2 with: msystem: UCRT64 update: true install: | mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-make mingw-w64-i686-gcc mingw-w64-i686-make - name: 为 x64 构建 run: | mingw32-make.exe build-in-ci-release-mode mv chsrc-ci-release.exe chsrc-x64-windows.exe - name: 为 x32 构建 env: MSYSTEM: MINGW32 run: | mingw32-make.exe build-in-ci-release-mode mv chsrc-ci-release.exe chsrc-x86-windows.exe - name: 为 Android 构建 run: | compiler=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android21-clang # 检查编译器版本 $compiler --version echo mingw32-make.exe build-in-ci-release-mode CC=$compiler CROSS_BUILD_WINDOWS_FOR_ANDROID=1 echo # 让我们看看里面有啥 ls echo mv chsrc-ci-release chsrc-arm64-android - name: List files run: ls *.exe - name: 上传至 'pre' release uses: softprops/action-gh-release@v2 # if: startsWith(github.ref, 'refs/tags/') with: tag_name: pre files: | chsrc-x64-windows.exe chsrc-x86-windows.exe chsrc-arm64-android ================================================ FILE: .github/workflows/build-on-macOS.yml ================================================ # --------------------------------------------------------------- # Workflow File : build-on-macOS.yml # File Authors : 曾奥然 # Contributors : Nul None # | # Created On : <2023-09-15> # Last Modified : <2025-12-18> # # Build chsrc on macOS and upload it to GitHub: the 'pre' release # --------------------------------------------------------------- name: 构建于 macOS on: push: branches: [ "gh-build" ] jobs: on-arm64: runs-on: macos-latest steps: - name: 检出代码 uses: actions/checkout@v6 - name: 检查编译器版本 run: | clang --version echo gcc --version echo gcc-14 --version - name: 为 arm64 (AArch64) 构建 run: | make build-in-ci-release-mode mv chsrc-ci-release chsrc-aarch64-macos - name: List files run: ls *-macos - name: 上传至 'pre' release uses: softprops/action-gh-release@v2 # if: startsWith(github.ref, 'refs/tags/') with: tag_name: pre files: | chsrc-aarch64-macos on-x64: # macos-13 是 x64,macos-14 是 AArch64(ARMv8-A) # 但是 macos-13 已经于 2025-12-04 下线 runs-on: macos-15-intel steps: - name: 检出代码 uses: actions/checkout@v6 - name: 检查编译器版本 run: | clang --version echo gcc --version echo gcc-14 --version - name: 为 x64 构建 run: | make build-in-ci-release-mode mv chsrc-ci-release chsrc-x64-macos - name: List files run: ls *-macos - name: 上传至 'pre' release uses: softprops/action-gh-release@v2 # if: startsWith(github.ref, 'refs/tags/') with: tag_name: pre files: | chsrc-x64-macos ================================================ FILE: .github/workflows/pkg-deb.yml ================================================ # --------------------------------------------------------------- # Workflow File : pkg-deb.yml # File Authors : sanchuanhehe # Contributors : 曾奥然 # | # Created On : <2025-06-10> # Last Modified : <2025-10-29> # # Build and publish deb packages # --------------------------------------------------------------- name: 构建发布deb包 on: release: types: [ released ] push: branches: [ "gh-build" ] workflow_dispatch: inputs: version: description: 'Version to build' required: true default: '0.3.0' # 短暂时间内不可达到的最新版本号 jobs: Build-deb: name: 构建deb包 runs-on: ubuntu-latest steps: - name: 检出代码 uses: actions/checkout@v6 with: ref: gh-build - name: 获取版本号 id: get_version run: | if [ "${{ github.event_name }}" = "release" ]; then version="${{ github.event.release.tag_name }}" # 删除前缀 'v' if present version=${version#v} elif [ "${{ github.event_name }}" = "push" ];then # 从源代码中提取版本号 version=$(sed -E -n 's/^#define Chsrc_Version +"([0-9]+\.[0-9]+\.[0-9]+).*"/\1/p' ./src/framework/version.h) else version="${{ github.event.inputs.version }}" fi echo "version=$version" >> $GITHUB_OUTPUT echo "Version: $version" - name: 验证版本号 run: | version="${{ steps.get_version.outputs.version }}" if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "Invalid version format: $version" exit 1 fi - name: 更新 debian/changelog run: | version="${{ steps.get_version.outputs.version }}" cd ./pkg/deb (cat << EOF; cat ./debian/changelog) > new_changelog chsrc ($version-1) unstable; urgency=medium * Release version $version -- 曾奥然 $(date -R) EOF mv -f new_changelog ./debian/changelog - name: 安装构建依赖 run: | sudo apt-get update sudo apt-get install -y debhelper devscripts build-essential fakeroot - name: 构建 run: | make build-deb - name: 移动构建产物到./dist和./dist-for-pre run: | version="${{ steps.get_version.outputs.version }}" # 创建两个目录来存放构建产物(产物内容一样,只是文件名不一样) mkdir dist dist-for-pre find ./pkg -name "chsrc_${version}*.deb" -exec mv {} dist/ \; cp -r dist/* dist-for-pre/ # 上传至 'pre' release 的文件名需要设置为 'latest', 从而稳定下载URL cd ./dist-for-pre for old_name in ./chsrc_${version}*.deb; do new_name="${old_name/${version}-1/latest-1}" mv "$old_name" "$new_name" done - name: 验证生成的deb包 run: | version="${{ steps.get_version.outputs.version }}" ls -la dist/ dpkg-deb --info dist/chsrc_${version}-1_amd64.deb dpkg-deb --contents dist/chsrc_${version}-1_amd64.deb - name: 测试deb包能否正常安装 run: | version="${{ steps.get_version.outputs.version }}" sudo dpkg -i dist/chsrc_${version}-1_amd64.deb || true sudo apt-get install -f -y || true bash pkg/deb/deb-installation-test.sh - name: 上传deb包到artifacts uses: actions/upload-artifact@v6 with: name: chsrc-deb-files path: dist/chsrc_*.deb retention-days: 30 - name: 上传附件到GitHub Releases(the newly created release) if: github.event_name == 'release' uses: softprops/action-gh-release@v2 with: # 用 * 省略版本号,以及指代各种架构 files: dist/chsrc_*.deb - name: 上传附件到GitHub Releases(the 'pre' release) if: github.event_name == 'push' uses: softprops/action-gh-release@v2 with: tag_name: pre # 用 * 指代各种架构 files: dist-for-pre/chsrc_latest-1_*.deb Create-APT-repository: name: 创建APT仓库 needs: Build-deb runs-on: ubuntu-latest if: github.event_name == 'release' steps: - name: Download all artifacts uses: actions/download-artifact@v7 with: pattern: chsrc-deb-files merge-multiple: true path: ./debs - name: Install repository tools run: | sudo apt-get update sudo apt-get install -y dpkg-dev - name: Create Packages file run: | cd debs dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz dpkg-scanpackages . /dev/null > Packages - name: Upload repository metadata uses: actions/upload-artifact@v6 with: name: debian-repository-metadata path: debs/Packages* retention-days: 30 ================================================ FILE: .github/workflows/pub-AUR-chsrc-and-chsrc-bin.yml ================================================ # --------------------------------------------------------------- # Workflow File : pub-AUR-chsrc-and-chsrc-bin.yml # File Authors : Terrasse # Contributors : Nul None # | # Created On : <2024-08-29> # Last Modified : <2025-03-18> # # Publish the 2 packages to AUR when a new release is created: # 1. chsrc # 2. chsrc-bin # # Note: only normal version tags like 'v1.2.3' will be published # --------------------------------------------------------------- name: Publish 'chsrc' and 'chsrc-bin' to AUR on: release: types: [ released ] jobs: publish: runs-on: ubuntu-latest steps: - name: Get the release tag id: get_tag run: | echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV - name: Validate version tag run: | if [[ ! $tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "Abnormal version tag: $tag" echo "valid=0" >> $GITHUB_ENV else version=$(echo $tag | sed 's/^v//') echo "version=$version" >> $GITHUB_ENV echo "valid=1" >> $GITHUB_ENV fi - name: Fetch PKGBUILD run: | wget https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD\?h\=chsrc-bin -O ./PKGBUILD_bin - name: Update PKGBUILD run: | sed -i "s/pkgver=.*/pkgver=$version/" PKGBUILD_bin - name: Publish chsrc-bin to AUR if: env.valid == '1' uses: KSXGitHub/github-actions-deploy-aur@v4.1.1 with: pkgname: chsrc-bin pkgbuild: ./PKGBUILD_bin updpkgsums: true test: true # Check that PKGBUILD could be built, and update pkgver commit_username: ${{ secrets.AUR_USERNAME }} commit_email: ${{ secrets.AUR_EMAIL }} ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }} commit_message: "github-action-auto-publish v${{ env.version }}" - name: Fetch PKGBUILD run: | wget https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD\?h\=chsrc -O ./PKGBUILD - name: Update PKGBUILD run: | sed -i "s/pkgver=.*/pkgver=$version/" PKGBUILD - name: Publish chsrc to AUR if: env.valid == '1' uses: KSXGitHub/github-actions-deploy-aur@v4.1.1 with: pkgname: chsrc pkgbuild: ./PKGBUILD updpkgsums: true test: true # Check that PKGBUILD could be built, and update pkgver commit_username: ${{ secrets.AUR_USERNAME }} commit_email: ${{ secrets.AUR_EMAIL }} ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }} commit_message: "github-action-auto-publish v${{ env.version }}" ================================================ FILE: .github/workflows/pub-AUR-chsrc-git.yml ================================================ # --------------------------------------------------------------- # Workflow File : pub-AUR-chsrc-git.yml # File Authors : Terrasse # Contributors : Nul None # | # Created On : <2024-08-29> # Last Modified : <2025-06-19> # # Publish package 'chsrc-git' to AUR when branch 'main' is updated. # --------------------------------------------------------------- name: Publish 'chsrc-git' to AUR on: workflow_dispatch: push: branches: [ "main" ] # chsrc-git syncs with main paths: - "src/**" - "lib/**" jobs: publish: runs-on: ubuntu-latest steps: - name: Fetch PKGBUILD run: | wget https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD\?h\=chsrc-git -O ./PKGBUILD - name: Publish to AUR uses: KSXGitHub/github-actions-deploy-aur@v4.1.1 with: pkgname: chsrc-git pkgbuild: ./PKGBUILD test: true # Check that PKGBUILD could be built, and update pkgver commit_username: ${{ secrets.AUR_USERNAME }} commit_email: ${{ secrets.AUR_EMAIL }} ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }} commit_message: "github-action-auto-publish\n${{ github.sha }}" ================================================ FILE: .github/workflows/pub-WinGet.yml ================================================ # --------------------------------------------------------------- # Workflow File : pub-WinGet.yml # File Authors : YU-7 <2747046473@qq.com> # Contributors : Nul None # | # Created On : <2024-12-25> # Last Modified : <2024-12-25> # # This workflow publish to winget # --------------------------------------------------------------- name: Publish to WinGet on: release: types: [released] jobs: publish: runs-on: windows-latest steps: - uses: vedantmgoyal9/winget-releaser@main with: identifier: RubyMetric.chsrc installers-regex: '\.exe$' # Only .exe files token: ${{ secrets.WINGET_TOKEN }} ================================================ FILE: .gitignore ================================================ ############################## # VS Code ############################## .vscode/* !.vscode/extensions.json !.vscode/settings.json !.vscode/c_cpp_properties.json !.vscode/tasks.json !.vscode/launch.json !.vscode/README.md ############################## # VS ############################## .vs ############################## # Binaries ############################## *.o *.obj *.a *.lib *.so *.dll *.out *.exe *.res ############################## # Built executables ############################## chsrc chsrc-debug chsrc-release chsrc-ci-release ############################## # Test files ############################## xy fw README.md.bak* *.tmp chsrc_tmp_test.txt chsrc_tmp_test.txt.bak # Generated when testing (when there's a bug) nul # 'chsrc set -scope=project' generated .bundle .npmrc ############################## # Texinfo ############################## chsrc.aux chsrc.log chsrc.toc *.info *.pdf ############################## # deb package ############################## # deb package 未归档的目录 pkg/deb/debian/chsrc/ # 下面这个目录包含创建出 $HOME 的虚拟环境 pkg/deb/debian/.debhelper/ pkg/deb/debian/debhelper-build-stamp pkg/deb/debian/files pkg/deb/debian/chsrc.debhelper.log pkg/deb/debian/chsrc.substvars # 以下为 deb package 构建的直接产物 chsrc_*.deb chsrc-dbgsym_*.ddeb chsrc_*.build chsrc_*.buildinfo chsrc_*.changes ================================================ FILE: .vscode/README.md ================================================ # Dev in VS Code 首先需要安装好 [just](https://github.com/casey/just),而不再硬性需要 `make` 1. `Ctrl-Shift-B` 直接构建 2. `F5` 直接开始 Debug ================================================ FILE: .vscode/c_cpp_properties.json ================================================ { "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**" ], "cStandard": "c17", "intelliSenseMode": "windows-gcc-x64" }, { "name": "Linux", "includePath": [ "${workspaceFolder}/**" ], "cStandard": "c17" }, { "name": "Mac", "includePath": [ "${workspaceFolder}/**" ], "cStandard": "c17" } ], "enableConfigurationSquiggles": true, "version": 4 } ================================================ FILE: .vscode/extensions.json ================================================ { "recommendations": [ "ms-vscode.cpptools", "ms-vscode.cpptools-extension-pack", "editorconfig.editorconfig", "redhat.vscode-yaml", "nefrob.vscode-just-syntax" ], "unwantedRecommendations": [ "esbenp.prettier-vscode" ] } ================================================ FILE: .vscode/launch.json ================================================ { "version": "0.2.0", "configurations": [ { "name": "Debug chsrc", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/chsrc-debug", "args": [ "get", "nodejs" ], "preLaunchTask": "构建 debug 版 chsrc", "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], // 如果你认为使用弹出窗口难以调试,可尝试设置为false "externalConsole": true, // lldb 等请自行设置,不作额外说明 "MIMode": "gdb", // "miDebuggerPath": "/path/to/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "Set Disassembly Flavor to Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ], "postDebugTask": "停止 debug 程序" }, { "name": "Debug framework", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/fw", "args": [], "preLaunchTask": "测试 framework", "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", // "miDebuggerPath": "/path/to/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "Set Disassembly Flavor to Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ] } ] } ================================================ FILE: .vscode/settings.json ================================================ { "editor.fontLigatures": false, "C_Cpp.autoAddFileAssociations": false, "C_Cpp.intelliSenseEngine": "Tag Parser", "C_Cpp.default.browse.limitSymbolsToIncludedHeaders": false, "editor.formatOnSave": false, "C_Cpp.clang_format_fallbackStyle": "GNU", "C_Cpp.formatting": "disabled", "[c]": { "editor.defaultFormatter": null, }, "[h]": { "editor.defaultFormatter": null }, "[jsonc]": { "editor.defaultFormatter": "vscode.json-language-features" }, "[yaml]": { "editor.defaultFormatter": "redhat.vscode-yaml" }, "vscode-just.formatOnSave": false, } ================================================ FILE: .vscode/tasks.json ================================================ { "tasks": [ { "type": "shell", "label": "构建 chsrc", "command": "just", "args": [ "build" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "dependsOn": "停止程序", "detail": "先停止原有程序,然后使用 just build 编译" }, { "type": "shell", "label": "构建 debug 版 chsrc", "command": "just", "args": [ "build-in-debug-mode" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": false }, }, { "type": "shell", "label": "测试 chsrc", "command": "just", "args": [ "test" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "test", "isDefault": true }, "detail": "使用 just test 测试" }, { "type": "shell", "label": "测试 framework", "command": "just", "args": [ "test-fw" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "test", "isDefault": false }, "detail": "使用 just test-fw 测试 framework" }, { "type": "shell", "label": "测试 xy.h", "command": "just", "args": [ "test-xy" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "test", "isDefault": false }, "detail": "使用 just test-xy 测试 xy.h" }, { "type": "shell", "label": "停止 debug 程序", "windows": { "command": "powershell", "args": [ "-c", "Get-Process -Name \"chsrc-debug\" -ErrorAction SilentlyContinue | Stop-Process -Force;", "Get-Process -Name \"gdb\" -ErrorAction SilentlyContinue | Stop-Process -Force;", "Get-Process -Name \"WindowsDebugLauncher\" -ErrorAction SilentlyContinue | Stop-Process -Force;" ] }, "linux": { "command": "bash", "args": [ "-c", "if pgrep -f chsrc-debug > /dev/null; then pkill -f chsrc-debug; fi" ] }, "osx": { "command": "bash", "args": [ "-c", "if pgrep -f chsrc-debug > /dev/null; then pkill -f chsrc-debug; fi" ] }, "group": "build", "presentation": { "echo": true, "reveal": "silent", "focus": false, "panel": "shared", "showReuseMessage": true, "clear": false }, "detail": "停止 debug 版本的 chsrc" }, { "type": "shell", "label": "停止程序", "windows": { "command": "powershell", "args": [ "-c", "Get-Process -Name \"chsrc\" -ErrorAction SilentlyContinue | Stop-Process -Force;" ] }, "linux": { "command": "bash", "args": [ "-c", "if pgrep -f chsrc > /dev/null; then pkill -f chsrc; fi" ] }, "osx": { "command": "bash", "args": [ "-c", "if pgrep -f chsrc > /dev/null; then pkill -f chsrc; fi" ] }, "group": "build", "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared", "showReuseMessage": true, "clear": false }, "detail": "跨平台停止 chsrc" } ], "version": "2.0.0" } ================================================ FILE: COPYING ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ================================================ FILE: LICENSE-MIT.txt ================================================ MIT License Copyright (c) 2023-2026 曾奥然 (Aoran Zeng), 郭恒 (Heng Guo) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: Makefile ================================================ #!/usr/bin/make -f # -------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # -------------------------------------------------------------- # Build File : Makefile # File Authors : 曾奥然 # Contributors : Yangmoooo # | sanchuanhehe # | # Created On : <2023-08-28> # Last Modified : <2025-10-15> # # 请阅读 ./doc/01-开发与构建.md 来使用 # -------------------------------------------------------------- #=========== OS Check ================ On-Linux = 0 On-Windows = 0 On-macOS = 0 ifeq ($(shell uname), Linux) On-Linux = 1 endif ifeq ($(shell uname), Darwin) On-macOS = 1 endif # 只有 MSYS2 会定义 $(OS) 变量 ifeq ($(OS), Windows_NT) On-Windows = 1 endif # 注意, 原生 Windows 会定义 $(ComSpec) 变量,且区分大小写 # 但是 MSYS2 并不会定义 #===================================== #======== Default Tooling ============ ifeq ($(On-Windows), 1) # MSYS2 环境 CC = cc else ifeq ($(On-macOS), 1) CC = clang else CC = cc endif ifeq ($(On-macOS), 1) DEBUGGER = lldb else DEBUGGER = gdb endif #===================================== #======== Compilation Config ========== CFLAGS += -Iinclude -Ilib ifeq ($(On-Windows), 1) CLANG_FLAGS = -target x86_64-pc-windows-gnu endif ifeq ($(CC), clang) CFLAGS += $(CLANG_FLAGS) endif override WARN += -Wall -Wextra -Wno-unused-variable -Wno-unused-function -Wno-missing-braces -Wno-misleading-indentation \ -Wno-missing-field-initializers -Wno-unused-parameter -Wno-sign-compare _C_Warning_Flags := $(WARN) DevMode-Target-Name = chsrc DebugMode-Target-Name = chsrc-debug ReleaseMode-Target-Name = chsrc-release CIReleaseMode-Target-Name = chsrc-ci-release CFLAGS_debug = -g -DXY_DEBUG CFLAGS_static = -static CFLAGS_optimization = -O2 ifdef DEBUG CFLAGS += $(CFLAGS_debug) endif STATIC = 0 ifeq ($(STATIC), 1) CFLAGS += $(CFLAGS_static) endif #===================================== #====== CI release mode 的配置 ======= ifeq ($(MAKECMDGOALS), build-in-ci-release-mode) CFLAGS += $(CFLAGS_optimization) # 仅在 Linux 上使用静态链接 ifeq ($(On-Linux), 1) CFLAGS += $(CFLAGS_static) endif # GitHub Actions 上的 macOS 中的 LLVM 太老了 # 而且 gcc 被重命名为了 clang # 需要直接指定版本 ifeq ($(On-macOS), 1) CC = gcc-14 endif endif #===================================== #============ Aliases ================ all: build b: build-in-dev-mode build: build-in-dev-mode bd: build-in-debug-mode br: build-in-release-mode bcir: build-in-ci-release-mode d: debug t: test check: test c: clean #===================================== build-in-dev-mode: @echo Starting: Build in DEV mode: \'$(CC)\' $(CFLAGS) -o $(DevMode-Target-Name) @$(CC) src/chsrc-main.c $(CFLAGS) $(_C_Warning_Flags) -o $(DevMode-Target-Name) @echo Finished: Build in DEV mode build-in-debug-mode: CFLAGS += $(CFLAGS_debug) build-in-debug-mode: @echo Starting: Build in DEBUG mode: \'$(CC)\' $(CFLAGS) -o $(DebugMode-Target-Name) @$(CC) src/chsrc-main.c $(CFLAGS) $(_C_Warning_Flags) -o $(DebugMode-Target-Name) @echo Finished: Build in DEBUG mode build-in-release-mode: CFLAGS += $(CFLAGS_optimization) build-in-release-mode: @echo Starting: Build in RELEASE mode: \'$(CC)\' $(CFLAGS) -o $(ReleaseMode-Target-Name) @$(CC) src/chsrc-main.c $(CFLAGS) $(_C_Warning_Flags) -o $(ReleaseMode-Target-Name) @echo Finished: Build in RELEASE mode # CI release mode 的配置在该文件上方 build-in-ci-release-mode: @echo Starting: Build in CI-RELEASE mode: \'$(CC)\' $(CFLAGS) -o $(CIReleaseMode-Target-Name) @$(CC) src/chsrc-main.c $(CFLAGS) $(_C_Warning_Flags) -o $(CIReleaseMode-Target-Name) @echo Finished: Build in CI-RELEASE mode # 永远重新编译 debug: build-in-debug-mode @$(DEBUGGER) $(DebugMode-Target-Name) test: test-make-env test-xy test-fw test-make-env: @echo "On-Linux: $(On-Linux)" @echo "On-Windows: $(On-Windows)" @echo "On-macOS: $(On-macOS)" @echo "CC: $(CC)" @echo "CFLAGS: $(CFLAGS)" @echo "USER: $$(whoami)" @echo "PWD: $(shell pwd)" @echo "UID: $$(id -u)" @echo "GID: $$(id -g)" # 检查HOME环境变量 @if [ -z "$(HOME)" ]; then \ echo "HOME environment variable is not set!"; \ else \ echo "HOME: $(HOME)"; \ fi # 这两个测试文件都用 DEBUG mode test-xy: CFLAGS += $(CFLAGS_debug) test-xy: @$(CC) test/xy.c $(CFLAGS) -o xy @./xy test-fw: CFLAGS += $(CFLAGS_debug) test-fw: @$(CC) test/fw.c $(CFLAGS) -o fw @./fw check: test # AUR package 安装时将执行此 target fastcheck: $(DevMode-Target-Name) @perl ./test/cli.pl fastcheck test-cli: $(DevMode-Target-Name) @perl ./test/cli.pl clean: -@rm *.exe 2>/dev/null -@rm *.res 2>/dev/null -@rm xy 2>/dev/null -@rm fw 2>/dev/null -@rm README.md.bak* 2>/dev/null -@rm chsrc 2>/dev/null -@rm chsrc-debug 2>/dev/null -@rm chsrc-release 2>/dev/null -@rm chsrc-ci-release 2>/dev/null # -include pkg/deb/Makefile # 不这么做,因为 pkg/deb/Makefile 需要在 pkg/deb 目录下执行 # 保持动词在前的任务名风格 build-deb: @$(MAKE) -C pkg/deb deb-build clean-deb: @$(MAKE) -C pkg/deb deb-clean install: $(ReleaseMode-Target-Name) install -D -m 755 $(ReleaseMode-Target-Name) $(DESTDIR)/usr/bin/chsrc install -D -m 644 doc/chsrc.1 $(DESTDIR)/usr/share/man/man1/chsrc.1 # 这样还是太麻烦,不用,我们还是靠 just 来调用吧 # # 通过 make rawstr4c ARGS="[--debug] Markdown.md" 来调用 #rawstr4c: # @bash ./tool/rawstr4c/run/run.sh $(ARGS) .PHONY: all b build bd br bcir d t check c \ build-in-dev-mode build-in-debug-mode build-in-release-mode build-in-ci-release-mode \ debug test test-make-env test-xy test-fw fastcheck test-cli clean install build-deb clean-deb rawstr4c ================================================ FILE: README.md ================================================
chsrc logo
全平台通用换源工具与框架 `chsrc`,**目标支持 Linux, Windows (native, MSYS2, Cygwin), macOS, BSD, Android 等尽可能多的操作系统环境,龙芯、飞腾、RISC-V 等尽可能多的 CPU**。 我们使用 **C11** 来完成上述目标。我们并不使用 Python 或 JavaScript 等解释语言,因为一个简单的换源工具,不应该强行塞给用户一个庞大的解释器和数十、数百 MB 其他文件。 本软件为**自由软件**,SDPX 软件许可证为 `GPL-3.0-or-later and MIT`
RubyMetric%2Fchsrc | Trendshift Featured|HelloGitHub

## ❤️ 致所有的朋友们 2025年8月11日,我因通宵重构本项目代码而被送去抢救([#252](https://github.com/RubyMetric/chsrc/issues/252),[突发!换源工具 chsrc 作者在重构过程中被送去 120 抢救](https://v2ex.com/t/1151802))。大家给予的关心和帮助让我非常非常感动!在此,我衷心感谢每一位朋友!**无论是开源还是闭源,无论是否为职业程序员,我们其实都是一群使用软件、热心互助、充满友爱的人,这是一个温暖的大家庭**。 我为 `chsrc` 采用 GPL 协议,怀着殷切的期望:**希望能够营造像 Richard Stallman 在创建 GNU 项目之前,在 MIT 那样的氛围——写自己用得上的软件,与大家一起开发、维护,简单纯粹,无关商业利益。就像小区、校园自发组建的足球篮球队,从一次偶然的加入,逐渐成长为互相支持的伙伴**。 这次经历让我更加深刻地体会到:开源,是一种把大家联系在一起的方式,**它让友情、互助和协作成为可能,也让我们在共同的目标中建立起长期的纽带**。 最后,希望大家能够**时刻关注**自己的身体,**你可以随时 `chsrc` `chown` `chgrp` `chmod` 无限次,但是无法 `chbody`**!
## 🤝 协作与贡献 > [!TIP] > **`chsrc` 不仅是一个命令行工具,同时也是一个体现了 Ruby on Rails 思想的 MVC 换源框架,它甚至使你能够在不了解C语言的情况下编写出新的换源方法 (recipe)。** 配合使用 [rawstr4c], 这将比写 shell 脚本更加轻松。 [如何编写 recipe?](./doc/10-如何编写recipe.md) --- > [!NOTE] > 这也许是你可以参与的第一个现实世界中有用的C语言项目,[用 VS Code 一分钟内上手编译、运行、测试 chsrc](./doc/01-开发与构建.md) > > 欢迎对 GitHub、Gitee 协作不熟悉的人以此为契机学习参与贡献, 欢迎任何编程初学者。[从开发到提交PR,我们覆盖全流程文档](./doc/) --- > [!IMPORTANT] > **`chsrc` 可换源 65+ 目标。每个人仅仅贡献和维护自己熟悉的部分,回报是得到其他所有领域专家的帮助。** [欢迎成为 recipe 维护者](https://github.com/RubyMetric/chsrc/issues/275)
可参与的任务与挑战: 1. [Shell auto-completion 终端命令自动补全](https://github.com/RubyMetric/chsrc/issues/204) 2. [搜集默认源地址,帮助使用 `reset` 功能](https://github.com/RubyMetric/chsrc/issues/111) 3. [搜集测速地址,进行精准测速](https://github.com/RubyMetric/chsrc/issues/205) 4. [chsrc-bootstrap: 为不存在预编译 `chsrc` 的平台提供支持](https://github.com/RubyMetric/chsrc/issues/230)
已由贡献者完成的挑战 1. [[Challenge] 编写统一安装的 shell 和 PowerShell 脚本](https://github.com/RubyMetric/chsrc/issues/98) 已由 [@Efterklang] 与 [@wickdynex] 完成
镜像站可用性 1. 2.
打包 想通过 `dnf`, `flatpak`, `snap` 等系统包管理工具来安装和更新`chsrc`?若可提供维护,请访问 [issue#16 on GitHub](https://github.com/RubyMetric/chsrc/issues/16) - [x] `Homebrew` - [x] `Scoop` - [x] `WinGet` - [x] `AUR` - [ ] `Flatpak` - [ ] `snap` - [ ] 缺乏其他平台/包维护者

## 📌 示例 桌面端:
chsrc set node

安卓:
chsrc set python and chsrc set termux

## 🚀 安装 Packaging status > [!IMPORTANT] > 若通过下述手动方式安装,则会下载到当前目录,可直接通过 `./chsrc` 运行
Windows - 可通过 `scoop` 安装,感谢 [@Gn3po4g] 与 [@niheaven] ```bash scoop install chsrc ```
- 可通过 `WinGet` 安装,感谢 [@YU-7] ```bash winget install RubyMetric.chsrc ```
- 可通过 `PowerShell` 脚本一键下载最新版二进制文件,感谢 [@wickdynex] 若下方链接无法访问,可使用 `https://gitee.com/RubyMetric/chsrc/raw/main/tool/installer.ps1` 替代 ```PowerShell # 使用 -Version 指定版本 (不指定时默认为 pre) # 1. 安装 pre 版本; 这比从包管理器安装的总是更新一些 # 2. 安装旧版本; 有时新版本可能引入某些 Bug,临时使用旧版本解决燃眉之急 "& { $(iwr -useb https://chsrc.run/windows) } -Version pre" | iex ```
- 或手动下载二进制文件,这是最新版,往往比 `scoop` 提供的更新,适用于修复 Bug、添加新功能后及时使用,以及未安装 `scoop` 时 ```bash # x64 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-x64-windows.exe -o chsrc.exe # x86 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-x86-windows.exe -o chsrc.exe ```
Linux - 可通过 `apt`/`dpkg` 安装,感谢 [@sanchuanhehe] ```bash # x64 $ curl -LO https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc_latest-1_amd64.deb # 也可以使用 Wget wget https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc_latest-1_amd64.deb $ sudo apt install ./chsrc_latest-1_amd64.deb # 或 $ sudo sudo dpkg -i chsrc_latest-1_amd64.deb ```
- 支持 `AUR`,可通过 `yay` 安装,感谢 [@Jerry-Terrasse] ```bash # AUR $ yay -S chsrc-bin # Binary from GitHub Release $ yay -S chsrc-git # Build from the latest main branch (stable) $ yay -S chsrc # Build from GitHub Release ```
- 可通过 `shell` 脚本一键安装最新版,感谢 [@Efterklang] 与 [@wickdynex] 若下方链接无法访问,可使用 `https://gitee.com/RubyMetric/chsrc/raw/main/tool/installer.sh` 替代 ```bash # 非root用户默认安装至 ~/.local/bin $ curl https://chsrc.run/posix | bash # 也可以使用 Wget $ wget -O - https://chsrc.run/posix | bash # root用户默认安装至 /usr/local/bin $ curl https://chsrc.run/posix | sudo bash # 使用 -d 指定目录安装 $ curl https://chsrc.run/posix | bash -s -- -d ./ # 使用 -v 指定版本 (不指定时默认为 pre) # 1. 安装 pre 版本; 这比从包管理器安装的总是更新一些 # 2. 安装旧版本; 有时新版本可能引入某些 Bug,临时使用旧版本解决燃眉之急 $ curl https://chsrc.run/posix | bash -s -- -v 0.2.1 # 使用 -l en 输出英文 $ curl https://chsrc.run/posix | bash -s -- -l en ```
- 可手动下载二进制文件安装 ```bash # x64 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-x64-linux -o chsrc; chmod +x ./chsrc # aarch64 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-aarch64-linux -o chsrc; chmod +x ./chsrc # riscv64 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-riscv64-linux -o chsrc; chmod +x ./chsrc # armv7 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-armv7-linux -o chsrc; chmod +x ./chsrc ``` 如果你所在的处理器架构没有预编译版本,可以使用 [chsrc-bootstrap]
macOS - 可通过 `homebrew` 安装,感谢 [@Aaron-212] 与 [@chenrui333] ```bash brew install chsrc ```
- 可通过 `shell` 脚本安装最新版,感谢 [@Efterklang] 与 [@wickdynex] 若下方链接无法访问,可使用 `https://gitee.com/RubyMetric/chsrc/raw/main/tool/installer.sh` 替代 ```bash # 非root用户默认安装至 ~/.local/bin $ curl https://chsrc.run/posix | bash # root用户默认安装至 /usr/local/bin $ curl https://chsrc.run/posix | sudo bash # 使用 -d 指定目录安装 $ curl https://chsrc.run/posix | bash -s -- -d ./ # 使用 -v 指定版本 (不指定时默认为 pre) # 1. 安装 pre 版本; 这比从包管理器安装的总是更新一些 # 2. 安装旧版本; 有时新版本可能引入某些 Bug,临时使用旧版本解决燃眉之急 $ curl https://chsrc.run/posix | bash -s -- -v 0.2.1 # 使用 -l en 输出英文 $ curl https://chsrc.run/posix | bash -s -- -l en ```
- 或手动下载二进制文件,这是最新版,往往比 `homebrew` 提供的更新,适用于修复 Bug、添加新功能后及时使用 ```bash # arm64/aarch64 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-aarch64-macos -o chsrc; chmod +x ./chsrc # x64 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-x64-macos -o chsrc; chmod +x ./chsrc ```
BSD 如果已安装好了编译 `chsrc` 所需要的依赖,可直接运行: ```bash git clone https://gitee.com/RubyMetric/chsrc.git; cd chsrc clang -Iinclude -Ilib src/chsrc-main.c -o chsrc ``` **如果还不存在这些依赖,你将会被死锁住: 我还没有换源,我该如何安装这些依赖呢?** 这就是 [chsrc-bootstrap] 起作用的时刻,你可使用BSD系统的原生脚本语言编写 `bootstrapper`,[并向我们提交](https://github.com/RubyMetric/chsrc/issues/230) 注: `chsrc` 实现的 `FreeBSD recipe` 长期存在问题,因此一个新的 `bootstrapper` 是相当必要的,请帮助你自己和大家!
Android/Termux Termux 中默认无 `Wget`,我们都用 `cURL` 来下载安装 ```bash # arm64/aarch64 curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-arm64-android -o chsrc; chmod +x ./chsrc ``` 如果你所在的处理器架构没有预编译版本,可以使用 [chsrc-bootstrap]: ```bash curl -L https://gitee.com/RubyMetric/chsrc/raw/main/bootstrap/Termux.bash | bash ```
其他平台 若你所在的平台不存在预编译好的 `chsrc`,你需要手动编译。如果已安装好了编译 `chsrc` 所需要的依赖,可直接运行: ```bash git clone https://gitee.com/RubyMetric/chsrc.git; cd chsrc; make ``` **如果还不存在这些依赖,你将会被死锁住: 我还没有换源,我该如何安装这些依赖呢?** 这就是 [chsrc-bootstrap] 起作用的时刻,你可使用该平台原生脚本语言编写 `bootstrapper`,[并向我们提交](https://github.com/RubyMetric/chsrc/issues/230)

## 💡 使用 ```bash 名称: chsrc - Change Source - (GPLv3+) 使用: chsrc [options] [target] [mirror] 命令: help, h 打印此帮助,或 -h, --help issue, i 查看相关issue list, ls, l 列出可用镜像站和可换源目标 list mirror|target 列出支持的: 镜像站/换源目标 list os|lang|ware 列出支持的: 操作系统/编程语言/软件 list 查看该目标可用源与支持功能 measure, m, cesu 对该目标所有源测速 get, g 查看该目标当前源的使用情况 set, s 换源,自动测速后挑选最快源 set first 换源,使用维护团队测速第一的源 set 换源,指定使用某镜像站 (通过list 查看) set 换源,用户自定义源URL reset 重置,使用上游默认使用的源 选项: -dry Dry Run,模拟换源过程,命令仅打印并不运行 -scope=project|user|system 仅对本项目换源 / 用户级换源 / 系统级换源 (通过ls 查看) -ipv6 使用IPv6测速 -en(glish) 使用英文输出 -no-color 无颜色输出 ```
```bash 自动测速,寻找最快者,换源 $ chsrc set ruby 不想自动测速的时候,可使用维护团队测试的最快镜像站 $ chsrc set ruby first 先列出可用的镜像站,然后选择其一,如使用 RubyChina 作为镜像站 $ chsrc ls ruby $ chsrc set ruby rubychina 若有自己的镜像,可以使用自定义URL $ chsrc set ruby https://gems.ruby-china.com/ 对支持 *项目级* 换源的目标,可以避免全局(*系统级* 或 *用户级*)换源 $ chsrc set -scope=project bundler $ chsrc set -scope project pdm ```
## 编程语言开发 ```bash chsrc set ruby|rb|gem|bundler|rubygems chsrc set python | py | pypi # 同时换 pip, poetry, pdm, uv 这4个包管理器,也可以4个独立换源 chsrc set pip chsrc set poetry chsrc set pdm chsrc set uv chsrc set rye chsrc set node | nodejs # 同时换 npm, yarn 和 pnpm 这3个包管理器,也可以3个独立换源 chsrc set npm chsrc set yarn chsrc set pnpm chsrc set nvm chsrc set bun chsrc set perl | cpan chsrc set php | composer chsrc set lua | luarocks chsrc set rust | cargo | crate chsrc set rustup chsrc set go chsrc set java | maven | mvn | maven-daemon | mvnd | gradle chsrc set clojure | clojars chsrc set dart | pub chsrc set flutter chsrc set haskell | hackage | cabal | stack chsrc set ocaml | opam # 同时会为 bioconductor 换源 chsrc set r | cran chsrc set julia ```
## 操作系统 ```bash sudo chsrc set ubuntu sudo chsrc set zorinos sudo chsrc set linuxmint sudo chsrc set debian sudo chsrc set fedora # 同时支持 Leap 和 Tumbleweed sudo chsrc set opensuse sudo chsrc set kali sudo chsrc set arch sudo chsrc set archlinuxcn sudo chsrc set manjaro sudo chsrc set gentoo sudo chsrc set rocky | rockylinux sudo chsrc set alma | almalinux sudo chsrc set alpine sudo chsrc set voidlinux sudo chsrc set solus sudo chsrc set ros | ros2 sudo chsrc set trisquel sudo chsrc set linuxlite sudo chsrc set raspi | raspberrypi sudo chsrc set armbian sudo chsrc set openwrt sudo chsrc set openeuler sudo chsrc set openanolis | anolis sudo chsrc set openkylin sudo chsrc set deepin chsrc set msys2 | msys # Android chsrc set termux # BSD sudo chsrc set freebsd sudo chsrc set openbsd sudo chsrc set netbsd ```
## 软件 ```bash chsrc set winget chsrc set brew | homebrew chsrc set cocoapods | cocoa | pod chsrc set dockerhub | docker chsrc set flathub | flatpak chsrc set nix chsrc set guix chsrc set emacs | elpa chsrc set tex | ctan | latex | texlive | miktex chsrc set conda | anaconda ```
## 📝 许可证 - `chsrc` 主程序采用 `GPL-3.0-or-later` 许可证,保证该软件的永久自由 - `xy.h` 使用 `MIT` 许可证,保证该库可以在尽可能多的情况下复用
## ❤️ 致谢 感谢各个镜像站提供的优质免费镜像服务 1. [mirror.c](./src/framework/mirror.c) 包含了通用的镜像站信息 2. 各个recipe内部定义的专用镜像站 另外特别感谢以下组织或项目: 1. [校园网联合镜像站(MirrorZ)](https://help.mirrors.cernet.edu.cn/) 2. [清华大学 Tuna](https://mirrors.tuna.tsinghua.edu.cn/) 3. [上海交通大学软件源镜像服务](https://mirrors.sjtug.sjtu.edu.cn/) 4. [中国科学技术大学 Linux 用户协会](https://github.com/ustclug) 5. [Thanks Mirror 项目](https://github.com/eryajf/Thanks-Mirror) by [@eryajf](https://github.com/eryajf)
## 🚀 赞赏支持 你是否因为 `chsrc` 而受到启发、节省了时间精力 or whatever? 爱发电主页: wechat-reward
[rawstr4c]: https://github.com/RubyMetric/rawstr4c [chsrc-bootstrap]: ./bootstrap/ [@Aaron-212]: https://github.com/Aaron-212 [@chenrui333]: https://github.com/chenrui333 [@niheaven]: https://github.com/niheaven [@Gn3po4g]: https://github.com/Gn3po4g [@Jerry-Terrasse]: https://github.com/Jerry-Terrasse [@Efterklang]: https://github.com/Efterklang [@wickdynex]: https://github.com/wickdynex [@YU-7]: https://github.com/YU-7 [@sanchuanhehe]: https://github.com/sanchuanhehe ================================================ FILE: bootstrap/README.md ================================================ # Bootstrap ```ruby begin download_prebuilt_chsrc_binary_for_my_platform rescue NoReadyMadeBinary => e bootstrap! e.my_platform end ```
## 预编译产生的死锁问题 我们支持预编译的操作系统目前只有 `Windows`, `Linux`, `macOS`,支持的架构请参考[项目 README](../README.md) 某些操作系统如 BSD 家族,甚至是更小众的操作系统, 或者上述操作系统的某些架构,无法享受直接下载二进制立即使用的便利性,这导致用户需要自己编译 `chsrc`. 然而用户自己编译则面临着一个问题: **需要提前安装项目依赖** 1. 把代码 `git clone` 下来 或 下载 `zip` 包进行解压缩 2. C语言编译器 3. `GNU make` 或 `just` (这二者非强制,但是有了更好) 可是如果用户还没有换源,他/她又如何获得上述这些程序呢?**这是一把死锁,导致用户最终回到手动换源的原始农耕时代。**
## `chsrc-bootstrap` to the Rescue `chsrc-bootstrap` 是一组使用原生脚本语言的脚本,每个脚本称为 `bootstrapper`,用来完成两件事情: 1. 帮助用户进行最基本的换源,让用户能够立即开始使用该系统安装其他软件 2. 用户自行决定是否需要安装 `chsrc` - 若不需要: 流程直接结束,用户已可以完成基本日常工作 - 若需要: 帮助用户安装好所需的最少依赖,并编译安装 `chsrc` 注意,最基本的换源,即不测速,由维护者暂时决定先切换到某一个具体的源,先让系统跑起来。 ### 支持的语言 上述提到的原生脚本语言只能是这些: 1. Windows: `PowerShell`, `CMD` 2. Linux: `Bash`, `sh` 备选: `Perl` 3. macOS: `Zsh`, `Bash`, `sh` 备选: `Ruby`, `Perl` 4. BSD: `sh` 备选: 待议 5. 其他平台: 一切该平台支持的脚本语言,无需额外安装
## `chsrc` 与 `chsrc-bootstrap` `chsrc` 需要实现 `chsrc-bootstrap` 中支持的平台。这是因为,`chsrc-bootstrap` 只换到了某一个具体的源,只是临时性的,后续用户可能还想要换到其他源,此时 `chsrc` 就可以接管。 而事实上,`chsrc-bootstrap` 可以完成一些操作,使得系统已经满足一些前置条件,这样的话 `chsrc` 可以在这些前置条件存在时接着换源,从而大幅减轻 `recipe` 的实现难度。
## 帮助 1. [FreeBSD 的换源](https://github.com/RubyMetric/chsrc/issues/11) 从项目一开始就是我们的目标之一,然而由于其: - 换源所需的步骤太多 - 面临着特有的鸡蛋困境 - BSD 工具和 GNU 工具总是有不兼容之处 - 在C语言中实现困难重重 因此我们一直没有很好的办法去实现 2. 任何你所在的平台,都可以添加到此 ================================================ FILE: bootstrap/Termux.bash ================================================ #!/usr/bin/env bash # --------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # --------------------------------------------------------------- # File Name : Termux.bash # File Authors : Aoran Zeng # Contributors : Nul None # Created On : <2025-07-12> # Last Modified : <2026-01-21> # # Termux: # # Bootstrap Termux: https://github.com/RubyMetric/chsrc/issues/173 # # @consult https://help.mirrors.cernet.edu.cn/termux/ # # 我们默认采用校园网联合镜像站提供的源 # --------------------------------------------------------------- bs_echo() { echo "[chsrc-bootstrap] $*" } if command -v termux-change-repo &>/dev/null; then termux-change-repo else # $PREFIX 的值为: /data/data/com.termux/files/usr # 用户主目录 ~ 为: /data/data/com.termux/files/home # 必要的 sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.cernet.edu.cn/termux/apt/termux-main stable main@' $PREFIX/etc/apt/sources.list apt-get update # x11-repo sed -i 's@^\(deb.*x11 main\)$@#\1\ndeb https://mirrors.cernet.edu.cn/termux/apt/termux-x11 x11 main @' $PREFIX/etc/apt/sources.list.d/x11.list # root-repo sed -i 's@^\(deb.*root main\)$@#\1\ndeb https://mirrors.cernet.edu.cn/termux/apt/termux-root root main @' $PREFIX/etc/apt/sources.list.d/root.list fi # 立即更新测试换源状态 apt-get update && apt-get upgrade bs_echo "基础换源已完成" read -p "是否需要安装 chsrc ? (y/n): " need_install_chsrc if [[ $need_install_chsrc == "y" || $need_install_chsrc == "Y" ]]; then bs_echo "正在安装依赖项..." apt-get install -y gcc make git git clone https://gitee.com/RubyMetric/chsrc.git --depth 1 bs_echo "依赖安装完成!" bs_echo "正在开始编译和安装" cd chsrc make build-in-release-mode make install bs_echo "chsrc 安装完成!" fi ================================================ FILE: doc/01-开发与构建.md ================================================ # 开发 chsrc ## 依赖与开发环境 请安装好: 1. `GCC` 或 `Clang` 2. [just] 或 `make` 3. `curl` 4. [rawstr4c] (可选) **我推荐你使用 VS Code 开发,你可以在一分钟内成功编译、运行和 Debug `chsrc`** 1. `Ctrl-Shift-B` 直接构建 2. `F5` 直接开始 Debug
## 获取代码 **请务必使用 dev 分支开发** ```bash git clone https://gitee.com/RubyMetric/chsrc.git -b dev ``` 关于分支的说明,可参考 [./03-CONTRIBUTING.md](./03-CONTRIBUTING.md)
## 编译运行 共有四种构建模式: 1. **`DEV mode`** 2. **`DEBUG mode`** 3. **`RELEASE mode`** 4. **`CI-RELEASE mode`** 开发时只需要前两种模式;第四种模式只在 GitHub Actions 使用。 **如果你使用 `just`,可以在 VS Code 中获得更好的体验,按 `Ctrl-Shift-B` 直接使用 DEV mode 构建** ```bash just (b) # 在Windows上默认使用 gcc 编译,在macOS上默认使用 clang 编译,在其他系统上默认使用 cc 编译 just CC=clang # 使用 clang 编译 just CC=gcc # 使用 gcc 编译 # 编译出 debug 版本: chsrc-debug just bd # 编译出 release 版本: chsrc-release just br ```
`make` 用户: ```bash make (b) # 在Windows上默认使用 cc 编译,在macOS上默认使用 clang 编译,在其他系统上默认使用 cc 编译 make CC=clang # 使用 clang 编译 make CC=gcc # 使用 gcc 编译 # 编译出 debug 版本: chsrc-debug make bd # 编译出 release 版本: chsrc-release make br ```
## Debug **如果你使用 `just`,可以在 VS Code 中获得更好的体验,按 F5 即可立即开始 Debug** ```bash # 重新编译出 ./chsrc-debug,并启动 GDB 调试 (在macOS上启动 LLDB 调试) $ just debug # 重新编译出 ./chsrc-debug,并启动 LLDB 调试 $ just DEBUGGER=lldb debug ```
`make` 用户: ```bash # 重新编译出 ./chsrc-debug,并启动 GDB 调试 (在macOS上启动 LLDB 调试) $ make debug # 重新编译出 ./chsrc-debug,并启动 LLDB 调试 $ make debug DEBUGGER=lldb ```
## 测试 `just` 用户只需要替换下述 `make` 为 `just` ```bash make test-xy # 测试 xy.h make test-fw # 测试 framework make test # 测试上述两个 make test-cli # 测试命令 make clean ```
## 提交 PR 关于分支的说明以及如何提交代码,请参考 [./03-CONTRIBUTING.md](./03-CONTRIBUTING.md)
[rawstr4c]: https://github.com/RubyMetric/rawstr4c [just]: https://github.com/casey/just ================================================ FILE: doc/02-提交与贡献.md ================================================ # 贡献说明 ## 分支 - `gh-build`:仅仅在发布版本时由 `@ccmywish` 推送,触发编译到 GitHub Releases 中 - `main`: stable,代码一定是可以编译运行的,我们假设 end users 在其他条件都得不到二进制时,会自己编译这个分支来运行 `chsrc` - `dev`:开发分支,工作分支,在此分支上解决冲突
## 提交与审阅 当你提交 PR 的时候,一定要将 PR 指定 chsrc 原仓库的 dev 分支。 ### 一个简单的 Bug 一个简单的 Bug fix,有写权限的维护者可以直接推送到主仓库的 `dev` 分支
### 不太容易修复的 Bug 以及新功能 这里要分两种情况考虑。(1)recipe 相关的 (2)framework 相关的 (1) **如果你是 recipe Chef,则你完全负责这个 recipe,如果你拥有写权限,你可以直接推送代码到 `dev` 分支** --- (2) 1. 需要先搜索你修改的部分涉及到的 recipe,然后提 issue @ 所有相关的 recipe Chef 来 review 2. 如果涉及了所有 recipe,则 @ framework Chef,而无需把所有 recipe 的 Chef 都喊过来,但是如果觉得有必要,可以 @ 任意你觉得有能力 review 和能给出建议的人来 review
### 最好总是 issue 或 PR 对于有写权限的维护者来说,即使是能够直接推代码,最好也都先提 issue 或 PR,因为这样能够让大家知道代码发生了哪些变动。 如果你觉得要和大家讨论,则 issue;如果你觉得没有讨论的必要了,则直接 PR 后自己立即合并即可。之所以多此一举,是因为这能够显式地记录代码的加入过程,其相当于一份文档方便未来的自己和他人查阅 ================================================ FILE: doc/03-为什么拒绝使用代码格式化工具.md ================================================ # chsrc 代码风格 Ruby 的语法优美性在编程行业中具有标杆地位。Matz(松本行弘)提出的 **代码应该为人类而写,偶尔为机器执行** 这一理念,已经成为现代编程语言设计的重要指导原则。`chsrc` 项目的第一作者深受 **Ruby** 语言哲学的影响。 本项目起源于 AI 编程尚未流行的时代,所有代码全部依赖人类的耐心来维护。现在,我们的代码以及这篇文章不仅会由人类阅读,也会由 AI 阅读。我们始终坚持:**代码的可读性和维护性是项目长期发展的根本保障**。
## 为什么我们坚持不使用代码格式化工具 代码格式化工具(code formatter)在多人协作的代码仓库中确实有其价值,**参与的人越多,统一格式的需求越迫切**。然而,`chsrc` 项目经过深思熟虑后,拒绝使用代码格式化工具。让我来说明这一决定背后的深层原因。 ### 被强迫使用 像 `Prettier` 这样的工具表面上带来了统一性,但其代价是什么?**它是极度专制的(opiniated)**,我们必须**完全交出代码审美的自主权**。今天我们大部分人使用 `Prettier`,**并非出于真心的认同,而是因为整个前端生态圈的集体胁迫——不用就意味着被边缘化**。 这种现象的本质令人深思:**少数 `Prettier` 维护者的个人偏好,竟然决定了全球数百万开发者的代码美学标准。这显然是一种技术独裁,坚决拒绝向格式化工具的霸权低头**。
### 一致性 ≠ 美观 ≠ 可读 格式化工具只保证了表面的一致性,就像一些校服,一致不一定代表美。同样,一致不一定代表代码就是最易读易懂的。 每一个有追求的程序员都应该保留**对代码美学的最后决定权,格式化工具的便利性不应该以牺牲美观性和可维护性为代价**。
### 满足不了我们的需求 C语言的格式化工具通常选择 `clang-format`,它的配置选项十分丰富,比 `Prettier` 要理性得多。然而,即便如此,**其配置的复杂性和局限性仍然无法满足 chsrc 对代码格式的严苛要求**。如果你是一位 `clang-format` 的配置专家,我们诚挚邀请您告诉我们如何优雅地处理以下代码场景,**也许这能改变我的立场**。
### 挑战案例 以下是我认为自动格式化工具很难完美处理的代码场景: `=` 对齐: ```c char *name = va_arg (args, char*); char *email = va_arg (args, char*); ``` 复杂逻辑的 `=` 对齐: ```c bool matched = iterate_menu (chsrc_pl_menu, input, &target_tmp); if (!matched) matched = iterate_menu (chsrc_os_menu, input, &target_tmp); if (!matched) matched = iterate_menu (chsrc_wr_menu, input, &target_tmp); ``` 预处理指令的层次缩进: ```c #ifdef _WIN32 #define XY_Build_On_Windows 1 #define xy_on_windows true #ifdef XY_DEBUG #define xy_debug_mode 1 #endif #endif ``` ...... 等等
## C语言代码风格 - 整体上基于 `GNU style`,但我们坚持自己的美学原则,在细节上有所改进 - 类型名: `PascalCase_t`,即 `UpperCamelCase_t` - 函数定义和调用时,**函数名和`()`之间始终保持一个空格**,如果是在宏中,可紧凑一些,无硬性规定 - 函数和函数定义之间**一般保持2个空行** - 若函数之间有高度关联性,**可用1个空行** - 若一系列函数和一系列函数存在主题性区别,**可用3个空行**
## Markdown 写作风格 维护者很多时候不是从渲染好的界面来看 Markdown 文件的,而是阅读 Markdown 源文件,所以 Markdown 在源文件层面也要易读。 我们保持每个主题之间 **1个`
` + 3个空行** 的简单风格。 拒绝使用 VS Code 的 `markdownlint` 插件,**因为它总是用它狭隘的标准给我们增加了巨多的黄色下划线**。
## 其他语言代码风格 我们秉承 **入乡随俗、尊重传统** 的原则,尊重每种语言社区的既定传统。比如,`YAML` 使用2个空格,`JSON`使用4个空格,`Perl` 使用 Larry Wall 钟爱的4个空格。 我们使用 `.editorconfig` 来确保这些格式的应用。
================================================ FILE: doc/10-如何编写recipe.md ================================================ # Write A Recipe Even If You Don't Know C ## 介绍 **`chsrc` 不仅是一个命令行工具,同时也是一个体现了 Ruby on Rails 思想的 MVC 换源框架,它甚至使你能够在不了解C语言的情况下编写出新的换源方法(recipe)。**
我鼓励你为新的软件添加换源支持,因为通过 `chsrc` 框架,这将比写 shell 脚本更加轻松,你的贡献也将非常有价值。理论上每一个 `recipe` 都需要有专人长时间维护 ([招募](https://github.com/RubyMetric/chsrc/issues/275))。 - 本项目采用 `GPLv3+` 协议,是真正的**自由软件**,而非仅仅是开源软件 - 代码规范灵活遵循 `GNU` 标准(若标准干扰了可维护性,则并不采纳) - 高度模块化,目录结构清晰易懂 - 极小依赖,极易构建,只需要 `GCC` 或 `Clang` 即可编译 (`make` 和 `just`可简化编译,但不是必需的) - 易于将 `shell` 脚本转换为等价的 `recipe` - 已有大量 `recipe` 可提供参考,并提供了 [recipe template] 供直接使用 - 本仓库外的子项目 [rawstr4c] 帮助你在C语言中维护复杂的字符串 - [chsrc-bootstrap] 帮助你在没有预编译 `chsrc` 的平台上 bootstrap 自己
成功案例: - [Armbian](../src/recipe/os/APT/Armbian.c) - [uv](../src/recipe/lang/Python/uv.c)
## 基本概念 - `target`: 所要换源的目标 - `target group`: 一个 `target` 包含了多个子 `target`,比如 `Python group` 包含了该语言的多个包管理器 - `category`: 是 `target category` 的简写,即 `target` 所属的类别,可以是 **编程语言**,**操作系统**,**软件** 三类之一 1. 在目录中,三者分别为 `lang`, `os`, `ware` 2. 在代码中,三者前缀分别为 `pl`, `os`, `wr` - `mirror`: 是 `mirror site` 的简写,指镜像站,如清华大学开源软件镜像站 - `source`: 该 `target` 所能换的具体的源,由 `mirror` 提供服务,往往一个 `mirror` 会提供许多 `source` - `recipe`: 是为一个 `target` 定义的具体换源方法,请参考 `src` 目录中的 `recipe` 目录 - `chef DSL`:是 `chef Domain Specific Language` 的简写,这是一组以 `chef_` 开头的函数,用来定义维护者信息、可用源等元信息 - **换源链接**: 指镜像站所提供的某一个具体的换源使用的URL - **测速链接**: 用来测速的URL,与 "换源链接" 不同,可分为 **精准测速** 和 **模糊测速** 1. 在代码中,测速链接一般使用 `smURL` (即 `speed measure URL`) 或 `speed_measure_url` 来指代 2. 在代码中,换源链接一般使用 `repoURL` (即 `repository URL`) 或直接用 `url` 来指代 - 为什么不用 `regurl`,因为使用术语 `repository` 的 target 远多于使用术语 `registry` 的。 - 为什么莫名其妙使用了大小写混合的API? 因为 `smurl` 和 `repourl` 的可读性太差 - **镜像源**: 为了方便,**偶尔**我们将直接称`mirror`和/或`source`为**镜像源**,这只是一种方便性的称呼,可以统称二者,也可以根据上下文指代二者之一
## 编写 `recipe` 步骤 1. 确定你要编写的 `target` 的标准名称,创建 `Target-Name.c` 文件 大小写需严格按官方,若名称包含空格,需使用 `-` 代替空格 2. 根据类别将上述文件放在 `recipe/` 目录的某个子目录中 3. 复制 [recipe template] 的内容到上述文件中,并替换 `<...>` 占位符 4. 参考现有 `recipe` 的写法 1. 看一眼就能上手的参照物是 [PHP recipe](../src/recipe/lang/PHP.c) 2. 最好的参照物是 [Ruby recipe](../src/recipe/lang/Ruby/Ruby.c) 3. 组换源参照物是 [Python group recipe](../src/recipe/lang/Python/Python.c) 5. 在 [Wiki] 中记录的镜像站中寻找可用源;可以额外补充镜像站 6. 使用 chef DSL 定义 `_prelude()` 函数 该函数将填充 target 所有的必要信息,包括维护信息、换源信息 7. [设置/修改 "换源链接" 和 "测速链接" (how?)](./11-如何设置换源链接与测速链接.md) 8. 按需实现 `_setsrc()` `_getsrc()` `_resetsrc()`, 可以使用这些函数: 1. `framework/core.c` 中以 `chsrc_` 开头的所有函数或宏 2. `xy.h` 中以 `xy_` 开头的所有函数或宏 3. `chec.c` 中以 `chef_` 开头的所有函数或宏 4. `helper.c` 中以 `hp_` 开头的所有函数或宏 一个简单的方法是,在 VS Code 中按快捷键 `Ctrl-T` 搜索上述三种前缀 9. 在 `recipe/menu.c` 中登记此 target 10. [编译、运行、测试 (how?)](./01-开发与构建.md),若无问题可提交 Pull Request
## 开发准则 1. 代码高度可移植 2. Convention over Configuration 3. [NO UFO 原则: 不要乱丢文件到$HOME等目录,尤其是使用各种隐晦的文件名](https://www.yuque.com/ccmywish/blog/no-ufo) `chsrc` 主程序不提供配置文件,不提供数据文件,干净无污染。那么在实现 `recipe` 的时候,除了备份文件外,也不要污染用户环境。
[rawstr4c]: https://github.com/RubyMetric/rawstr4c [chsrc-bootstrap]: ../bootstrap/ [recipe template]: ../src/recipe/recipe-template.c [Wiki]: https://github.com/RubyMetric/chsrc/wiki ================================================ FILE: doc/11-如何设置换源链接与测速链接.md ================================================ # 新增/修改 "换源链接" 在 `_prelude()` 函数中,在 `def_sources_begin()` 的位置的**第二列**,新增/修改你想要替换的链接。 **注: 以下代码仅起解释作用,JavaScript换源的真正实现可能不同** ```c def_sources_begin() {&UpstreamProvider, "https://registry.npmjs.org/", FeedByPrelude}, {&NpmMirror, "https://registry.npmmirror.com", FeedByPrelude}, {&Huawei, "https://mirrors.huaweicloud.com/repository/npm/", FeedByPrelude}, {&Tencent, "https://mirrors.cloud.tencent.com/npm/", FeedByPrelude}, {&某新镜像站, "某镜像站提供的换源链接", FeedByPrelude} def_sources_end() // // 调整上述某一个镜像站的所提供源的 "换源链接" chef_set_repoURL (this, &UpstreamProvider, "新的换源链接"); ```
# 设置/修改 "测速链接" 在 `_prelude()` 函数中,在 `def_sources_begin()` 的位置的**第三列**,设置/修改你想要替换的链接。 1. 在下面代码的第一行中,我们直接给了一个链接,**这就是精准测速链接** 2. 在下面代码的第二行和第三行中,我们都设置了一个宏 `DelegateToMirror`,这意味着我们没有直接提供精准测速链接,而是让 `chsrc` 去测试其对应镜像站定义的测速链接 - `NpmMirror` 是专用镜像站,所以其测速链接被设置为了 `ACCURATE`,即精准测速 - `Huawei` 是通用镜像站,所以其测速链接被设置为了 `ROUGH `,即模糊测速 3. 在下面代码的第四行中,我们设置了一个宏 `FeedByPrelude`,这意味着我们将在 `_prelude()` 函数的后续动态计算 URL **注: 以下代码仅起解释作用,JavaScript换源的真正实现可能不同** ```c def_sources_begin() {&UpstreamProvider, "https://registry.npmjs.org/", "https://registry.npmjs.org/BigFile.tar.gz"}, {&NpmMirror, "https://registry.npmmirror.com", DelegateToMirror}, {&Huawei, "https://mirrors.huaweicloud.com/repository/npm/", DelegateToMirror}, {&Tencent, "https://mirrors.cloud.tencent.com/npm/", FeedByPrelude}, def_sources_end() // // 调整/设置上述某一个镜像站的所提供源的 "精准测速链接" chef_set_smURL (this, &UpstreamProvider, "新的测速链接"); // 设置上述某一个镜像站的所提供源的 "精准测速链接" 设置为 "换源链接" + postfix chef_set_smURL_with_postfix (this, &UpstreamProvider, "/dir/BigFile.tar.gz"); // 基于 "换源链接" 做更自定义的操作 chef_set_smURL_with_func (this, &UpstreamProvider, func, data); // 把所有上述源中还未设置 "精准测速链接" 的源进行设置 chef_set_rest_smURL_with_postfix (this, "/dir/BigFile.tar.gz"); chef_set_rest_smURL_with_func (this, func, data); // 调整某一个镜像站(Provider)的 "测速链接" chef_set_provider_smURL (&Tencent, "https://mirrors.cloud.tencent.com/npm/BigFile.tar.gz"); // 调整某一个镜像站(Provider)的 "测速精度" chef_set_provider_sm_accuracy (&UpstreamProvider, ROUGH); ``` ================================================ FILE: doc/50-协作者与维护者.md ================================================ # 协作者与维护者 想要达到最理想的维护状态,每一个 recipe 都需要有专人长时间维护。**我们的代码里使用了 recipe (某个菜的烹饪方法) 这个词,因而整个项目便和 "饮食" 有关,比如 `menu`: 汇集了所有的 target 的菜单。因此,我们的维护者和贡献者身份依然使用了这个惯例:** 1. **Chef** 品控 (主厨): 对一个 recipe 完全负责,有写权限时可以直接推代码 **目前项目的发展阶段还处于 *外行实现内行* 的情况,比如 Homebrew recipe,实现者根本不是 Homebrew 的真实用户,只是根据各种文档来实现,然后等待用户反馈。所以这里当前的实现者最多只能是 Cook,无法承担 Chef 的责任** 2. **Cook** 掌勺:一个 recipe 的主要作者 3. **Saucier** 调味:一个 recipe 的次要贡献者 (除主要作者外的其他人)
**Chef 采用申请制,每个 recipe 仅有1人,请提交 PULL REQUEST 在对应的 recipe 文件中添加自己,并在 [issue #275 ](https://github.com/RubyMetric/chsrc/issues/275) 留言。** ================================================ FILE: doc/README.md ================================================ # 文档说明 E2E (End-to-End) 开发文档: - 开发环境准备,如何编译和测试,请参考 [01-开发与构建.md](./01-开发与构建.md) - 直接推送还是提交 PR,请参考 [02-提交与贡献.md](./02-提交与贡献.md) - 代码风格,请参考 [03-为什么拒绝使用代码格式化工具.md](./03-为什么拒绝使用代码格式化工具.md)
具体 recipe 相关: - 如何编写一个具体的 recipe,请参考 [10-如何编写recipe.md](./10-如何编写recipe.md) - 如何修改换源链接、模糊/精准测速链接,请参考 [11-如何设置换源链接与测速链接.md](./11-如何设置换源链接与测速链接.md)
- 关于维护者身份的说明,请参考 [50-协作者与维护者.md](./50-协作者与维护者.md)
用户手册: 1. [chsrc.1](./chsrc.1) 2. [chsrc.texi](./chsrc.texi)
## 贡献指导 若有任何问题,可在 [GitHub discussions](https://github.com/RubyMetric/chsrc/discussions) 中询问和讨论
================================================ FILE: doc/chsrc.1 ================================================ .\" -------------------------------------------------------------- .\" SPDX-License-Identifier: GFDL-1.3-or-later .\" -------------------------------------------------------------- .\" Doc Type : Man Page .\" Doc Authors : 曾奥然 .\" Contributors : Nul None .\" | .\" Created On : <2024-08-21> .\" Last Modified : <2026-02-24> .\" ---------------------------------------------------------------- .TH chsrc 1 "2026-02-24" "v0.2.5" "RubyMetric chsrc" .SH NAME chsrc - Change Source for every software on every platform from the command line .SH SYNOPSIS .SY chsrc [options] [target] [mirror] .YS .SH DESCRIPTION .SS 基本命令 .TP .B help 打印此帮助,或 h, -h, --help .TP .B issue 查看相关issue .TP \fBlist\fR (或 \fBls\fR, 或 \fBl\fR) 列出可用镜像源,和可换源目标 .TP .B list mirror/target 列出可用镜像源,或可换源目标 .B list os/lang/ware 列出可换源的操作系统/编程语言/软件 .SS 测速命令 .TP .B measure/cesu \fI\fI 对该目标所有源测速 .SS 查看配置命令 .TP .B list \fI\fR 查看该目标可用源与支持功能 .TP .B get \fI\fR 查看该目标当前源的使用情况 .SS 换源命令 .TP .B set \fI\fR 换源,自动测速后挑选最快源 .TP .B set \fI\fR first 换源,使用维护团队测速第一的源 .TP .B set \fI\fR \fI\fR 换源,指定使用某镜像站 (通过list 查看) .TP .B set \fI\fR \fI\fR 换源,用户自定义源URL .TP .B reset \fI\fR 重置,使用上游默认使用的源 .SH OPTIONS .TP \fB-dry\fR Dry Run,模拟换源过程,命令仅打印并不运行 .TP \fB-scope\fR 设置换源的作用域,可选值有:project|user|system|default (通过ls \fI\fR查看支持情况) .TP \fB-ipv6\fR 使用IPv6测速 .TP \fB-en(glish)\fR 使用英文输出 .TP \fB-no-color\fR 无颜色输出 .SH "EXIT STATUS" .br .TP 0 正常执行,一般表示换源成功 .TP 1 用户环境导致的错误,如命令缺失 .TP 2 chsrc 暂未实现支持 .TP 3 维护者导致的镜像站、源信息相关错误 .TP 4 致命错误,由内部Bug导致 .TP 5 致命未知错误,往往代表内部未知Bug .SH EXAMPLES .EX # 测速,寻找最快者,换源 $ chsrc set ruby # 列出可用的镜像站 # 使用 RubyChina 作为镜像站 $ chsrc ls ruby $ chsrc set ruby rubychina # 可以使用自定义URL $ chsrc set ruby https://gems.ruby-china.com/ # 使用维护团队测试的最快镜像站 $ chsrc set ruby first .EE .SH FILES .B 遵循 No UFO(Unidentified File Objects)原则:https://www.yuque.com/ccmywish/blog/no-ufo .PP 因此不会有任何文件存放在你的计算机中! .\" .SH SEE ALSO .SH AUTHOR Written by Aoran Zeng, Heng Guo and contributors. (See chsrc.c) .SH "REPORTING BUGS" On Gitee: https://gitee.com/RubyMetric/chsrc/issues .br On GitHub: https://github.com/RubyMetric/chsrc/issues .SH COPYRIGHT Copyright (C) 2026 Aoran Zeng, Heng Guo. License GPLv3+: GNU GPL version 3 or later .br This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. ================================================ FILE: doc/chsrc.texi ================================================ @c -------------------------------------------------------------- @c SPDX-License-Identifier: GFDL-1.3-or-later @c -------------------------------------------------------------- @c Doc Type : Texinfo @c Doc Authors : 曾奥然 @c Contributors : Nul None @c | @c Created On : <2024-08-22> @c Last Modified : <2026-02-24> @c -------------------------------------------------------------- \input texinfo @setfilename chsrc.info @settitle chsrc @set Chsrc_Version v0.2.5 @set Doc_Publish_Date 2026-02-24 @copying @comment 这是软件copyright,不是文档copyright,因此不放在titlepage Copyright @copyright{} 2023-2026 曾奥然, 郭恒 @end copying @titlepage @title chsrc printed manual @subtitle Change Source everywhere for every software! @author 曾奥然 @c @insertcopying For chsrc @value{Chsrc_Version} @page @end titlepage @contents @node Top @top chsrc on-terminal manual The tool and framework @command{chsrc} is a great helper to Change Source for every software on every platform from the command line. This texinfo documentation is published on @b{@value{Doc_Publish_Date}} for @command{chsrc} @b{@value{Chsrc_Version}} @exdent 使用: chsrc [options] [target] [mirror] Exit status: @display 0 正常执行,一般表示换源成功 1 用户环境导致的错误,如命令缺失 2 chsrc 暂未实现支持 3 维护者导致的镜像站、源信息相关错误 4 致命错误,由内部Bug导致 5 致命未知错误,往往代表内部未知Bug @end display @noindent 维护: @itemize @item On Gitee: https://gitee.com/RubyMetric/chsrc/issues @item On GitHub: https://github.com/RubyMetric/chsrc/issues @end itemize @menu * commands:: 命令 * options:: 选项 * examples:: 例子 @end menu @insertcopying @node commands @chapter 命令 @section 基本命令 @table @samp @item help 打印此帮助,或 h, -h, --help @item issue 查看相关issue @item list (或 ls, 或 l) 列出可用镜像源,和可换源目标 @item list mirror/target 列出可用镜像源,或可换源目标 @item list os/lang/ware 列出可换源的操作系统/编程语言/软件 @end table @section 测速命令 @table @samp @item measure @itemx cesu 对该目标所有源测速 @end table @page @section 查看配置命令 @table @samp @item list 查看该目标可用源与支持功能 @item get 查看该目标当前源的使用情况 @end table @section 换源命令 @table @samp @item set 换源,自动测速后挑选最快源 @item set first 换源,使用维护团队测速第一的源 @item set 换源,指定使用某镜像站 (通过list 查看) @item set https://url 换源,用户自定义源URL @item reset 重置,使用上游默认使用的源 @end table @node options @chapter 选项 @table @samp @item -dry Dry Run,模拟换源过程,命令仅打印并不运行 @item -ipv6 使用IPv6测速 @item -scope 设置换源的作用域,可选值有:project|user|system|default (通过ls 查看支持情况) @item -en(glish) 使用英文输出 @item -no-color 无颜色输出 @end table @node examples @chapter 例子 @example # 测速,寻找最快者,换源 $ chsrc set ruby # 列出可用的镜像站 # 使用 RubyChina 作为镜像站 $ chsrc ls ruby $ chsrc set ruby rubychina # 可以使用自定义URL $ chsrc set ruby https://gems.ruby-china.com/ # 使用维护团队测试的最快镜像站 $ chsrc set ruby first @end example @bye ================================================ FILE: include/.keep ================================================ ================================================ FILE: justfile ================================================ #!/usr/bin/env just --justfile # -------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # -------------------------------------------------------------- # Build File : justfile # File Authors : 曾奥然 # Contributors : Mikachu2333 # | # Created On : <2025-06-18> # Last Modified : <2025-10-15> # # 该文件主要用于在原生Windows上执行项目的基本任务,而不借助于 # GNU make 以及相应的 MSYS2、Cygwin 环境 # -------------------------------------------------------------- # just (build) -> chsrc.exe # just bd (build-in-debug-mode) -> chsrc-debug.exe # just br (build-in-release-mode) -> chsrc-release.exe # just debug # just test # just clean # # just STATIC=1 br 静态链接 (注意只能在 br 任务中使用) # # 不支持 just DEBUG=1,请直接使用 just bd (等价于 just build-in-debug-mode) # # -------------------------------------------------------------- # 注意,由于我们要在 GitHub Actions 上编译 x32 版的 chsrc,所以需要使用 make, # 而不清楚 just 在 MINGW32 中的情况,所以我们在此 justfile 中并不实现关于 CI 的功能 # -------------------------------------------------------------- set windows-shell := ['cmd', '/c'] CC := if os() == 'windows' { "gcc" } else if os() == 'macos' { "clang" } else { "cc" } DEBUGGER := if os() == 'windows' { "gdb" } else if os() == 'macos' { "lldb" } else { "gdb" } CFLAGS_chk_Clang := if os() == 'windows' { if CC == 'clang' { '-target x86_64-pc-windows-gnu' } else {''} } else {''} CFLAGS_base := '-Iinclude -Ilib ' + CFLAGS_chk_Clang WARN := '-Wall -Wextra -Wno-unused-variable -Wno-unused-function -Wno-missing-braces -Wno-misleading-indentation' + ' ' + \ '-Wno-missing-field-initializers -Wno-unused-parameter -Wno-sign-compare' CFLAGS_warn := WARN CFLAGS_debug := '-g -DXY_DEBUG' DevMode-Target-Name := 'chsrc' DebugMode-Target-Name := 'chsrc-debug' ReleaseMode-Target-Name := 'chsrc-release' STATIC := '0' CFLAGS_static := "-static" CFLAGS_chk_static := if STATIC == '1' { CFLAGS_static } else { "" } CFLAGS_optimization := "-O2" CFLAGS_dev_mode_prompt := CFLAGS_base CFLAGS_debug_mode_prompt := CFLAGS_base + ' ' + CFLAGS_debug CFLAGS_release_mode_prompt := CFLAGS_base + ' ' + CFLAGS_optimization + ' ' + CFLAGS_chk_static CFLAGS_dev_mode := CFLAGS_dev_mode_prompt + ' ' + CFLAGS_warn CFLAGS_debug_mode := CFLAGS_debug_mode_prompt + ' ' + CFLAGS_warn CFLAGS_release_mode := CFLAGS_release_mode_prompt + ' ' + CFLAGS_warn #======================= BIN_xy := if os() == 'windows' {'xy.exe'} else {'./xy'} BIN_fw := if os() == 'windows' {'fw.exe'} else {'./fw'} BIN_rm := if os() == 'windows' {'del'} else {'rm'} #======================= alias b := build-in-dev-mode alias bd:= build-in-debug-mode alias br:= build-in-release-mode alias build:=build-in-dev-mode alias d := debug alias t := test alias check := test alias c := clean default: build-in-dev-mode build-in-dev-mode: @echo Starting: Build in DEV mode: '{{CC}}' {{CFLAGS_dev_mode_prompt}} -o {{DevMode-Target-Name}} @{{CC}} src/chsrc-main.c {{CFLAGS_dev_mode}} -o {{DevMode-Target-Name}} @echo Finished: Build in DEV mode build-in-debug-mode: @echo Starting: Build in DEBUG mode: '{{CC}}' {{CFLAGS_debug_mode_prompt}} -o {{DebugMode-Target-Name}} @{{CC}} src/chsrc-main.c {{CFLAGS_debug_mode}} -o {{DebugMode-Target-Name}} @echo Finished: Build in DEBUG mode build-in-release-mode: @echo Starting: Build in RELEASE mode: '{{CC}}' {{CFLAGS_release_mode_prompt}} -o {{ReleaseMode-Target-Name}} @{{ if os() == 'windows' { \ '(if exist chsrc.res del chsrc.res)' + \ ' & windres src/resource/chsrc.rc -O coff -o chsrc.res' \ } else { '' } }} @{{ if os() == 'windows' { \ CC + ' src/chsrc-main.c chsrc.res ' + CFLAGS_release_mode + ' -o ' + ReleaseMode-Target-Name \ } else { \ CC + ' src/chsrc-main.c ' + CFLAGS_release_mode + ' -o ' + ReleaseMode-Target-Name \ } }} @echo Finished: Build in RELEASE mode debug: build-in-debug-mode @{{DEBUGGER}} {{DebugMode-Target-Name}} test: test-xy test-fw # 这两个测试文件都用 DEBUG mode test-xy: @{{CC}} test/xy.c {{CFLAGS_debug_mode}} -o xy @{{BIN_xy}} test-fw: @{{CC}} test/fw.c {{CFLAGS_debug_mode}} -o fw @{{BIN_fw}} fastcheck: @perl ./test/cli.pl fastcheck test-cli: @perl ./test/cli.pl clean: -@{{BIN_rm}} *.exe -@{{BIN_rm}} *.res -@{{BIN_rm}} xy -@{{BIN_rm}} fw -@{{BIN_rm}} chsrc -@{{BIN_rm}} chsrc-debug -@{{BIN_rm}} chsrc-release -@{{BIN_rm}} README.md.bak* # 使用: just rawstr4c [--debug] Markdown.md # rawstr4c *args: # @{{ if os() == 'windows' { 'tool\\rawstr4c\\run\\run.bat' } else { 'bash ./tool/rawstr4c/run/run.sh' } }} {{args}} ================================================ FILE: lefthook.yml ================================================ # --------------------------------------------------------------- # Lefthook File : lefthook.yml # File Authors : 曾奥然 # Contributors : Nul None # | # Created On : <2025-10-28> # Last Modified : <2025-10-30> # --------------------------------------------------------------- min_version: 2.0.1 pre-commit: only: - ref: dev parallel: false jobs: - name: 试运行 DEBUG mode # 触发所有 _prelude() 函数检查 run: | just build-in-debug-mode ./chsrc-debug get pip glob: "*.{c,h}" exclude: test/* - name: 测试 xy.h run: just test-xy glob: - lib/xy.h - test/xy.c - name: 测试 framework run: just test-fw glob: - "src/framework/*.{c,h}" - src/chsrc-main.c - test/fw.c post-merge: only: - ref: main jobs: - name: 测试编译 DEV mode run: just build-in-dev-mode pre-push: only: - ref: gh-build jobs: - name: 测试编译 RELEASE mode run: just build-in-release-mode - name: 测试运行 run: just test-cli ================================================ FILE: lib/xy.h ================================================ /** ------------------------------------------------------------ * Copyright © 2023-2026 曾奥然, 郭恒 * SPDX-License-Identifier: MIT * ------------------------------------------------------------- * Lib Authors : 曾奥然 * | 郭恒 <2085471348@qq.com> * Contributors : Mikachu2333 * | juzeon * | BingChunMoLi * | * Created On : <2023-08-28> * Last Modified : <2025-12-31> * * * xy: 襄阳、咸阳 * * 为跨平台命令行应用程序准备的 C11 实用函数和宏 (utilities) * * 该库的特点是混合多种编程语言风味 (绝大多数为 Ruby),每个 API * 均使用 @flavor 标注其参考依据 * ------------------------------------------------------------*/ #ifndef XY_H #define XY_H #define _XY_Version "v0.2.2.0-2025/10/28" #define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h" #define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h" #if defined(__STDC__) && __STDC_VERSION__ < 201112L # error "xy.h requires C11 or later, please use a new compiler which at least supports C11" #endif #if defined(__STDC__) && __STDC_VERSION__ < 201710L # warning "xy.h recommends a C17 or later compiler" #endif #include #include #include #include #include #include #include #include #include #include #include // opendir() closedir() #if defined(__STDC__) && __STDC_VERSION__ >= 202311 #define XY_Deprecate_This(msg) [[deprecated(msg)]] #elif defined(__GNUC__) || defined(__clang__) #define XY_Deprecate_This(msg) __attribute__((deprecated(msg))) #elif defined(_MSC_VER) #define XY_Deprecate_This(msg) __declspec(deprecated(msg)) #else #define XY_Deprecate_This(msg) #endif /* 全局变量 与 全局状态 */ struct { bool enable_color; bool inited; bool on_windows; bool on_linux; bool on_macos; bool on_bsd; bool on_android; /* @flavor 同 just 中的 os_family(),只区分 windows, unix */ char *os_family; char *os_devnull; } xy = { .enable_color = true, /* 由 xy_init() 赋值 */ .inited = false, .on_windows = false, .on_linux = false, .on_macos = false, .on_bsd = false, .on_android = false, .os_family = NULL, .os_devnull = NULL }; #ifdef _WIN32 #define XY_Build_On_Windows 1 #include #include #elif defined(__linux__) || defined(__linux) #define XY_Build_On_Linux 1 #define XY_Build_On_Unix 1 #elif defined(__APPLE__) #define XY_Build_On_macOS 1 #define XY_Build_On_Unix 1 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) #define XY_Build_On_BSD 1 #define XY_Build_On_Unix 1 #endif /** * assert() 会被 NDEBUG 关闭,但我们也没有必要强制开启它,还是留给用户定义 */ // #undef NDEBUG #define xy_noop() ((void)0) #define assert_str(a, b) assert (xy_streql ((a), (b))) #define xy_throw(reason) assert(!reason) /** * @depreacated 避免消极用语 * * @flavor Perl, PHP, Raku */ // #define die xy_throw /** * @depreacated 避免消极用语 */ // #define xy_panic xy_throw #define xy_unsupported() xy_throw("Unsuppoted") #define xy_unimplemented() xy_throw("Unimplemented temporarily") #define xy_unreached() xy_throw("This code shouldn't be reached") #define xy_cant_be_null(p) if(!p) xy_throw("This pointer can't be null") static inline void _xy_internal_warn (char *str) { fprintf (stderr, "[xy.h] %s\n", str); } static void _xy_print_int (int n) {printf ("%d", n);} static void _xy_print_long (long n) {printf ("%ld", n);} static void _xy_print_long_long (long long n) {printf ("%lld", n);} static void _xy_print_float (float n) {printf ("%f", n);} static void _xy_print_double (double n) {printf ("%f", n);} static void _xy_print_bool (bool b) {printf("%s", (b) ? "true" : "false");} static void _xy_print_str (char *str) {printf ("%s", str);} static void _xy_print_const_str (const char *str) {printf ("%s", str);} static void _xy_println_int (int n) {printf ("%d\n", n);} static void _xy_println_long (long n) {printf ("%ld\n", n);} static void _xy_println_long_long (long long n) {printf ("%lld\n", n);} static void _xy_println_float (float n) {printf ("%f\n", n);} static void _xy_println_double (double n) {printf ("%f\n", n);} static void _xy_println_bool (bool b) {printf("%s\n", (b) ? "true" : "false");} static void _xy_println_str (char *str) {printf ("%s\n", str);} static void _xy_println_const_str (const char *str) {printf ("%s\n", str);} /** * @flavor Ruby, Python */ #define print(x) _Generic((x), \ int: _xy_print_int, \ long: _xy_print_long, \ long long: _xy_print_long_long, \ float: _xy_print_float, \ double: _xy_print_double, \ bool: _xy_print_bool, \ char *: _xy_print_str, \ const char *: _xy_print_const_str, \ default: xy_throw("Unsupported type for print()!") \ )(x) /** * @flavor JVM family, Rust */ #define println(x) _Generic((x), \ int: _xy_println_int, \ long: _xy_println_long, \ long long: _xy_println_long_long, \ float: _xy_println_float, \ double: _xy_println_double, \ bool: _xy_println_bool, \ char *: _xy_println_str, \ const char *: _xy_println_const_str, \ default: xy_throw("Unsupported type for println()/say()!") \ )(x) /* @flavor Raku, Perl */ #define say println /* @flavor PHP */ #define echo println /** * @flavor HTML */ void br () { puts (""); } void p (const char *s) { printf ("%s\n", s); } #define xy_c_array_len(arr) (sizeof (arr) / sizeof (arr[0])) static inline void * xy_malloc0 (size_t size) { void *ptr = malloc (size); memset (ptr, 0, size); return ptr; } /** * @brief 替换指针内容并自动释放旧内存 * * @param pptr 指向要被替换内存区域的指针的指针 * *pptr 可为 NULL * @param new_mem 新的内存区域 */ static inline void xy_ptr_replace (char **pptr, char *new_mem) { xy_cant_be_null (pptr); if (*pptr) free (*pptr); *pptr = new_mem; } /****************************************************** * String ******************************************************/ /** * @brief 将 str 中所有的 pat 字符串替换成 replace,返回一个全新的字符串 * * @flavor Ruby: String#gsub * * @param str 原字符串 * @param pat 要替换的字符串 * @param replace 替换成的字符串 * * @return 替换后的新字符串 */ static char * xy_str_gsub (const char *str, const char *pat, const char *replace) { size_t replace_len = strlen (replace); size_t pat_len = strlen (pat); int unit = replace_len - pat_len; if (unit <= 0) unit = 0; size_t len = strlen (str); const char *cur = str; int count = 0; while (cur < str + len) { char *fnd = strstr (cur, pat); if (fnd) { count++; cur = fnd + pat_len; } else break; } // puti(count); DEBUG 匹配次数 char *ret = malloc (unit * count + len + 1); char *retcur = ret; cur = str; while (cur < str + len) { char *fnd = strstr (cur, pat); if (fnd) { ptrdiff_t diff = fnd - cur; strncpy (retcur, cur, diff); cur = fnd + pat_len; retcur += diff; strcpy (retcur, replace); retcur += replace_len; } else break; } strcpy (retcur, cur); return ret; } /** * @flavor 见 xy_strcat() */ static char * xy_2strcat (const char *str1, const char *str2) { size_t len = strlen (str1); size_t size = len + strlen (str2) + 1; char *ret = malloc (size); strcpy (ret, str1); strcpy (ret + len, str2); return ret; } /** * @brief 将多个字符串连接成一个字符串 * * @flavor C语言存在 strcat(),然而限制比较大,我们重新实现 * 'concat' 这个API广泛应用于包括 Ruby、JavaScript、JVM family、C# * * 但由于 xy_str_concat() 显著长于 xy_strcat(),而这个 API 在 chsrc 中 * 又大量使用,所以我们选择后者这个更简短的形式 * * @param count 连接的字符串数量 * @param ... 连接的字符串 * * @return 拼接的新字符串 */ static char * xy_strcat (unsigned int count, ...) { size_t al_fixed = 256; char *ret = calloc (1, al_fixed); // 已分配次数 int al_times = 1; // 当前已分配量 size_t al_cur = al_fixed; const char *str = NULL; // 需要分配的量 size_t al_need = 0; // 用于 strcpy() 到 ret 的哪个位置 char *cur = ret + 0; va_list args; va_start (args, count); for (int i = 0; i < count; i++) { bool need_realloc = false; str = va_arg (args, const char *); al_need += strlen (str) + 1; while (al_need > al_cur) { al_times += 1; al_cur = al_times * al_fixed; need_realloc = true; } // printf("al_times %d, al_need %zd, al_cur %zd\n", al_times, al_need, // al_cur); if (need_realloc) { ptrdiff_t diff = cur - ret; ret = realloc (ret, al_cur); cur = ret + diff; } if (NULL == ret) { _xy_internal_warn ("xy_strcat(): No availble memory to allocate!"); return NULL; } strcpy (cur, str); // puts(ret); cur += strlen (str); } va_end (args); *cur = '\0'; return ret; } /** * @brief 复制一个字符串,返回复制的新字符串 * * @param str 要复制的字符串 * * @return 复制的新字符串 */ static char * xy_strdup (const char *str) { if (!str) { _xy_internal_warn ("xy_strdup(): called with NULL!"); return NULL; } size_t len = strlen (str); char *new = xy_malloc0 (len + 1); strcpy (new, str); return new; } #define _XY_Str_Bold 1 #define _XY_Str_Faint 2 #define _XY_Str_Italic 3 #define _XY_Str_Underline 4 #define _XY_Str_Blink 5 #define _XY_Str_Cross 9 #define xy_str_to_bold(str) _xy_str_to_terminal_style (_XY_Str_Bold, str) #define xy_str_to_faint(str) _xy_str_to_terminal_style (_XY_Str_Faint, str) #define xy_str_to_italic(str) _xy_str_to_terminal_style (_XY_Str_Italic, str) #define xy_str_to_underline(str) _xy_str_to_terminal_style (_XY_Str_Underline, str) #define xy_str_to_blink(str) _xy_str_to_terminal_style (_XY_Str_Blink, str) #define xy_str_to_cross(str) _xy_str_to_terminal_style (_XY_Str_Cross, str) #define _XY_Str_Red 31 #define _XY_Str_Green 32 #define _XY_Str_Yellow 33 #define _XY_Str_Blue 34 #define _XY_Str_Magenta 35 #define _XY_Str_Cyan 36 #define xy_str_to_red(str) _xy_str_to_terminal_style (_XY_Str_Red, str) #define xy_str_to_green(str) _xy_str_to_terminal_style (_XY_Str_Green, str) #define xy_str_to_yellow(str) _xy_str_to_terminal_style (_XY_Str_Yellow, str) #define xy_str_to_blue(str) _xy_str_to_terminal_style (_XY_Str_Blue, str) #define xy_str_to_magenta(str) _xy_str_to_terminal_style (_XY_Str_Magenta, str) #define xy_str_to_purple xy_str_to_magenta #define xy_str_to_cyan(str) _xy_str_to_terminal_style (_XY_Str_Cyan, str) static char * _xy_str_to_terminal_style (int style, const char *str) { char *color_fmt_str = NULL; if (!xy.enable_color) { color_fmt_str = "%s"; goto new_str; } switch (style) { case _XY_Str_Red: color_fmt_str = "\e[31m%s\e[0m"; break; case _XY_Str_Green: color_fmt_str = "\e[32m%s\e[0m"; break; case _XY_Str_Yellow: color_fmt_str = "\e[33m%s\e[0m"; break; case _XY_Str_Blue: color_fmt_str = "\e[34m%s\e[0m"; break; case _XY_Str_Magenta: color_fmt_str = "\e[35m%s\e[0m"; break; case _XY_Str_Cyan: color_fmt_str = "\e[36m%s\e[0m"; break; case _XY_Str_Bold: color_fmt_str = "\e[1m%s\e[0m"; break; case _XY_Str_Faint: color_fmt_str = "\e[2m%s\e[0m"; break; case _XY_Str_Italic: color_fmt_str = "\e[3m%s\e[0m"; break; case _XY_Str_Underline: color_fmt_str = "\e[4m%s\e[0m"; break; case _XY_Str_Blink: color_fmt_str = "\e[5m%s\e[0m"; break; case _XY_Str_Cross: color_fmt_str = "\e[9m%s\e[0m"; break; } size_t len = 0; new_str: // -2 把中间%s减掉 len = strlen (color_fmt_str) - 2; char *buf = malloc (strlen (str) + len + 1); sprintf (buf, color_fmt_str, str); return buf; } static bool xy_streql (const char *str1, const char *str2) { if (NULL==str1 || NULL==str2) { return false; } return strcmp (str1, str2) == 0 ? true : false; } static bool xy_streql_ic (const char *str1, const char *str2) { if (NULL == str1 || NULL == str2) { return false; } size_t len1 = strlen (str1); size_t len2 = strlen (str2); if (len1 != len2) { return false; } for (size_t i = 0; i < len1; i++) { if (tolower (str1[i]) != tolower (str2[i])) { return false; } } return true; } /** * @flavor Ruby: String#end_with? */ static bool xy_str_end_with (const char *str, const char *suffix) { size_t len1 = strlen (str); size_t len2 = strlen (suffix); if (0 == len2) return true; // 空字符串直接返回 if (len1 < len2) return false; const char *cur1 = str + len1 - 1; const char *cur2 = suffix + len2 - 1; for (int i = 0; i < len2; i++) { if (*cur1 != *cur2) return false; cur1--; cur2--; } return true; } /** * @flavor Ruby: String#start_with? */ static bool xy_str_start_with (const char *str, const char *prefix) { if (NULL==str || NULL==prefix) { return false; } size_t len1 = strlen (str); size_t len2 = strlen (prefix); if (0 == len2) return true; // 空字符串直接返回 if (len1 < len2) return false; const char *cur1 = str; const char *cur2 = prefix; for (int i = 0; i < len2; i++) { if (*cur1 != *cur2) return false; cur1++; cur2++; } return true; } /** * @flavor Ruby: String#delete_prefix */ static char * xy_str_delete_prefix (const char *str, const char *prefix) { bool yes = xy_str_start_with (str, prefix); if (!yes) { return xy_strdup (str); } else { size_t len = strlen (prefix); return xy_strdup (str + len); } } /** * @flavor Ruby: String#delete_suffix */ static char * xy_str_delete_suffix (const char *str, const char *suffix) { char *new = xy_strdup (str); bool yes = xy_str_end_with (str, suffix); if (!yes) return new; size_t len1 = strlen (str); size_t len2 = strlen (suffix); char *cur = new + len1 - len2; *cur = '\0'; return new; } /** * @flavor Ruby: String#strip */ static char * xy_str_strip (const char *str) { xy_cant_be_null (str); const char *start = str; while (*start && strchr ("\n\r\v\t\f ", *start)) start++; if ('\0' == *start) return xy_strdup (""); const char *end = start + strlen (start) - 1; while (end >= start && strchr ("\n\r\v\t\f ", *end)) end--; size_t len = (size_t) (end - start + 1); char *ret = xy_malloc0 (len + 1); memcpy (ret, start, len); ret[len] = '\0'; return ret; } typedef struct { bool found; size_t begin; size_t end; } XyStrFindResult_t; /** * @brief 查找子串,返回是否命中以及子串在原串中的起止位置(0 基,end 为闭区间) */ static XyStrFindResult_t xy_str_find (const char *str, const char *substr) { XyStrFindResult_t result = { .found = false, .begin = 0, .end = 0 }; if (!str || !substr) return result; if ('\0' == substr[0]) { result.found = true; return result; } const char *pos = strstr (str, substr); if (!pos) return result; result.found = true; result.begin = (size_t) (pos - str); result.end = result.begin + strlen (substr) - 1; return result; } /** * @brief 获取字符串下一行的内容 * * @note 将忽略开头的换行,截取至下一个换行前(不含换行符) */ static char * xy_str_next_nonempty_line (const char *str) { if (!str) return xy_strdup (""); const char *cur = str; while (*cur == '\n') cur++; if ('\0' == *cur) return xy_strdup (""); const char *newline = strchr (cur, '\n'); size_t len = newline ? (size_t) (newline - cur) : strlen (cur); char *ret = xy_malloc0 (len + 1); strncpy (ret, cur, len); ret[len] = '\0'; return ret; } /** * @brief 读取文件内容并返回字符串,失败时返回空字符串 * * @note 已处理 \r\n 和 \r,返回的字符串均为 \n 换行 * * @flavor Ruby: IO::read */ static char * xy_file_read (const char *path) { FILE *fp = fopen (path, "rb"); if (!fp) return xy_strdup (""); if (fseek (fp, 0, SEEK_END) != 0) { fclose (fp); return xy_strdup (""); } long size = ftell (fp); if (size < 0) { fclose (fp); return xy_strdup (""); } rewind (fp); char *buf = xy_malloc0 ((size_t) size + 1); if (!buf) { fclose (fp); return xy_strdup (""); } size_t read_bytes = fread (buf, 1, (size_t) size, fp); if (read_bytes < (size_t) size && ferror (fp)) { fclose (fp); free (buf); return xy_strdup (""); } fclose (fp); buf[read_bytes] = '\0'; char *formatted_str = xy_str_gsub (buf, "\r\n", "\n"); xy_ptr_replace (&formatted_str, xy_str_gsub (formatted_str, "\r", "\n")); free (buf); return formatted_str; } /****************************************************** * Logging ******************************************************/ #define _XY_Log_Plain 000000001 #define _XY_Log_Success 000000001 << 1 #define _XY_Log_Info 000000001 << 2 #define _XY_Log_Warn 000000001 << 3 #define _XY_Log_Error 000000001 << 4 #define xy_log(prompt, str) _xy_log (_XY_Log_Plain, prompt, str) #define xy_succ(prompt,str) _xy_log (_XY_Log_Success, prompt, str) #define xy_info(prompt,str) _xy_log (_XY_Log_Info, prompt, str) #define xy_warn(prompt,str) _xy_log (_XY_Log_Warn, prompt, str) #define xy_error(prompt,str) _xy_log (_XY_Log_Error, prompt, str) static void _xy_log (int level, const char *prompt, const char *content) { char *str = NULL; bool to_stderr = false; /** * 'app: content' */ if (level & _XY_Log_Plain) { str = xy_strcat (3, prompt, ": ", content); } else if (level & _XY_Log_Success) { str = xy_strcat (3, prompt, ": ", xy_str_to_green (content)); } else if (level & _XY_Log_Info) { str = xy_strcat (3, prompt, ": ", xy_str_to_blue (content)); } else if (level & _XY_Log_Warn) { str = xy_strcat (3, prompt, ": ", xy_str_to_yellow (content)); to_stderr = true; } else if (level & _XY_Log_Error) { str = xy_strcat (3, prompt, ": ", xy_str_to_red (content)); to_stderr = true; } else { xy_unreached(); } if (to_stderr) { fprintf (stderr, "%s\n", str); } else { puts (str); } free (str); } /** * @flavor brkt 系列输出受 Python 的 pip 启发,为了输出方便,使用 xy.h 的程序应该 * * 1.若想完全自定义颜色和输出位置: * * 应基于下述 xy_log_brkt_to 定义自己的输出函数 * * 2.若想自定义颜色,而保持输出到stdout: * * 应基于下述 xy_log_brkt 定义 * * 3.若对log无细致要求,想要快速使用log功能: * * 应基于下述 xy__brkt 定义自己的 app__brkt(),或直接使用 xy__brkt */ static void xy_log_brkt_to (const char *prompt, const char *content, FILE *stream) { char *str = xy_strcat (4, "[", prompt, "] ", content); fprintf (stream, "%s\n", str); free (str); } #define xy_log_brkt(prompt1,prompt2,content) _xy_log_brkt(_XY_Log_Plain, prompt1,prompt2,content) #define xy_succ_brkt(prompt1,prompt2,content) _xy_log_brkt(_XY_Log_Success,prompt1,prompt2,content) #define xy_info_brkt(prompt1,prompt2,content) _xy_log_brkt(_XY_Log_Info, prompt1,prompt2,content) #define xy_warn_brkt(prompt1,prompt2,content) _xy_log_brkt(_XY_Log_Warn, prompt1,prompt2,content) #define xy_error_brkt(prompt1,prompt2,content) _xy_log_brkt(_XY_Log_Error, prompt1,prompt2,content) static void _xy_log_brkt (int level, const char *prompt1, const char *prompt2, const char *content) { char *str = NULL; bool to_stderr = false; if (level & _XY_Log_Plain) { str = xy_strcat (6, "[", prompt1, " ", prompt2, "] ", content); } else if (level & _XY_Log_Success) { /* [app 成功] [app success] */ str = xy_strcat (6, "[", xy_str_to_green (prompt1), " ", xy_str_to_bold (xy_str_to_green (prompt2)), "] ", xy_str_to_green (content)); } else if (level & _XY_Log_Info) { /* [app 信息] [app info] [app 提示] [app notice] */ str = xy_strcat (6, "[", xy_str_to_blue (prompt1), " ", xy_str_to_bold (xy_str_to_blue (prompt2)), "] ", xy_str_to_blue (content)); } else if (level & _XY_Log_Warn) { /* [app 警告] [app warn] */ str = xy_strcat (6, "[", xy_str_to_yellow (prompt1), " ", xy_str_to_bold (xy_str_to_yellow (prompt2)), "] ", xy_str_to_yellow (content)); to_stderr = true; } else if (level & _XY_Log_Error) { /* [app 错误] [app error] */ str = xy_strcat (6, "[", xy_str_to_red (prompt1), " ", xy_str_to_bold (xy_str_to_red (prompt2)), "] ", xy_str_to_red (content)); to_stderr = true; } else { xy_unreached(); } if (to_stderr) { fprintf (stderr, "%s\n", str); } else { puts (str); } free (str); } /****************************************************** * cross OS ******************************************************/ static char * xy_quiet_cmd (const char *cmd) { char *ret = NULL; if (xy.on_windows) ret = xy_2strcat (cmd, " >nul 2>nul "); else ret = xy_2strcat (cmd, " 1>/dev/null 2>&1 "); return ret; } /** * @brief 执行 `cmd`,返回其输出到 stdout 的内容中的第 `n` 行,并对已经遍历过的行执行 `func` * * @param cmd 要执行的命令 * @param n 指定命令执行输出到 stdout 内容中的某一行,0 表示最后一行,n (n>0) 表示第n行 * @param func 对遍历时经过的行的内容,进行函数调用,如果返回 true,则提前停止遍历 * * @return * 1. 第 `n` 行的内容 * 或 * 2. `func` 调用后返回为 true 的行 * * @note * 1. 由于目标行第 `n` 行会被返回出来,所以 `func` 并不执行目标行,只会执行遍历过的行 * 或 * 2. 由于 `func` 调用后返回为 true 的行会被返回出来,所以该返回出的行也被 `func` 执行过了 */ static char * xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char *)) { const int size = 512; char *buf = (char *) malloc (size); FILE *stream = popen (cmd, "r"); if (stream == NULL) { _xy_internal_warn ("xy_run_iter_lines(): popen() failed"); return NULL; } char *ret = NULL; unsigned long count = 0; while (true) { if (NULL == fgets (buf, size, stream)) break; /* 存在换行的总是会把换行符读出来,删掉 */ ret = xy_str_delete_suffix (buf, "\n"); count += 1; if (n == count) break; if (func) { if (func (ret)) break; } } pclose (stream); return ret; } /** * @brief 执行命令 `cmd`,返回第 `n` 行输出到 stdout 的内容 * * @return 返回第 `n` 行输出到 stdout 的内容 */ static char * xy_run (const char *cmd, unsigned long n) { return xy_run_iter_lines (cmd, n, NULL); } /** * @brief 执行命令 `cmd`,仅返回其 Exit Code,stdout 与 stderr 均不输出到终端 * * @return 返回 `cmd` 的 Exit Code */ int xy_run_get_status (char *cmd) { char * command = xy_quiet_cmd (cmd); int status = system (command); return status; } /** * @brief 获得执行命令 `cmd` 输出到 stdout 的内容,而 stderr 依然输出到终端 * * @param[in] cmd 要执行的命令 * @param[out] output 捕获的标准输出, * 为NULL时表示不关心stdout输出,但依然允许stderr输出 * * @return 返回命令的执行状态 */ static int xy_run_get_stdout (const char *cmd, char **output) { int cap = 8192; /* 假如1行100个字符,大约支持80行输出 */ char *buf = (char *) xy_malloc0 (cap); FILE *stream = popen (cmd, "r"); if (stream == NULL) { _xy_internal_warn ("xy_run_get_stdout(): popen() failed"); return -1; } int size = 0; size_t n; while ((n = fread (buf + size, 1, cap - size, stream)) > 0) { size += n; if (size == cap) { cap *= 2; char *new_buf = realloc (buf, cap); buf = new_buf; } } buf[size] = '\0'; int status = pclose (stream); if (output) *output = buf; return status; } /** * @brief 该函数返回所在 os family 的对应字符串 */ static const char * xy_os_depend_str (const char *str_for_win, const char *str_for_unix) { if (xy.on_windows) return str_for_win; else return str_for_unix; } /** * @brief 返回当前操作系统的 HOME 目录 * * @note Windows 上返回 %USERPROFILE%,Linux 等返回 $HOME * @warning Windows 上要警惕 Documents 等目录被移动位置的情况,避免使用本函数的 HOME 路径直接拼接 Documents,应参考 _xy_win_documents() 的实现 */ #define xy_os_home _xy_os_home () static char * _xy_os_home () { char *home = NULL; if (xy.on_windows) home = getenv ("USERPROFILE"); else home = getenv ("HOME"); return home; } /** * @brief 返回 Windows 上的 Documents 目录 * * @note 警告,不可使用 HOME 目录直接拼接,若用户移动了 Documents,将导致错误 * @warning 非 Windows 返回 NULL */ static char * _xy_win_documents () { #ifdef XY_Build_On_Windows char documents_path[MAX_PATH]; HRESULT result = SHGetFolderPathA (NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documents_path); if (SUCCEEDED (result)) return xy_strdup (documents_path); return xy_2strcat (xy_os_home, "\\Documents"); #else return NULL; #endif } #define xy_win_powershell_profile _xy_win_powershell_profile () #define xy_win_powershellv5_profile _xy_win_powershellv5_profile () /** * @brief 返回 Windows 上 pwsh (>=v5) 的配置文件路径 * * @warning 非 Windows 返回 NULL */ static char * _xy_win_powershell_profile () { if (xy.on_windows) { char *documents_dir = _xy_win_documents (); char *profile_path = xy_2strcat (documents_dir, "\\PowerShell\\Microsoft.PowerShell_profile.ps1"); free (documents_dir); return profile_path; } else return NULL; } /** * @brief 返回 Windows 上自带的 powershell (v5) 的配置文件路径 * * @warning 非 Windows 返回 NULL */ static char * _xy_win_powershellv5_profile () { if (xy.on_windows) { char *documents_dir = _xy_win_documents (); char *profile_path = xy_2strcat (documents_dir, "\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1"); free (documents_dir); return profile_path; } else return NULL; } #define xy_zshrc "~/.zshrc" #define xy_bashrc "~/.bashrc" #define xy_fishrc "~/.config/fish/config.fish" /** * @note Windows上,`path` 不要夹带变量名,因为最终 access() 不会帮你转换 */ static bool xy_file_exist (const char *path) { char *expanded_path = NULL; const char *check_path = path; if (xy_str_start_with (path, "~")) { expanded_path = xy_2strcat (xy_os_home, path + 1); check_path = expanded_path; } // 0 即 F_OK bool result = (0 == access (check_path, 0)) ? true : false; if (expanded_path) free (expanded_path); return result; } /** * @note `xy_file_exist()` 和 `xy_dir_exist()` 两个函数在所有平台默认都支持使用 '~', * 但实现中都没有调用 `xy_normalize_path()`,以防万一,调用前可能需要用户手动调用它 */ static bool xy_dir_exist (const char *path) { char *allocated_dir = NULL; const char *dir = path; if (xy.on_windows) { if (xy_str_start_with (path, "~")) { allocated_dir = xy_2strcat (xy_os_home, path + 1); dir = allocated_dir; } } if (xy.on_windows) { #ifdef XY_Build_On_Windows // 也可以用 opendir() #include DWORD attr = GetFileAttributesA (dir); bool result = false; if (attr == INVALID_FILE_ATTRIBUTES) { // Q: 我们应该报错吗? result = false; } else if (attr & FILE_ATTRIBUTE_DIRECTORY) { result = true; } else { result = false; } if (allocated_dir) free (allocated_dir); return result; #endif } else { char *tmp_cmd = xy_2strcat ("test -d ", dir); int status = system (tmp_cmd); free (tmp_cmd); bool result = (0==status); if (allocated_dir) free (allocated_dir); return result; } return false; } /** * @brief 规范化路径 * * @details * 1. 删除路径左右两边多出来的空白符 * - 防止通过间接方式得到的路径包含了空白字符 (如 grep 出来的结果) * - 防止维护者多写了空白字符 * 2. 将 ~ 转换为绝对路径 * 3. 在Windows上,使用标准的 \ 作为路径分隔符 */ static char * xy_normalize_path (const char *path) { char *new = xy_str_strip (path); if (xy_str_start_with (new, "~")) { char *tmp = xy_str_delete_prefix (new, "~"); char *joined = xy_2strcat (xy_os_home, tmp); free (tmp); xy_ptr_replace (&new, joined); } if (xy.on_windows) { xy_ptr_replace (&new, xy_str_gsub (new, "/", "\\")); } return new; } /** * @brief 返回一个路径的父目录名 * * @note * - 返回的是真正的 "目录名" (就像文件名一样),而不是 "路径",所以一定是不含末尾斜杠的 * - 在Windows上,使用标准的 \ 作为路径分隔符 */ static char * xy_parent_dir (const char *path) { char *dir = xy_normalize_path (path); /* 不管是否为Windows,全部统一使用 / 作为路径分隔符,方便后续处理 */ xy_ptr_replace (&dir, xy_str_gsub (dir, "\\", "/")); if (xy_str_end_with (dir, "/")) xy_ptr_replace (&dir, xy_str_delete_suffix (dir, "/")); char *last = NULL; last = strrchr (dir, '/'); if (!last) { /* 路径中没有一个 / 是很奇怪的,我们直接返回 . 作为当前目录 */ return "."; } *last = '\0'; /* Windows上重新使用 \ 作为路径分隔符 */ if (xy.on_windows) { xy_ptr_replace (&dir, xy_str_gsub (dir, "/", "\\")); } return dir; } /** * @internal 仅由 xy_init () 调用 */ void _xy_detect_os () { /** * 首先判断是否为 Windows * * SystemDrive 为 C: */ char *drive = getenv ("SystemDrive"); if (drive) { char path[256]; snprintf (path, sizeof (path), "%s\\Users", drive); DIR *d = opendir (path); if (d) { xy.on_windows = true; xy.os_family = "windows"; closedir (d); return; } } /* 判断 Linux */ FILE *fp = fopen ("/proc/version", "r"); if (fp) { char buf[256] = {0}; fread (buf, 1, sizeof (buf) - 1, fp); fclose (fp); if (strstr (buf, "Linux")) { xy.on_linux = true; xy.os_family = "unix"; return; } } /** * 判断 Android * * @consult https://android.googlesource.com/platform/system/core/+/refs/heads/main/rootdir/init.environ.rc.in */ char *android_env = getenv ("ANDROID_ROOT"); if (android_env) { if (xy_str_find (android_env, "/system").found) { xy.on_android = true; xy.os_family = "unix"; return; } } /* 判断 macOS */ DIR *d = opendir ("/System/Applications"); if (d) { closedir (d); d = opendir ("/Library/Apple"); if (d) { xy.on_macos = true; xy.os_family = "unix"; closedir (d); return; } } /* 最后判断 BSD */ fp = popen ("uname -s", "r"); if (fp) { char buf[256]; fgets (buf, sizeof (buf), fp); pclose (fp); if (strstr (buf, "BSD") != NULL) { xy.on_bsd = true; xy.os_family = "unix"; return; } } else { DIR *bsd_dir = opendir ("/etc/rc.d"); if (bsd_dir) { closedir (bsd_dir); xy.on_bsd = true; xy.os_family = "unix"; return; } } if (!(xy.on_windows || xy.on_linux || xy.on_android || xy.on_macos || xy.on_bsd)) xy_throw ("Unknown operating system"); } void xy_use_utf8 () { #ifdef XY_Build_On_Windows SetConsoleOutputCP (65001); #endif } /** * @note 该函数必须被首先调用,方能使用各个跨操作系统的函数 */ void xy_init () { _xy_detect_os (); if (xy.on_windows) xy.os_devnull = "nul"; else xy.os_devnull = "/dev/null"; xy_use_utf8 (); xy.inited = true; } /****************************************************** * Container ******************************************************/ typedef struct XySeqItem_t { struct XySeqItem_t *prev; struct XySeqItem_t *next; void *data; } XySeqItem_t; typedef struct XySeq_t { XySeqItem_t *first_item; XySeqItem_t *last_item; uint32_t length; } XySeq_t; XySeq_t* xy_seq_new (void) { XySeq_t *seq = xy_malloc0 (sizeof (XySeq_t)); if (!seq) return NULL; seq->first_item = NULL; seq->last_item = NULL; seq->length = 0; return seq; } /** * @flavor Python: len() */ uint32_t xy_seq_len (XySeq_t *seq) { xy_cant_be_null (seq); return seq->length; } /** * @flavor Ruby: Enumerable#first */ void * xy_seq_first (XySeq_t *seq) { xy_cant_be_null (seq); return seq->first_item ? seq->first_item->data : NULL; } /** * @flavor Ruby: Enumerable#last */ void * xy_seq_last (XySeq_t *seq) { xy_cant_be_null (seq); return seq->last_item ? seq->last_item->data : NULL; } /** * @flavor Ruby: Array#at * * @note 序号从1开始 * * @return 如果seq中并没有第n个数据,则返回NULL */ void * xy_seq_at (XySeq_t *seq, int n) { xy_cant_be_null (seq); if (0 == n) xy_throw ("The index must begin from 1, not 0"); if (1 == n) return seq->first_item ? seq->first_item->data : NULL; XySeqItem_t *it = seq->first_item; for (uint32_t i = 1; i < n && it; i++) { it = it->next; } return it ? it->data : NULL; } /** * @flavor Perl: push */ void xy_seq_push (XySeq_t *seq, void *data) { xy_cant_be_null (seq); XySeqItem_t *it = xy_malloc0 (sizeof (XySeqItem_t));; it->data = data; it->prev = NULL; it->next = NULL; // 更新 item 间关系 XySeqItem_t *l = seq->last_item; if (l) { it->prev = l; l->next = it; } // 更新 seq 信息 seq->last_item = it; if (0==seq->length) seq->first_item = it; seq->length++; } /** * @flavor Perl: pop */ void * xy_seq_pop (XySeq_t *seq) { xy_cant_be_null (seq); if (0==seq->length) return NULL; // 更新 item 间关系 XySeqItem_t *l = seq->last_item; XySeqItem_t *p = l->prev; // 考虑 seq 当前只有1项的情况 if (p) p->next = NULL; l->prev = NULL; // 更新 seq 信息 seq->last_item = p; // 考虑 seq 当前只有1项的情况 if (!p) seq->first_item = NULL; seq->length--; void *data = l->data; free (l); return data; } /** * @flavor Ruby: Array#each */ void xy_seq_each (XySeq_t *seq, void (*func) (void *, void *), void *user_data) { xy_cant_be_null (seq); xy_cant_be_null (func); for (XySeqItem_t *it = seq->first_item; it; it = it->next) { func (it->data, user_data); } } /** * @flavor Ruby: Enumerable#find */ void * xy_seq_find (XySeq_t *seq, bool (*func) (void *, void *), void *user_data) { xy_cant_be_null (seq); xy_cant_be_null (func); for (XySeqItem_t *it = seq->first_item; it; it = it->next) { if (func (it->data, user_data)) { return it->data; } } return NULL; } #define _XY_Map_Buckets_Count 97 struct _XyHashBucket_t { struct _XyHashBucket_t *next; char *key; void *value; }; typedef struct XyMap_t { struct _XyHashBucket_t **buckets; uint32_t length; } XyMap_t; XyMap_t * xy_map_new () { XyMap_t *map = xy_malloc0 (sizeof (XyMap_t)); map->buckets = xy_malloc0 (sizeof (struct _XyHashBucket_t *) * _XY_Map_Buckets_Count); map->length = 0; return map; } uint32_t xy_map_len (XyMap_t *map) { xy_cant_be_null (map); return map->length; } unsigned long xy_hash (const char* str) { unsigned long h = 5381; int c; while ((c = *str++)) h = ((h << 5) + h) + c; /* h * 33 + c */ return h; } /** * @flavor JavaScript: map.set */ void xy_map_set (XyMap_t *map, const char *key, void *value) { xy_cant_be_null (map); xy_cant_be_null (key); unsigned long hash = xy_hash (key); uint32_t index = hash % _XY_Map_Buckets_Count; // 若 key 已经存在 struct _XyHashBucket_t *maybe = map->buckets[index]; while (maybe) { if (xy_streql (maybe->key, key)) { maybe->value = value; return; } maybe = maybe->next; } // 若 key 不存在 struct _XyHashBucket_t *bucket = xy_malloc0 (sizeof (struct _XyHashBucket_t)); bucket->key = xy_strdup (key); bucket->value = value; bucket->next = map->buckets[index]; map->buckets[index] = bucket; map->length++; } /** * @flavor JavaScript: map.get */ void * xy_map_get (XyMap_t *map, const char *key) { xy_cant_be_null (map); xy_cant_be_null (key); unsigned long hash = xy_hash (key); uint32_t index = hash % _XY_Map_Buckets_Count; struct _XyHashBucket_t *maybe = map->buckets[index]; while (maybe) { if (xy_streql (maybe->key, key)) { return maybe->value; } maybe = maybe->next; } return NULL; } /** * @flavor Ruby: Hash#each */ void xy_map_each ( XyMap_t *map, void (*func) (const char *key, void *value, void *user_data), void *user_data) { xy_cant_be_null (map); xy_cant_be_null (func); for (uint32_t i = 0; i < _XY_Map_Buckets_Count; i++) { struct _XyHashBucket_t *bucket = map->buckets[i]; while (bucket) { func (bucket->key, bucket->value, user_data); bucket = bucket->next; } } } #endif ================================================ FILE: pkg/AUR/README.md ================================================ # AUR package 本文档说明了 `chsrc` 项目的 AUR 包的相关情况
## 相关文件 CI 维护者 [@Jerry-Terrasse](https://github.com/Jerry-Terrasse) - [.github/workflows/pub-AUR-chsrc-git.yml](../../.github/workflows/pub-AUR-chsrc-git.yml) - [.github/workflows/pub-AUR-chsrc-and-chsrc-bin.yml`](../../.github/workflows/pub-AUR-chsrc-git.yml)
## 分发 位于 https://aur.archlinux.org/packages - [chsrc-bin](https://aur.archlinux.org/packages/chsrc-bin): 从 GitHub Releases 直接下载的二进制文件 - [chsrc](https://aur.archlinux.org/packages/chsrc):从 GitHub Releases 的代码构建 - [chsrc-git](https://aur.archlinux.org/packages/chsrc-git):从最新源码构建的版本,偶尔可能不稳定 ================================================ FILE: pkg/README.md ================================================ # Packages ## 分发情况 - [x] `Homebrew` - [x] `Scoop` - [x] `WinGet` - [x] `AUR` - [ ] `Flatpak` - [ ] `snap` - [ ] ...
```bash $ brew install chsrc $ scoop install chsrc $ winget install RubyMetric.chsrc yay -S chsrc-bin # Binary from GitHub Release yay -S chsrc # Build from GitHub Release yay -S chsrc-git # Build from the latest main branch (stable) ```
- Homebrew https://github.com/Homebrew/homebrew-core/blob/master/Formula/c/chsrc.rb - Scoop https://github.com/ScoopInstaller/Main/blob/master/bucket/chsrc.json - WinGet https://github.com/microsoft/winget-pkgs/tree/master/manifests/r/RubyMetric/chsrc - AUR: - https://aur.archlinux.org/packages/chsrc-bin - https://aur.archlinux.org/packages/chsrc - https://aur.archlinux.org/packages/chsrc-git
## 打包情况 - [x] `deb`
================================================ FILE: pkg/deb/BUILD.md ================================================ # 构建 deb package ## 准备 安装构建所需的依赖: ```bash sudo apt-get update sudo apt-get install build-essential debhelper devscripts fakeroot ```
## 构建 ```bash git clone https://github.com/RubyMetric/chsrc.git cd chsrc # 进入 deb 目录 cd pkg/deb # 执行构建命令 ./Makefile deb-make ``` ### 调试构建 ```bash # 启用详细输出 DEB_BUILD_OPTIONS="nocheck" debuild -us -uc -b # 检查构建日志 less ../chsrc_*.build # 检查包内容 dpkg --contents ../chsrc_*.deb # 或 dpkg-deb --contents ../chsrc_*.deb ``` ### 交叉编译 为不同架构进行构建: ```bash # For ARM64 CC=aarch64-linux-gnu-gcc dpkg-buildpackage -us -uc -b -aarm64 # For ARMv7 (armhf) CC=arm-linux-gnueabihf-gcc dpkg-buildpackage -us -uc -b -aarmhf ```
### 安装 ```bash sudo dpkg -i ../chsrc_*.deb sudo apt-get install -f # 修复依赖问题 ```
### 测试安装情况 ```bash # 运行测试 bash ./deb-installation-test.sh # 查看文档安装情况 man chsrc # 查看 deb 包的 copyright cat /usr/share/doc/chsrc/copyright # 查看 deb 包 changelog zless /usr/share/doc/chsrc/changelog.Debian.gz ```
### 清理构建产物 ```bash ./Makefile deb-clean ```
## 卸载 ```bash sudo apt-get remove chsrc ``` 包括删除配置在内的完全删除: ```bash sudo apt-get purge chsrc ```
## 故障排查 常见问题: 1. **构建失败**: 检查 debian/control 中的依赖是否正确 2. **交叉编译失败**: 确认目标架构的工具链已正确安装 3. **安装测试失败**: 检查 postinst 脚本是否有错误
================================================ FILE: pkg/deb/CI.md ================================================ # deb package CI/CD 本文档说明了 chsrc 项目的 deb 包自动构建和发布流程。 ## CI 文件 CI 维护者 [@sanchuanhehe](https://github.com/sanchuanhehe) - [.github/workflows/pkg-deb.yml](../../.github/workflows/pkg-deb.yml)
## 支持的架构 当前支持以下架构的 deb 包构建: - `amd64` (x86_64)
## CI 构建产物 每次 CI 构建会生成: 1. **deb 包文件**: `chsrc_-1_.deb` 2. **仓库元数据**: `Packages` 和 `Packages.gz` 文件用于创建 APT 仓库
## 自动触发 deb 包构建 CI 会在以下情况下自动触发: 1. **Push 事件**:当 push 到 `gh-build` 分支时自动构建,并上传 deb 包 到 `pre` 这个特定的 release 中 2. **Release 事件**: 当创建新的 release 时自动构建,并上传 deb 包到最新的这个 release 中 2. **手动触发**: 可以在 GitHub Actions 页面手动触发构建
## 手动发布流程 1. 确保所有代码已合并到主分支 2. 更新版本号和 changelog 3. 创建并推送 git tag: `git tag v1.2.3 && git push origin v1.2.3` 4. 在 GitHub 上创建 release 5. CI 将自动构建并上传 deb 包
================================================ FILE: pkg/deb/Makefile ================================================ #!/usr/bin/make -f # -------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # -------------------------------------------------------------- # Build File : Makefile # File Authors : sanchuanhehe # Contributors : 曾奥然 # | # Created On : <2025-06-14> # Last Modified : <2025-06-16> # # deb package targets # # @issue https://github.com/RubyMetric/chsrc/pull/206 # 这些本是 debhelper 兼容性版本 (debian/compat) 为 9 时所需要定义的 targets # 然而现在已经为版本 13 (debian/compat已移除),不再需要这些 targets,仅出于 # 实用目的保留。由于以上原因,也不要修改这些 targets 的名称。 # -------------------------------------------------------------- all: deb-build deb-prepare: @echo "Starting: Prepare for building deb package" @echo "Finished: Prepare for building deb package" deb-build: deb-prepare @echo "Starting: Build deb package" @debuild -us -uc -b @echo "Finished: Build deb package" deb-clean: @echo "Starting: Clean deb build artifacts" -@rm -rf debian/chsrc/ -@rm -f ../chsrc_*.deb ../chsrc-dbgsym_*.ddeb ../chsrc_*.changes ../chsrc_*.buildinfo ../chsrc_*.build @echo "Finished: Clean deb build artifacts" .PHONY: deb-prepare deb-build deb-clean ================================================ FILE: pkg/deb/README.md ================================================ # deb package 本文档说明了 `chsrc` 项目的 deb 包的相关情况
## 相关文件 - `.github/workflows/pkg-deb.yml` - CI 配置文件 - `./CI.md` - CI 情况说明 - `./debian/` - deb 包构建配置目录 - `./BUILD.md` - 如何手动构建 - `./Makefile` - deb 包构建 Makefile - `./deb-installation-test.sh` - deb 包 **已正确安装** 测试脚本
## 安装 如果你是普通用户,你应该从 [GitHub Releases](https://github.com/RubyMetric/chsrc/releases) 下载合适的 deb 包,然后运行以下命令安装: ```bash sudo dpkg -i chsrc_*.deb sudo apt-get install -f # Fix any dependency issues ``` 如果你是高级用户,你可以自己阅读本目录下的 [./BUILD.md](./BUILD.md) 来自己构建 deb 包并按照上述同样的方式安装。
## `debian/` 目录结构 ``` debian/ ├── changelog # 版本更新日志 ├── compat # debhelper 兼容性版本 ├── control # 包控制信息和依赖 ├── copyright # 版权信息 ├── postinst # 安装后脚本 ├── prerm # 卸载前脚本 └── rules # 构建规则 ``` 其中,最后三个是 `+x` 的可执行文件。
================================================ FILE: pkg/deb/deb-installation-test.sh ================================================ #!/bin/bash # -------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # -------------------------------------------------------------- # Test File : deb-installation-test.sh # File Authors : sanchuanhehe # Contributors : 曾奥然 # | # Created On : <2025-06-14> # Last Modified : <2025-06-16> # # Test script for deb package installation # -------------------------------------------------------------- set -e echo "Testing installation of deb package 'chsrc' ..." # Test 1: Check if chsrc binary exists and is executable if [ ! -f "/usr/bin/chsrc" ]; then echo "ERROR: /usr/bin/chsrc not found" exit 1 fi if [ ! -x "/usr/bin/chsrc" ]; then echo "ERROR: /usr/bin/chsrc is not executable" exit 1 fi echo "✓ /usr/bin/chsrc binary exists and is executable" # Test 2: Check if man page exists if [ ! -f "/usr/share/man/man1/chsrc.1" ]; then echo "WARNING: chsrc man page not found at /usr/share/man/man1/chsrc.1" else echo "✓ chsrc man page exists" fi # Test 3: Test basic functionality echo "Testing basic chsrc functionality..." if /usr/bin/chsrc help >/dev/null 2>&1; then echo "✓ command 'chsrc help' works" else echo "ERROR: command 'chsrc help' failed" exit 1 fi if /usr/bin/chsrc list >/dev/null 2>&1; then echo "✓ command 'chsrc list' works" else echo "ERROR: command 'chsrc list' failed" exit 1 fi echo "All installation tests of deb package 'chsrc' passed!" ================================================ FILE: pkg/deb/debian/changelog ================================================ chsrc (0.2.3-1) unstable; urgency=medium * v0.2.3 已发布 -- 曾奥然 Wed, 29 Oct 2025 11:29:05 +0800 chsrc (0.1.9-1) unstable; urgency=medium * v0.1.9 已发布 -- 曾奥然 Sun, 15 Jun 2025 13:32:50 +0800 chsrc (0.0.1-1) unstable; urgency=medium * 首个 deb 包已完成! -- sanchuanhehe Mon, 10 Jun 2025 00:00:00 +0000 ================================================ FILE: pkg/deb/debian/control ================================================ Source: chsrc Section: utils Priority: optional Maintainer: sanchuanhehe Build-Depends: debhelper-compat (= 13), build-essential, libc6-dev Standards-Version: 4.6.0 Homepage: https://chsrc.run/ Vcs-Git: https://github.com/RubyMetric/chsrc.git Vcs-Browser: https://github.com/RubyMetric/chsrc Package: chsrc Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends} Description: Change Source - A tool for changing software sources chsrc is a command-line tool for changing software sources (mirrors) for various package managers and programming language ecosystems. It supports automatic detection and switching of sources for better download speeds in different regions. ================================================ FILE: pkg/deb/debian/copyright ================================================ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Upstream-Name: chsrc Upstream-Contact: 曾奥然 Source: https://github.com/RubyMetric/chsrc Files: * Copyright: 2023-2026 曾奥然 2023-2026 郭恒 <2085471348@qq.com> License: GPL-3+ Comment: The authors' names are indicated in each source code file's header. Files: lib/xy.h Copyright: 2023-2026 曾奥然 2023-2026 郭恒 <2085471348@qq.com> License: MIT Files: pkg/deb/debian/* Copyright: 2025-2026 sanchuanhehe License: GPL-3+ License: GPL-3+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. . This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. . You should have received a copy of the GNU General Public License along with this program. If not, see . . On Debian systems, the complete text of the GNU General Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". Comment: On Debian systems, the full text of the GNU General Public License version 3 can be found in the file '/usr/share/common-licenses/GPL-3'. License: MIT MIT License Copyright (c) 2023-2026 曾奥然 (Aoran Zeng), 郭恒 (Heng Guo) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: pkg/deb/debian/postinst ================================================ #!/bin/sh # -------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # -------------------------------------------------------------- # deb File : postinst # File Authors : sanchuanhehe # Contributors : Nil Null # | # Created On : <2025-06-14> # Last Modified : <2025-06-16> # # postinst script for chsrc # -------------------------------------------------------------- set -e case "$1" in configure) # Update man database if command -v mandb >/dev/null 2>&1; then mandb -q /usr/share/man/man1/chsrc.1 2>/dev/null || true fi # Make sure chsrc is executable chmod +x /usr/bin/chsrc echo "chsrc has been successfully installed!" echo "Run 'chsrc help' to get started." ;; abort-upgrade|abort-remove|abort-deconfigure) ;; *) echo "postinst called with unknown argument \`$1'" >&2 exit 1 ;; esac #DEBHELPER# exit 0 ================================================ FILE: pkg/deb/debian/prerm ================================================ #!/bin/sh # -------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # -------------------------------------------------------------- # deb File : prerm # File Authors : sanchuanhehe # Contributors : Nil Null # | # Created On : <2025-06-14> # Last Modified : <2025-06-16> # # prerm script for chsrc # -------------------------------------------------------------- set -e case "$1" in remove|upgrade|deconfigure) # Nothing special to do during removal ;; failed-upgrade) ;; *) echo "prerm called with unknown argument \`$1'" >&2 exit 1 ;; esac #DEBHELPER# exit 0 ================================================ FILE: pkg/deb/debian/rules ================================================ #!/usr/bin/make -f # -------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # -------------------------------------------------------------- # Build File : rules # File Authors : sanchuanhehe # | 曾奥然 # Contributors : Nil Null # | # Created On : <2025-06-14> # Last Modified : <2025-06-20> # # 该文件是 Makefile 格式 # -------------------------------------------------------------- # # debuild 调用 dpkg-buildpackage # # dpkg-buildpackage 通过调用 fakeroot debian/rules 执行本文件 # # 由于下面的 %,所有任务都将被传递给 dh # # 比如: # # debian/rules # # 会转换为: # # dh # # 来执行 # # 简单理解,将顺序调用: # 1. dh clean # 2. dh build (debian/rules override_dh_auto_build) # 3. dh binary (debian/rules override_dh_auto_install) # -------------------------------------------------------------- # 由于 debuild 要寻找 debian/ 目录,因此当前工作目录一定在 pkg/deb 下 Chsrc-Root-Dir = $(CURDIR)/../../ %: dh $@ override_dh_auto_build: # 切换到 chsrc 根目录 @$(MAKE) -C $(Chsrc-Root-Dir) build-in-release-mode override_dh_auto_install: @$(MAKE) -C $(Chsrc-Root-Dir) install DESTDIR=$(CURDIR)/debian/chsrc ================================================ FILE: src/chsrc-main.c ================================================ /** ------------------------------------------------------------ * Copyright © 2023-2026 曾奥然, 郭恒 * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * * chsrc: Change Source * * 全平台通用换源工具与框架 * * Change Source everywhere for every software * * 项目创建者: 曾奥然, 郭恒 * ------------------------------------------------------------- * * 这是一个高度依靠贡献者的项目,我们直接把每一位贡献者写进了代码 * 里,这是比 Git commit message 更加稳定的方式。但我们不满足于仅 * 把大家写在注释里,因为C语言在编译时会无情地把这些信息抹去。 * * 所以,我们让每一位贡献者直接参与运行时! * * 想象一下,每一位贡献者都化作一串比特,穿梭于全国各地的桌面PC中, * 流淌在数据中心的机房,分身在手机、路由器、小型嵌入式设备中... * 每当 chsrc 执行一次 recipe 相关的操作,这些比特便会复苏,贡献者 * 的信息随之浮现。它提醒着使用者:在你与开源世界的每一次交互背后, * 都是许多人的协作,跨越省份与疆域,以代码相连。 * * 我们希望营造像 Richard Stallman 在创建 GNU 项目之前,在 MIT 时 * 那样的氛围 —— 写自己用得上的软件,与大家一起开发、维护,简单纯 * 粹,无关商业利益。就像小区、校园自发组建的足球篮球队,从一次偶 * 然的加入,逐渐成长为互相支持的伙伴。 * ------------------------------------------------------------*/ #define Chsrc_Maintain_URL "https://github.com/RubyMetric/chsrc" #define Chsrc_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc" #include "framework/version.h" #include "framework/core.c" #include "framework/chef.c" void chsrc_register_contributors () { /* 项目创建者 */ chef_register_contributor ("@ccmywish", "曾奥然", "ccmywish@qq.com", NULL); // 该 ID 为 Gitee ID chef_register_contributor ("@G_I_Y", "郭恒", "2085471348@qq.com", NULL); /* 所有贡献者 (按参与贡献时间排序) */ chef_register_contributor ("@Aaron-212", "Aaron Ruan", "aaron212cn@outlook.com", NULL); chef_register_contributor ("@chenrui333", "Rui Chen", "rui@chenrui.dev", NULL); chef_register_contributor ("@livelycode36", "Shengwei Chen", "414685209@qq.com", NULL); chef_register_contributor ("@Gn3po4g", "Peng Gao", "gn3po4g@outlook.com", NULL); chef_register_contributor ("@BlockLune", "BlockLune", "blocklune@gmail.com", NULL); chef_register_contributor ("@MrWillCom", "Mr. Will", "mr.will.com@outlook.com", NULL); chef_register_contributor ("@Jerry-Terrasse", "Terrasse", "terrasse@qq.com", NULL); chef_register_contributor ("@lontten", "Lontten", "lontten@163.com", NULL); chef_register_contributor ("@happy-game", "Happy Game", "happygame1024@gmail.com", NULL); chef_register_contributor ("@Word2VecT", "Word2VecT", "tangzinan@bupt.edu.cn", NULL); chef_register_contributor ("@wickdynex", "Xuan", "wick.dynex@qq.com", NULL); chef_register_contributor ("@Efterklang", "GnixAij", "gaojiaxing0220@gmail.com", NULL); chef_register_contributor ("@czyt", "czyt", "czyt.go@gmail.com", NULL); chef_register_contributor ("@XUANJI233", "XUANJI233", "xuanji233@outlook.com", NULL); chef_register_contributor ("@Yangmoooo", "Yangmoooo", "yangmoooo@outlook.com", NULL); chef_register_contributor ("@zouri", "Zouri", "guoshuaisun@outlook.com", NULL); chef_register_contributor ("@xyx1926885268", "Yongxiang", "1926885268@qq.com", NULL); chef_register_contributor ("@YU-7", "YU-7", "2747046473@qq.com", NULL); chef_register_contributor ("@juzeon", "juzeon", "skyjuzheng@gmail.com", NULL); chef_register_contributor ("@jialinlvcn", "Jialin Lyu", "jialinlvcn@aliyun.com", NULL); chef_register_contributor ("@Kattos", "ccy", "icuichengyi@gmail.com", NULL); chef_register_contributor ("@xrgzs", "MadDogOwner", "xiaoran@xrgzs.top", NULL); chef_register_contributor ("@sanchuanhehe", "sanchuanhehe", "wyihe5520@gmail.com", NULL); chef_register_contributor ("@Mikachu2333", "Mikachu2333", "mikachu.23333@zohomail.com", NULL); chef_register_contributor ("@techoc", "Rui Yang", "techoc@foxmail.com", NULL); chef_register_contributor ("@BingChunMoLi", "BingChunMoLi", "bingchunmoli@bingchunmoli.com", NULL); // 该 ID 为 Gitee ID chef_register_contributor ("@hezonglun", "HeZongLun", "hezonglun123456@outlook.com", NULL); chef_register_contributor ("@Young-Lord", "LY", "ly-niko@qq.com", NULL); chef_register_contributor ("@MingriLingran", "MingriLingran", "i@linran.moe", NULL); chef_register_contributor ("@usernameisnull", "MaBing", "cumt_ttr@163.com", NULL); /** * AI贡献者: * * ChatGPT、GitHub Copilot、DeepSeek * * 这几位是贡献者显式说明的,也许还有隐式使用的一些AI并没有被记录下来,可以添加在这里 */ } #include "recipe/menu.c" #include "rawstr4c.h" void cli_print_available_mirrors () { { char *msg = ENGLISH ? "To specify a source, use chsrc set " : "指定使用某源,请使用 chsrc set "; say (bdblue(xy_strcat (3, msg, "", " \n"))); } { char *msg = ENGLISH ? "Available Mirror Sites: \n" : "可用镜像站: \n"; say (bdgreen(msg)); } { char *msg1 = ENGLISH ? "Mirror abbr" : "镜像站简写"; char *msg2 = ENGLISH ? "Mirror URL" : "镜像站URL"; char *msg3 = ENGLISH ? "Mirror Name" : "镜像站"; char *format = ENGLISH ? " %-13s%-28s%-35s%s\n" : " %-13s%-33s%-42s%s\n"; printf (format, "code", msg1, msg2, msg3); say ("--------- -------------- ------------------------------------- ---------------------"); } for (int i = 0; i < xy_c_array_len (chsrc_available_mirrors); i++) { MirrorSite_t *mir = chsrc_available_mirrors[i]; printf ("%-14s%-18s%-41s ", mir->code, mir->abbr, mir->site); say (mir->name); } } /** * @brief 遍历以 / 为分隔符的别名字符串,对每个别名调用回调函数 * * @param aliases 空格分隔的 alias 字符串 * @param callback 对每个 alias 调用的回调函数 * @param user_data 传递给回调函数的用户数据 * * @return 如果回调函数返回true则停止遍历并返回true,否则返回false */ bool iterate_aliases (const char *aliases, bool (*callback)(const char *alias, void *user_data), void *user_data) { char *aliases_copy = xy_strdup (aliases); char *tok_start = aliases_copy; char *cursor; bool result = false; while (*tok_start != '\0') { cursor = tok_start; while (*cursor != '/' && *cursor != '\0') cursor++; // 结束当前token char space_or_eos = *cursor; *cursor = '\0'; // 调用回调函数 if (callback(tok_start, user_data)) { result = true; break; } *cursor = space_or_eos; if (space_or_eos == '\0') { break; } tok_start = cursor+1; } return result; } /** * 用于 cli_print_targets_for_menu 的回调函数,打印每个别名 */ bool callback_print_alias (const char *alias, void *user_data) { printf ("%s ", alias); return false; // 继续遍历,不停止 } void callback_print_targets (void *data, void *DUMMY) { Target_t *target = (Target_t *) data; // 使用通用的别名遍历函数打印所有别名 iterate_aliases (target->aliases, callback_print_alias, NULL); br(); // 每个target换行 } void cli_print_targets_for_menu (XySeq_t *menu) { xy_seq_each (menu, callback_print_targets, NULL); br(); // 最后额外换行 } void cli_print_supported_targets () { { char *msg = ENGLISH ? "Support following targets (same line indicates these targets are compatible)" : "支持对以下目标换源 (同一行表示这几个命令兼容)" ; say (bdblue(msg)); br(); } { char *msg = ENGLISH ? "Programming Languages" : "编程语言"; say (bdgreen(msg)); say ("-------------------------"); cli_print_targets_for_menu (ProgStore.pl); } { char *msg = ENGLISH ? "Operating Systems" : "操作系统"; say (bdgreen(msg)); say ("-------------------------"); cli_print_targets_for_menu (ProgStore.os); } { char *msg = ENGLISH ? "Softwares" : "软件"; say (bdgreen(msg)); say ("-------------------------"); cli_print_targets_for_menu (ProgStore.wr); } } void cli_print_menu (char *menu) { if (xy_streql (menu, "pl")) { char *msg = ENGLISH ? "Support following Programming Languages (same line indicates these targets are compatible)\n" : "支持对以下编程语言生态换源 (同一行表示这几个目标兼容)\n"; say (bdgreen(msg)); cli_print_targets_for_menu (ProgStore.pl); } else if (xy_streql (menu, "os")) { char *msg = ENGLISH ? "Support following Operating Systems (same line indicates these targets are compatible)\n" : "支持对以下操作系统换源 (同一行表示这几个目标兼容)\n"; say (bdgreen(msg)); cli_print_targets_for_menu (ProgStore.os); } else if (xy_streql (menu, "wr")) { char *msg = ENGLISH ? "Support following Softwares (same line indicates these targets are compatible)\n" : "支持对以下软件换源 (同一行表示这几个目标兼容)\n"; say (bdgreen(msg)); cli_print_targets_for_menu (ProgStore.wr); } } /** * 用于 chsrc list */ void cli_print_target_available_sources (Source_t sources[], size_t size) { for (int i=0;icode, mir->abbr, src.url); say (mir->name); } } void cli_print_target_features (Target_t *target, const char *input_target_name) { { char *msg = ENGLISH ? "\nAvailable Features:\n" : "\n可用功能:\n"; say (bdgreen(msg)); } { char *msg = ENGLISH ? " Get: View the current source state " : " Get: 查看当前源状态 "; char *get_msg = xy_strcat (3, msg, "| chsrc get ", input_target_name); if (target->getfn != NULL) printf (" %s%s\n", bdgreen(YesMark), purple(get_msg)); else printf (" %s%s\n", bdred(NoMark), get_msg);br(); } { char *msg = ENGLISH ? " Reset: Reset to the default source " : " Reset: 重置回默认源 "; char *reset_msg = xy_strcat (3, msg, "| chsrc reset ", input_target_name); if (target->resetfn != NULL) printf (" %s%s\n", bdgreen(YesMark), purple(reset_msg)); else printf (" %s%s\n", bdred(NoMark), reset_msg);br(); } { char *msg = ENGLISH ? " UserDefine: using user-defined source link " : " UserDefine: 用户自定义换源链接 "; char *user_define_msg = xy_strcat (5, msg, "| chsrc set ", input_target_name, " https://user-define-url.org/", input_target_name); if (target->can_user_define) printf (" %s%s\n", bdgreen(YesMark), purple(user_define_msg)); else printf (" %s%s\n", bdred(NoMark), user_define_msg);br(); } { char *scope_msg = NULL; for (int i=0; iscope_caps[i]; char *scope_name; if (i == 0) { scope_name = CHINESE ? " 项目级换源" : " project scope"; scope_msg = xy_strcat (3, scope_name, " | chsrc set -scope=project ", input_target_name); } else if (i == 1) { scope_name = CHINESE ? " 用户级换源" : " user scope"; scope_msg = xy_strcat (3, scope_name, " | chsrc set -scope=user ", input_target_name); } else if (i == 2) { scope_name = CHINESE ? " 系统级换源" : " system scope"; scope_msg = xy_strcat (3, scope_name, " | chsrc set -scope=system ", input_target_name); } else { xy_unreached(); } char *msg = NULL; switch (cap) { case ScopeCap_Unknown: msg = xy_strcat (6, " ", bdred(NoMark), scope_msg, " (", "是否支持该作用域尚不了解,欢迎贡献", ")"); puts (msg); break; case ScopeCap_Unable: msg = xy_strcat (6, " ", bdred(NoMark), scope_msg, " (", "不支持", ")"); puts (msg); break; case ScopeCap_Able_But_Not_Implemented: msg = xy_strcat (6, " ", bdyellow(HalfYesMark), scope_msg, " (", "支持但未实现", ")"); puts (msg); break; case ScopeCap_Able_And_Implemented: msg = xy_strcat (3, " ", bdgreen(YesMark), purple (xy_strcat (4, scope_msg, " (", "支持且已实现", ")"))); puts (msg); break; default: xy_unreached(); } } Scope_t default_scope = target->default_scope; char *default_scope_name = NULL; switch (default_scope) { case ProjectScope: default_scope_name = CHINESE ? "项目级" : "Project Scope"; break; case UserScope: default_scope_name = CHINESE ? "用户级" : "User Scope"; break; case SystemScope: default_scope_name = CHINESE ? "系统级" : "System Scope"; break; case ImplementationDefinedScope: default_scope_name = CHINESE ? "由实现定义" : "Implementation Defined Scope"; break; default: xy_unreached(); } char *msg = xy_strcat (3, bdblue (" = "), purple (xy_strcat (5, "默认作用域 | chsrc set -scope=default ", input_target_name, " (= ", default_scope_name ,")"))); puts (msg); br(); } { char *msg = ENGLISH ? " English: Output in English " : " English: 英文输出 "; char *english_msg = xy_strcat (3, msg, "| chsrc set -en ", input_target_name); if (target->can_english) printf (" %s%s\n", bdgreen(YesMark), purple(english_msg)); else printf (" %s%s\n", bdred(NoMark), english_msg);br(); } if (target->note) { char *msg = ENGLISH ? "NOTE: " : "备注: "; printf ("%s%s\n\n", bdyellow (msg), bdyellow (target->note)); } } /** * @brief 简略打印维护信息 * * 用于 chsrc get/set/reset */ void cli_print_target_maintain_info_briefly (Target_t *target, const char *input_target_name) { if (target->sources_last_updated) { char *msg = ENGLISH ? "Ingredient(Sources) Last Updated: " : "食源检查: "; printf ("%s%s ", msg, purple(target->sources_last_updated)); } if (target->last_updated) { char *msg = ENGLISH ? "Recipe Last Updated: " : "食谱更新: "; printf ("%s%s ", msg, purple(target->last_updated)); } char num[32]; sprintf(num, "%d", target->cooks_n + target->sauciers_n); char *msg = ENGLISH ? "Contributors: " : "后厨人数: "; printf ("%s%s ", msg, purple(num)); msg = ENGLISH ? xy_strcat (3, "(See chsrc ls ", input_target_name, ")") : xy_strcat (3, "(详查 chsrc ls ", input_target_name, ")"); printf ("%s\n", msg); } /** * @brief 详细打印维护信息 * * 用于 chsrc ls */ void cli_print_target_maintain_info (Target_t *target, const char *input_target_name) { if (target->created_on) { char *msg = ENGLISH ? "Recipe Created On: " : "食谱创建: "; printf ("%s%s ", bdblue(msg), target->created_on); } if (target->last_updated) { char *msg = ENGLISH ? "Recipe Last Updated: " : "食谱更新: "; printf ("%s%s ", bdblue(msg), target->last_updated); } if (target->sources_last_updated) { char *msg = ENGLISH ? "Ingredient(Sources) Last Updated: " : "食源检查: "; printf ("%s%s\n", bdblue(msg), target->sources_last_updated); } { char *msg = ENGLISH ? "Current Chef: " : "品控: "; if (target->chef) { printf ("%s%s <%s>\n", bdblue(msg), target->chef->name ? target->chef->name : "Unknown", target->chef->email ? target->chef->email : "unknown@example.com"); } else { char *msg1 = CHINESE ? "该 recipe 的负责人暂空缺, 欢迎担任" : "Vacant, Welcome to hold the position"; printf ("%s%s\n", bdblue(msg), bdgreen(msg1)); } } { char *msg = ENGLISH ? "Cooks: " : "掌勺: "; if (target->cooks && target->cooks_n > 0) { printf ("%s", bdblue(msg)); for (size_t i = 0; i < target->cooks_n; i++) { if (i > 0) printf (", "); printf ("%s <%s>", target->cooks[i]->name ? target->cooks[i]->name : "Unknown", target->cooks[i]->email ? target->cooks[i]->email : "unknown@example.com"); } printf ("\n"); } else { char *msg1 = CHINESE ? "暂空缺, 欢迎担任" : "Vacant, Welcome to hold the position"; printf ("%s%s\n", bdblue(msg), bdgreen(msg1)); } } { char *msg = ENGLISH ? "Sauciers: " : "调味: "; if (target->sauciers && target->sauciers_n > 0) { printf ("%s", bdblue(msg)); for (size_t i = 0; i < target->sauciers_n; i++) { if (i > 0) printf (", "); printf ("%s <%s>", target->sauciers[i]->name, target->sauciers[i]->email ); } br(); } else { char *msg1 = CHINESE ? "暂空缺, 欢迎参与贡献" : "Vacant, Welcome to contribute!"; printf ("%s%s\n", bdblue(msg), bdgreen(msg1)); } } } void cli_print_version () { char *version_string = "v" Chsrc_Version " (" Chsrc_Release_Date ")"; char *str = xy_str_gsub (CHINESE ? RAWSTR_chsrc_for_v_CHINESE : RAWSTR_chsrc_for_v_ENGLISH, "@ver@", version_string); println (str); } void cli_print_help () { char *version_string = "v" Chsrc_Version " (" Chsrc_Release_Date ")"; const char *raw = CHINESE ? RAWSTR_chsrc_USAGE_CHINESE : RAWSTR_chsrc_USAGE_ENGLISH; char *str = xy_str_gsub (raw, "@ver@", version_string); println (str); } void cli_print_issues () { println (RAWSTR_chsrc_for_issue); /* if (chsrc_check_program ("gh")) { char *cmd = xy_quiet_cmd ("gh browse --repo RubyMetric/chsrc"); system (cmd); } */ } /** * @brief 用于 callback_find_target() 的回调函数,检查别名是否匹配用户输入 */ bool callback_match_alias (const char *alias, void *user_data) { const char *input = (const char *)user_data; return xy_streql_ic (input, alias); } /** * @brief 用于 iterate_menu() 的回调函数 */ bool callback_is_one_of_target_aliases (void *data, void *input) { Target_t *target = (Target_t *) data; if (iterate_aliases (target->aliases, callback_match_alias, input)) { target->preludefn(); return true; } else return false; } /** * 查询用户输入 `input` 是否与该 `menu` 中的某个 target 匹配 * 若匹配将直接调用 prelude * * @param[in] menu menu * @param[in] input 用户输入的目标名 * @param[out] target 返回匹配到的 Target_t 指针 * * @return 匹配到则返回true,未匹配到则返回false */ bool iterate_menu (XySeq_t *menu, const char *input, Target_t **target) { Target_t *t = xy_seq_find (menu, callback_is_one_of_target_aliases, (void *) input); if (t) { *target = t; t->preludefn(); return true; } else { *target = NULL; return false; } } void callback_perform_all_prelude_for_menu (void *data, void *DUMMY) { Target_t *target = (Target_t *) data; if (!target->preludefn) { chef_debug_target (target); chsrc_panic ("未定义 _prelude() !"); } target->preludefn(); } /** * @brief 用于检查所有 _prelude() 是否能正常工作 * * 为了防止 DEBUG 模式下运行流程和普通模式下运行流程不一样,我们只在 Get, Set, Reset * 之后才运行该函数 */ void chsrc_perform_all_prelude () { chsrc_debug ("prelude", "DEBUG模式下, 额外检查所有 _prelude() 是否能正常工作"); xy_seq_each (ProgStore.pl, callback_perform_all_prelude_for_menu, NULL); xy_seq_each (ProgStore.os, callback_perform_all_prelude_for_menu, NULL); xy_seq_each (ProgStore.wr, callback_perform_all_prelude_for_menu, NULL); } /** * @brief 在必要的时期,最后告诉用户一些信息 */ void chsrc_op_epilogue () { br(); chsrc_note2 (RAWSTR_chsrc_op_epilogue); } typedef enum { TargetOp_Get_Source = 1, TargetOp_Set_Source, TargetOp_Reset_Source, TargetOp_Measure_Source, TargetOp_List_Config } TargetOp; /** * 寻找target,并根据 `code` 执行相应的操作 * * @param input 用户输入的目标 * @param code 对target要执行的操作 * @param option 额外的指示,可为NULL * * @return 找到目标返回true,未找到返回false */ bool get_target (const char *input, TargetOp code, char *option) { chsrc_register_contributors (); Target_t *target = NULL; bool matched = iterate_menu (ProgStore.pl, input, &target); if (!matched) matched = iterate_menu (ProgStore.os, input, &target); if (!matched) matched = iterate_menu (ProgStore.wr, input, &target); if (!matched) return false; if (TargetOp_Set_Source==code) { if (target->setfn) { /* Hook时机: 开始运行前可以在这里进行一些拦截操作 */ chsrc_check_scope_capability (target); /* 用户要求设置的作用域,真的能够执行吗? */ target->setfn(option); } else chsrc_error (xy_strcat (3, "暂未对 ", input, " 实现 set 功能,邀您帮助: chsrc issue")); } else if (TargetOp_Reset_Source==code) { if (target->resetfn) { target->resetfn(option); } else chsrc_error (xy_strcat (3, "暂未对 ", input, " 实现 reset 功能,邀您帮助: chsrc issue")); } else if (TargetOp_Get_Source==code) { if (target->getfn) { target->getfn(""); } else chsrc_error (xy_strcat (3, "暂未对 ", input, " 实现 get 功能,邀您帮助: chsrc issue")); } else if (TargetOp_List_Config==code) { { char *msg = ENGLISH ? "To specify a source, use chsrc set " : "指定使用某源,请使用 chsrc set "; say (bdblue(xy_strcat (3, msg, input, " \n"))); } { char *msg = ENGLISH ? "Available Sources: \n" : "可用源: \n"; say (bdgreen(msg)); } { char *msg1 = ENGLISH ? "Mirror abbr" : "镜像站简写"; char *msg2 = ENGLISH ? "Source URL" : "换源链接"; char *msg3 = ENGLISH ? "Mirror Name" : "镜像站"; char *format = ENGLISH ? " %-13s%-33s%-38s%s\n" : " %-13s%-36s%-46s%s\n"; printf (format, "code", msg1, msg2, msg3); say ("--------- -------------- ----------------------------------------------- ---------------------"); } cli_print_target_available_sources (target->sources, target->sources_n); cli_print_target_features (target, input); { char *msg = ENGLISH ? "Maintainer Information:\n" : "维护信息:\n"; say (bdgreen(msg)); cli_print_target_maintain_info (target, input); } } else if (TargetOp_Measure_Source==code) { auto_select_mirror (target->sources, target->sources_n, input); return true; } /* 简短显示维护信息 */ if (TargetOp_Get_Source==code || TargetOp_Set_Source==code || TargetOp_Reset_Source==code) { cli_print_target_maintain_info_briefly (target, input); } if (TargetOp_Set_Source==code || TargetOp_Measure_Source==code) { // 2025-09-19: 已告知该消息给用户一个多月,我们不再告知 // chsrc_op_epilogue (); } #ifdef XY_DEBUG chef_debug_target (target); chsrc_perform_all_prelude (); #endif return true; } int main (int argc, char const *argv[]) { chsrc_init_framework (); chsrc_init_menu (); argc -= 1; if (0==argc) { cli_print_help (); return Exit_OK; } const char *command = argv[1]; // chsrc set target mirror // 1 2 3 int cli_arg_Target_pos = 2; int cli_arg_Mirror_pos = cli_arg_Target_pos + 1; const char *target = NULL; /** * (1) * chsrc set -scope=project -en target mirror * 1 2 3 4 5 * argc = 5 * * (2) 考虑到这种情况,i必须还是从1开始 * chsrc -en -h * 1 2 * * argc = 2 */ /* 从第一个参数遍历到最后一个参数 */ for (int i=1; i<=argc; i++) { if (xy_str_start_with (argv[i], "-")) { if (xy_streql (argv[i], "-ipv6")) { ProgMode.Ipv6Mode = true; } else if (xy_streql (argv[i], "-local")) { ProgMode.Scope = ProjectScope; char *msg = CHINESE ? " -local 选项已弃用,请使用 -scope=project" : " -local is deprecated, please use -scope=project"; chsrc_warn (msg); } else if (xy_str_start_with (argv[i], "-scope")) { const char *scope = NULL; if (xy_streql (argv[i], "-scope")) { scope = argv[i+1]; cli_arg_Target_pos++; cli_arg_Mirror_pos++; } else if (xy_str_start_with (argv[i], "-scope=")) { scope = argv[i] + 7; } if (xy_streql_ic (scope, "project")) { ProgMode.Scope = ProjectScope; } else if (xy_streql_ic (scope, "user")) { ProgMode.Scope = UserScope; } else if (xy_streql_ic (scope, "system")) { ProgMode.Scope = SystemScope; } else if (xy_streql_ic (scope, "default")) { ProgMode.Scope = ImplementationDefinedScope; } else { if (ENGLISH) { char *msg = "Invalid scope: "; chsrc_error (xy_strcat (3, msg, scope, ". Valid scopes are: default, project, user, system")); } else { char *msg = "无效的换源作用域: "; chsrc_error (xy_strcat (4, msg, scope, "。", "有效的换源作用域为: default, project, user, system")); } return Exit_Unknown; } } else if (xy_streql (argv[i], "-en") || xy_streql (argv[i], "-english")) { ProgMode.EnglishMode = true; } else if (xy_streql (argv[i], "-dry")) { ProgMode.DryRunMode = true; } else if (xy_streql (argv[i], "-no-color") || xy_streql (argv[i], "-no-colour")) { ProgMode.NoColorMode = true; xy.enable_color = false; } else if ( xy_streql (argv[i], "-h") || xy_streql (argv[i], "-help") || xy_streql (argv[i], "--help")) { command = "help"; /* 交到下方处理 */ } else if ( xy_streql (argv[i], "-v") || xy_streql (command, "-version") || xy_streql (command, "--version")) { command = "version"; /* 交到下方处理 */ } else { char *msg = ENGLISH ? "Unknown option: " : "未识别的命令行选项 "; chsrc_error (xy_2strcat (msg, argv[i])); return Exit_Unknown; } cli_arg_Target_pos++; cli_arg_Mirror_pos++; } } bool matched = false; if (in_dry_run_mode()) { char *dry_msg = ENGLISH ? "Enable [Dry Run] mode. " "Simulate the source changing process (skipping speed measurement). " "Commands only print but don't run\n" : "开启Dry Run模式,模拟换源过程(跳过测速),命令仅打印并不运行\n"; chsrc_log (bdyellow(dry_msg)); } /* chsrc help */ if ( xy_streql (command, "h") || xy_streql (command, "-h") || xy_streql (command, "help") || xy_streql (command, "-help") || xy_streql (command, "--help")) { cli_print_help (); return Exit_OK; } /* chsrc -v */ else if ( xy_streql (command, "-v") || xy_streql (command, "-version") || xy_streql (command, "--version") || xy_streql (command, "ver") || xy_streql (command, "version")) { cli_print_version (); return Exit_OK; } /* chsrc list */ else if ( xy_streql (command, "list") || xy_streql (command, "l") || xy_streql (command, "ls")) { if (argc < cli_arg_Target_pos) { cli_print_available_mirrors (); br(); cli_print_supported_targets (); } else { target = argv[cli_arg_Target_pos]; if ( xy_streql (target, "mirrors") || xy_streql (target, "mirror")) { cli_print_available_mirrors (); return Exit_OK; } else if ( xy_streql (target, "targets") || xy_streql (target, "target")) { cli_print_supported_targets (); return Exit_OK; } else if (xy_streql (target, "os")) { cli_print_menu ("os"); return Exit_OK; } else if ( xy_streql (target, "lang") || xy_streql (target, "pl") || xy_streql (target, "language")) { cli_print_menu ("pl"); return Exit_OK; } else if ( xy_streql (target, "ware") || xy_streql (target, "software")) { cli_print_menu ("wr"); return Exit_OK; } matched = get_target (target, TargetOp_List_Config, NULL); if (!matched) goto not_matched; } return Exit_OK; } #define MSG_EN_USE_LIST_TARGETS "Use `chsrc list targets` to see all supported targets" #define MSG_CN_USE_LIST_TARGETS "使用 chsrc list targets 查看所有支持的目标" /* chsrc measure */ else if ( xy_streql (command, "measure") || xy_streql (command, "mea") || xy_streql (command, "m") || xy_streql (command, "cesu") || xy_streql (command, "ce") || xy_streql (command, "c")) { if (argc < cli_arg_Target_pos) { char *msg = ENGLISH ? "Please provide the target name you want to measure. " MSG_EN_USE_LIST_TARGETS : "请提供想要测速源的目标名。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); return Exit_Unknown; } ProgMode.MeasureMode = true; target = argv[cli_arg_Target_pos]; matched = get_target (target, TargetOp_Measure_Source, NULL); if (!matched) goto not_matched; return Exit_OK; } /* chsrc get */ else if ( xy_streql (command, "get") || xy_streql (command, "g")) { if (argc < cli_arg_Target_pos) { char *msg = ENGLISH ? "Please provide the target name you want to view the source. " MSG_EN_USE_LIST_TARGETS : "请提供想要查看源的目标名。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); return Exit_Unknown; } target = argv[cli_arg_Target_pos]; matched = get_target (target, TargetOp_Get_Source, NULL); if (!matched) goto not_matched; return Exit_OK; } /* chsrc set */ else if ( xy_streql (command, "set") || xy_streql (command, "s")) { if (argc < cli_arg_Target_pos) { char *msg = ENGLISH ? "Please provide the target name you want to set the source. " MSG_EN_USE_LIST_TARGETS : "请提供想要设置源的目标名。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); return Exit_Unknown; } target = argv[cli_arg_Target_pos]; char *mirrorCode_or_url = NULL; if (argc >= cli_arg_Mirror_pos) { mirrorCode_or_url = xy_strdup (argv[cli_arg_Mirror_pos]); } matched = get_target (target, TargetOp_Set_Source, mirrorCode_or_url); if (!matched) goto not_matched; return Exit_OK; } /* chsrc reset */ else if ( xy_streql (command, "reset") || xy_streql (command, "rest") || xy_streql (command, "r")) { if (argc < cli_arg_Target_pos) { char *msg = ENGLISH ? "Please provide the target name you want to reset the source. " MSG_EN_USE_LIST_TARGETS : "请提供想要重置源的目标名。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); return Exit_Unknown; } ProgMode.ResetMode = true; target = argv[cli_arg_Target_pos]; matched = get_target (target, TargetOp_Reset_Source, NULL); if (!matched) goto not_matched; return Exit_OK; } /* chsrc issue */ else if ( xy_streql (command, "issue") || xy_streql (command, "issues") || xy_streql (command, "isue") || xy_streql (command, "i")) { cli_print_issues (); return Exit_OK; } else { char *msg1 = ENGLISH ? "Unknown command `" : "不支持的命令 "; char *msg2 = ENGLISH ? "`. Use `chsrc help` to view usage" : ". 请使用 chsrc help 查看使用方式"; chsrc_error (xy_strcat (3, msg1, command, msg2)); return Exit_Unknown; } not_matched: if (!matched) { char *msg = ENGLISH ? "Unknown target. " MSG_EN_USE_LIST_TARGETS : "暂不支持的换源目标。" MSG_CN_USE_LIST_TARGETS; chsrc_error (msg); return Exit_Unknown; } } ================================================ FILE: src/framework/chef.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Name : chef.c * File Authors : @ccmywish * Contributors : @BingChunMoLi * Created On : <2025-08-09> * Last Modified : <2026-02-24> * * chef DSL: for chefs (recipe makers) to define a target * ------------------------------------------------------------*/ #pragma once void chef_debug_target (Target_t *target) { #ifdef XY_DEBUG if (!target) { chsrc_debug2 ("target", "Target is NULL"); return; } say ("DEBUG Target Information:"); printf (" Aliases: %s\n", target->aliases); printf (" Get Function: %p\n", target->getfn); printf (" Set Function: %p\n", target->setfn); printf (" Reset Function: %p\n", target->resetfn); printf (" Prelude Function: %p\n", target->preludefn); printf (" Inited?: %d\n", target->inited); printf (" Sources: %p\n", target->sources); printf (" Sources Count: %d\n", target->sources_n); printf (" Chef: %p\n", target->chef); printf (" Cooks: %p\n", target->cooks); printf (" Cooks Count: %d\n", target->cooks_n); printf (" Sauciers: %p\n", target->sauciers); printf (" Sauciers Count: %d\n", target->sauciers_n); #endif } void chef_debug_contributor (Contributor_t *contributor) { #ifdef XY_DEBUG if (!contributor) { chsrc_debug2 ("contrib", "Contributor is NULL"); return; } say ("DEBUG Contributor Information:"); printf (" ID: %s\n", contributor->id); printf (" Name: %s\n", contributor->name); printf (" Email: %s\n", contributor->email); printf (" DisplayName: %s\n", contributor->display_name); #endif } /** * @brief 登记所有贡献者 * * @param id 贡献者 ID,这个ID最好是GitHub用户名,但也可以不是,只需要在 chsrc 内部进行区分即可 * @param display_name 如果没有提供该参数,则使用 name */ void chef_register_contributor (char *id, char *name, char *email, char *display_name) { if (!id || !name || !email) xy_unreached(); Contributor_t *contributor = xy_malloc0 (sizeof (Contributor_t)); contributor->id = xy_strdup (id); contributor->name = xy_strdup (name); contributor->email = xy_strdup (email); if (!display_name) contributor->display_name = xy_strdup (name); else contributor->display_name = xy_strdup (display_name); xy_map_set (ProgStore.contributors, id, contributor); } /** * @brief 修改 Provider 的测速地址 * * @note 这个修改的是全局 Provider 里的信息。往往用来设置 UpstreamProvider */ void chef_set_provider_smURL (SourceProvider_t *provider, char *url) { provider->psmi.skip = NotSkip; provider->psmi.url = xy_strdup (url); chsrc_debug ("m", xy_strcat (4, "recipe 重新为 ", provider->code, "(镜像站信息本身) 设置测速链接: ", url)); } /** * @brief 修改 Provider 的测速精度 * * @note 这个修改的是全局 Provider 里的信息。往往用来设置 UpstreamProvider */ void chef_set_provider_sm_accuracy (SourceProvider_t *provider, bool accuracy) { provider->psmi.accurate = accuracy; chsrc_debug ("m", xy_strcat (4, "recipe 重新为 ", provider->code, "(镜像站信息本身) 设置测速精度: ", accuracy ? "精准" : "粗略")); } /** * @brief 修改 或 补充 某个镜像站的换源链接,即修改 Source_t.url * * @example 见 os_ubuntu_resetsrc() 中对非 x86_64 架构源地址的修改 */ void chef_set_repoURL (Target_t *target, SourceProvider_t *provider, char *url) { xy_cant_be_null (target); xy_cant_be_null (provider); xy_cant_be_null (url); for (int i=0; i < target->sources_n; i++) { Source_t *src = &target->sources[i]; SourceProvider_t *p = src->provider; if (p == provider) { src->url = xy_strdup (url); return; } } xy_unreached(); } /** * @brief 提供一个函数,这个函数基于 "换源链接" 和用户提供的数据来构造和填充精准测速链接 */ void chef_set_smURL_with_func ( Target_t *target, SourceProvider_t *provider, char *(*func)(const char *url, const char *user_data), char *user_data) { xy_cant_be_null (target); xy_cant_be_null (provider); xy_cant_be_null (func); for (int i=0; i < target->sources_n; i++) { Source_t *src = &target->sources[i]; SourceProvider_t *p = src->provider; if (p == provider) { if (src->url) { src->speed_measure_url = func (src->url, user_data); return; } else { chsrc_panic ("该函数基于已有的换源链接来生成测速链接,但该源的换源链接为空"); } } } xy_unreached(); } /** * @brief 给 "换源链接" 增加一个后缀来构造和填充专用测速链接 */ void chef_set_smURL_with_postfix (Target_t *target, SourceProvider_t *provider, char *postfix) { chef_set_smURL_with_func (target, provider, xy_2strcat, postfix); } /** * @internal 该函数仅用于实现 chef_set_smURL() */ char * _chef_strdup_2nd_argument (const char *DUMMY, const char *str) { return xy_strdup (str); } /** * @breif 设置 或 修改 某个镜像站的 *精准*测速链接,即修改 Source_t.speed_measure_url */ void chef_set_smURL (Target_t *target, SourceProvider_t *provider, char *url) { chef_set_smURL_with_func (target, provider, _chef_strdup_2nd_argument, url); } /** * @brief 针对每一个剩下的还未设置专用测速链接的源,对其 "换源链接" 使用函数 `func` 来生成专用测速链接 */ void chef_set_rest_smURL_with_func ( Target_t *target, char *(*func)(const char *url, const char *user_data), char *user_data) { xy_cant_be_null (target); Source_t *sources = target->sources; int n = target->sources_n; for (int i=0; iprovider->type; if (src->url) { /* 为空时才修改 或者里面是脏数据 */ if (NULL==src->speed_measure_url || !hp_is_url (src->speed_measure_url)) { src->speed_measure_url = func (src->url, user_data); } } } } /** * @brief 针对每一个剩下的还未设置专用测速链接的源,对其 "换源链接" 增加一个后缀来生成专用测速链接 */ void chef_set_rest_smURL_with_postfix (Target_t *target, char *postfix) { chef_set_rest_smURL_with_func (target, xy_2strcat, postfix); } /** * @note 用于: 组中的 item target 在 standalone 模式时正确填充源信息 */ void chef_use_other_target_sources (Target_t *this, Target_t *other) { if (!other->inited) { if (other->preludefn) other->preludefn(); else { chef_debug_target (other); chsrc_panic ("`other` 未定义 _prelude() !"); } } this->sources = other->sources; this->sources_n = other->sources_n; } void chef_allow_english (Target_t *target) { xy_cant_be_null (target); target->can_english = true; } void chef_deny_english (Target_t *target) { xy_cant_be_null (target); target->can_english = false; } /** * @brief 设置该 target 的作用域能力 */ void chef_set_scope_cap (Target_t *target, Scope_t scope, ScopeCapability_t cap) { xy_cant_be_null (target); /* 我们在这里固定好索引的位置,而不是直接用 enum 的值,防止以后顺序或者新增枚举值 */ if (scope == ProjectScope) { target->scope_caps[ScopeCap_Slot_Project] = cap; } else if (scope == UserScope) { target->scope_caps[ScopeCap_Slot_User] = cap; } else if (scope == SystemScope) { target->scope_caps[ScopeCap_Slot_System] = cap; } else { chsrc_panic ("无效的 scope 参数"); } } /** * @brief 设置该 target 的默认作用域 * * @note 该函数必须在 chef_set_scope_cap() 之后调用,以确保默认作用域的能力已经被明确了 */ void chef_set_default_scope (Target_t *target, Scope_t scope) { xy_cant_be_null (target); target->default_scope = scope; ScopeCapability_t cap = ScopeCap_Unknown; if (scope == ProjectScope) cap = target->scope_caps[ScopeCap_Slot_Project]; else if (scope == UserScope) cap = target->scope_caps[ScopeCap_Slot_User]; else if (scope == SystemScope) cap = target->scope_caps[ScopeCap_Slot_System]; else if (scope == ImplementationDefinedScope) { /* ImplementationDefinedScope 即由 chsrc 根据实际情况来决定,因此我们不对它检查 */ return; } else { chsrc_panic ("无效的 scope 参数"); } /* 防止 chef 们写错 */ if (cap != ScopeCap_Able_And_Implemented) { chsrc_panic ("该作用域未被明确支持,无法设置为默认作用域"); } } /** * @brief 由于操作系统相关的 target 换源都是系统级,所以 scope 都是固定的,我们提供此快捷函数来设置 */ void chef_set_os_scope (Target_t *target) { xy_cant_be_null (target); chef_set_scope_cap (target, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (target, UserScope, ScopeCap_Unable); chef_set_scope_cap (target, SystemScope, ScopeCap_Able_And_Implemented); chef_set_default_scope (target, SystemScope); } void chef_allow_user_define (Target_t *target) { xy_cant_be_null (target); target->can_user_define = true; target->can_user_define_explain = NULL; } void chef_deny_user_define (Target_t *target) { xy_cant_be_null (target); target->can_user_define = false; char *reason = CHINESE ? "URL将会根据内部实现重写,因此不能自定义" : "The URL will be rewritten based on internal implementation, so it cannot be customized"; target->can_user_define_explain = reason; } void chef_set_note (Target_t *target, const char *note_zh, const char *note_en) { xy_cant_be_null (target); const char *msg = CHINESE ? note_zh : note_en; if (msg) target->note = xy_strdup (msg); } /** * @brief 验证该 `id` 所指的贡献者确有其人 */ Contributor_t * chef_verify_contributor (const char *id) { xy_cant_be_null (id); Contributor_t *c = xy_map_get (ProgStore.contributors, id); if (!c) { char error[256]; snprintf (error, sizeof (error), "贡献者 %s 不存在, 是否写错?或请在 chsrc-main.c 中登记该贡献者", id); chsrc_panic (error); } return c; } /** * @brief 设置 Chef (recipe 负责人) */ void chef_set_chef (Target_t *target, const char *id) { xy_cant_be_null (target); /* Chef 可为空 */ if (!id) { target->chef = NULL; return; } Contributor_t *c = chef_verify_contributor (id); target->chef = c; } /** * @brief 设置 Cooks (recipe 核心作者) */ void chef_set_cooks (Target_t *target, size_t count, ...) { xy_cant_be_null (target); if (count == 0) { chsrc_panic ("recipe 一定至少有1位作者(cooks)"); return; } va_list args; va_start (args, count); target->cooks = xy_malloc0 (count * sizeof (Contributor_t*)); target->cooks_n = count; for (size_t i = 0; i < count; i++) { char *id = va_arg (args, char*); target->cooks[i] = chef_verify_contributor (id); } va_end (args); } void chef_set_sauciers (Target_t *target, uint32_t count, ...) { xy_cant_be_null (target); if (count == 0) { target->sauciers = NULL; target->sauciers_n = 0; return; } va_list args; va_start (args, count); target->sauciers = xy_malloc0 (count * sizeof (Contributor_t*)); target->sauciers_n = count; for (uint32_t i = 0; i < count; i++) { char *id = va_arg (args, char*); target->sauciers[i] = chef_verify_contributor (id); } } void chef_set_recipe_created_on (Target_t *target, char *date) { xy_cant_be_null (target); xy_cant_be_null (date); target->created_on = xy_strdup (date); } void chef_set_recipe_last_updated (Target_t *target, char *date) { xy_cant_be_null (target); xy_cant_be_null (date); target->last_updated = xy_strdup (date); } void chef_set_sources_last_updated (Target_t *target, char *date) { xy_cant_be_null (target); xy_cant_be_null (date); target->sources_last_updated = xy_strdup (date); } /** * @note 某些 target 需要修改 User-Agent * 由于单独测速 (chsrc measure) 的时候也需要进行此项修改, * 所以该函数不能仅仅放在 _setsrc() 里,而是应当放在 _prelude() 里 */ void chef_set_user_agent (char *user_agent) { ProgStatus.user_agent = user_agent; } ================================================ FILE: src/framework/core.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Name : core.c * File Authors : @ccmywish * | @G_I_Y * Contributors : @Gn3po4g * | @happy-game * | @Yangmoooo * | @BingChunMoLi * | @Mikachu2333 * | * Created On : <2023-08-29> * Last Modified : <2026-02-24> * * chsrc framework * ------------------------------------------------------------*/ #if defined(__STDC__) && __STDC_VERSION__ < 201112L # error "chsrc requires C11 or later, please use a new compiler which at least supports C11" #endif #if defined(__STDC__) && __STDC_VERSION__ < 201710L # warning "chsrc recommends a C17 or later compiler" #endif #include "xy.h" #include "struct.h" #include "mirror.c" #include "helper.c" #define App_Name "chsrc" static int chsrc_get_cpucore (); /* Global Program Mode */ struct { // 用户命令 bool MeasureMode; bool ResetMode; // 内部实现 bool TargetGroupMode; // 用户命令选项 bool Ipv6Mode; Scope_t Scope; bool EnglishMode; bool DryRunMode; bool NoColorMode; } ProgMode = { .MeasureMode = false, .ResetMode = false, .TargetGroupMode = false, .Ipv6Mode = false, .Scope = ImplementationDefinedScope, .EnglishMode = false, .DryRunMode = false, .NoColorMode = false }; /* recipe 相关 mode */ bool chsrc_in_target_group_mode() {return ProgMode.TargetGroupMode;} // 并非作为 follower target,而是自身作为一个独立的 target 执行 bool chsrc_in_standalone_mode() {return !ProgMode.TargetGroupMode;} void chsrc_set_target_group_mode(){ProgMode.TargetGroupMode = true;} bool chsrc_in_reset_mode(){return ProgMode.ResetMode;} /* 默认换源作用域就是 ImplementationDefinedScope */ bool chsrc_in_default_scope_mode(){return ProgMode.Scope == ImplementationDefinedScope;} bool chsrc_in_user_scope_mode(){return ProgMode.Scope == UserScope;} bool chsrc_in_project_scope_mode(){return ProgMode.Scope == ProjectScope;} bool chsrc_in_system_scope_mode(){return ProgMode.Scope == SystemScope;} bool chsrc_in_english_mode(){return ProgMode.EnglishMode;} bool chsrc_in_no_color_mode(){return ProgMode.NoColorMode;} /* Convenience */ #define ENGLISH chsrc_in_english_mode() #define CHINESE !chsrc_in_english_mode() /* 仅 framework 相关 mode */ static bool in_measure_mode(){return ProgMode.MeasureMode;} static bool in_ipv6_mode(){return ProgMode.Ipv6Mode;} static bool in_dry_run_mode(){return ProgMode.DryRunMode;} /** * Target Group mode (相反则称为 standalone mode) * * 1. 一个 target group 包含了多个 target,这些都被叫做 follower target * 2. 触发该运行模式的 target 被称为 leader target,其往往只是一个virtual target,类似 APT 中的 virtual package * * 目前使用该模式的有两个: Python 和 Node.js,因为二者的包管理器存在多个 */ typedef enum ChgType_t { ChgType_Auto, ChgType_Reset, ChgType_SemiAuto, ChgType_Manual, ChgType_Untested } ChgType_t; /* Global Program Status */ struct { int leader_selected_index; /* leader target 选中的索引 */ ChgType_t chgtype; /* 换源实现的类型 */ /* 此时 chsrc_run() 不再是recipe中指定要运行的一个外部命令,而是作为一个功能实现的支撑 */ bool chsrc_run_faas; char *user_agent; } ProgStatus = { .leader_selected_index = -1, .chgtype = ChgType_Auto, .chsrc_run_faas = false, .user_agent = "chsrc/" Chsrc_Version, }; /* Global Program Store */ struct { XySeq_t *pl; XySeq_t *os; XySeq_t *wr; XyMap_t *contributors; /* 所有贡献者 */ } ProgStore = { .pl = NULL, .os = NULL, .wr = NULL, .contributors = NULL, }; #define Exit_OK 0 #define Exit_Fatal 1 #define Exit_Unknown 2 #define Exit_Unsupported 3 #define Exit_UserCause 4 #define Exit_MaintainerCause 5 #define Exit_ExternalError 6 #define chsrc_log(str) xy_log(App_Name,str) #define chsrc_succ(str) xy_succ(App_Name,str) #define chsrc_info(str) xy_info(App_Name,str) #define chsrc_warn(str) xy_warn(App_Name,str) #define chsrc_error(str) xy_error(App_Name,str) #ifdef XY_DEBUG #define chsrc_debug(dom,str) xy_warn(App_Name "(DEBUG " dom ")",str) #else #define chsrc_debug(dom,str) #endif #define chsrc_verbose(str) xy_info(App_Name "(VERBOSE)",str) #define chsrc_panic(reason) xy_error(App_Name "(PANIC)",reason); exit(Exit_MaintainerCause) #define faint(str) xy_str_to_faint(str) #define red(str) xy_str_to_red(str) #define blue(str) xy_str_to_blue(str) #define green(str) xy_str_to_green(str) #define yellow(str) xy_str_to_yellow(str) #define purple(str) xy_str_to_purple(str) #define bold(str) xy_str_to_bold(str) #define bdred(str) xy_str_to_bold(xy_str_to_red(str)) #define bdblue(str) xy_str_to_bold(xy_str_to_blue(str)) #define bdgreen(str) xy_str_to_bold(xy_str_to_green(str)) #define bdyellow(str) xy_str_to_bold(xy_str_to_yellow(str)) #define bdpurple(str) xy_str_to_bold(xy_str_to_purple(str)) /* 2系列都是带有括号的 */ #define chsrc_succ2(str) xy_succ_brkt(App_Name,ENGLISH?"SUCCEED":"成功",str) #define chsrc_log2(str) xy_info_brkt(App_Name,"LOG",str) #define chsrc_warn2(str) xy_warn_brkt(App_Name,ENGLISH?"WARN":"警告",str) #define chsrc_error2(str) xy_error_brkt(App_Name,ENGLISH?"ERROR":"错误",str) #ifdef XY_DEBUG #define chsrc_debug2(dom,str) xy_warn_brkt(App_Name,"DEBUG " dom,str) #else #define chsrc_debug2(dom,str) #endif #define chsrc_verbose2(str) xy_info_brkt(App_Name,"VERBOSE",str) /** * @note 输出在 stdout 中 */ void chsrc_note2 (const char *str) { char *msg = ENGLISH ? "NOTE" : "提示"; xy_log_brkt (blue(App_Name), bdblue(msg), blue(str)); } /** * @note 输出在 stdout 中 */ void chsrc_alert2 (const char *str) { char *msg = ENGLISH ? "ALERT" : "提醒"; xy_log_brkt (yellow(App_Name), bdyellow(msg), yellow(str)); } void chsrc_init_framework () { xy_init (); ProgStore.contributors = xy_map_new (); ProgStore.pl = xy_seq_new (); ProgStore.os = xy_seq_new (); ProgStore.wr = xy_seq_new (); } void chsrc_log_write (const char *filename, bool is_overwrite) { char *msg = is_overwrite ? (ENGLISH ? "OVERWRITE" : "覆写") : (ENGLISH ? "WRITE" : "写入"); xy_log_brkt (blue(App_Name), bdblue(msg), blue(filename)); } void chsrc_log_backup (const char *filename) { char *msg = ENGLISH ? "BACKUP" : "备份"; char *bak = xy_2strcat (filename, ".bak"); xy_log_brkt (blue(App_Name), bdblue(msg), xy_strcat (3, bdyellow(filename), " -> ", bdgreen(bak))); } #define YesMark "✓" #define NoMark "x" #define HalfYesMark "⍻" static void log_check_result (const char *check_what, const char *check_type, bool exist) { char *chk_msg = NULL; char *not_exist_msg = NULL; char *exist_msg = NULL; if (ENGLISH) { chk_msg = "CHECK"; not_exist_msg = " doesn't exist"; exist_msg = " exists"; } else { chk_msg = "检查"; not_exist_msg = " 不存在"; exist_msg = " 存在"; } if (!exist) { xy_log_brkt (App_Name, bdred (chk_msg), xy_strcat (5, red (NoMark " "), check_type, " ", red (check_what), not_exist_msg)); } else { xy_log_brkt (App_Name, bdgreen (chk_msg), xy_strcat (5, green (YesMark " "), check_type, " ", green (check_what), exist_msg)); } } static void log_cmd_result (bool result, int exit_status, bool use_yellow_for_error) { char *run_msg = NULL; char *succ_msg = NULL; char *fail_msg = NULL; if (ENGLISH) { run_msg = "RUN"; succ_msg = YesMark " executed successfully"; fail_msg = NoMark " executed unsuccessfully, exit status: "; } else { run_msg = "运行"; succ_msg = YesMark " 命令执行成功"; fail_msg = NoMark " 命令执行失败,退出状态: "; } if (result) xy_log_brkt (green (App_Name), bdgreen (run_msg), green (succ_msg)); else { char buf[8] = {0}; sprintf (buf, "%d", exit_status); if (use_yellow_for_error) { char *log = xy_2strcat (yellow (fail_msg), bdyellow (buf)); xy_log_brkt (yellow (App_Name), bdyellow (run_msg), log); } else { char *log = xy_2strcat (red (fail_msg), bdred (buf)); xy_log_brkt (red (App_Name), bdred (run_msg), log); } } } #define Quiet_When_Exist 0x00 #define Noisy_When_Exist 0x01 #define Quiet_When_NonExist 0x00 #define Noisy_When_NonExist 0x10 /** * 检测二进制程序是否存在 * * @param check_cmd 检测 `prog_name` 是否存在的一段命令,一般来说,填 `prog_name` 本身即可, * 但是某些情况下,需要使用其他命令绕过一些特殊情况,比如 python 这个命令在Windows上 * 会自动打开 Microsoft Store,需避免 * * @param prog_name 要检测的二进制程序名 * */ bool query_program_exist (char *check_cmd, char *prog_name, int mode) { char *which = check_cmd; int status = system (which); // char buf[32] = {0}; sprintf(buf, "错误码: %d", status); char *msg = ENGLISH ? "command" : "命令"; if (0 != status) { if (mode & Noisy_When_NonExist) { // xy_warn (xy_strcat(4, "× 命令 ", progname, " 不存在,", buf)); log_check_result (prog_name, msg, false); } return false; } else { if (mode & Noisy_When_Exist) log_check_result (prog_name, msg, true); return true; } } /** * @brief 生成用于 “检测一个程序是否存在” 的命令,该内部函数由 chsrc_check_program() 家族调用 * * @note * 1. Unix 中,'where' 命令仅在 Zsh 中可以使用,sh 和 Bash 中均无法使用,因为其并非二进制程序 * 2. 因部分 Linux 发行版中没有 'which' 和 'whereis' 命令,使用 'command -v' 代替 */ static char * cmd_to_check_program (char *prog_name) { char *check_tool = xy.on_windows ? "where " : "command -v "; char *quiet_cmd = xy_quiet_cmd (xy_2strcat (check_tool, prog_name)); return quiet_cmd; } /** * @brief 通过 `调用程序名 --version` 的方式检测程序是否存在 * * @deprecated 因存在以下三个问题弃用: * * 1. 该程序得到直接执行,可能不太安全 (虽然基本不可能) * 2. 有一些程序启动速度太慢,即使只调用 --version,也依旧会花费许多时间,比如 mvn * 3. 有些程序并不支持 --version 选项 (虽然基本不可能) */ XY_Deprecate_This("Use cmd_to_check_program() instead") static char * cmd_to_check_program2 (char *prog_name) { char *quiet_cmd = xy_quiet_cmd (xy_2strcat (prog_name, " --version")); return quiet_cmd; } /** * @brief 检测程序是否存在 * * @note * 1. 一般只在 recipe 中使用,显式检测每一个需要用到的 program * 2. 无论存在与否,*均输出检测信息* * */ bool chsrc_check_program (char *prog_name) { return query_program_exist (cmd_to_check_program(prog_name), prog_name, Noisy_When_Exist|Noisy_When_NonExist); } /** * @brief 检测程序是否存在 * * @note * 1. 此函数没有强制性,只返回检查结果 * 2. 无论存在与否,*均不输出检测信息* */ bool chsrc_check_program_quietly (char *prog_name) { return query_program_exist (cmd_to_check_program(prog_name), prog_name, Quiet_When_Exist|Quiet_When_NonExist); } /** * @brief 检测程序是否存在 * * @note 存在时不输出检测信息,不存在时才输出检测信息 * */ bool chsrc_check_program_quietly_when_exist (char *prog_name) { return query_program_exist (cmd_to_check_program(prog_name), prog_name, Quiet_When_Exist|Noisy_When_NonExist); } /** * @brief 确保程序一定存在 * * @note * 1. 此函数具有强制性,检测不到就直接退出 * 2. 存在时不输出检测信息,不存在时才输出检测信息 * */ void chsrc_ensure_program (char *prog_name) { bool exist = query_program_exist (cmd_to_check_program(prog_name), prog_name, Quiet_When_Exist|Noisy_When_NonExist); if (exist) { // OK, nothing should be done } else { char *msg1 = ENGLISH ? "not found " : "未找到 "; char *msg2 = ENGLISH ? " command, please check for existence" : " 命令,请检查是否存在"; chsrc_error (xy_strcat (3, msg1, prog_name, msg2)); exit (Exit_UserCause); } } bool chsrc_check_file (char *path) { char *msg = ENGLISH ? "file" : "文件"; if (xy_file_exist (path)) { log_check_result (path, msg, true); return true; } else { log_check_result (path, msg, false); return false; } } /** * 用于 _setsrc 函数,检测用户输入的镜像站code,是否存在于该target可用源中 * * @note 一个源Source必定来自于一个Provider,所以该函数名叫 query_mirror_exist * * @param target_name 目标名 * @param input 如果用户输入 default 或者 def,则选择第一个源 */ int query_mirror_exist (Source_t *sources, size_t size, char *target_name, char *input) { if (hp_is_url (input)) { char *msg = ENGLISH ? "Using user-defined sources for this software is not supported at this time, please contact the developers to ask why or request support" : "暂不支持对该软件使用用户自定义源,请联系开发者询问原因或请求支持"; chsrc_error (msg); exit (Exit_Unsupported); } if (0==size) { char *msg1 = ENGLISH ? "Currently " : "当前 "; char *msg2 = ENGLISH ? " doesn't have any source available. Please contact the maintainers" : " 无任何可用源,请联系维护者"; chsrc_error (xy_strcat (3, msg1, target_name, msg2)); exit (Exit_MaintainerCause); } if (1==size) { char *msg1 = ENGLISH ? "Currently " : "当前 "; char *msg2 = ENGLISH ? " only the upstream source exists. Please contact the maintainers" : " 仅存在上游默认源,请联系维护者"; chsrc_error (xy_strcat (3, msg1, target_name, msg2)); exit (Exit_MaintainerCause); } /* if (xy_streql ("reset", input)) 不再使用这种方式 */ if (chsrc_in_reset_mode()) { char *msg = ENGLISH ? "Will reset to the upstream's default source" : "将重置为上游默认源"; say (msg); return 0; /* 返回第1个,因为第1个是上游默认源 */ } if (2==size) { char *msg1 = ENGLISH ? " is " : " 是 "; char *msg2 = ENGLISH ? "'s ONLY mirror available currently, thanks for their generous support" : " 目前唯一可用镜像站,感谢他们的慷慨支持"; const char *name = ENGLISH ? sources[1].mirror->abbr : sources[1].mirror->name; chsrc_succ (xy_strcat (4, name, msg1, target_name, msg2)); } if (xy_streql ("first", input)) { char *msg = ENGLISH ? "Will use the first speedy source measured by maintainers" : "将使用维护团队测速第一的源"; say (msg); return 1; /* 返回第2个,因为第1个是上游默认源 */ } int idx = 0; Source_t src = sources[0]; bool exist = false; for (int i=0; icode, input)) { idx = i; exist = true; break; } } if (!exist) { { char *msg1 = ENGLISH ? "Mirror site " : "镜像站 "; char *msg2 = ENGLISH ? " doesn't exist" : " 不存在"; chsrc_error (xy_strcat (3, msg1, input, msg2)); } char *msg = ENGLISH ? "To see available sources, use chsrc list " : "查看可使用源,请使用 chsrc list "; chsrc_error (xy_2strcat (msg, target_name)); exit (Exit_UserCause); } return idx; } /** * 该函数来自 oh-my-mirrorz.py,由 @ccmywish 翻译为C语言,但功劳和版权属于原作者 * * @param speed 单位为Byte/s */ char * to_human_readable_speed (double speed) { char *scale[] = {"Byte/s", "KByte/s", "MByte/s", "GByte/s", "TByte/s"}; int i = 0; while (speed > 1024.0) { i += 1; speed /= 1024.0; } char *buf = xy_malloc0 (64); sprintf (buf, "%.2f %s", speed, scale[i]); char *new = NULL; if (i <= 1 ) new = red (buf); else { if (i == 2 && speed < 2.00) new = yellow (buf); else new = green (buf); } return new; } /** * 测速代码参考自 https://github.com/mirrorz-org/oh-my-mirrorz/blob/master/oh-my-mirrorz.py * 功劳和版权属于原作者,由 @ccmywish 修改为C语言,并做了额外调整 * * @return 返回测得的速度,若出错,返回-1 * * 该函数实际原型为 char * (*)(const char*) */ void * measure_speed_for_url (void *url) { char *time_sec = NULL; time_sec = "8"; /** * 现在我们切换至跳转后的链接来测速,不再使用下述判断 * * if (xy_str_start_with(url, "https://registry.npmmirror")) * { * // 这里 npmmirror 跳转非常慢,需要1~3秒,所以我们给它留够至少8秒测速时间,否则非常不准 * time_sec = "10"; * } */ char *ipv6 = ""; // 默认不启用 if (in_ipv6_mode()) { ipv6 = "--ipv6"; } char *os_devnull = xy.os_devnull; /** * @note 我们用 —L,因为部分链接会跳转到其他地方,比如: RubyChina, npmmirror */ char *curl_cmd = xy_strcat (10, "curl -qsL ", ipv6, " -o ", os_devnull, " -w \"%{http_code} %{speed_download}\" -m", time_sec, " -A ", ProgStatus.user_agent, " ", url); // chsrc_note2 (xy_2strcat ("测速命令 ", curl_cmd)); char *curl_buf = xy_run (curl_cmd, 0); return curl_buf; } /** * @return 返回速度speed,单位为 Byte/s */ double parse_and_say_curl_result (char *curl_buf) { // 分隔两部分数据 char *split = strchr (curl_buf, ' '); if (split) *split = '\0'; // say(curl_buf); say(split+1); int http_code = atoi (curl_buf); double speed = atof (split+1); char *speedstr = to_human_readable_speed (speed); if (200!=http_code) { char *http_code_str = yellow (xy_2strcat ("HTTP码 ", curl_buf)); say (xy_strcat (3, speedstr, " | ", http_code_str)); } else { say (speedstr); } return speed; } int get_max_ele_idx_in_dbl_ary (double *array, int size) { double maxval = array[0]; int maxidx = 0; for (int i=1; imaxval) { maxval = array[i]; maxidx = i; } } return maxidx; } /** * @param sources 所有待测源 * @param size 待测源的数量 * @param[out] speed_records 速度值记录,单位为Byte/s */ void measure_speed_for_every_source (Source_t sources[], int size, double speed_records[]) { // bool get_measured[size]; /* 是否真正执行了测速 */ int get_measured_n = 0; /* 测速了几个 */ char *measure_msgs[size]; double speed = 0.0; for (int i=0; ipsmi; bool provider_skip = psmi.skip; bool has_dedicated_speed_url = false; /** * 存在两类测速链接 * 1. 有*专用测速链接*时,我们选专用,这是精准测速 * 2. 若无,我们用*镜像站整体测速链接*来进行代替, * 若是专用镜像站,则是精准测速 * 若是通用镜像站,则是模糊测速 */ const char *provider_speed_url = psmi.url; const char *dedicated_speed_url = src.speed_measure_url; /* 最终用来测速的 URL */ char *url = NULL; if (!provider_skip && !provider_speed_url) /* 没有声明跳过,但是却没有提供 URL,这是维护者维护时出了纰漏,我们软处理 */ { char *msg1 = ENGLISH ? "Maintainers don't offer " : "维护者未提供 "; char *msg2 = ENGLISH ? " mirror site's speed measure link, so skip it" : " 镜像站测速链接,跳过该站点(需修复)"; chsrc_warn (xy_strcat (3, msg1, provider->code, msg2)); speed = 0; speed_records[i] = speed; // get_measured[i] = false; measure_msgs[i] = NULL; } else if (!provider_skip && provider_speed_url) { if (hp_is_url (provider_speed_url)) { url = xy_strdup (provider_speed_url); chsrc_debug ("m", xy_2strcat ("使用镜像站整体测速链接: ", url)); } } else if (provider_skip) { /* Provider 被声明为跳过测速,下方判断精准测速链接有无提供,若也没有提供,将会输出跳过原因 */ } if (dedicated_speed_url) { if (hp_is_url (dedicated_speed_url)) { url = xy_strdup (dedicated_speed_url); has_dedicated_speed_url = true; chsrc_debug ("m", xy_2strcat ("使用专用测速链接: ", url)); } else { /* 防止维护者没填,这里有一些脏数据,我们软处理:假装该链接URL不存在 */ has_dedicated_speed_url = false; chsrc_debug ("m", xy_2strcat ("专用测速链接为脏数据,请修复: ", provider->name)); } } if (provider_skip && !has_dedicated_speed_url) { if (xy_streql ("upstream", provider->code)) { /* 上游源不测速,但不置0,因为要避免这么一种情况: 可能其他镜像站测速都为0,最后反而选择了该 upstream */ speed = -1024*1024*1024; if (!src.url) { psmi.skip_reason_CN = "缺乏对上游默认源进行测速的URL,请帮助补充"; psmi.skip_reason_EN = "Lack of URL to measure upstream default source provider, please help to add"; } } else if (xy_streql ("user", provider->code)) { /* 代码不会执行至此 */ speed = 1024*1024*1024; } else { /* 不测速的 Provider */ speed = 0; } // get_measured[i] = false; speed_records[i] = speed; const char *msg = ENGLISH ? provider->abbr : provider->name; const char *skip_reason = ENGLISH ? psmi.skip_reason_EN : psmi.skip_reason_CN; if (NULL==skip_reason) { skip_reason = ENGLISH ? "SKIP for no reason" : "无理由跳过"; } measure_msgs[i] = xy_strcat (4, faint(" x "), msg, " ", yellow(faint(skip_reason))); println (measure_msgs[i]); /* 下一位 */ continue; } /* 此时,一定获得了一个用于测速的链接 */ if (url) { const char *msg = ENGLISH ? provider->abbr : provider->name; bool is_accurate = false; if (has_dedicated_speed_url) { is_accurate = true; } else if (provider->psmi.accurate) { is_accurate = true; } char *accurate_msg = CHINESE ? (is_accurate ? bdblue(faint("[精准测速]")) : faint("[模糊测速]")) : (is_accurate ? bdblue(faint("[accurate]")) : faint("[rough]")); if (xy_streql ("upstream", provider->code)) { measure_msgs[i] = xy_strcat (7, faint(" ^ "), msg, " (", src.url, ") ", accurate_msg, faint(" ... ")); } else { measure_msgs[i] = xy_strcat (5, faint(" - "), msg, " ", accurate_msg, faint(" ... ")); } print (measure_msgs[i]); fflush (stdout); char *curl_result = measure_speed_for_url (url); double speed = parse_and_say_curl_result (curl_result); speed_records[i] = speed; /* 释放 url 内存 */ if (url) free (url); } else { xy_unreached(); } } } /** * 自动测速选择镜像站和源 */ int auto_select_mirror (Source_t *sources, size_t size, const char *target_name) { /* reset 时选择默认源 */ if (chsrc_in_reset_mode()) return 0; if (!in_dry_run_mode()) { char *msg = ENGLISH ? "Measuring speed in sequence" : "测速中"; xy_log_brkt (App_Name, bdpurple (ENGLISH ? "MEASURE" : "测速"), msg); br(); } if (0==size || 1==size) { char *msg1 = ENGLISH ? "Currently " : "当前 "; char *msg2 = ENGLISH ? "No any source, please contact maintainers: chsrc issue" : " 无任何可用源,请联系维护者: chsrc issue"; chsrc_error (xy_strcat (3, msg1, target_name, msg2)); exit (Exit_MaintainerCause); } if (in_dry_run_mode()) /* Dry Run 时,跳过测速 */ { return 1; /* 原则第一个源 */ } bool only_one = false; if (2==size) only_one = true; /** --------------------------------------------- */ bool exist_curl = chsrc_check_program_quietly_when_exist ("curl"); if (!exist_curl) { char *msg = ENGLISH ? "No curl, unable to measure speed" : "没有curl命令,无法测速"; chsrc_error (msg); exit (Exit_UserCause); } if (xy.on_windows) { char *curl_version = xy_run ("curl --version", 1); /** * https://github.com/RubyMetric/chsrc/issues/144 * * Cygwin上,curl 的版本信息为: * * curl 8.9.1 (x86_64-pc-cygwin) * */ if (strstr (curl_version, "pc-cygwin")) { char *msg = ENGLISH ? "You're using curl built by Cygwin which has a bug! Please use another curl!" : "你使用的是Cygwin构建的curl,该版本的curl存在bug,请改用其他版本的curl"; chsrc_error (msg); exit (Exit_UserCause); } } /** --------------------------------------------- */ /* 总测速记录值 */ double speed_records[size]; measure_speed_for_every_source (sources, size, speed_records); br(); /* DEBUG */ /* for (int i=0; iabbr : sources[fast_idx].mirror->name; say (xy_strcat (5, msg1, bdgreen(name), green(is), green(target_name), green(msg2))); } else { char *msg = ENGLISH ? "FASTEST mirror site: " : "最快镜像站: "; const char *name = ENGLISH ? sources[fast_idx].mirror->abbr : sources[fast_idx].mirror->name; say (xy_2strcat (msg, green(name))); } // https://github.com/RubyMetric/chsrc/pull/71 if (in_measure_mode()) { char *msg = ENGLISH ? "URL of above source: " : "镜像源地址: "; say (xy_2strcat (msg, green(sources[fast_idx].url))); } return fast_idx; } int use_specific_mirror_or_auto_select (char *input, Target_t *t) { if (input) { return query_mirror_exist (t->sources, t->sources_n, t->aliases, input); } else { return auto_select_mirror (t->sources, t->sources_n, t->aliases); } } bool source_is_upstream (Source_t *source) { return xy_streql (source->mirror->code, "upstream"); } bool source_is_userdefine (Source_t *source) { return xy_streql (source->mirror->code, "user"); } bool source_has_empty_url (Source_t *source) { return source->url == NULL; } /** * @brief 为该 target 确定最终将使用的源 * * 用户*只可能*通过下面5种方式来换源,无论哪一种都会返回一个 Source_t 出来 * * 1. 用户指定了一个 Mirror Code,即 chsrc set * 2. 用户指定了一个 URL, 即 chsrc set https://ur * 3. 用户什么都没指定, 即 chsrc set * 4. 用户正在重置源, 即 chsrc reset * * 如果处于 Target Group 模式下,leader target 已经测速过了,follower target * 不能再次测速,而是直接选择 leader 测过的结果 * * 5. leader target 测速出来的某个源 * */ Source_t chsrc_yield_source (Target_t *t, char *option) { /** * 防止某些意外时刻 _setsrc() 等函数会被直接调,但此时 _prelude() 还没有执行过 * 我们在这里卡一道,确保 _prelude() 被调用 * * 目前可能出现这种情况的时候:组换源的时候,组成菜的 _setsrc() 被直接调用 */ if (!t->inited) t->preludefn(); Source_t source; if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index==-1) { ProgStatus.leader_selected_index = use_specific_mirror_or_auto_select (option, t); source = t->sources[ProgStatus.leader_selected_index]; } else if (chsrc_in_target_group_mode() && ProgStatus.leader_selected_index!=-1) { source = t->sources[ProgStatus.leader_selected_index]; } else if (hp_is_url (option)) { Source_t tmp = { &UserDefinedProvider, option }; source = tmp; } else { int index = use_specific_mirror_or_auto_select (option, t); source = t->sources[index]; } return source; } #define hr() say ("--------------------------------"); /** * 用于 _setsrc 函数 * * 1. 告知用户选择了什么源和镜像 * 2. 对选择的源和镜像站进行一定的校验 */ void chsrc_confirm_source (Source_t *source) { // 由于实现问题,我们把本应该独立出去的上游默认源,也放在了可以换源的数组中,而且放在第一个 // chsrc 已经规避用户使用未实现的 `chsrc reset` // 但是某些用户可能摸索着强行使用 chsrc set target upstream,从而执行起该禁用的功能, // 之所以禁用,是因为有的 reset 我们并没有实现,我们在这里阻止这些邪恶的用户 if (source_is_upstream (source) && source_has_empty_url (source)) { char *msg = ENGLISH ? "Not implement `reset` for the target yet" : "暂未对该目标实现重置"; chsrc_error (msg); exit (Exit_Unsupported); } else if (source_has_empty_url (source)) { char *msg = ENGLISH ? "URL of the source doesn't exist, please report a bug to the dev team" : \ "该源URL不存在,请向维护团队提交bug"; chsrc_error (msg); exit (Exit_MaintainerCause); } else { char *msg = ENGLISH ? "SELECT mirror site: " : "选中镜像站: "; say (xy_strcat (5, msg, green (source->mirror->abbr), " (", green (source->mirror->code), ")")); } hr(); } Source_t chsrc_yield_source_and_confirm (Target_t *t, char *option) { Source_t source = chsrc_yield_source(t, option); chsrc_confirm_source(&source); return source; } void chsrc_determine_chgtype (ChgType_t type) { ProgStatus.chgtype = chsrc_in_reset_mode() ? ChgType_Reset : type; } #define MSG_EN_PUBLIC_URL "If the URL you specify is a public service, you are invited to contribute: chsrc issue" #define MSG_CN_PUBLIC_URL "若您指定的URL为公有服务,邀您参与贡献: chsrc issue" #define MSG_EN_FULLY_AUTO "Fully-Auto changed source. " #define MSG_CN_FULLY_AUTO "全自动换源完成" #define MSG_EN_SEMI_AUTO "Semi-Auto changed source. " #define MSG_CN_SEMI_AUTO "半自动换源完成" #define MSG_EN_THANKS "Thanks to the mirror site: " #define MSG_CN_THANKS "感谢镜像提供方: " #define MSG_EN_BETTER "If you have a better source changing method , please help: chsrc issue" #define MSG_CN_BETTER "若您有更好的换源方案,邀您帮助: chsrc issue" #define MSG_EN_CONSTRAINT "Implementation constraints require manual operation according to the above prompts. " #define MSG_CN_CONSTRAINT "因实现约束需按上述提示手工操作" #define MSG_EN_STILL "Still need to operate manually according to the above prompts. " #define MSG_CN_STILL "仍需按上述提示手工操作" #define thank_mirror(msg) say(xy_2strcat(msg,purple(ENGLISH?source->mirror->abbr:source->mirror->name))) /** * @param source 可为NULL * * @dependency @gvar:ProgStatus.chgtype */ void chsrc_conclude (Source_t *source) { hr(); // fprintf (stderr, "chsrc: now change type: %d\n", ProgStatus.chgtype); if (chsrc_in_reset_mode()) { // source_is_upstream (source) char *msg = ENGLISH ? "Has been reset to the upstream default source" : "已重置为上游默认源"; chsrc_log (purple (msg)); } else if (ChgType_Auto == ProgStatus.chgtype) { if (source) { if (source_is_userdefine (source)) { char *msg = ENGLISH ? MSG_EN_FULLY_AUTO MSG_EN_PUBLIC_URL \ : MSG_CN_FULLY_AUTO ", " MSG_CN_PUBLIC_URL; chsrc_log (msg); } else { char *msg = ENGLISH ? MSG_EN_FULLY_AUTO MSG_EN_THANKS \ : MSG_CN_FULLY_AUTO ", " MSG_CN_THANKS; thank_mirror (msg); } } else { char *msg = ENGLISH ? MSG_EN_FULLY_AUTO : MSG_CN_FULLY_AUTO; chsrc_log (msg); } } else if (ChgType_SemiAuto == ProgStatus.chgtype) { if (source) { if (source_is_userdefine (source)) { char *msg = ENGLISH ? MSG_EN_SEMI_AUTO MSG_EN_STILL MSG_EN_PUBLIC_URL \ : MSG_CN_SEMI_AUTO ", " MSG_CN_STILL "。" MSG_CN_PUBLIC_URL; chsrc_log (msg); } else { char *msg = ENGLISH ? MSG_EN_SEMI_AUTO MSG_EN_STILL MSG_EN_THANKS \ : MSG_CN_SEMI_AUTO ", " MSG_CN_STILL "。" MSG_CN_THANKS; thank_mirror (msg); } } else { char *msg = ENGLISH ? MSG_EN_SEMI_AUTO MSG_EN_STILL \ : MSG_CN_SEMI_AUTO ", " MSG_CN_STILL; chsrc_log (msg); } char *msg = ENGLISH ? MSG_EN_BETTER : MSG_CN_BETTER; chsrc_warn (msg); } else if (ChgType_Manual == ProgStatus.chgtype) { if (source) { if (source_is_userdefine (source)) { char *msg = ENGLISH ? MSG_EN_CONSTRAINT MSG_EN_PUBLIC_URL \ : MSG_CN_CONSTRAINT "; " MSG_CN_PUBLIC_URL; chsrc_log (msg); } else { char *msg = ENGLISH ? MSG_EN_CONSTRAINT MSG_EN_THANKS \ : MSG_CN_CONSTRAINT ", " MSG_CN_THANKS; thank_mirror (msg); } } else { char *msg = ENGLISH ? MSG_EN_CONSTRAINT : MSG_CN_CONSTRAINT; chsrc_log (msg); } char *msg = ENGLISH ? MSG_EN_BETTER : MSG_CN_BETTER; chsrc_warn (msg); } else if (ChgType_Untested == ProgStatus.chgtype) { if (source) { if (source_is_userdefine (source)) { char *msg = ENGLISH ? MSG_EN_PUBLIC_URL : MSG_CN_PUBLIC_URL; chsrc_log (msg); } else { char *msg = ENGLISH ? MSG_EN_THANKS : MSG_CN_THANKS; thank_mirror (msg); } } else { char *msg = ENGLISH ? "Auto changed source" : "自动换源完成"; chsrc_log (msg); } char *msg = ENGLISH ? "The method hasn't been tested or has any feedback, please report usage: chsrc issue" : "该换源步骤已实现但未经测试或存在任何反馈,请报告使用情况: chsrc issue"; chsrc_warn (msg); } else { fprintf (stderr, "chsrc: Wrong change type: %d\n", ProgStatus.chgtype); xy_unreached(); } } /** * @brief 检测该 target 是否实现了用户所指定的 scope 能力 * * @note 此函数目前只支持中文 */ void chsrc_check_scope_capability (Target_t *target) { ScopeCapability_t cap = ScopeCap_Unknown; char *msg1 = "不支持"; char *msg2 = "换源,请使用 chsrc ls "; char *msg3 = " 查看支持的作用域以及默认作用域"; char *aliases = target->aliases; char *scope_name = NULL; if (chsrc_in_project_scope_mode()) { cap = target->scope_caps[ScopeCap_Slot_Project]; if (cap != ScopeCap_Able_And_Implemented) { scope_name = "项目级"; char* msg = xy_strcat (5, msg1, scope_name, msg2, aliases, msg3); chsrc_error (msg); exit (Exit_UserCause); } } if (chsrc_in_user_scope_mode()) { cap = target->scope_caps[ScopeCap_Slot_User]; if (cap != ScopeCap_Able_And_Implemented) { scope_name = "用户级"; char* msg = xy_strcat (5, msg1, scope_name, msg2, aliases, msg3); chsrc_error (msg); exit (Exit_UserCause); } } if (chsrc_in_system_scope_mode()) { cap = target->scope_caps[ScopeCap_Slot_System]; if (cap != ScopeCap_Able_And_Implemented) { scope_name = "系统级"; char* msg = xy_strcat (5, msg1, scope_name, msg2, aliases, msg3); chsrc_error (msg); exit (Exit_UserCause); } } } void chsrc_ensure_root () { char *euid = getenv ("$EUID"); if (NULL==euid) { char *buf = xy_run ("id -u", 0); if (0!=atoi(buf)) goto not_root; else return; } else { if (0!=atoi(euid)) goto not_root; else return; } char *msg = NULL; not_root: msg = ENGLISH ? "Use sudo before the command or switch to root to ensure the necessary permissions" : "请在命令前使用 sudo 或切换为root用户来保证必要的权限"; chsrc_error (msg); exit (Exit_UserCause); } #define RunOpt_Default 0x0000 // 默认若命令运行失败,直接退出 #define RunOpt_Dont_Notify_On_Success 0x0010 // 运行成功不提示用户,只有运行失败时才提示用户 #define RunOpt_No_Last_New_Line 0x0100 // 不输出最后的空行 #define RunOpt_Dont_Abort_On_Failure 0x1000 // 命令运行失败也不退出 static void chsrc_run (const char *cmd, int run_option) { if (ProgStatus.chsrc_run_faas) { run_option |= RunOpt_Dont_Notify_On_Success|RunOpt_No_Last_New_Line; } else { if (ENGLISH) xy_log_brkt (blue (App_Name), bdblue ("RUN"), blue (cmd)); else xy_log_brkt (blue (App_Name), bdblue ("运行"), blue (cmd)); } if (in_dry_run_mode()) { return; // Dry Run 此时立即结束,并不真正执行 } int status = system (cmd); bool use_yellow_for_error = (run_option & RunOpt_Dont_Abort_On_Failure) != 0; if (0==status) { if (! (RunOpt_Dont_Notify_On_Success & run_option)) { log_cmd_result (true, status, use_yellow_for_error); } } else { log_cmd_result (false, status, use_yellow_for_error); if (! (run_option & RunOpt_Dont_Abort_On_Failure)) { char *msg = ENGLISH ? "Fatal error, forced end" : "关键错误,强制结束"; chsrc_error (msg); exit (Exit_ExternalError); } } if (! (RunOpt_No_Last_New_Line & run_option)) { br(); } } static void chsrc_run_as_a_service (const char *cmd) { int run_option = RunOpt_Default; ProgStatus.chsrc_run_faas = true; run_option |= RunOpt_Dont_Notify_On_Success|RunOpt_No_Last_New_Line; chsrc_run (cmd, run_option); ProgStatus.chsrc_run_faas = false; } /** * @brief 以纯粹的方式直接运行命令,不做任何多余处理, * 命令执行前显示给用户,并保留所有输出给用户 * * @return 返回命令的退出状态 */ int chsrc_run_directly (const char *cmd) { if (ENGLISH) xy_log_brkt (blue (App_Name), bdblue ("RUN"), blue (cmd)); else xy_log_brkt (blue (App_Name), bdblue ("运行"), blue (cmd)); if (in_dry_run_mode()) { return 0; // Dry Run 此时立即结束,并不真正执行 } int status = system (cmd); return status; } /** * @brief 在本目录创建一个临时文件 * * @param[in] filename 文件名,不包含后缀名 * @param[in] postfix 后缀名,需要自己加 '.' * @param[in] loud 创建成功时是否提示用户 * @param[out] tmpfilepath 生成的临时文件名,非 Windows 可以为 NULL * * @return 返回一个 FILE*,调用者需要关闭该文件 */ FILE * chsrc_make_tmpfile (char *filename, char *postfix, bool loud, char **tmpfilepath) { char *tmpfile = NULL; FILE *f = NULL; #ifdef XY_Build_On_Windows /** * Windows 上使用 GetTempPath 和 GetTempFileName 创建临时文件 * 这是 Windows API 推荐的标准方法 * * 由于 GetTempFileName 不支持自定义后缀,我们需要: * 1. 使用 GetTempFileName 生成唯一的临时文件名 * 2. 将其重命名为带有正确后缀的文件名(PowerShell 需要 .ps1 后缀) */ char temp_path[MAX_PATH]; char temp_filename[MAX_PATH]; /* 获取系统临时目录 */ DWORD ret = GetTempPathA (MAX_PATH, temp_path); if (ret == 0 || ret > MAX_PATH) { char *msg = CHINESE ? "无法获取系统临时目录" : "Unable to get system temp directory"; chsrc_error2 (msg); exit (Exit_ExternalError); } /* 生成唯一的临时文件名 (会自动创建文件) */ ret = GetTempFileNameA (temp_path, "chsrc_", 0, temp_filename); if (ret == 0) { char *msg = CHINESE ? "无法生成临时文件名" : "Unable to generate temporary filename"; chsrc_error2 (msg); exit (Exit_ExternalError); } tmpfile = xy_strcat (4, temp_filename, "_", filename, postfix); /* 删除 GetTempFileName 自动创建的文件 */ DeleteFileA (temp_filename); /* 创建带有正确后缀的文件 */ f = fopen (tmpfile, "w+"); if (!f) { char *msg = CHINESE ? "无法创建临时文件: " : "Unable to create temporary file: "; msg = xy_2strcat (msg, tmpfile); chsrc_error2 (msg); exit (Exit_ExternalError); } #else /** * 非 Windows 平台使用 mkstemps() 创建临时文件 * 这是 POSIX 标准方法,可以指定后缀名 */ tmpfile = xy_strcat (5, "/tmp/", "chsrc_tmp_", filename, "_XXXXXX", postfix); size_t postfix_len = strlen (postfix); /* mkstemps() 会原子性地创建文件并返回文件描述符 */ int fd = mkstemps (tmpfile, postfix_len); if (fd == -1) { char *msg = CHINESE ? "无法创建临时文件: " : "Unable to create temporary file: "; msg = xy_2strcat (msg, tmpfile); chsrc_error2 (msg); exit (Exit_ExternalError); } f = fdopen (fd, "w+"); if (!f) { close (fd); /* 关闭文件描述符以避免泄漏 */ char *msg = CHINESE ? "无法打开临时文件: " : "Unable to open temporary file: "; msg = xy_2strcat (msg, tmpfile); chsrc_error2 (msg); exit (Exit_ExternalError); } #endif if (loud) { char *msg = CHINESE ? "已创建临时文件: " : "Temporary file created: "; msg = xy_2strcat (msg, tmpfile); chsrc_succ2 (msg); } /** * 允许生成文件后不了解其文件名,调用者只了解 FILE* * 这样的话,其实是无法删除该文件的,但是生成在 /tmp 目录下我们恰好可以不用清理 * 但是在 Windows 上,就没有办法了,所以我们禁止在 Windows 上不指定返回出的临时文件名 */ if (xy.on_windows && !tmpfilepath) { chsrc_error2 ("在 Windows 上,创建临时文件时必须指定返回的临时文件名"); xy_unreached(); } if (tmpfilepath) { *tmpfilepath = xy_normalize_path (xy_strdup (tmpfile)); } return f; } /** * 以 bash file.bash 的形式运行脚本内容 */ void chsrc_run_as_bash_file (const char *script_content) { char *tmpfile = NULL; FILE *f = chsrc_make_tmpfile ("bash_script", ".bash", false, &tmpfile); fwrite (script_content, strlen (script_content), 1, f); fclose (f); // chmod (tmpfile, 0700); char *msg = CHINESE ? "即将执行 Bash 脚本内容:" : "The Bash script content will be executed:"; chsrc_note2 (msg); println (faint(script_content)); char *cmd = xy_2strcat ("bash ", tmpfile); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); remove (tmpfile); free (tmpfile); /* 释放 tmpfile 路径内存 */ } /** * 以 sh file.sh 的形式运行脚本内容 */ void chsrc_run_as_sh_file (const char *script_content) { char *tmpfile = NULL; FILE *f = chsrc_make_tmpfile ("sh_script", ".sh", false, &tmpfile); fwrite (script_content, strlen (script_content), 1, f); fclose (f); // chmod (tmpfile, 0700); char *msg = CHINESE ? "即将执行 sh 脚本内容:" : "The sh script content will be executed:"; chsrc_note2 (msg); println (faint(script_content)); char *cmd = xy_2strcat ("sh ", tmpfile); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); remove (tmpfile); free (tmpfile); } /** * 以 pwsh file.ps1 的形式运行脚本内容 */ void chsrc_run_as_pwsh_file (const char *script_content) { char *tmpfile = NULL; FILE *f = chsrc_make_tmpfile ("pwsh_script", ".ps1", false, &tmpfile); fwrite (script_content, strlen (script_content), 1, f); fclose (f); char *msg = CHINESE ? "即将执行 PowerShell (v7以上) 脚本内容:" : "The PowerShell script content will be executed:"; chsrc_note2 (msg); println (faint(script_content)); char *cmd = xy_2strcat ("pwsh ", tmpfile); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); remove (tmpfile); free (tmpfile); } /** * 以 powershell file.ps1 的形式运行脚本内容 */ void chsrc_run_as_powershellv5_file (const char *script_content) { char *tmpfile = NULL; FILE *f = chsrc_make_tmpfile ("psv5_script", ".ps1", false, &tmpfile); fwrite (script_content, strlen (script_content), 1, f); fclose (f); char *msg = CHINESE ? "即将执行 PowerShell v5 脚本内容:" : "The PowerShell v5 script content will be executed:"; chsrc_note2 (msg); println (faint(script_content)); // -ExecutionPolicy Bypass char *cmd = xy_2strcat ("powershell -File ", tmpfile); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); remove (tmpfile); free (tmpfile); } /** * 使用 pwsh 或 旧的 powershell (v5) 运行脚本内容,优先使用 pwsh */ void chsrc_run_as_powershell_file (const char *script_content) { // if (chsrc_check_program_quietly_when_exist ("pwsh")) if (chsrc_check_program_quietly ("pwsh")) { chsrc_run_as_pwsh_file (script_content); } else { chsrc_alert2 (CHINESE ? "未检测到 PowerShell 7 及以上版本,默认使用 PowerShell v5" : "PowerShell 7 or above not detected, switch to PowerShell v5"); chsrc_run_as_powershellv5_file (script_content); } } /** * @param cmdline 需要自己负责转义 * * @danger 需要经过 Bash 的转义,很容易出错,不要用这个函数 */ XY_Deprecate_This("Don't use this function") void chsrc_run_in_inline_bash_shell (const char *cmdline) { char *cmd = xy_strcat (3, "bash -c '", cmdline, "'"); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); } /** * @param cmdline 需要自己负责转义 * * @danger 需要经过 PowerShell 的转义,很容易出错,不要用这个函数 */ XY_Deprecate_This("Don't use this function") void chsrc_run_in_inline_pwsh_shell (const char *cmdline) { char *cmd = xy_strcat (3, "pwsh -Command '", cmdline, "'"); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); } static void chsrc_view_env (const char *var1, ...) { char *cmd = NULL; const char *var = var1; va_list vars; va_start (vars, var1); bool first = true; while (var) { if (xy.on_windows) { if (first) { cmd = xy_strcat (3, "set ", var, " "); first = false; } else { cmd = xy_strcat (4, cmd, "& set ", var, " "); } } else { if (first) { cmd = xy_strcat (5, "echo ", var, "=$", var, " "); first = false; } else { cmd = xy_strcat (6, cmd, "; echo ", var, "=$", var, " "); } } var = va_arg (vars, const char *); } va_end (vars); if (var1) { /** * 不用 chsrc_run(),因为在Windows上,set在遇到环境变量未定义时会返回非0,导致 chsrc_run() 报告运行失败 * 这个错误过于醒目。我们应该像在 sh 一样,默默地没有输出即可,而不是报错 */ // chsrc_run (cmd, RunOpt_Dont_Notify_On_Success|RunOpt_No_Last_New_Line|RunOpt_Dont_Abort_On_Failure); int status = system (cmd); if (status!=0) { xy_noop(); } } else { /* 必须给一个参数 */ xy_unreached(); } } static void chsrc_view_file (const char *path) { char *cmd = NULL; path = xy_normalize_path (path); if (xy.on_windows) { cmd = xy_2strcat ("type ", path); } else { cmd = xy_2strcat ("cat ", path); } chsrc_run_as_a_service (cmd); } static void chsrc_ensure_dir (const char *dir) { dir = xy_normalize_path (dir); if (xy_dir_exist (dir)) { return; } // 不存在就生成 char *mkdir_cmd = NULL; if (xy.on_windows) { mkdir_cmd = "md "; // 已存在时返回 errorlevel = 1 } else { mkdir_cmd = "mkdir -p "; } char *cmd = xy_2strcat (mkdir_cmd, dir); cmd = xy_quiet_cmd (cmd); chsrc_run_as_a_service (cmd); char *msg = ENGLISH ? "Directory doesn't exist, created automatically " : "目录不存在,已自动创建 "; chsrc_alert2 (xy_2strcat (msg, dir)); } static void chsrc_append_to_file (const char *str, const char *filename) { if (in_dry_run_mode()) { goto log_anyway; } char *file = xy_normalize_path (filename); char *dir = xy_parent_dir (file); chsrc_ensure_dir (dir); FILE *f = fopen (file, "a"); if (NULL==f) { char *msg = ENGLISH ? xy_2strcat ("Unable to open file to write: ", file) : xy_2strcat ("无法打开文件以写入: ", file); chsrc_error2 (msg); exit (Exit_UserCause); } size_t len = strlen (str); size_t ret = fwrite (str, len, 1, f); if (ret != 1) { char *msg = ENGLISH ? xy_2strcat ("Write failed to ", file) : xy_2strcat ("写入文件失败: ", file); chsrc_error2 (msg); exit (Exit_UserCause); } fclose (f); log_anyway: /* 输出recipe指定的文件名 */ chsrc_log_write (filename, false); /* char *cmd = NULL; if (xy.on_windows) { cmd = xy_strcat (4, "echo ", str, " >> ", file); } else { cmd = xy_strcat (4, "echo '", str, "' >> ", file); } chsrc_run_a_service (cmd); */ } /** * @note 本函数不会在 `str` 末尾添加换行符,所以你可能需要在 `str` 中手动添加 */ static void chsrc_prepend_to_file (const char *str, const char *filename) { if (in_dry_run_mode()) { goto log_anyway; } char *file = xy_normalize_path (filename); char *file_content = xy_file_read (file); char *content = xy_2strcat (str, file_content); FILE *f = fopen (file, "w"); if (f) { fwrite (content, 1, strlen (content), f); fclose (f); } else { chsrc_error2 ("文件打开失败"); exit (Exit_UserCause); } log_anyway: /* 输出recipe指定的文件名 */ chsrc_log_write (filename, false); } static void chsrc_overwrite_file (const char *str, const char *filename) { if (in_dry_run_mode()) { goto log_anyway; } char *file = xy_normalize_path (filename); char *dir = xy_parent_dir (file); chsrc_ensure_dir (dir); FILE *f = fopen (file, "w"); if (NULL==f) { char *msg = ENGLISH ? xy_2strcat ("Unable to open file to overwrite: ", file) : xy_2strcat ("无法打开文件以覆盖: ", file); chsrc_error2 (msg); exit (Exit_UserCause); } size_t len = strlen (str); size_t ret = fwrite (str, len, 1, f); if (ret != 1) { fclose (f); char *msg = ENGLISH ? xy_2strcat ("Write failed to ", file) : xy_2strcat ("写入文件失败: ", file); chsrc_error2 (msg); exit (Exit_UserCause); } fclose (f); log_anyway: /* 输出recipe指定的文件名 */ chsrc_log_write (filename, true); } static void chsrc_backup (const char *path) { if (in_dry_run_mode()) { goto log_anyway; } char *cmd = NULL; bool exist = xy_file_exist (path); if (!exist) { char *msg = ENGLISH ? "File doesn't exist, skip backup: " : "文件不存在,跳过备份: "; chsrc_alert2 (xy_2strcat (msg, path)); return; } if (xy.on_bsd || xy.on_macos) { /* BSD 和 macOS 的 cp 不支持 --backup 选项 */ cmd = xy_strcat (5, "cp -f ", path, " ", path, ".bak"); } else if (xy.on_windows) { /** * @note /Y 表示覆盖 * @note 默认情况下会输出一个 "已复制 1个文件" */ cmd = xy_strcat (5, "copy /Y ", path, " ", path, ".bak 1>nul"); } else { /** * @see https://github.com/RubyMetric/chsrc/issues/152#issuecomment-2542673273 * * busybox cp 会在 stderr 输出 unrecognized option: version * stderr 导入到 stdout,以便我们 xy_run() 可以接受到输出 * */ char *ver = xy_run ("cp --version 2>&1", 1); /* cp (GNU coreutils) 9.4 */ if (strstr (ver, "GNU coreutils")) { cmd = xy_strcat (5, "cp ", path, " ", path, ".bak --backup='t'"); } else { /* 非 GNU 的 cp 可能不支持 --backup ,如 busybox cp */ cmd = xy_strcat (5, "cp -f ", path, " ", path, ".bak"); } } chsrc_run_as_a_service (cmd); log_anyway: chsrc_log_backup (path); } /** * 检查过程中全程保持安静 */ static char * chsrc_get_cpuarch () { char *ret; char *msg; #if XY_Build_On_Windows SYSTEM_INFO info; GetSystemInfo (&info); WORD num = info.wProcessorArchitecture; switch (num) { case PROCESSOR_ARCHITECTURE_AMD64: ret = "x86_64"; break; case PROCESSOR_ARCHITECTURE_ARM: ret = "arm"; break; case PROCESSOR_ARCHITECTURE_INTEL: ret = "x86"; break; case PROCESSOR_ARCHITECTURE_IA64: ret = "IA-64"; break; case PROCESSOR_ARCHITECTURE_UNKNOWN: default: msg = ENGLISH ? "Unable to detect CPU type" : "无法检测到CPU类型"; chsrc_error (msg); exit (Exit_UserCause); } return ret; #else bool exist; exist = chsrc_check_program_quietly ("arch"); if (exist) { ret = xy_run ("arch", 0); return ret; } exist = chsrc_check_program_quietly ("uname"); if (exist) { ret = xy_run ("uname -m", 0); return ret; } else { msg = ENGLISH ? "Unable to detect CPU type" : "无法检测到CPU类型"; chsrc_error (msg); exit (Exit_UserCause); } #endif } static int chsrc_get_cpucore () { int cores = 2; #if XY_Build_On_Windows SYSTEM_INFO info; GetSystemInfo (&info); DWORD num = info.dwNumberOfProcessors; cores = (int)num; #else long num = sysconf(_SC_NPROCESSORS_ONLN); cores = (int)num; #endif return cores; } ================================================ FILE: src/framework/helper.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Name : helper.c * File Authors : 曾奥然 * Contributors : Nul None * Created On : <2025-07-14> * Last Modified : <2025-10-28> * * For chefs (recipe makers) and sometimes framewoker * to do some work not releated to OS operations * ------------------------------------------------------------*/ bool hp_is_url (const char *str) { return (xy_str_start_with (str, "http://") || xy_str_start_with (str, "https://")); } /** * @return 一律返回新字符串 */ char * hp_remove_trailing_slash (char *str) { char *newstr = xy_strdup (str); size_t len = strlen (newstr); if (len > 0 && newstr[len - 1] == '/') newstr[len - 1] = '\0'; return newstr; } /** * @return 一律返回新字符串 */ char * hp_ensure_trailing_slash (char *str) { size_t len = strlen (str); if (len == 0 || str[len - 1] == '/') return xy_strdup (str); return xy_2strcat (str, "/"); } ================================================ FILE: src/framework/mirror.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Name : mirror.c * File Authors : @ccmywish * | @G_I_Y * Contributors : @livelycode36 * | @jialinlvcn * | @Mikachu2333 * | @BingChunMoLi * | * Created On : <2023-08-29> * Last Modified : <2025-10-07> * * 通用镜像站 * ------------------------------------------------------------*/ #define Big_File_ubuntu "/18.04/ubuntu-18.04.6-desktop-amd64.iso" // 2.3 GB #define Big_File_ctan "/systems/texlive/Images/texlive.iso" // 4.8 GB #define Big_File_archlinux "/iso/latest/archlinux-x86_64.iso" // 800 MB #define Big_File_deepin "/20.9/deepin-desktop-community-20.9-amd64.iso" // 4 GB /** * 教育网镜像 * * @sync https://github.com/RubyMetric/chsrc/wiki * * 排序部分参考 https://github.com/mirrorz-org/oh-my-mirrorz */ MirrorSite_t MirrorZ = { IS_GeneralMirrorSite, "mirrorz", "MirrorZ", "校园网联合镜像站(MirrorZ)", "https://mirrors.cernet.edu.cn/", {NotSkip, NA, NA, "https://mirrors.cernet.edu.cn/ubuntu/dists/noble/Contents-amd64.gz", ROUGH} }, Tuna = { IS_GeneralMirrorSite, "tuna", "TUNA", "清华大学开源软件镜像站", "https://mirrors.tuna.tsinghua.edu.cn/", {NotSkip, NA, NA, "https://mirrors.tuna.tsinghua.edu.cn/speedtest/1000mb.bin", ROUGH} }, /** * @note 2025-03-17 SJTUG 共设两台服务器。思源服务器同步新镜像,致远服务器兼容原 SJTU 镜像站。 * @note 有些target(例如flathub)思源站的兼容性不好,可以考虑将两个服务器分开测试 */ Sjtug_Zhiyuan = { IS_GeneralMirrorSite, "sjtu-zy", "SJTUG-zhiyuan", "上海交通大学致远镜像站", "https://mirrors.sjtug.sjtu.edu.cn/", {NotSkip, NA, NA, "https://mirrors.sjtug.sjtu.edu.cn/ctan" Big_File_ctan, ROUGH} }, Sjtug_Siyuan = { IS_GeneralMirrorSite, "sjtu-sy", "SJTUG-siyuan", "上海交通大学思源镜像站", "https://mirror.sjtu.edu.cn/", {NotSkip, NA, NA, "https://mirror.sjtu.edu.cn/ctan" Big_File_ctan, ROUGH} }, Bfsu = { IS_GeneralMirrorSite, "bfsu", "BFSU", "北京外国语大学开源软件镜像站", "https://mirrors.bfsu.edu.cn/", {NotSkip, NA, NA, "https://mirrors.bfsu.edu.cn/speedtest/1000mb.bin", ROUGH} }, Ustc = { IS_GeneralMirrorSite, "ustc", "USTC", "中国科学技术大学开源软件镜像", "https://mirrors.ustc.edu.cn/", {NotSkip, NA, NA, "https://mirrors.ustc.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Zju = { IS_GeneralMirrorSite, "zju", "ZJU", "浙江大学开源软件镜像站", "https://mirrors.zju.edu.cn/", {NotSkip, NA, NA, "https://mirrors.zju.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Jlu = { IS_GeneralMirrorSite, "jlu", "JLU", "吉林大学开源镜像站", "https://mirrors.jlu.edu.cn/", {NotSkip, NA, NA, "https://mirrors.jlu.edu.cn/_static/speedtest.bin", ROUGH} }, Lzuoss = { IS_GeneralMirrorSite, "lzu", "LZUOSS", "兰州大学开源社区镜像站", "https://mirror.lzu.edu.cn/", {NotSkip, NA, NA, "https://mirror.lzu.edu.cn/CTAN" Big_File_ctan, ROUGH} }, Pku = { IS_GeneralMirrorSite, "pku", "PKU", "北京大学开源镜像站", "https://mirrors.pku.edu.cn/", {NotSkip, NA, NA, "https://mirrors.pku.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Bjtu = { IS_GeneralMirrorSite, "bjtu", "BJTU", "北京交通大学自由与开源软件镜像站", "https://mirror.bjtu.edu.cn/", {NotSkip, NA, NA, "https://mirror.bjtu.edu.cn/archlinux" Big_File_archlinux, ROUGH} }, Sustech = { IS_GeneralMirrorSite, "sustech", "SUSTech", "南方科技大学开源软件镜像站", "https://mirrors.sustech.edu.cn/", {NotSkip, NA, NA, "https://mirrors.sustech.edu.cn/site/speedtest/1000mb.bin", ROUGH} }, Nju = { IS_GeneralMirrorSite, "nju", "NJU", "南京大学开源镜像站", "https://mirrors.nju.edu.cn/", {NotSkip, NA, NA, "https://mirrors.nju.edu.cn/archlinux" Big_File_archlinux, ROUGH} }, Xjtu = { IS_GeneralMirrorSite, "xjtu", "XJTU", "西安交通大学软件镜像站", "https://mirrors.xjtu.edu.cn/", {NotSkip, NA, NA, "https://mirrors.xjtu.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Hust = { IS_GeneralMirrorSite, "hust", "HUST", "华中科技大学开源镜像站", "https://mirrors.hust.edu.cn/", {NotSkip, NA, NA, "https://mirrors.hust.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Iscas = { IS_GeneralMirrorSite, "iscas", "ISCAS", "中科院软件所智能软件研究中心开源镜像站", "https://mirror.iscas.ac.cn/", {NotSkip, NA, NA, "https://mirror.iscas.ac.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Hit = { IS_GeneralMirrorSite, "hit", "HIT", "哈尔滨工业大学开源软件镜像站", "https://mirrors.hit.edu.cn/", {NotSkip, NA, NA, "https://mirrors.hit.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Scau = { IS_GeneralMirrorSite, "scau", "SCAU", "华南农业大学开源软件镜像站", "https://mirrors.scau.edu.cn/", {NotSkip, NA, NA, "https://mirrors.scau.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, NJTech = { IS_GeneralMirrorSite, "njtech", "NJTech", "南京工业大学开源软件镜像站", "https://mirrors.njtech.edu.cn/", {NotSkip, NA, NA, "https://mirrors.njtech.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Nyist = { IS_GeneralMirrorSite, "nyist", "NYIST", "南阳理工学院开源软件镜像站", "https://mirror.nyist.edu.cn/", {NotSkip, NA, NA, "https://mirror.nyist.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Sdu = { IS_GeneralMirrorSite, "sdu", "SDU", "山东大学镜像站", "https://mirrors.sdu.edu.cn/", {NotSkip, NA, NA, "https://mirrors.sdu.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Qlu = { IS_GeneralMirrorSite, "qlu", "QLU", "齐鲁工业大学开源镜像站", "https://mirrors.qlu.edu.cn/", {NotSkip, NA, NA, "https://mirrors.qlu.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Cqupt = { IS_GeneralMirrorSite, "cqupt", "CQUPT", "重庆邮电大学开源镜像站", "https://mirrors.cqupt.edu.cn/", {NotSkip, NA, NA, "https://mirrors.cqupt.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, /** * @note 2023-09-05 封杀策略过严,谨慎使用 */ Cqu = { IS_GeneralMirrorSite, "cqu", "CQU", "重庆大学开源软件镜像站", "https://mirrors.cqu.edu.cn/", {NotSkip, NA, NA, "https://mirrors.cqu.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Neosoft = { IS_GeneralMirrorSite, "neosoft", "Neosoft", "大连东软信息学院开源镜像站", "https://mirrors.neusoft.edu.cn/", {NotSkip, NA, NA, "https://mirrors.neusoft.edu.cn/ubuntu-releases" Big_File_ubuntu, ROUGH} }; /** * 商业公司提供的源 */ MirrorSite_t Ali = { IS_GeneralMirrorSite, "ali", "Ali OPSX Public", "阿里巴巴开源镜像站(公网)", "https://developer.aliyun.com/mirror/", {NotSkip, NA, NA, "https://mirrors.aliyun.com/ubuntu/ls-lR.gz", ROUGH} // 31MB左右 }, /* // https://mirrors.cloud.aliyuncs.com/ Ali_ECS_VPC = { IS_GeneralMirrorSite, "ali-ECS-VPC", "Ali OPSX ECS VPC", "阿里巴巴开源镜像站(ECS VPC网络)", "https://developer.aliyun.com/mirror/", {NotSkip, NA, NA, "https://mirrors.cloud.aliyuncs.com/deepin-cd" Big_File_deepin, ROUGH} }, // https://mirrors.aliyuncs.com/ Ali_ECS_classic = { IS_GeneralMirrorSite, "ali-ECS", "Ali OPSX ECS", "阿里巴巴开源镜像站(ECS 经典网络)", "https://developer.aliyun.com/mirror/", {NotSkip, NA, NA, "https://mirrors.aliyuncs.com/deepin-cd" Big_File_deepin, ROUGH} }, */ Tencent = { IS_GeneralMirrorSite, "tencent", "Tencent Public", "腾讯软件源(公网)", "https://mirrors.tencent.com/", {NotSkip, NA, NA, "https://mirrors.cloud.tencent.com/mariadb/mariadb-12.1.0/winx64-packages/mariadb-12.1.0-winx64-debugsymbols.zip", ROUGH} // 110MB左右 }, /* Tencent_Intra = { IS_GeneralMirrorSite, "tencent-intra", "Tencent Intranet", "腾讯软件源(内网)", "https://mirrors.tencent.com/", {NotSkip, NA, NA, "https://mirrors.cloud.tencentyun.com/ubuntu-releases" Big_File_ubuntu, ROUGH} }, */ Huawei = { IS_GeneralMirrorSite, "huawei", "Huawei Cloud", "华为开源镜像站", "https://mirrors.huaweicloud.com/", {NotSkip, NA, NA, "https://mirrors.huaweicloud.com/ubuntu-releases" Big_File_ubuntu, ROUGH} }, HuaweiCDN = { IS_GeneralMirrorSite, "huawei-cdn", "Huawei Cloud CDN", "华为开源镜像站(CDN)", "https://repo.huaweicloud.com/", {NotSkip, NA, NA, "https://repo.huaweicloud.com/ubuntu-releases" Big_File_ubuntu, ROUGH} }, Volcengine = { IS_GeneralMirrorSite, "volc", "Volcengine", "火山引擎开源软件镜像站(公网)", "https://developer.volcengine.com/mirror/", {NotSkip, NA, NA, "https://mirrors.volces.com/ubuntu-releases" Big_File_ubuntu, ROUGH} }, /* Volceengine_Intra = { IS_GeneralMirrorSite, "volc-intra", "Volcengine Intranet", "火山引擎开源软件镜像站(内网)", "https://developer.volcengine.com/mirror/", "https://mirrors.ivolces.com/ubuntu-releases" Big_File_ubuntu, ROUGH}, */ /** * @note 2025-06-20 网易开源镜像站的速度始终过低,不建议再使用 */ Netease = { IS_GeneralMirrorSite, "netease", "Netease", "网易开源镜像站", "https://mirrors.163.com/", {NotSkip, NA, NA, "https://mirrors.163.com/deepin-cd" Big_File_deepin, ROUGH} }, /** * @note 2025-06-20 搜狐开源镜像站的速度始终过低,不建议再使用 */ Sohu = { IS_GeneralMirrorSite, "sohu", "SOHU", "搜狐开源镜像站", "https://mirrors.sohu.com/", {NotSkip, NA, NA, "https://mirrors.sohu.com/deepin-cd" Big_File_deepin, ROUGH} }; ================================================ FILE: src/framework/struct.h ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Name : struct.h * File Authors : @ccmywish * | @G_I_Y * Contributors : @livelycode36 * | * Created On : <2023-08-29> * Last Modified : <2026-02-24> * * chsrc struct * ------------------------------------------------------------*/ typedef struct ProviderSpeedMeasureInfo_t { bool skip; /* 是否默认跳过 */ char *skip_reason_CN; /* 跳过的原因(中文)*/ char *skip_reason_EN; /* 跳过的原因(英文)*/ char *url; /* 测速链接 */ bool accurate; /* 是否为精准测速,上游源和专用镜像站为 ACCURATE,通用镜像站为 ROUGH */ } ProviderSpeedMeasureInfo_t; #define SKIP true #define NotSkip false #define ToFill NULL #define NA NULL #define ACCURATE true #define ROUGH false typedef enum ProviderType_t { IS_GeneralMirrorSite, /* 通用镜像站 */ IS_DedicatedMirrorSite, /* 专用镜像站 */ IS_UpstreamProvider, /* 上游默认源 */ IS_UserDefinedProvider, /* 用户提供 */ } ProviderType_t; typedef struct SourceProvider_t { const ProviderType_t type; /* 类型 */ const char *code; /* 用于用户指定某一 Provider */ const char *abbr; /* 需要使用 Provider 的英文名时,用这个代替,因为大部分 Provider 没有提供正式的英文名 */ const char *name; /* Provider 中文名 */ const char *site; /* Provider 首页 */ ProviderSpeedMeasureInfo_t psmi; } SourceProvider_t; typedef SourceProvider_t MirrorSite_t; SourceProvider_t UpstreamProvider = { IS_UpstreamProvider, /* 引入新的上游默认源时,请使下面第一行的前三个字段保持不变,只添加第四个字段 */ "upstream", "Upstream", "上游默认源", NULL, /* 引入新的上游默认源时,请完全修改下面这个结构体,可使用 def_need_measure_info 宏 */ {SKIP, "URL未知,邀您参与贡献!", "URL unknown, welcome to contribute!", NULL, ACCURATE} }; #define def_need_measure_info {SKIP, "缺乏较大的测速对象,邀您参与贡献!", "Lack of large object URL, welcome to contribute!", NULL, ACCURATE} SourceProvider_t UserDefinedProvider = { IS_UserDefinedProvider, "user", "用户自定义", "用户自定义", NULL, {SKIP, "用户自定义源不测速", "SKIP for user-defined source", NULL, ACCURATE} }; typedef struct Source_t { union { SourceProvider_t *provider; MirrorSite_t *mirror; }; /* 用于换源的 URL,也称 repoURL */ char *url; /* 对该 source 的专用测速链接,这就是精准测速,也称 smURL */ char *speed_measure_url; } Source_t; /* 不用给专用测速链接,因为 Upstream 的整体测速链接已是精准测速 */ #define DelegateToUpstream NULL /* 不用给专用测速链接,因为该镜像站是专用镜像站,其整体测速链接已是精准测速 */ #define DelegateToMirror NULL /* 看到该注释的贡献者,你可以帮忙寻找专用测速链接 */ #define NeedContribute NULL /* 由 prelude() 填充 */ #define FeedByPrelude NULL /** * 换源的作用域 * * 在 chsrc v0.2.4.2 以前,我们一直使用的是 `-local` 这个选项,其含义是启用 *项目级* 换源 * * 1. 默认不使用该选项时,含义是 *全局* 换源, * * 全局分为 (1)系统级 (2)用户级 * * 大多数第三方配置软件往往默认进行的是 *用户级* 的配置。所以 chsrc 首先将尝试使用 *用户级* 配置 * * 2. 若不存在 *用户级* 的配置,chsrc 将采用 *系统级* 的配置 * * 3. 最终效果本质由第三方软件决定,如 poetry 默认实现的就是项目级的换源 * * 但是后来,我们认为非 -local 时的行为(即默认时)比较模糊,所以我们现在清晰地把作用域指明出来,总共有3种类型的作用: * 分别是 ProjectScope、UserScope 和 SystemScope,分别对应项目级、用户级和系统级的换源配置 * * 还有一个叫 ImplementationDefinedScope 的作用域,它不是一种新类型,而是表示根据实际情况决定的作用域。 * chsrc 将根据该 target 的实际情况来选择最合适的作用域来进行换源配置。最好的情况下,ImplementationDefinedScope 是三者之一, * 这也是这里设计的初衷。然而现在有些 recipe 的换源行为,会在某种 Scope 不能够成功时退而求其次地使用另一个 Scope * 来进行换源配置,这时就只能用 ImplementationDefinedScope 来表示。 */ #define NumberOfScopeType 3 typedef enum Scope_t { ProjectScope, UserScope, SystemScope, /** * 这是 target 默认的作用域,一种特殊的作用域,即根据 target 的实际情况来决定的。 * 它不是一种真正的类型,因为最终换源后,用户看到的作用域依然是 ProjectScope、UserScope 或 SystemScope 中的一个 */ ImplementationDefinedScope, } Scope_t; #define ScopeCap_Slot_Project 0 #define ScopeCap_Slot_User 1 #define ScopeCap_Slot_System 2 typedef enum ScopeCapability_t { ScopeCap_Unknown, /* 未知,缺乏对该 target 的细致了解 */ ScopeCap_Unable, /* 不支持该作用域 */ ScopeCap_Able_But_Not_Implemented, /* 支持但chsrc尚未实现 */ ScopeCap_Able_And_Implemented /* 支持且chsrc已经实现 */ } ScopeCapability_t; typedef enum Capability_t { CanNot, FullyCan, PartiallyCan } Capability_t; typedef struct Contributor_t { char *id; /* 全局唯一贡献者标识符,防止反复写信息,以 @ 开头 */ char *name; /* 贡献者姓名; 鉴于该项目完全依赖于贡献者,建议留下真实姓名或者昵称 */ char *email; char *display_name; /* recipe 结束时会显示贡献者信息,如果你不愿显示真实姓名或者昵称,可以另外提供一个名字 */ } Contributor_t; typedef struct Target_t { /* 以 / 为分隔符的多个目标别名 */ char *aliases; void (*getfn) (char *option); void (*setfn) (char *option); void (*resetfn) (char *option); /* 初始化函数,用于填充该 struct 的各种信息 */ void (*preludefn) (void); bool inited; /* 是否执行过了 preludefn() */ Source_t *sources; int sources_n; /* Features */ bool can_english; /* 是否支持英文输出 */ bool can_user_define; /* 是否支持用户自定义URL来换源 */ char *can_user_define_explain; /* 用户自定义URL的说明 */ /** * 各作用域的支持情况 * 参考 ScopeCap_Slot_Xxx 的值 */ ScopeCapability_t scope_caps[NumberOfScopeType]; Scope_t default_scope; /* 默认作用域 */ char *note; /* 备注 */ /* recipe 维护信息 */ char *created_on; char *last_updated; char *sources_last_updated; Contributor_t *chef; /* 该 recipe *当前*的总负责人 (可以任职也可以休职) */ Contributor_t **cooks; /* 该 recipe 的主要作者 */ int cooks_n; Contributor_t **sauciers; /* 该 recipe 的次要贡献者 (除主要作者外的其他人) */ int sauciers_n; } Target_t; #define def_target(t, aliases) void t##_getsrc(char *option);void t##_setsrc(char *option);void t##_resetsrc(char *option); Target_t t##_target={aliases}; #define chef_allow_gsr(t) this->getfn = t##_getsrc; this->setfn = t##_setsrc; this->resetfn = t##_resetsrc; #define chef_allow_s(t) this->getfn = NULL; this->setfn = t##_setsrc; this->resetfn = NULL; #define chef_allow_sr(t) this->getfn = NULL; this->setfn = t##_setsrc; this->resetfn = t##_resetsrc; #define chef_allow_gs(t) this->getfn = t##_getsrc; this->setfn = t##_setsrc; this->resetfn = NULL; #define chef_allow_NOOP(t) #define chef_prep_this(t,op) Target_t *this = &t##_target; this->inited = true; chef_allow_##op(t); #define chef_use_this(t) Target_t *this = &t##_target; #define chsrc_use_this_source(t) Target_t *this = &t##_target; Source_t source = chsrc_yield_source_and_confirm (this, option); /** * 用于定义换源列表 * * def_sources_begin() * {&UpstreamProvider, "换源URL", "精准测速链接"}, * {&某镜像站1, "换源URL", "精准测速链接"}, * {&某镜像站2, "换源URL", NULL }, // 若精准测速链接为空,则为模糊测速,默认使用该镜像站的整体测速链接 * def_sources_end() * * 出于代码美观考虑,上述第三列可以写 FeedByPrelude,然后下面使用文档 ./doc/11-如何设置换源链接与测速链接.md 中的函数来填充 */ #define def_sources_begin() Source_t sources[] = { #define def_sources_end() }; \ this->sources_n = xy_c_array_len(sources); \ char *_sources_storage = xy_malloc0 (sizeof(sources)); \ memcpy (_sources_storage, sources, sizeof(sources)); \ this->sources = (Source_t *)_sources_storage; ================================================ FILE: src/framework/version.h ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Name : version.h * File Authors : @ccmywish * | @Mikachu2333 * Contributors : Nil Null * | * Created On : <2025-10-10> * Last Modified : * * 发布新版本前请修改此文件 * ------------------------------------------------------------*/ #define Chsrc_Version "0.2.5-alpha1" // 以下四个宏仅用于 resource/chsrc.rc #define Chsrc_Version_Major 0 #define Chsrc_Version_Minor 2 #define Chsrc_Version_Patch 5 #define Chsrc_Version_Pre 0 #define Chsrc_Release_Date "2026/02/24" ================================================ FILE: src/rawstr4c.h ================================================ #pragma once /** * Generated by rawstr4c v1.1.0-2025/09/27 */ char RAWSTR_chsrc_USAGE_CHINESE[] = "\345\220\215\347\247\260\072\012\040\040\040\143\150\163\162\143\040\055\040\103\150\141\156\147\145\040\123\157\165\162\143\145\040\055\040\050\107\120\114\166\063\053\051\012\012\347\211\210\346\234\254\072\012\040\040\040\100\166\145\162\100\012\012\344\275\277\347\224\250\072\012\040\040\040\143\150\163\162\143\040\074\143\157\155\155\141\156\144\076\040\133\157\160\164\151\157\156\163\135\040\133\164\141\162\147\145\164\135\040\133\155\151\162\162\157\162\135\012\012\345\221\275\344\273\244\072\012\040\040\040\150\145\154\160\054\040\040\150\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\346\211\223\345\215\260\346\255\244\345\270\256\345\212\251\357\274\214\346\210\226\040\055\150\054\040\055\055\150\145\154\160\012\040\040\040\151\163\163\165\145\054\040\151\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\346\237\245\347\234\213\347\233\270\345\205\263\151\163\163\165\145\012\012\040\040\040\154\151\163\164\054\040\154\163\054\040\154\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\345\210\227\345\207\272\345\217\257\347\224\250\351\225\234\345\203\217\347\253\231\345\222\214\345\217\257\346\215\242\346\272\220\347\233\256\346\240\207\012\040\040\040\154\151\163\164\040\040\155\151\162\162\157\162\174\164\141\162\147\145\164\040\040\040\040\040\040\040\040\345\210\227\345\207\272\346\224\257\346\214\201\347\232\204\072\040\351\225\234\345\203\217\347\253\231\057\346\215\242\346\272\220\347\233\256\346\240\207\012\040\040\040\154\151\163\164\040\040\157\163\174\154\141\156\147\174\167\141\162\145\040\040\040\040\040\040\040\040\040\345\210\227\345\207\272\346\224\257\346\214\201\347\232\204\072\040\346\223\215\344\275\234\347\263\273\347\273\237\057\347\274\226\347\250\213\350\257\255\350\250\200\057\350\275\257\344\273\266\012\040\040\040\154\151\163\164\040\040\040\074\164\141\162\147\145\164\076\040\040\040\040\040\040\040\040\040\040\040\040\346\237\245\347\234\213\350\257\245\347\233\256\346\240\207\345\217\257\347\224\250\346\272\220\344\270\216\346\224\257\346\214\201\345\212\237\350\203\275\012\012\040\040\040\155\145\141\163\165\162\145\054\040\155\054\040\143\145\163\165\040\074\164\141\162\147\145\164\076\040\040\345\257\271\350\257\245\347\233\256\346\240\207\346\211\200\346\234\211\346\272\220\346\265\213\351\200\237\012\012\040\040\040\147\145\164\054\040\147\040\074\164\141\162\147\145\164\076\040\040\040\040\040\040\040\040\040\040\040\040\346\237\245\347\234\213\350\257\245\347\233\256\346\240\207\345\275\223\345\211\215\346\272\220\347\232\204\344\275\277\347\224\250\346\203\205\345\206\265\012\012\040\040\040\163\145\164\054\040\163\040\074\164\141\162\147\145\164\076\040\040\040\040\040\040\040\040\040\040\040\040\346\215\242\346\272\220\357\274\214\350\207\252\345\212\250\346\265\213\351\200\237\345\220\216\346\214\221\351\200\211\346\234\200\345\277\253\346\272\220\012\040\040\040\163\145\164\040\040\040\040\074\164\141\162\147\145\164\076\040\040\146\151\162\163\164\040\040\040\040\040\346\215\242\346\272\220\357\274\214\344\275\277\347\224\250\347\273\264\346\212\244\345\233\242\351\230\237\346\265\213\351\200\237\347\254\254\344\270\200\347\232\204\346\272\220\012\040\040\040\163\145\164\040\040\040\040\074\164\141\162\147\145\164\076\040\074\155\151\162\162\157\162\076\040\040\040\346\215\242\346\272\220\357\274\214\346\214\207\345\256\232\344\275\277\347\224\250\346\237\220\351\225\234\345\203\217\347\253\231\040\050\351\200\232\350\277\207\154\151\163\164\040\074\164\141\162\147\145\164\076\346\237\245\347\234\213\051\012\040\040\040\163\145\164\040\040\040\040\074\164\141\162\147\145\164\076\040\040\074\125\122\114\076\040\040\040\040\040\346\215\242\346\272\220\357\274\214\347\224\250\346\210\267\350\207\252\345\256\232\344\271\211\346\272\220\125\122\114\012\040\040\040\162\145\163\145\164\040\040\074\164\141\162\147\145\164\076\040\040\040\040\040\040\040\040\040\040\040\040\351\207\215\347\275\256\357\274\214\344\275\277\347\224\250\344\270\212\346\270\270\351\273\230\350\256\244\344\275\277\347\224\250\347\232\204\346\272\220\012\012\351\200\211\351\241\271\072\012\040\040\040\055\144\162\171\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\104\162\171\040\122\165\156\357\274\214\346\250\241\346\213\237\346\215\242\346\272\220\350\277\207\347\250\213\357\274\214\345\221\275\344\273\244\344\273\205\346\211\223\345\215\260\345\271\266\344\270\215\350\277\220\350\241\214\012\040\040\040\055\163\143\157\160\145\075\160\162\157\152\145\143\164\174\165\163\145\162\174\163\171\163\164\145\155\040\344\273\205\345\257\271\346\234\254\351\241\271\347\233\256\346\215\242\346\272\220\040\057\040\347\224\250\346\210\267\347\272\247\346\215\242\346\272\220\040\057\040\347\263\273\347\273\237\347\272\247\346\215\242\346\272\220\040\050\351\200\232\350\277\207\154\163\040\074\164\141\162\147\145\164\076\346\237\245\347\234\213\051\012\040\040\040\055\151\160\166\066\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\344\275\277\347\224\250\111\120\166\066\346\265\213\351\200\237\012\040\040\040\055\145\156\050\147\154\151\163\150\051\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\344\275\277\347\224\250\350\213\261\346\226\207\350\276\223\345\207\272\012\040\040\040\055\156\157\055\143\157\154\157\162\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\346\227\240\351\242\234\350\211\262\350\276\223\345\207\272\012\012\347\273\264\346\212\244\072\012\040\040\040\351\202\200\350\257\267\346\202\250\346\213\205\344\273\273\040\040\103\150\145\146\054\040\344\270\272\347\224\250\346\210\267\346\212\212\345\205\263\346\202\250\347\206\237\346\202\211\347\232\204\040\162\145\143\151\160\145\012\012\040\040\040\346\272\220\344\273\243\347\240\201\345\234\260\345\235\200\072\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\012\040\040\040\346\210\220\344\270\272\347\273\264\346\212\244\350\200\205\072\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\151\163\163\165\145\163\057\062\067\065"; char RAWSTR_chsrc_USAGE_ENGLISH[] = "\116\101\115\105\072\012\040\040\040\143\150\163\162\143\040\055\040\103\150\141\156\147\145\040\123\157\165\162\143\145\040\055\040\050\107\120\114\166\063\053\051\012\012\126\105\122\123\111\117\116\072\012\040\040\040\100\166\145\162\100\012\012\125\123\101\107\105\072\012\040\040\040\143\150\163\162\143\040\074\143\157\155\155\141\156\144\076\040\133\157\160\164\151\157\156\163\135\040\133\164\141\162\147\145\164\135\040\133\155\151\162\162\157\162\135\012\012\103\117\115\115\101\116\104\123\072\012\040\040\040\150\145\154\160\054\040\040\150\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\120\162\151\156\164\040\164\150\151\163\040\150\145\154\160\054\040\157\162\040\055\150\054\040\055\055\150\145\154\160\012\040\040\040\151\163\163\165\145\054\040\151\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\123\145\145\040\162\145\154\141\164\145\144\040\151\163\163\165\145\163\012\012\040\040\040\154\151\163\164\054\040\154\163\054\040\154\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\114\151\163\164\040\141\166\141\151\154\141\142\154\145\040\155\151\162\162\157\162\040\163\151\164\145\163\040\141\156\144\040\163\165\160\160\157\162\164\145\144\040\164\141\162\147\145\164\163\012\040\040\040\154\151\163\164\040\040\155\151\162\162\157\162\174\164\141\162\147\145\164\040\040\040\040\040\040\040\040\114\151\163\164\040\163\165\160\160\157\162\164\145\144\072\040\040\155\151\162\162\157\162\040\163\151\164\145\163\057\163\165\160\160\157\162\164\145\144\040\164\141\162\147\145\164\163\012\040\040\040\154\151\163\164\040\040\157\163\174\154\141\156\147\174\167\141\162\145\040\040\040\040\040\040\040\040\040\114\151\163\164\040\163\165\160\160\157\162\164\145\144\072\040\117\123\145\163\057\120\162\157\147\162\141\155\155\151\156\147\040\114\141\156\147\165\141\147\145\163\057\123\157\146\164\167\141\162\145\163\012\040\040\040\154\151\163\164\040\040\040\074\164\141\162\147\145\164\076\040\040\040\040\040\040\040\040\040\040\040\040\126\151\145\167\040\141\166\141\151\154\141\142\154\145\040\163\157\165\162\143\145\163\040\141\156\144\040\163\165\160\160\157\162\164\151\156\147\040\146\145\141\164\165\162\145\163\040\146\157\162\040\074\164\141\162\147\145\164\076\012\012\040\040\040\155\145\141\163\165\162\145\054\040\155\054\040\143\145\163\165\040\074\164\141\162\147\145\164\076\040\040\115\145\141\163\165\162\145\040\166\145\154\157\143\151\164\171\040\157\146\040\141\154\154\040\163\157\165\162\143\145\163\040\157\146\040\074\164\141\162\147\145\164\076\012\012\040\040\040\147\145\164\054\040\147\040\074\164\141\162\147\145\164\076\040\040\040\040\040\040\040\040\040\040\040\040\126\151\145\167\040\164\150\145\040\143\165\162\162\145\156\164\040\163\157\165\162\143\145\040\163\164\141\164\145\040\146\157\162\040\074\164\141\162\147\145\164\076\012\012\040\040\040\163\145\164\054\040\163\040\074\164\141\162\147\145\164\076\040\040\040\040\040\040\040\040\040\040\040\040\103\150\141\156\147\145\040\163\157\165\162\143\145\054\040\163\145\154\145\143\164\040\164\150\145\040\146\141\163\164\145\163\164\040\163\157\165\162\143\145\040\142\171\040\141\165\164\157\155\141\164\151\143\040\163\160\145\145\144\040\155\145\141\163\165\162\145\155\145\156\164\012\040\040\040\163\145\164\040\040\040\040\074\164\141\162\147\145\164\076\040\040\146\151\162\163\164\040\040\040\040\040\103\150\141\156\147\145\040\163\157\165\162\143\145\054\040\163\145\154\145\143\164\040\164\150\145\040\146\141\163\164\145\163\164\040\163\157\165\162\143\145\040\155\145\141\163\165\162\145\144\040\142\171\040\164\150\145\040\155\141\151\156\164\141\151\156\145\162\163\040\164\145\141\155\012\040\040\040\163\145\164\040\040\040\040\074\164\141\162\147\145\164\076\040\074\155\151\162\162\157\162\076\040\040\040\103\150\141\156\147\145\040\163\157\165\162\143\145\054\040\163\160\145\143\151\146\171\040\141\040\155\151\162\162\157\162\040\163\151\164\145\040\050\126\151\141\040\140\154\151\163\164\040\074\164\141\162\147\145\164\076\140\051\012\040\040\040\163\145\164\040\040\040\040\074\164\141\162\147\145\164\076\040\040\074\125\122\114\076\040\040\040\040\040\103\150\141\156\147\145\040\163\157\165\162\143\145\054\040\165\163\151\156\147\040\165\163\145\162\055\144\145\146\151\156\145\144\040\163\157\165\162\143\145\040\125\122\114\012\040\040\040\162\145\163\145\164\040\040\074\164\141\162\147\145\164\076\040\040\040\040\040\040\040\040\040\040\040\040\122\145\163\145\164\040\040\163\157\165\162\143\145\040\164\157\040\164\150\145\040\165\160\163\164\162\145\141\155\047\163\040\144\145\146\141\165\154\164\012\012\117\120\124\111\117\116\123\072\012\040\040\040\055\144\162\171\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\104\162\171\040\122\165\156\056\040\123\151\155\165\154\141\164\145\040\164\150\145\040\163\157\165\162\143\145\040\143\150\141\156\147\151\156\147\040\160\162\157\143\145\163\163\054\040\143\157\155\155\141\156\144\040\157\156\154\171\040\160\162\151\156\164\163\054\040\156\157\164\040\162\165\156\012\040\040\040\055\163\143\157\160\145\075\160\162\157\152\145\143\164\174\165\163\145\162\174\163\171\163\164\145\155\040\103\150\141\156\147\145\040\163\157\165\162\143\145\040\157\156\154\171\040\146\157\162\040\164\150\151\163\040\160\162\157\152\145\143\164\040\057\040\165\163\145\162\040\154\145\166\145\154\040\057\040\163\171\163\164\145\155\040\154\145\166\145\154\040\050\126\151\141\040\140\154\163\040\074\164\141\162\147\145\164\076\140\051\012\040\040\040\055\151\160\166\066\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\123\160\145\145\144\040\155\145\141\163\165\162\145\155\145\156\164\040\165\163\151\156\147\040\111\120\166\066\012\040\040\040\055\145\156\050\147\154\151\163\150\051\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\117\165\164\160\165\164\040\151\156\040\105\156\147\154\151\163\150\012\040\040\040\055\156\157\055\143\157\154\157\162\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\117\165\164\160\165\164\040\167\151\164\150\157\165\164\040\143\157\154\157\162\012\012\115\101\111\116\124\101\111\116\072\012\040\040\040\127\145\040\151\156\166\151\164\145\040\171\157\165\040\164\157\040\142\145\143\157\155\145\040\141\040\103\150\145\146\040\164\157\040\145\156\163\165\162\145\040\164\150\145\040\161\165\141\154\151\164\171\040\157\146\040\162\145\143\151\160\145\163\040\171\157\165\040\141\162\145\040\146\141\155\151\154\151\141\162\040\167\151\164\150\040\146\157\162\040\165\163\145\162\163\072\012\012\040\040\040\123\157\165\162\143\145\040\103\157\144\145\072\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\012\040\040\040\102\145\143\157\155\145\040\141\040\115\141\151\156\164\141\151\156\145\162\072\040\040\040\040\040\040\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\151\163\163\165\145\163\057\062\067\065"; char RAWSTR_chsrc_for_v_CHINESE[] = "\143\150\163\162\143\040\100\166\145\162\100\012\012\103\157\160\171\162\151\147\150\164\040\050\103\051\040\062\060\062\063\055\062\060\062\066\040\346\233\276\345\245\245\347\204\266\054\040\351\203\255\346\201\222\012\350\256\270\345\217\257\350\257\201\040\107\120\114\166\063\053\357\274\232\107\116\125\040\107\120\114\040\347\254\254\040\063\040\347\211\210\346\210\226\346\233\264\351\253\230\347\211\210\346\234\254\040\074\150\164\164\160\163\072\057\057\147\156\165\056\157\162\147\057\154\151\143\145\156\163\145\163\057\147\160\154\056\150\164\155\154\076\012\350\277\231\346\230\257\350\207\252\347\224\261\350\275\257\344\273\266\357\274\232\346\202\250\345\217\257\344\273\245\350\207\252\347\224\261\344\277\256\346\224\271\345\222\214\345\210\206\345\217\221\345\256\203\343\200\202\012\345\234\250\346\263\225\345\276\213\345\205\201\350\256\270\347\232\204\346\234\200\345\244\247\350\214\203\345\233\264\345\206\205\357\274\214\346\234\254\350\275\257\344\273\266\346\214\211\047\345\216\237\346\240\267\047\346\217\220\344\276\233\357\274\214\344\270\215\344\275\234\344\273\273\344\275\225\346\230\216\347\244\272\346\210\226\346\232\227\347\244\272\347\232\204\344\277\235\350\257\201\343\200\202\012\012\347\224\261\344\275\234\350\200\205\357\274\232\346\233\276\345\245\245\347\204\266\343\200\201\351\203\255\346\201\222\357\274\214\345\215\217\344\275\234\350\200\205\357\274\232\115\151\153\141\143\150\165\062\063\063\063\343\200\201\110\141\160\160\171\040\107\141\155\145\040\344\273\245\345\217\212\345\220\204\344\275\215\350\264\241\347\214\256\350\200\205\345\274\200\345\217\221\343\200\202\050\350\257\246\350\247\201\040\143\150\163\162\143\055\155\141\151\156\056\143\054\040\346\210\226\040\140\143\150\163\162\143\040\154\163\040\074\164\141\162\147\145\164\076\140\051"; char RAWSTR_chsrc_for_v_ENGLISH[] = "\143\150\163\162\143\040\100\166\145\162\100\012\012\103\157\160\171\162\151\147\150\164\040\050\103\051\040\062\060\062\063\055\062\060\062\066\040\101\157\162\141\156\040\132\145\156\147\054\040\110\145\156\147\040\107\165\157\012\114\151\143\145\156\163\145\040\107\120\114\166\063\053\072\040\107\116\125\040\107\120\114\040\166\145\162\163\151\157\156\040\063\040\157\162\040\154\141\164\145\162\040\074\150\164\164\160\163\072\057\057\147\156\165\056\157\162\147\057\154\151\143\145\156\163\145\163\057\147\160\154\056\150\164\155\154\076\012\124\150\151\163\040\151\163\040\146\162\145\145\040\163\157\146\164\167\141\162\145\072\040\171\157\165\040\141\162\145\040\146\162\145\145\040\164\157\040\143\150\141\156\147\145\040\141\156\144\040\162\145\144\151\163\164\162\151\142\165\164\145\040\151\164\056\012\124\150\145\162\145\040\151\163\040\116\117\040\127\101\122\122\101\116\124\131\054\040\164\157\040\164\150\145\040\145\170\164\145\156\164\040\160\145\162\155\151\164\164\145\144\040\142\171\040\154\141\167\056\012\012\127\162\151\164\164\145\156\040\142\171\040\141\165\164\150\157\162\163\072\040\101\157\162\141\156\040\132\145\156\147\054\040\110\145\156\147\040\107\165\157\054\040\143\157\154\154\141\142\157\162\141\164\157\162\163\072\040\115\151\153\141\143\150\165\062\063\063\063\054\040\110\141\160\160\171\040\107\141\155\145\054\040\141\156\144\040\143\157\156\164\162\151\142\165\164\157\162\163\056\040\050\123\145\145\040\143\150\163\162\143\055\155\141\151\156\056\143\054\040\157\162\040\140\143\150\163\162\143\040\154\163\040\074\164\141\162\147\145\164\076\140\051"; char RAWSTR_chsrc_for_issue[] = "\346\210\221\344\273\254\345\220\214\346\227\266\345\234\250\040\107\151\164\110\165\142\040\345\222\214\040\107\151\164\145\145\040\346\216\245\345\217\227\040\151\163\163\165\145\040\345\222\214\040\102\165\147\040\346\212\245\345\221\212\072\012\012\040\040\055\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\151\163\163\165\145\163\012\040\040\055\040\150\164\164\160\163\072\057\057\147\151\164\145\145\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\151\163\163\165\145\163\012\012\012\346\254\242\350\277\216\345\217\202\344\270\216\345\205\267\344\275\223\344\273\273\345\212\241\072\012\012\040\040\040\123\150\145\154\154\040\141\165\164\157\055\143\157\155\160\154\145\164\151\157\156\040\347\273\210\347\253\257\345\221\275\344\273\244\350\207\252\345\212\250\350\241\245\345\205\250\072\012\012\040\040\040\040\040\040\040\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\151\163\163\165\145\163\057\062\060\064\012\012\040\040\040\346\220\234\351\233\206\344\270\212\346\270\270\351\273\230\350\256\244\346\272\220\345\234\260\345\235\200\357\274\214\345\270\256\345\212\251\350\277\233\350\241\214\040\143\150\163\162\143\040\162\145\163\145\164\072\012\012\040\040\040\040\040\040\040\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\151\163\163\165\145\163\057\061\061\061\012\012\040\040\040\346\220\234\351\233\206\346\265\213\351\200\237\345\234\260\345\235\200\357\274\214\350\277\233\350\241\214\347\262\276\345\207\206\346\265\213\351\200\237\072\012\012\040\040\040\040\040\040\040\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\151\163\163\165\145\163\057\062\060\065\012\012\040\040\040\345\270\256\345\212\251\346\262\241\346\234\211\351\242\204\347\274\226\350\257\221\347\232\204\345\271\263\345\217\260\347\274\226\345\206\231\040\163\150\145\154\154\040\350\204\232\346\234\254\072\012\012\040\040\040\040\040\040\040\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\151\163\163\165\145\163\057\062\063\060\012\012\012\346\224\257\346\214\201\347\232\204\351\200\232\347\224\250\351\225\234\345\203\217\347\253\231\072\012\040\040\055\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\122\165\142\171\115\145\164\162\151\143\057\143\150\163\162\143\057\167\151\153\151\012"; char RAWSTR_chsrc_op_epilogue[] = "\012\040\040\040\052\040\347\262\276\345\207\206\346\265\213\351\200\237\072\040\350\203\275\347\234\237\345\256\236\345\217\215\346\230\240\344\275\240\346\234\252\346\235\245\344\275\277\347\224\250\350\257\245\350\265\204\346\272\220\346\227\266\347\232\204\351\200\237\345\272\246\357\274\214\345\233\240\344\270\272\345\256\203\347\233\264\346\216\245\346\265\213\351\207\217\344\275\240\345\205\263\346\263\250\347\232\204\351\202\243\344\270\252\350\265\204\346\272\220\343\200\202\012\040\040\040\052\040\346\250\241\347\263\212\346\265\213\351\200\237\072\040\344\273\205\344\273\243\350\241\250\350\257\245\351\225\234\345\203\217\347\253\231\346\217\220\344\276\233\346\234\215\345\212\241\347\232\204\344\270\200\344\270\252\345\217\257\350\203\275\351\200\237\345\272\246\343\200\202\345\233\240\350\200\214\345\217\257\350\203\275\344\274\232\345\207\272\347\216\260\346\265\213\351\200\237\346\225\260\345\200\274\350\276\203\351\253\230\357\274\214\344\275\206\345\256\236\351\231\205\344\275\277\347\224\250\344\275\223\351\252\214\344\270\215\344\275\263\347\232\204\347\216\260\350\261\241\343\200\202\012\345\275\223\344\275\240\351\201\207\345\210\260\346\250\241\347\263\212\346\265\213\351\200\237\346\227\266\357\274\214\350\257\267\345\260\275\345\217\257\350\203\275\345\220\221\346\210\221\344\273\254\346\217\220\344\272\244\345\207\206\347\241\256\347\232\204\346\265\213\351\200\237\351\223\276\346\216\245\072\040\143\150\163\162\143\040\151\163\163\165\145"; ================================================ FILE: src/rawstr4c.md ================================================ # [rawstr4c] input for chsrc `chsrc` 使用的 C标准 (最低要求) 是 `gnu11` (`c11` 的超集),这也就是说,我们项目是可以,而且是 **推荐** 同时混用 `R"()"` 和 `rawstr4c` 的 `LLVM` 对 `R"()"` 的支持是在 2024年07月 以后。但是在 GitHub Actions 中,所有出现的 `LLVM` 版本都太低了, 这使得我们被迫把已经写过的 `R"()"` 全部再转换为 `rawstr4c`. 用户端的编译器一般比较新,然而可能也没有新到如此的地步,通过使用 `rawstr4c` 我们也放宽了用户对编译器的要求。 我们预计等2~3年后,在项目中重新开始 `R"()"` 的写法
- prefix = `RAWSTR_chsrc` - output = `:global-variable-only-header` - translate = `:oct` - no-postfix = `true`
## 中文帮助 - name = `USAGE_CHINESE` ``` 名称: chsrc - Change Source - (GPLv3+) 版本: @ver@ 使用: chsrc [options] [target] [mirror] 命令: help, h 打印此帮助,或 -h, --help issue, i 查看相关issue list, ls, l 列出可用镜像站和可换源目标 list mirror|target 列出支持的: 镜像站/换源目标 list os|lang|ware 列出支持的: 操作系统/编程语言/软件 list 查看该目标可用源与支持功能 measure, m, cesu 对该目标所有源测速 get, g 查看该目标当前源的使用情况 set, s 换源,自动测速后挑选最快源 set first 换源,使用维护团队测速第一的源 set 换源,指定使用某镜像站 (通过list 查看) set 换源,用户自定义源URL reset 重置,使用上游默认使用的源 选项: -dry Dry Run,模拟换源过程,命令仅打印并不运行 -scope=project|user|system 仅对本项目换源 / 用户级换源 / 系统级换源 (通过ls 查看) -ipv6 使用IPv6测速 -en(glish) 使用英文输出 -no-color 无颜色输出 维护: 邀请您担任 Chef, 为用户把关您熟悉的 recipe 源代码地址: https://github.com/RubyMetric/chsrc 成为维护者: https://github.com/RubyMetric/chsrc/issues/275 ```
## 英文帮助 - name = `USAGE_ENGLISH` ``` NAME: chsrc - Change Source - (GPLv3+) VERSION: @ver@ USAGE: chsrc [options] [target] [mirror] COMMANDS: help, h Print this help, or -h, --help issue, i See related issues list, ls, l List available mirror sites and supported targets list mirror|target List supported: mirror sites/supported targets list os|lang|ware List supported: OSes/Programming Languages/Softwares list View available sources and supporting features for measure, m, cesu Measure velocity of all sources of get, g View the current source state for set, s Change source, select the fastest source by automatic speed measurement set first Change source, select the fastest source measured by the maintainers team set Change source, specify a mirror site (Via `list `) set Change source, using user-defined source URL reset Reset source to the upstream's default OPTIONS: -dry Dry Run. Simulate the source changing process, command only prints, not run -scope=project|user|system Change source only for this project / user level / system level (Via `ls `) -ipv6 Speed measurement using IPv6 -en(glish) Output in English -no-color Output without color MAINTAIN: We invite you to become a Chef to ensure the quality of recipes you are familiar with for users: Source Code: https://github.com/RubyMetric/chsrc Become a Maintainer: https://github.com/RubyMetric/chsrc/issues/275 ```
## for `chsrc -v` - name = `for__v_CHINESE` ``` chsrc @ver@ Copyright (C) 2023-2026 曾奥然, 郭恒 许可证 GPLv3+:GNU GPL 第 3 版或更高版本 这是自由软件:您可以自由修改和分发它。 在法律允许的最大范围内,本软件按'原样'提供,不作任何明示或暗示的保证。 由作者:曾奥然、郭恒,协作者:Mikachu2333、Happy Game 以及各位贡献者开发。(详见 chsrc-main.c, 或 `chsrc ls `) ```
## for `chsrc -v -en` - name = `for__v_ENGLISH` ``` chsrc @ver@ Copyright (C) 2023-2026 Aoran Zeng, Heng Guo License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by authors: Aoran Zeng, Heng Guo, collaborators: Mikachu2333, Happy Game, and contributors. (See chsrc-main.c, or `chsrc ls `) ```
## for `chsrc issue` - name = `for_issue` ``` 我们同时在 GitHub 和 Gitee 接受 issue 和 Bug 报告: - https://github.com/RubyMetric/chsrc/issues - https://gitee.com/RubyMetric/chsrc/issues 欢迎参与具体任务: Shell auto-completion 终端命令自动补全: https://github.com/RubyMetric/chsrc/issues/204 搜集上游默认源地址,帮助进行 chsrc reset: https://github.com/RubyMetric/chsrc/issues/111 搜集测速地址,进行精准测速: https://github.com/RubyMetric/chsrc/issues/205 帮助没有预编译的平台编写 shell 脚本: https://github.com/RubyMetric/chsrc/issues/230 支持的通用镜像站: - https://github.com/RubyMetric/chsrc/wiki ```
## 最后告诉用户一些维护信息 - name = `op_epilogue` ``` * 精准测速: 能真实反映你未来使用该资源时的速度,因为它直接测量你关注的那个资源。 * 模糊测速: 仅代表该镜像站提供服务的一个可能速度。因而可能会出现测速数值较高,但实际使用体验不佳的现象。 当你遇到模糊测速时,请尽可能向我们提交准确的测速链接: chsrc issue ```
[rawstr4c]: https://github.com/RubyMetric/rawstr4c ================================================ FILE: src/recipe/lang/Clojure.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_clojure, "clojure/clojars/cloj/lein"); void pl_clojure_prelude () { chef_prep_this (pl_clojure, s); chef_set_recipe_created_on (this, "2023-09-10"); chef_set_recipe_last_updated (this, "2026-02-24"); chef_set_sources_last_updated (this, "2025-08-21"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider,"https://repo.clojars.org/", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/clojars/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/clojars/", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/clojars/", DelegateToMirror}, {&Nyist, "https://mirror.nyist.edu.cn/clojars/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/clojars/", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/clojars/", DelegateToMirror} def_sources_end() } void pl_clojure_setsrc (char *option) { chsrc_use_this_source (pl_clojure); if (chsrc_in_project_scope_mode()) { chsrc_note2 ("请在项目根目录中的 project.clj 中手动添加 :mirrors 关键字:"); char *config = xy_str_gsub (RAWSTR_pl_clojure_project_clj, "@url@", source.url); println (config); } else { chsrc_note2 ("请在 ~/.lein/projfiles.clj 中手动添加 :mirrors 关键字:"); char *config = xy_str_gsub (RAWSTR_pl_clojure_projfiles_clj, "@url@", source.url); println (config); } chsrc_determine_chgtype (ChgType_Manual); chsrc_conclude (&source); } ================================================ FILE: src/recipe/lang/Dart/Flutter.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_dart_flutter, "flutter"); void pl_dart_flutter_prelude (void) { chef_prep_this (pl_dart_flutter, gsr); chef_set_recipe_created_on (this, "2023-09-10"); chef_set_recipe_last_updated (this, "2025-07-11"); chef_set_sources_last_updated (this, "2025-04-15"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 2, "@czyt", "@xrgzs"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://storage.googleapis.com", FeedByPrelude}, {&FlutterCN, "https://storage.flutter-io.cn", FeedByPrelude}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn", FeedByPrelude }, /* 官方文档也没有给后缀,怀疑是否存在问题 */ {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/flutter", FeedByPrelude}, {&Nju, "https://mirror.nju.edu.cn/flutter", FeedByPrelude} def_sources_end() chef_set_rest_smURL_with_postfix (this, "/flutter_infra_release/releases/stable/linux/flutter_linux_v1.0.0-stable.tar.xz"); } /** * chsrc get flutter */ void pl_dart_flutter_getsrc (char *option) { chsrc_view_env ("FLUTTER_STORAGE_BASE_URL", NULL); } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/flutter * @consult https://mirror.sjtu.edu.cn/docs/flutter_infra * @consult https://mirror.nju.edu.cn/mirrorz-help/flutter/?mirror=NJU */ void pl_dart_flutter_setsrc (char *option) { chsrc_use_this_source (pl_dart_flutter); char *w = NULL; char *cmd = NULL; if (xy.on_windows) { cmd = xy_strcat (3, "setx FLUTTER_STORAGE_BASE_URL \"", source.url, "\""); chsrc_run (cmd, RunOpt_No_Last_New_Line); } else { char *zshrc = xy_zshrc; char *bashrc = xy_bashrc; chsrc_backup (zshrc); w = xy_strcat (3, "export FLUTTER_STORAGE_BASE_URL=\"", source.url, "\"\n"); chsrc_append_to_file (w, zshrc); if (xy_file_exist (bashrc)) { chsrc_backup (bashrc); chsrc_append_to_file (w, bashrc); } } chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void pl_dart_flutter_resetsrc (char *option) { pl_dart_flutter_setsrc (option); } ================================================ FILE: src/recipe/lang/Dart/Pub.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_dart, "dart/pub"); void pl_dart_prelude (void) { chef_prep_this (pl_dart, gsr); chef_set_recipe_created_on (this, "2023-09-10"); chef_set_recipe_last_updated (this, "2025-07-11"); chef_set_sources_last_updated (this, "2025-04-15"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 2, "@czyt", "@xrgzs"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://pub.dev", FeedByPrelude}, {&FlutterCN, "https://pub.flutter-io.cn", FeedByPrelude}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/dart-pub", FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/dart-pub", FeedByPrelude}, {&Nju, "https://mirror.nju.edu.cn/dart-pub", FeedByPrelude} def_sources_end() chef_set_rest_smURL_with_postfix (this, "/packages/flutter_vision/versions/1.1.4.tar.gz"); } void pl_dart_getsrc (char *option) { chsrc_view_env ("PUB_HOSTED_URL", NULL); } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/dart-pub/ * @consult https://mirror.sjtu.edu.cn/docs/dart-pub * @consult https://mirror.nju.edu.cn/mirrorz-help/dart-pub/?mirror=NJU */ void pl_dart_setsrc (char *option) { chsrc_use_this_source (pl_dart); char *w = NULL; char *cmd = NULL; if (xy.on_windows) { cmd = xy_strcat (3, "setx PUB_HOSTED_URL \"", source.url, "\""); chsrc_run (cmd, RunOpt_No_Last_New_Line); } else { char *zshrc = xy_zshrc; char *bashrc = xy_bashrc; chsrc_backup (zshrc); w = xy_strcat (3, "export PUB_HOSTED_URL=\"", source.url, "\"\n"); chsrc_append_to_file (w, zshrc); if (xy_file_exist (bashrc)) { chsrc_backup (bashrc); chsrc_append_to_file (w, bashrc); } } chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void pl_dart_resetsrc (char *option) { pl_dart_setsrc (option); } ================================================ FILE: src/recipe/lang/Dart/common.h ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Authors : MadDogOwner * Contributors : Nil Null * | * Created On : <2025-04-15> * Major Revision : 1 * Last Modified : <2025-07-11> * ------------------------------------------------------------*/ static MirrorSite_t FlutterCN = { IS_DedicatedMirrorSite, "cfug", "CFUG", "Flutter 社区", "https://flutter.cn/", {NotSkip, NA, NA, "https://storage.flutter-io.cn/flutter_infra_release/releases/stable/linux/flutter_linux_v1.0.0-stable.tar.xz", ACCURATE} // 231 MB }; ================================================ FILE: src/recipe/lang/Go.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ static MirrorSite_t GoProxyCN = { IS_DedicatedMirrorSite, "goproxy.cn", "Goproxy.cn", "Goproxy.cn (七牛云)", "https://goproxy.cn/", {NotSkip, NA, NA, "https://goproxy.cn/github.com/aws/aws-sdk-go/@v/v1.45.2.zip", ACCURATE} // 30 MB }, GoProxyIO = { IS_DedicatedMirrorSite, "goproxy.io", "GOPROXY.IO", "GOPROXY.IO", "https://goproxy.io/", {NotSkip, NA, NA, "https://goproxy.io/github.com/aws/aws-sdk-go/@v/v1.45.2.zip", ACCURATE} // 30 MB }; def_target(pl_go, "go/golang/goproxy"); void pl_go_prelude () { chef_prep_this (pl_go, gsr); chef_set_recipe_created_on (this, "2023-08-30"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-07-12"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 2, "@czyt", "@techoc"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://proxy.golang.org", DelegateToUpstream}, {&GoProxyCN, "https://goproxy.cn", DelegateToMirror}, {&Ali, "https://mirrors.aliyun.com/goproxy/", DelegateToMirror}, // 暂时停用华为镜像源, 详见 https://github.com/RubyMetric/chsrc/issues/227 // {&Huawei, "https://mirrors.huaweicloud.com/goproxy/", DelegateToMirror}, {&GoProxyIO, "https://goproxy.io", DelegateToMirror} def_sources_end() } void pl_go_check_cmd () { char *check_cmd = xy_quiet_cmd ("go version"); bool exist = query_program_exist (check_cmd, "go", Noisy_When_Exist|Noisy_When_NonExist); if (!exist) { chsrc_error ("未找到 go 相关命令,请检查是否存在"); exit (Exit_UserCause); } } void pl_go_getsrc (char *option) { pl_go_check_cmd (); chsrc_run ("go env GOPROXY", RunOpt_Default); } /** * @consult https://goproxy.cn/ */ void pl_go_setsrc (char *option) { pl_go_check_cmd (); chsrc_use_this_source (pl_go); char *cmd = "go env -w GO111MODULE=on"; chsrc_run (cmd, RunOpt_Default); cmd = xy_strcat (3, "go env -w GOPROXY=", source.url, ",direct"); chsrc_run (cmd, RunOpt_Default); chsrc_conclude (&source); } void pl_go_resetsrc (char *option) { pl_go_setsrc (option); } ================================================ FILE: src/recipe/lang/Haskell.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_haskell, "haskell/cabal/stack/hackage"); void pl_haskell_prelude () { chef_prep_this (pl_haskell, s); chef_set_recipe_created_on (this, "2023-09-10"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-08-22"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://hackage.haskell.org", DelegateToUpstream}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/hackage", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/hackage", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/hackage", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/hackage", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/hackage", DelegateToMirror}, {&Nyist, "https://mirror.nyist.edu.cn/hackage", DelegateToMirror} def_sources_end() } /** * @consult https://help.mirrors.cernet.edu.cn/hackage/ */ void pl_haskell_setsrc (char *option) { chsrc_use_this_source (pl_haskell); char *content = xy_str_gsub (RAWSTR_pl_haskell_cabal_config, "@url@", source.url); char *config = NULL; if (xy.on_windows) { config = xy_normalize_path ("~/AppData/Roaming/cabal/config"); } else { config = "~/.cabal/config"; } chsrc_note2 (xy_strcat (3, "请向 ", config, " 中手动添加:")); println (content); config = xy_normalize_path ("~/.stack/config.yaml"); content = xy_str_gsub (RAWSTR_pl_haskell_stackage_yaml, "@url@", source.url); chsrc_note2 (xy_strcat (3, "请向 ", config, " 中手动添加:")); println (content); chsrc_conclude (&source); } ================================================ FILE: src/recipe/lang/Java.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_java, "java/maven/mvn/maven-daemon/mvnd/gradle"); void pl_java_prelude () { chef_prep_this (pl_java, gsr); chef_set_recipe_created_on (this, "2023-08-31"); chef_set_recipe_last_updated (this, "2025-08-27"); chef_set_sources_last_updated (this, "2024-12-18"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@BingChunMoLi", "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Able_And_Implemented); /** * TODO: 当前实现将首先尝试 SystemScope, 若失败则尝试 UserScope * 所以并不是真正意义上的某种 Scope,而是两者的叠加,后续考虑强制执行用户所选择的 Scope 以达到用户期待 */ chef_set_default_scope (this, ImplementationDefinedScope); chef_deny_english(this); chef_allow_user_define(this); // 阿里巴巴开源镜像站需要修改为此才能测速 // https://github.com/RubyMetric/chsrc/issues/238#issuecomment-3162367686 chef_set_user_agent ("Maven/3.9.11"); def_sources_begin() {&UpstreamProvider, "https://repo1.maven.org/maven2/", FeedByPrelude}, {&Ali, "https://maven.aliyun.com/repository/public/", FeedByPrelude}, {&Huawei, "https://mirrors.huaweicloud.com/repository/maven/", FeedByPrelude}, {&HuaweiCDN, "https://repo.huaweicloud.com/repository/maven/", FeedByPrelude}, {&Tencent, "https://mirrors.cloud.tencent.com/nexus/repository/maven-public/",FeedByPrelude}, // 网易的24小时更新一次 {&Netease, "http://mirrors.163.com/maven/repository/maven-public/", FeedByPrelude} def_sources_end() // 220MB chef_set_rest_smURL_with_postfix (this, "com/tencentcloudapi/tencentcloud-sdk-java/3.1.1033/tencentcloud-sdk-java-3.1.1033-javadoc.jar"); } void pl_java_check_cmd (bool *maven_exist, bool *gradle_exist, bool *maven_daemon_exist) { *maven_exist = chsrc_check_program ("mvn"); *gradle_exist = chsrc_check_program ("gradle"); *maven_daemon_exist = chsrc_check_program ("mvnd"); if (! *maven_exist && ! *gradle_exist && ! *maven_daemon_exist) { chsrc_error ("maven(maven-daemon) 与 gradle 命令均未找到,请检查是否存在其一"); exit (Exit_UserCause); } } bool pl_java_is_maven_home_line (const char *line) { if (xy_str_start_with (line, "Maven home:")) return true; else return false; } /** * @consult https://github.com/RubyMetric/chsrc/pull/268#issuecomment-3209071819 */ char * pl_java_find_maven_config () { char *output; int status = xy_run_get_stdout ("mvn -v", &output); if (0==status) { char *maven_home_line = xy_run_iter_lines ("mvn -v", 0, pl_java_is_maven_home_line); char *maven_home = xy_str_delete_prefix (maven_home_line, "Maven home: "); char *maven_config = xy_normalize_path (xy_2strcat (maven_home, "/conf/settings.xml")); return maven_config; } chsrc_alert2 ("未找到 maven home, 将使用用户配置文件"); return "~/.m2/settings.xml"; } char * pl_java_find_maven_daemon_config () { char *output; int status = xy_run_get_stdout ("mvnd -v", &output); if (0==status) { char *maven_home_line = xy_run_iter_lines ("mvnd -v", 0, pl_java_is_maven_home_line); char *maven_home = xy_str_delete_prefix (maven_home_line, "Maven home: "); char *maven_config = xy_normalize_path (xy_2strcat (maven_home, "/conf/settings.xml")); return maven_config; } chsrc_alert2 ("未找到 maven home, 将使用用户配置文件"); return "~/.m2/settings.xml"; } void pl_java_getsrc (char *option) { bool maven_exist, gradle_exist, maven_daemon_exist; pl_java_check_cmd (&maven_exist, &gradle_exist, &maven_daemon_exist); if (maven_exist) { char *maven_config = pl_java_find_maven_config (); chsrc_note2 (xy_2strcat ("请查看 ", maven_config)); } if (maven_daemon_exist) { char *maven_config = pl_java_find_maven_daemon_config (); chsrc_note2 (xy_2strcat ("请查看 ", maven_config)); } } /** * @consult https://developer.aliyun.com/mirror/maven */ void pl_java_setsrc (char *option) { bool maven_exist, gradle_exist, maven_daemon_exist; pl_java_check_cmd (&maven_exist, &gradle_exist, &maven_daemon_exist); chsrc_use_this_source(pl_java); if (maven_exist) { char *file = xy_str_gsub (RAWSTR_pl_java_maven_config, "@1@", source.mirror->code); file = xy_str_gsub (file, "@name@", source.mirror->name); file = xy_str_gsub (file, "@url@", source.url); char *maven_config = pl_java_find_maven_config (); chsrc_note2 (xy_strcat (3, "请在 maven 配置文件 ", maven_config, " 中添加:")); println (file); } if (maven_daemon_exist) { char *file = xy_str_gsub (RAWSTR_pl_java_maven_config, "@1@", source.mirror->code); file = xy_str_gsub (file, "@name@", source.mirror->name); file = xy_str_gsub (file, "@url@", source.url); char *maven_daemon_config = pl_java_find_maven_daemon_config (); chsrc_note2 (xy_strcat (3, "请在 maven daemon 配置文件 ", maven_daemon_config, " 中添加:")); println (file); } if (gradle_exist) { if (maven_exist) br(); char* file = xy_str_gsub (RAWSTR_pl_java_build_gradle, "@url@", source.url); chsrc_note2 ("请在 build.gradle 中添加:"); println (file); } chsrc_conclude (&source); } void pl_java_resetsrc (char *option) { pl_java_setsrc (option); } ================================================ FILE: src/recipe/lang/JavaScript/Bun.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_js_bun, "bun"); void pl_js_bun_prelude (void) { chef_prep_this (pl_js_bun, gsr); chef_set_recipe_created_on (this, "2024-09-29"); chef_set_recipe_last_updated (this, "2025-07-22"); chef_set_sources_last_updated (this, "2025-07-22"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@lontten"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_js_group_target); } /** * chsrc get bun */ void pl_js_bun_getsrc (char *option) { chsrc_view_file ("~/.bunfig.toml"); } /** * @consult https://bun.sh/docs/runtime/bunfig#global-vs-local * @consult https://github.com/RubyMetric/chsrc/issues/83 * @consult https://github.com/RubyMetric/chsrc/pull/90 * * chsrc set bun */ void pl_js_bun_setsrc (char *option) { // 用的是 npm Registry 的源 Source_t source = chsrc_yield_source_and_confirm (&pl_js_group_target, option); char *content = RAWSTR_pl_js_bun_config; content = xy_str_gsub (content, "@url@", source.url); if (chsrc_in_project_scope_mode()) { chsrc_note2 ("请手动写入以下内容到本项目根目录的 bunfig.toml 文件中"); } else { chsrc_note2 (xy_strcat (3, "请手动写入以下内容到 ", xy_normalize_path ("~/.bunfig.toml"), " 文件中")); } println (content); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } /** * chsrc reset bun */ void pl_js_bun_resetsrc (char *option) { pl_js_bun_setsrc (option); } ================================================ FILE: src/recipe/lang/JavaScript/JavaScript.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Authors : Aoran Zeng * Contributors : Mr. Will * | * Created On : <2023-08-30> * Major Revision : 2 * Last Modified : <2025-07-11> * ------------------------------------------------------------*/ void pl_js_check_cmd (bool *npm_exist, bool *yarn_exist, bool *pnpm_exist) { *npm_exist = chsrc_check_program ("npm"); *yarn_exist = chsrc_check_program ("yarn"); *pnpm_exist = chsrc_check_program ("pnpm"); if (!*npm_exist && !*yarn_exist && !*pnpm_exist) { char *msg = ENGLISH ? "No npm, yarn or pnpm command found, check if at least one is present" : "未找到 npm 或 yarn 或 pnpm 命令,请检查是否存在其一"; chsrc_error (msg); exit (Exit_UserCause); } } void pl_js_group_getsrc (char *option) { bool npm_exist, yarn_exist, pnpm_exist; pl_js_check_cmd (&npm_exist, &yarn_exist, &pnpm_exist); hr(); if (npm_exist) { pl_js_npm_getsrc (option); br(); } if (yarn_exist) { pl_js_yarn_getsrc (option); br(); } if (pnpm_exist) { pl_js_pnpm_getsrc (option); br(); } } void pl_js_group_setsrc (char *option) { { char *msg = ENGLISH ? "Three package managers will be replaced for you at the same time: " "npm, pnpm, yarn. If you need to change the source independently, " "please run independently `chsrc set `" : "将同时更换3个包管理器 npm, pnpm, Yarn 的源,若需要独立换源,请独立运行 chsrc set "; chsrc_alert2 (msg); } bool npm_exist, yarn_exist, pnpm_exist; pl_js_check_cmd (&npm_exist, &yarn_exist, &pnpm_exist); chsrc_set_target_group_mode (); chsrc_use_this_source (pl_js_group); if (npm_exist) { pl_js_npm_setsrc (option); br(); } if (yarn_exist) { pl_js_yarn_setsrc (option); br(); } if (pnpm_exist) { pl_js_pnpm_setsrc (option); } chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void pl_js_group_resetsrc (char *option) { pl_js_group_setsrc (option); } ================================================ FILE: src/recipe/lang/JavaScript/Yarn.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_js_yarn, "yarn"); void pl_js_yarn_prelude (void) { chef_prep_this (pl_js_yarn, gsr); chef_set_recipe_created_on (this, "2023-09-09"); chef_set_recipe_last_updated (this, "2025-07-11"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@MrWillCom"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_js_group_target); } static double pl_js_yarn_get_yarn_version () { char *ver = xy_run ("yarn --version", 0); double version = atof (ver); return version; } void pl_js_yarn_getsrc (char *option) { // 最后一个版本应该是 v1.22.22 if (pl_js_yarn_get_yarn_version () >= 2) // https://github.com/RubyMetric/chsrc/issues/53 // 从 Yarn V2 开始,使用新的配置名 chsrc_run ("yarn config get npmRegistryServer", RunOpt_No_Last_New_Line); else chsrc_run ("yarn config get registry", RunOpt_No_Last_New_Line); } /** * @consult https://github.com/RubyMetric/chsrc/issues/53 * @consult https://yarnpkg.com/cli/config/set */ void pl_js_yarn_setsrc (char *option) { Source_t source = chsrc_yield_source (&pl_js_group_target, option); if (chsrc_in_standalone_mode()) chsrc_confirm_source(&source); char *cmd = NULL; // 从 Yarn V2 开始,使用新的配置名 if (pl_js_yarn_get_yarn_version () >= 2) { if (chsrc_in_project_scope_mode()) // Yarn 默认情况下就是基于本项目换源 cmd = xy_2strcat ("yarn config set npmRegistryServer ", source.url); else cmd = xy_2strcat ("yarn config set npmRegistryServer --home ", source.url); chsrc_run (cmd, RunOpt_No_Last_New_Line); } else { if (chsrc_in_project_scope_mode()) { char *msg = ENGLISH ? "Yarn v1 doesn't support `-scope=project`. SKIP changing source!" : "Yarn v1 不支持项目级换源,跳过换源"; chsrc_error (msg); // 不能直接退出,因为 Leader target 不能就此结束 return; } // 不再阻止换源命令输出到终端,即不再调用 xy_quiet_cmd() cmd = xy_2strcat ("yarn config set registry ", source.url); chsrc_run (cmd, RunOpt_No_Last_New_Line); } if (chsrc_in_standalone_mode()) { chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } } void pl_js_yarn_resetsrc (char *option) { pl_js_yarn_setsrc (option); } ================================================ FILE: src/recipe/lang/JavaScript/common.h ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ static MirrorSite_t NpmMirror = { IS_DedicatedMirrorSite, "npmmirror", "npmmirror", "npmmirror (阿里云赞助)", "https://npmmirror.com/", {SKIP, NULL, NULL, NULL, ACCURATE} }; def_target(pl_js_group, "js/javascript/node/nodejs"); void pl_js_group_prelude (void) { chef_prep_this (pl_js_group, gsr); chef_set_recipe_created_on (this, "2023-09-09"); chef_set_recipe_last_updated (this, "2025-07-11"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, "@happy-game"); // 组换源的 leader target 应把所有 follower target 的贡献者都记录过来 chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 2, "@lontten", "@MrWillCom"); /* ProjectScope 支持 npm, yarn v2, pnpm, 不支持 yarn v1 */ chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://registry.npmjs.org/", FeedByPrelude}, /* @note 根据 pnpm 官网,有最后的斜线 */ {&NpmMirror, "https://registry.npmmirror.com", FeedByPrelude}, {&Huawei, "https://mirrors.huaweicloud.com/repository/npm/", FeedByPrelude}, {&Tencent, "https://mirrors.cloud.tencent.com/npm/", FeedByPrelude} def_sources_end() // 29MB 大小 chef_set_rest_smURL_with_postfix (this, "/@tensorflow/tfjs/-/tfjs-4.22.0.tgz"); } def_target(pl_js_nodejs_binary, "__internal_target_nodejs_binary__"); void pl_js_nodejs_binary_prelude (void) { chef_prep_this (pl_js_nodejs_binary, NOOP); chef_set_recipe_created_on (this, "2023-09-09"); chef_set_recipe_last_updated (this, "2025-08-22"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://nodejs.org/dist/", FeedByPrelude}, {&NpmMirror, "https://npmmirror.com/mirrors", FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/nodejs-release/",FeedByPrelude}, {&Bfsu, "https://mirrors.bfsu.edu.cn/nodejs-release/",FeedByPrelude}, {&Ustc, "https://mirrors.ustc.edu.cn/node/",FeedByPrelude}, {&Huawei, "https://mirrors.huaweicloud.com/nodejs/",FeedByPrelude}, {&Tencent, "https://mirrors.cloud.tencent.com/nodejs-release/", FeedByPrelude} def_sources_end() chef_set_rest_smURL_with_postfix (this, "/v23.4.0/node-v23.4.0-linux-x64.tar.xz"); } ================================================ FILE: src/recipe/lang/JavaScript/npm.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_js_npm, "npm"); void pl_js_npm_prelude (void) { chef_prep_this (pl_js_npm, gsr); chef_set_recipe_created_on (this, "2023-08-30"); chef_set_recipe_last_updated (this, "2025-07-11"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@MrWillCom"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_js_group_target); } void pl_js_npm_getsrc (char *option) { chsrc_run ("npm config get registry", RunOpt_No_Last_New_Line); } /** * @consult https://npmmirror.com/ */ void pl_js_npm_setsrc (char *option) { Source_t source = chsrc_yield_source (&pl_js_group_target, option); if (chsrc_in_standalone_mode()) chsrc_confirm_source(&source); char *cmd = NULL; if (chsrc_in_project_scope_mode()) cmd = xy_2strcat ("npm config --location project set registry ", source.url); else cmd = xy_2strcat ("npm config set registry ", source.url); chsrc_run (cmd, RunOpt_No_Last_New_Line); if (chsrc_in_standalone_mode()) { chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } } void pl_js_npm_resetsrc (char *option) { pl_js_npm_setsrc (option); } ================================================ FILE: src/recipe/lang/JavaScript/nvm.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_js_nvm, "nvm"); void pl_js_nvm_prelude (void) { chef_prep_this (pl_js_nvm, gsr); chef_set_recipe_created_on (this, "2024-09-23"); chef_set_recipe_last_updated (this, "2025-06-19"); chef_set_sources_last_updated (this, "2025-06-19"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_set_note (this, "nvm 不支持 Fish shell", "nvm does not support Fish"); chef_use_other_target_sources (this, &pl_js_nodejs_binary_target); } void pl_js_nvm_getsrc (char *option) { chsrc_view_env ("NVM_NODEJS_ORG_MIRROR", NULL); } /** * @consult https://github.com/nvm-sh/nvm?tab=readme-ov-file#use-a-mirror-of-node-binaries * @consult https://mirrors.tuna.tsinghua.edu.cn/help/nodejs-release/ * @issue https://github.com/RubyMetric/chsrc/issues/81 * * @note nvm 不支持 Fish */ void pl_js_nvm_setsrc (char *option) { Source_t source = chsrc_yield_source_and_confirm (&pl_js_nodejs_binary_target, option); char *w = xy_strcat (3, "export NVM_NODEJS_ORG_MIRROR=", source.url, "\n"); char *zshrc = xy_zshrc; char *bashrc = xy_bashrc; chsrc_append_to_file (w, bashrc); if (xy_file_exist (zshrc)) chsrc_append_to_file (w, zshrc); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void pl_js_nvm_resetsrc (char *option) { // pl_js_nvm_setsrc (ChgType_Reset); chsrc_error ("暂不支持对 nvm 重置"); exit (Exit_Unsupported); } ================================================ FILE: src/recipe/lang/JavaScript/pnpm.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_js_pnpm, "pnpm"); void pl_js_pnpm_prelude (void) { chef_prep_this (pl_js_pnpm, gsr); chef_set_recipe_created_on (this, "2024-04-18"); chef_set_recipe_last_updated (this, "2025-07-11"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_js_group_target); } void pl_js_pnpm_getsrc (char *option) { chsrc_run ("pnpm config get registry", RunOpt_No_Last_New_Line); } /** * @consult https://pnpm.io/feature-comparison * @consult https://pnpm.io/cli/config */ void pl_js_pnpm_setsrc (char *option) { Source_t source = chsrc_yield_source (&pl_js_group_target, option); if (chsrc_in_standalone_mode()) chsrc_confirm_source(&source); char *cmd = NULL; if (chsrc_in_project_scope_mode()) cmd = xy_2strcat ("pnpm config --location project set registry ", source.url); else cmd = xy_2strcat ("pnpm config -g set registry ", source.url); chsrc_run (cmd, RunOpt_No_Last_New_Line); if (chsrc_in_standalone_mode()) { chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } } void pl_js_pnpm_resetsrc (char *option) { pl_js_pnpm_setsrc (option); } ================================================ FILE: src/recipe/lang/Julia.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_julia, "julia"); void pl_julia_prelude () { chef_prep_this (pl_julia, gs); chef_set_recipe_created_on (this, "2023-08-31"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-08-22"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://pkg.julialang.org", DelegateToUpstream}, {&Pku, "https://mirrors.pku.edu.cn/julia", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/julia", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/julia", DelegateToMirror} def_sources_end() } #define PL_Julia_Config "~/.julia/config/startup.jl" /** * Julia的换源可以通过两种方式 * 1. 写入 startup.jl * 2. 使用环境变量 * * 我们采用第一种 */ void pl_julia_getsrc (char *option) { chsrc_view_file (PL_Julia_Config); } /** * @consult * 1. https://help.mirrors.cernet.edu.cn/julia/ * 2. https://docs.julialang.org/en/v1/manual/command-line-interface/#Startup-file */ void pl_julia_setsrc (char *option) { chsrc_use_this_source (pl_julia); char *w = xy_strcat (3, "ENV[\"JULIA_PKG_SERVER\"] = \"", source.url, "\""); chsrc_append_to_file (w, PL_Julia_Config); chsrc_conclude (&source); } ================================================ FILE: src/recipe/lang/Lua.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ static MirrorSite_t Api7 = { IS_DedicatedMirrorSite, "api7", "api7.ai", "深圳支流科技有限公司", "https://www.apiseven.com/", {SKIP, ToFill, ToFill, NULL, ROUGH} }; def_target(pl_lua, "lua/luarocks"); void pl_lua_prelude () { chef_prep_this (pl_lua, gs); chef_set_recipe_created_on (this, "2023-09-27"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-08-22"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://luarocks.org", DelegateToUpstream}, {&Api7, "https://luarocks.cn", DelegateToMirror} def_sources_end() } void pl_lua_getsrc (char *option) { chsrc_view_file ("~/.luarocks/config.lua"); chsrc_view_file ("~/.luarocks/upload_config.lua"); } /** * @consult https://luarocks.cn/ */ void pl_lua_setsrc (char *option) { chsrc_use_this_source (pl_lua); char *config = xy_strcat (3, "rocks_servers = {\n" " \"", source.url, "\"\n" "}"); chsrc_note2 ("请手动修改 ~/.luarocks/config.lua 文件 (用于下载):"); println (config); char *upload_config = xy_strcat (3, "key = \"\"\n" "server = \"", source.url, "\""); chsrc_note2 ("请手动修改 ~/.luarocks/upload_config.lua 文件 (用于上传):"); println (upload_config); chsrc_conclude (&source); } ================================================ FILE: src/recipe/lang/NuGet.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_nuget, "nuget/net/.net/dotnet"); void pl_nuget_prelude () { chef_prep_this (pl_nuget, gs); chef_set_recipe_created_on (this, "2023-09-10"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-04-18"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_deny_user_define(this); def_sources_begin() {&UpstreamProvider, "https://www.nuget.org/api/v3/", DelegateToUpstream}, {&Huawei, "https://mirrors.huaweicloud.com/repository/nuget/v3", DelegateToMirror} def_sources_end() } void pl_nuget_getsrc (char *option) { chsrc_error ("暂时无法查看NuGet源,若有需求,请提交issue"); } /** * 暂时未实现该换源功能,可参照 https://mirrors.huaweicloud.com/mirrorDetail/5ebf85de07b41baf6d0882ab?mirrorName=nuget&catalog=language */ void pl_nuget_setsrc (char *option) { chsrc_error ("暂时无法为NuGet换源,若有需求,请提交issue"); } ================================================ FILE: src/recipe/lang/OCaml.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_ocaml, "ocaml/opam"); void pl_ocaml_prelude () { chef_prep_this (pl_ocaml, gs); chef_set_recipe_created_on (this, "2023-09-15"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-07-14"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://opam.ocaml.org/", DelegateToUpstream}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/git/opam-repository.git", DelegateToMirror} def_sources_end() } void pl_ocaml_check_cmd () { chsrc_ensure_program ("opam"); } void pl_ocaml_getsrc (char *option) { pl_ocaml_check_cmd (); chsrc_run ("opam repo get-url default", RunOpt_Default); } /** * @consult https://mirrors.sjtug.sjtu.edu.cn/docs/git/opam-repository.git */ void pl_ocaml_setsrc (char *option) { pl_ocaml_check_cmd (); chsrc_use_this_source (pl_ocaml); char *cmd = xy_strcat (3, "opam repo set-url default ", source.url, " --all --set-default"); chsrc_run (cmd, RunOpt_Default); chsrc_alert2 ("如果是首次使用 opam ,请使用以下命令进行初始化"); println (xy_2strcat ("opam init default ", source.url)); chsrc_conclude (&source); } ================================================ FILE: src/recipe/lang/PHP.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_php, "php/composer"); void pl_php_prelude () { chef_prep_this (pl_php, gs); chef_set_recipe_created_on (this, "2023-08-30"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-08-24"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://packagist.org/", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/composer/", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com/composer/", DelegateToMirror}, // {&Tencent_Intra, "https://mirrors.tencentyun.com/composer/", DelegateToMirror}, {&Huawei, "https://mirrors.huaweicloud.com/repository/php/", DelegateToMirror} def_sources_end() } void pl_php_check_cmd () { chsrc_ensure_program ("composer"); } /** * 已在Windows上测试通过,待其他平台PHP用户确认 */ void pl_php_getsrc (char *option) { pl_php_check_cmd (); chsrc_run ("composer config -g repositories", RunOpt_Default); } /** * @consult https://developer.aliyun.com/composer */ void pl_php_setsrc (char *option) { pl_php_check_cmd (); chsrc_use_this_source (pl_php); char *where = " -g "; if (chsrc_in_project_scope_mode()) { where = " "; } char *cmd = xy_strcat (4, "composer config", where, "repo.packagist composer ", source.url); chsrc_run (cmd, RunOpt_Default); chsrc_conclude (&source); } ================================================ FILE: src/recipe/lang/Perl.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_perl, "perl/cpan"); void pl_perl_prelude () { chef_prep_this (pl_perl, gs); chef_set_recipe_created_on (this, "2023-09-31"); chef_set_recipe_last_updated (this, "2025-10-07"); chef_set_sources_last_updated (this, "2025-10-07"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 2, "@hezonglun", "@Mikachu2333"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_deny_english (this); chef_allow_user_define (this); def_sources_begin() {&UpstreamProvider, "https://www.cpan.org/", FeedByPrelude}, {&MirrorZ, "https://mirrors.cernet.edu.cn/CPAN/", FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/CPAN/", FeedByPrelude}, {&Bfsu, "https://mirrors.bfsu.edu.cn/CPAN/", FeedByPrelude}, {&Cqu, "https://mirrors.cqu.edu.cn/CPAN/", FeedByPrelude}, {&Bjtu, "https://mirror.bjtu.edu.cn/cpan/", FeedByPrelude}, {&Nju, "https://mirrors.nju.edu.cn/CPAN/", FeedByPrelude}, {&Nyist, "https://mirror.nyist.edu.cn/CPAN/", FeedByPrelude}, {&Ustc, "https://mirrors.ustc.edu.cn/CPAN/", FeedByPrelude}, {&Sjtug_Siyuan, "https://mirror.sjtu.edu.cn/cpan/", FeedByPrelude}, {&Iscas, "https://mirror.iscas.ac.cn/CPAN/", FeedByPrelude}, {&Zju, "https://mirrors.zju.edu.cn/CPAN/", FeedByPrelude}, {&Lzuoss, "https://mirrors.lzu.edu.cn/CPAN/", FeedByPrelude}, {&Hust, "https://mirrors.hust.edu.cn/CPAN/", FeedByPrelude}, {&Ali, "https://mirrors.aliyun.com/CPAN/", FeedByPrelude} def_sources_end() chef_set_rest_smURL_with_postfix (this, "authors/id/D/DB/DBAURAIN/Bio-MUST-Apps-FortyTwo-0.213470.tar.gz"); } void pl_perl_check_cmd () { chsrc_ensure_program ("perl"); } void pl_perl_getsrc (char *option) { pl_perl_check_cmd (); // @ccmywish: 注意,prettyprint 仅仅是一个内部实现,可能不稳定,如果需要更稳定的, // 可以使用 CPAN::Shell->o('conf', 'urllist'); // 另外,上述两种方法无论哪种,都要首先load() char *cmd = "perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->prettyprint('urllist')\" "; chsrc_run (cmd, RunOpt_Default); } /** * @consult https://help.mirrors.cernet.edu.cn/CPAN/ */ void pl_perl_setsrc (char *option) { chsrc_use_this_source (pl_perl); char *cmd = xy_strcat (3, "perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->edit('urllist', 'unshift', '", source.url, "'); CPAN::HandleConfig->commit()\""); chsrc_run (cmd, RunOpt_Default); chsrc_alert2 ("请使用 perl -v 以及 cpan -v,若 Perl >= v5.36 或 CPAN >= 2.29,请额外手动调用下面的命令"); p ("perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->edit('pushy_https', 0);; CPAN::HandleConfig->commit()\""); chsrc_conclude (&source); } ================================================ FILE: src/recipe/lang/Python/PDM.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_python_pdm, "pdm"); void pl_python_pdm_prelude (void) { chef_prep_this (pl_python_pdm, gsr); chef_set_recipe_created_on (this, "2024-06-05"); chef_set_recipe_last_updated (this, "2025-07-11"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_python_group_target); } void pl_python_pdm_getsrc (char *option) { // @HELP 不加 --global 时,会检测到本项目的配置吗? // 如果不行的话,就要分别改成 --global 和 --local char *cmd = "pdm config pypi.url"; chsrc_run (cmd, RunOpt_Default); } /** * @consult https://github.com/RubyMetric/chsrc/issues/19 */ void pl_python_pdm_setsrc (char *option) { Source_t source = chsrc_yield_source (&pl_python_group_target, option); if (chsrc_in_standalone_mode()) chsrc_confirm_source(&source); char *cmd = NULL; if (chsrc_in_project_scope_mode()) cmd = xy_2strcat ("pdm config --local pypi.url ", source.url); else cmd = xy_2strcat ("pdm config --global pypi.url ", source.url); chsrc_run (cmd, RunOpt_No_Last_New_Line); if (chsrc_in_standalone_mode()) { chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } } void pl_python_pdm_resetsrc (char *option) { pl_python_pdm_setsrc (option); } ================================================ FILE: src/recipe/lang/Python/Poetry.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_python_poetry, "poetry"); void pl_python_poetry_prelude (void) { chef_prep_this (pl_python_poetry, gsr); chef_set_recipe_created_on (this, "2024-08-08"); chef_set_recipe_last_updated (this, "2025-07-11"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); /* Poetry 仅支持项目级换源 */ chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Unable); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, ProjectScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_python_group_target); } void pl_python_poetry_getsrc (char *option) { chsrc_note2 ("poetry换源情况: 请查看本项目 pyproject.toml 中 [[tool.poetry.source]]"); } /** * @consult https://python-poetry.org/docs/repositories/#project-configuration */ void pl_python_poetry_setsrc (char *option) { Source_t source = chsrc_yield_source (&pl_python_group_target, option); if (chsrc_in_standalone_mode()) chsrc_confirm_source(&source); char *cmd = xy_2strcat ("poetry source add my_mirror ", source.url); chsrc_run (cmd, RunOpt_No_Last_New_Line); if (chsrc_in_standalone_mode()) { chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } } void pl_python_poetry_resetsrc (char *option) { pl_python_poetry_setsrc (option); } ================================================ FILE: src/recipe/lang/Python/Python.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Authors : @ccmywish * Contributors : @happy-game * | * Created On : <2023-09-03> * Last Modified : <2025-12-31> * ------------------------------------------------------------*/ void pl_python_group_getsrc (char *option) { bool pdm_exist = false, poetry_exist = false, uv_exist = false; pl_python_check_unofficial_pkger (&poetry_exist, &pdm_exist, &uv_exist); // 交给后面检查命令的存在性 pl_python_pip_getsrc (option); br(); if (poetry_exist) { pl_python_poetry_getsrc (option); br(); } if (pdm_exist) { pl_python_pdm_getsrc (option); } if (uv_exist) { pl_python_uv_getsrc (option); } } void pl_python_group_setsrc (char *option) { { char *msg = ENGLISH ? "Three package managers will be replaced for you at the same time: " "pip, Poetry, PDM. If you need to change the source independently, " "please run independently `chsrc set `" : "将同时更换4个包管理器 pip, Poetry, PDM, uv 的源,若需要独立换源,请独立运行 chsrc set "; chsrc_alert2 (msg); } bool pdm_exist = false, poetry_exist = false, uv_exist = false; pl_python_check_unofficial_pkger (&poetry_exist, &pdm_exist, &uv_exist); chsrc_set_target_group_mode (); chsrc_use_this_source (pl_python_group); // 交给后面检查命令的存在性 pl_python_pip_setsrc (option); br(); if (poetry_exist) { pl_python_poetry_setsrc (option); br(); } if (pdm_exist) { pl_python_pdm_setsrc (option); } if (uv_exist) { pl_python_uv_setsrc (option); } chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void pl_python_group_resetsrc (char *option) { pl_python_group_setsrc (option); } ================================================ FILE: src/recipe/lang/Python/Rye.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * 由于Rye已经有后继uv了,所以我们不把该管理器纳入Python group中 * ------------------------------------------------------------*/ def_target(pl_python_rye, "rye"); void pl_python_rye_prelude (void) { chef_prep_this (pl_python_rye, gsr); chef_set_recipe_created_on (this, "2024-12-06"); chef_set_recipe_last_updated (this, "2025-08-09"); chef_set_sources_last_updated (this, "2025-08-09"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_python_group_target); } char * pl_python_find_rye_config () { char *buf = xy_run ("rye config --show-path", 0); char *rye_config = xy_normalize_path (buf); return rye_config; } void pl_python_rye_getsrc (char *option) { char *rye_config = pl_python_find_rye_config (); chsrc_note2 (xy_strcat (3, "请查看 ", rye_config, " 配置文件中的 [[sources]] 节内容")); } /** * @consult https://github.com/RubyMetric/chsrc/issues/127 */ void pl_python_rye_setsrc (char *option) { /* 并不在 Python group 中,所以不考虑 target group 情况,仅使用 Python group 提供的源 */ Source_t source = chsrc_yield_source_and_confirm (&pl_python_group_target, option); const char *content = RAWSTR_pl_python_rye_config; content = xy_str_gsub (content, "@1@", source.mirror->abbr); content = xy_str_gsub (content, "@2@", source.url); char *rye_config = pl_python_find_rye_config (); chsrc_note2 (xy_strcat (3, "请在配置文件 ", rye_config, " 中添加:")); println (content); chsrc_determine_chgtype (ChgType_Manual); chsrc_conclude (&source); } void pl_python_rye_resetsrc (char *option) { pl_python_rye_setsrc (option); } ================================================ FILE: src/recipe/lang/Python/common.h ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ #include "rawstr4c.h" def_target(pl_python_group, "python/pypi/py"); /** * @note 测速链接的这个前缀是 ${host}/pipi/web/pacakges/56/e4.... * 下面有几个镜像站微调了这个路径,我们只要确认能找到 packages 目录就好 * * @note 2025-09-29 更新了测试的 pkg 链接,换用了一个 40M 的文件 * * @warning 2025-09-29 Sjtug 需要特殊处理 */ static char * pl_python_speed_url_constructor (const char *url, const char *user_data) { char *str = xy_str_delete_suffix (url, "/simple"); str = xy_2strcat (str, "/packages/fa/80/eb88edc2e2b11cd2dd2e56f1c80b5784d11d6e6b7f04a1145df64df40065/opencv_python-4.12.0.88-cp37-abi3-win_amd64.whl"); if (strstr (url, "mirror.sjtu.edu.cn")) // e.g. https://mirror.sjtu.edu.cn/pypi-packages/fa/80/eb88edc2e2b11cd2dd2e56f1c80b5784d11d6e6b7f04a1145df64df40065/opencv_python-4.12.0.88-cp37-abi3-win_amd64.whl str = xy_str_gsub (str, "pypi-packages/packages", "pypi-packages");// 针对 Sjtug return str; } void pl_python_group_prelude (void) { chef_prep_this (pl_python_group, gsr); chef_set_recipe_created_on (this, "2023-09-03"); chef_set_recipe_last_updated (this, "2025-12-31"); chef_set_sources_last_updated (this, "2025-09-30"); chef_set_chef (this, "@happy-game"); // 组换源的 leader target 应把所有 follower target 的贡献者都记录过来 chef_set_cooks (this, 2, "@ccmywish", "@happy-game"); chef_set_sauciers (this, 3, "@xyx1926885268", "@Kattos", "@Mikachu2333"); /* 部分包管理器支持 ProjectScope,但是为了让流程执行下去,我们这里都写 ScopeCap_Able_And_Implemented */ chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://pypi.org/simple", FeedByPrelude}, {&MirrorZ, "https://mirrors.cernet.edu.cn/pypi/web/simple", FeedByPrelude}, {&Bfsu, "https://mirrors.bfsu.edu.cn/pypi/web/simple", FeedByPrelude}, // 不要添加Zju,浙大的PyPI服务在校外访问会自动转向Tuna {&Lzuoss, "https://mirror.lzu.edu.cn/pypi/web/simple", FeedByPrelude}, // 2025-09-29 此源已停用 // @ref https://mirrors.jlu.edu.cn/_news/#2025-04-06-pypi-repo-down // {&Jlu, "https://mirrors.jlu.edu.cn/pypi/web/simple", FeedByPrelude}, {&Sjtug_Siyuan, "https://mirror.sjtu.edu.cn/pypi-packages", FeedByPrelude}, {&Tuna, "https://pypi.tuna.tsinghua.edu.cn/simple", FeedByPrelude}, {&Ali, "https://mirrors.aliyun.com/pypi/simple", FeedByPrelude}, {&Nju, "https://mirror.nju.edu.cn/pypi/web/simple", FeedByPrelude}, {&Pku, "https://mirrors.pku.edu.cn/pypi/web/simple", FeedByPrelude}, {&Tencent, "https://mirrors.cloud.tencent.com/pypi/simple", FeedByPrelude}, // {&Tencent_Intra, "https://mirrors.cloud.tencentyun.com/pypi/simple",FeedByPrelude} {&Huawei, "https://mirrors.huaweicloud.com/repository/pypi/simple",FeedByPrelude}, {&Hust, "https://mirrors.hust.edu.cn/pypi/web/simple",FeedByPrelude} /* 不启用原因:24小时更新一次 */ // {&Netease, "https://mirrors.163.com/.help/pypi.html", NULL} def_sources_end() chef_set_rest_smURL_with_func (this, pl_python_speed_url_constructor, NULL); } void pl_python_check_unofficial_pkger (bool *poetry_exist, bool *pdm_exist, bool *uv_exist) { *poetry_exist = chsrc_check_program ("poetry"); *pdm_exist = chsrc_check_program ("pdm"); *uv_exist = chsrc_check_program ("uv"); } /** * @param[out] prog_name 返回 Python 的可用名,如果不可用,则返回 NULL */ void pl_python_get_py_program_name (char **prog_name) { *prog_name = NULL; bool py_exist = false; /** * @issue https://gitee.com/RubyMetric/chsrc/issues/I9VZL2 * * 由于Python2和Python3的历史,目前(2024-06)许多python命令实际上仍然是python2 * 因此我们首先测试 python3 */ py_exist = chsrc_check_program ("python3"); if (py_exist) { *prog_name = "python3"; // https://github.com/RubyMetric/chsrc/issues/327 if (xy.on_windows) { int status = xy_run_get_status ("python3 --version"); if (status == 9009) { chsrc_error2 (CHINESE ? "用户环境中的 `python3` 命令,是微软商店的占位符,并非真正可用的 Python。请安装真正的 Python 后重试!" : "`python3` in your environment is a placeholder of Microsoft Store, not the real Python which can be used, please install the real Python and try again!"); exit (Exit_UserCause); } } } else { /** * 不要直接: * * $ python * * 这样调用 `python` 自己,而是使用 `python --version`,或者其他方式 * 因为直接执行 `python` 会使 Windows 弹出Microsoft Store */ py_exist = chsrc_check_program ("python"); if (py_exist) *prog_name = "python"; else { chsrc_error ("未找到 Python 相关命令,请检查是否存在"); exit (Exit_UserCause); } } } ================================================ FILE: src/recipe/lang/Python/pip.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_python_pip, "pip"); void pl_python_pip_prelude (void) { chef_prep_this (pl_python_pip, gsr); chef_set_recipe_created_on (this, "2023-09-03"); chef_set_recipe_last_updated (this, "2025-09-12"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@happy-game"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_python_group_target); } void pl_python_pip_getsrc (char *option) { char *py_prog_name = NULL; pl_python_get_py_program_name (&py_prog_name); char *cmd = xy_2strcat (py_prog_name, " -m pip config get global.index-url"); int status = chsrc_run_directly (cmd); if (0 == status) { // 执行成功时显示当前源 xy_noop (); } else { // 执行失败时显示默认源 if (ENGLISH) chsrc_note2 ("No source configured in pip, showing default upstream source:"); else chsrc_note2 ("pip 中未配置源,显示默认上游源:"); Source_t default_source = chsrc_yield_source (&pl_python_group_target, "upstream"); say (default_source.url); } } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ */ void pl_python_pip_setsrc (char *option) { // 对于不支持的情况,尽早结束 if (chsrc_in_project_scope_mode()) { char *msg = ENGLISH ? "pip doesn't support `-scope=project`. SKIP changing source!" : "pip 不支持项目级换源,跳过换源"; chsrc_error (msg); // 不能直接退出,因为 Leader target 不能就此结束 return; } Source_t source = chsrc_yield_source (&pl_python_group_target, option); if (chsrc_in_standalone_mode()) chsrc_confirm_source(&source); char *py_prog_name = NULL; pl_python_get_py_program_name (&py_prog_name); // 这里用的是 config --user,会写入用户目录(而不是项目目录) // https://github.com/RubyMetric/chsrc/issues/39 // 经测试,Windows上调用换源命令,会写入 C:\Users\RubyMetric\AppData\Roaming\pip\pip.ini char *cmd = xy_2strcat (py_prog_name, xy_2strcat (" -m pip config --user set global.index-url ", source.url)); chsrc_run (cmd, RunOpt_No_Last_New_Line); if (chsrc_in_standalone_mode()) { chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } } void pl_python_pip_resetsrc (char *option) { pl_python_pip_setsrc (option); } ================================================ FILE: src/recipe/lang/Python/rawstr4c.h ================================================ #pragma once /** * Generated by rawstr4c v1.1.0-2025/09/27 */ char RAWSTR_pl_python_uv_config_source_content[] = "\x5b\x5b\x69\x6e\x64\x65\x78\x5d\x5d\x0a\x75\x72\x6c\x20\x3d\x20\x22\x40\x75\x72\x6c\x40\x22\x0a\x64\x65\x66\x61\x75\x6c\x74\x20\x3d\x20\x74\x72\x75\x65\x0a"; char RAWSTR_pl_python_get_uv_config[] = "\x67\x72\x65\x70\x20\x2d\x41\x20\x32\x20\x27\x69\x6e\x64\x65\x78\x27\x20\x40\x66\x40\x20\x7c\x20\x73\x65\x64\x20\x2d\x6e\x20\x27\x73\x2f\x5e\x75\x72\x6c\x20\x3d\x20\x22\x5c\x28\x2e\x2a\x5c\x29\x22\x2f\x5c\x31\x2f\x70\x27"; char RAWSTR_pl_python_get_uv_config_on_windows[] = "\x24\x6c\x69\x6e\x65\x73\x20\x3d\x20\x47\x65\x74\x2d\x43\x6f\x6e\x74\x65\x6e\x74\x20\x40\x66\x40\x3b\x0a\x0a\x66\x6f\x72\x28\x24\x69\x6e\x64\x65\x78\x3d\x30\x3b\x20\x24\x69\x6e\x64\x65\x78\x20\x2d\x6c\x74\x20\x24\x6c\x69\x6e\x65\x73\x2e\x43\x6f\x75\x6e\x74\x3b\x20\x24\x69\x6e\x64\x65\x78\x2b\x2b\x29\x20\x7b\x0a\x20\x20\x69\x66\x28\x24\x6c\x69\x6e\x65\x73\x5b\x24\x69\x6e\x64\x65\x78\x5d\x20\x2d\x6d\x61\x74\x63\x68\x20\x27\x5e\x5c\x5b\x5c\x5b\x69\x6e\x64\x65\x78\x5c\x5d\x5c\x5d\x24\x27\x29\x20\x7b\x0a\x20\x20\x20\x20\x24\x6c\x69\x6e\x65\x73\x5b\x24\x69\x6e\x64\x65\x78\x2e\x2e\x28\x24\x69\x6e\x64\x65\x78\x2b\x33\x29\x5d\x0a\x20\x20\x7d\x0a\x7d"; char RAWSTR_pl_python_set_uv_config[] = "\x40\x73\x65\x64\x40\x20\x27\x2f\x5e\x5c\x5b\x5c\x5b\x69\x6e\x64\x65\x78\x5c\x5d\x5c\x5d\x24\x2f\x2c\x2f\x5e\x64\x65\x66\x61\x75\x6c\x74\x20\x3d\x20\x74\x72\x75\x65\x24\x2f\x7b\x73\x7c\x5e\x75\x72\x6c\x20\x3d\x20\x22\x2e\x2a\x22\x24\x7c\x75\x72\x6c\x20\x3d\x20\x22\x40\x75\x72\x6c\x40\x22\x7c\x3b\x7d\x27\x20\x40\x66\x40"; char RAWSTR_pl_python_set_uv_config_on_windows[] = "\x70\x6f\x77\x65\x72\x73\x68\x65\x6c\x6c\x20\x2d\x43\x6f\x6d\x6d\x61\x6e\x64\x20\x22\x24\x63\x6f\x6e\x74\x65\x6e\x74\x20\x3d\x20\x47\x65\x74\x2d\x43\x6f\x6e\x74\x65\x6e\x74\x20\x27\x40\x66\x40\x27\x3b\x20\x24\x63\x6f\x6e\x74\x65\x6e\x74\x20\x3d\x20\x24\x63\x6f\x6e\x74\x65\x6e\x74\x20\x2d\x72\x65\x70\x6c\x61\x63\x65\x20\x27\x5e\x75\x72\x6c\x20\x3d\x20\x5c\x22\x2e\x2a\x5c\x22\x24\x27\x2c\x20\x27\x75\x72\x6c\x20\x3d\x20\x5c\x22\x40\x75\x72\x6c\x40\x5c\x22\x27\x3b\x20\x24\x63\x6f\x6e\x74\x65\x6e\x74\x20\x7c\x20\x53\x65\x74\x2d\x43\x6f\x6e\x74\x65\x6e\x74\x20\x27\x40\x66\x40\x27\x22"; char RAWSTR_pl_python_test_uv_if_set_source[] = "\x67\x72\x65\x70\x20\x2d\x71\x20\x27\x5e\x5c\x5b\x5c\x5b\x69\x6e\x64\x65\x78\x5d\x5d\x24\x27\x20\x40\x66\x40"; char RAWSTR_pl_python_test_uv_if_set_source_on_windows[] = "\x70\x6f\x77\x65\x72\x73\x68\x65\x6c\x6c\x20\x2d\x43\x6f\x6d\x6d\x61\x6e\x64\x20\x22\x69\x66\x20\x28\x47\x65\x74\x2d\x43\x6f\x6e\x74\x65\x6e\x74\x20\x40\x66\x40\x20\x7c\x20\x53\x65\x6c\x65\x63\x74\x2d\x53\x74\x72\x69\x6e\x67\x20\x27\x5e\x5c\x5b\x5c\x5b\x69\x6e\x64\x65\x78\x5c\x5d\x5c\x5d\x24\x27\x29\x20\x7b\x20\x65\x78\x69\x74\x20\x30\x20\x7d\x20\x65\x6c\x73\x65\x20\x7b\x20\x65\x78\x69\x74\x20\x31\x20\x7d\x22"; char RAWSTR_pl_python_rye_config[] = "\x5b\x5b\x73\x6f\x75\x72\x63\x65\x73\x5d\x5d\x0a\x6e\x61\x6d\x65\x20\x3d\x20\x22\x40\x31\x40\x22\x0a\x75\x72\x6c\x20\x20\x3d\x20\x22\x40\x32\x40\x22"; ================================================ FILE: src/recipe/lang/Python/rawstr4c.md ================================================ # rawstr4c input for Python - prefix = `RAWSTR_pl_python` - output = `:global-variable-only-header` - translate = `:hex` - no-postfix = `true`
## uv ### uv config source content ```toml [[index]] url = "@url@" default = true ``` ### Get uv config ```sh grep -A 2 'index' @f@ | sed -n 's/^url = "\(.*\)"/\1/p' ``` ### Get uv config on Windows 为什么不用 `Select-String`,原因见: 还有另外一个好处:如果 `[[index]]` 配置被写入了多次,可以这样检查出来 我们倾向于使用脚本而非命令,因为内容较长,便于用户检查到底执行了什么内容。 ```powershell $lines = Get-Content @f@; for($index=0; $index -lt $lines.Count; $index++) { if($lines[$index] -match '^\[\[index\]\]$') { $lines[$index..($index+3)] } } ``` ### Set uv config ```sh @sed@ '/^\[\[index\]\]$/,/^default = true$/{s|^url = ".*"$|url = "@url@"|;}' @f@ ``` ### Set uv config on Windows ```powershell powershell -Command "$content = Get-Content '@f@'; $content = $content -replace '^url = \".*\"$', 'url = \"@url@\"'; $content | Set-Content '@f@'" ``` ### Test uv if set source ```sh grep -q '^\[\[index]]$' @f@ ``` ### Test uv if set source on Windows ```powershell powershell -Command "if (Get-Content @f@ | Select-String '^\[\[index\]\]$') { exit 0 } else { exit 1 }" ```
## Rye - name = `rye_config` ```toml [[sources]] name = "@1@" url = "@2@" ``` ================================================ FILE: src/recipe/lang/Python/uv.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_python_uv, "uv"); void pl_python_uv_prelude (void) { chef_prep_this (pl_python_uv, gsr); chef_set_recipe_created_on (this, "2024-12-11"); chef_set_recipe_last_updated (this, "2025-12-29"); chef_set_sources_last_updated (this, "2025-08-09"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@happy-game", "@MingriLingran"); chef_set_sauciers (this, 2, "@Kattos", "@ccmywish"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Able_But_Not_Implemented); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); chef_use_other_target_sources (this, &pl_python_group_target); } /** * chsrc get uv * * uv的配置优先级顺序如下(高到低): * 1. $workspaces/uv.toml * 2. $workspaces/pyproject.toml * 3. ~/.config/uv/uv.toml * 4. /etc/uv/uv.toml */ #define PL_Python_uv_ConfigFile "uv.toml" #define PL_Python_uv_Local_ConfigPath "./" #define PL_Python_uv_User_ConfigPath "~/.config/uv/" char * pl_python_find_uv_config (bool mkdir) { if (chsrc_in_project_scope_mode()) { return xy_2strcat (PL_Python_uv_Local_ConfigPath, PL_Python_uv_ConfigFile); } else { if (xy.on_windows) { /* config path on Windows */ char *appdata = getenv ("APPDATA"); if (!appdata) { chsrc_error2 ("未能获取 APPDATA 环境变量"); return NULL; } char *config_dir = xy_2strcat(appdata, "\\uv\\"); if (mkdir) { chsrc_ensure_dir (config_dir); } return xy_2strcat (config_dir, PL_Python_uv_ConfigFile); } else { /* config path on Linux or macOS */ if (mkdir) { chsrc_ensure_dir (PL_Python_uv_User_ConfigPath); } return xy_2strcat (PL_Python_uv_User_ConfigPath, PL_Python_uv_ConfigFile); } } } void pl_python_uv_getsrc (char *option) { char *uv_config = pl_python_find_uv_config (false); if (!chsrc_check_file (uv_config)) { chsrc_error2 ("未找到 uv 配置文件"); return; } /* 获取 [[index]] 配置项的 url */ if (xy.on_windows) { /* 在 Windows 上使用 PowerShell 替代 grep */ char *script = xy_str_gsub (RAWSTR_pl_python_get_uv_config_on_windows, "@f@", uv_config); chsrc_run_as_powershell_file (script); } else { /* 在类 Unix 系统上使用 grep */ char *cmd = xy_str_gsub (RAWSTR_pl_python_get_uv_config, "@f@", uv_config); chsrc_run (cmd, RunOpt_Default); } } /** * @consult https://docs.astral.sh/uv/configuration/files/ * https://github.com/RubyMetric/chsrc/issues/139 */ void pl_python_uv_setsrc (char *option) { chsrc_ensure_program ("uv"); Source_t source = chsrc_yield_source (&pl_python_group_target, option); if (chsrc_in_standalone_mode()) chsrc_confirm_source(&source); char *uv_config = pl_python_find_uv_config (true); if (NULL==uv_config) { chsrc_error2 ("无法获取 uv 配置文件路径"); return; } chsrc_backup (uv_config); const char *source_content = xy_str_gsub (RAWSTR_pl_python_uv_config_source_content, "@url@", source.url); if (!xy_file_exist (uv_config)) { /* 当 uv_config 不存在,直接写入文件 */ chsrc_append_to_file (source_content, uv_config); } else { /* 当 uv_config 存在,如果存在 [[index]] 则更新,否则追加到文件末尾 */ char *cmd = NULL; if (xy.on_windows) { /* 在 Windows 上使用 PowerShell 替代 grep */ cmd = xy_str_gsub (RAWSTR_pl_python_test_uv_if_set_source_on_windows, "@f@", uv_config); } else { cmd = xy_str_gsub (RAWSTR_pl_python_test_uv_if_set_source, "@f@", uv_config); } int status = xy_run_get_status (cmd); if (0==status) { if (xy.on_windows) { /* 在 Windows 上使用 PowerShell 替代 sed */ char *powershell_cmd_with_file = xy_str_gsub(RAWSTR_pl_python_set_uv_config_on_windows, "@f@", uv_config); char *powershell_cmd = xy_str_gsub(powershell_cmd_with_file, "@url@", source.url); chsrc_run (powershell_cmd, RunOpt_Default); } else { /* 非 Windows 系统使用 sed */ char *sed_cmd = NULL; #if defined(XY_Build_On_macOS) || defined(XY_Build_On_BSD) sed_cmd = "sed -i '' "; #else sed_cmd = "sed -i "; #endif char *update_config_cmd = xy_str_gsub (RAWSTR_pl_python_set_uv_config, "@sed@", sed_cmd); update_config_cmd = xy_str_gsub (update_config_cmd, "@f@", uv_config); update_config_cmd = xy_str_gsub (update_config_cmd, "@url@", source.url); chsrc_run (update_config_cmd, RunOpt_Default); } } else { chsrc_append_to_file (source_content, uv_config); } } if (chsrc_in_standalone_mode()) { chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } } void pl_python_uv_resetsrc (char *option) { pl_python_uv_setsrc (option); } ================================================ FILE: src/recipe/lang/R.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_r, "r/cran"); void pl_r_prelude () { chef_prep_this (pl_r, gs); chef_set_recipe_created_on (this, "2023-09-21"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-08-21"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unknown); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unknown); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); // 以下注释的,是不含有bioconductor的镜像站,我们在换cran的同时,也直接帮助用户换bioconductor def_sources_begin() {&UpstreamProvider, "https://cran.r-project.org/", DelegateToUpstream}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/cran/", DelegateToMirror}, // {&Ali, "https://mirrors.aliyun.com/CRAN/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/CRAN/", DelegateToMirror}, // {&Sustech, "https://mirrors.sustech.edu.cn/CRAN", DelegateToMirror}, // {&Bfsu, "https://mirrors.bfsu.edu.cn/CRAN/", DelegateToMirror}, // {&Bjtu, "https://mirror.bjtu.edu.cn/cran/", DelegateToMirror}, def_sources_end() } #define PL_R_Config_Windows "~/Documents/.Rprofile" #define PL_R_Config_POSIX "~/.Rprofile" void pl_r_getsrc (char *option) { /** * 或参考:https://zhuanlan.zhihu.com/p/585036231 * * options()$repos * options()$BioC_mirror */ if (xy.on_windows) { chsrc_view_file (PL_R_Config_Windows); } else { chsrc_view_file (PL_R_Config_POSIX); } } /** * @consult https://help.mirrors.cernet.edu.cn/CRAN/ */ void pl_r_setsrc (char *option) { chsrc_use_this_source (pl_r); char *bioconductor_url = xy_str_delete_suffix (xy_str_delete_suffix (source.url, "cran/"), "CRAN/"); bioconductor_url = xy_2strcat(bioconductor_url, "bioconductor"); const char *w1 = xy_strcat (3, "options(\"repos\" = c(CRAN=\"", source.url, "\"))\n" ); const char *w2 = xy_strcat (3, "options(BioC_mirror=\"", bioconductor_url, "\")\n" ); char *w = xy_2strcat (w1, w2); // 或者我们调用 r.exe --slave -e 上面的内容 char *config = xy.on_windows ? PL_R_Config_Windows : PL_R_Config_POSIX; chsrc_append_to_file (w, config); chsrc_conclude (&source); } ================================================ FILE: src/recipe/lang/Ruby/README.md ================================================ # Ruby 换源问题 https://github.com/ustclug/discussions/issues/438 - 清华、北京外国语: 不可用,原因是: 实现不足 https://github.com/tuna/issues/issues/374 - 腾讯: 不可用,原因是: 缺乏 `versions` 文件 - 阿里: 不可用,原因是: 缺乏 `versions` 文件 - 华为: 不可用,原因是:版本太过老旧,应该是没有同步 ================================================ FILE: src/recipe/lang/Ruby/Ruby.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ static MirrorSite_t RubyChina = { IS_DedicatedMirrorSite, "rubychina", "RubyChina", "Ruby China 社区", "https://gems.ruby-china.com/", {NotSkip, NA, NA, "https://gems.ruby-china.com/rubygems/gems/nokogiri-1.15.0-java.gem", ACCURATE} // 9.9 MB }; def_target(pl_ruby, "gem/ruby/rb/rubygem/rubygems/bundler"); void pl_ruby_prelude (void) { chef_prep_this (pl_ruby, gsr); chef_set_recipe_created_on (this, "2023-08-29"); chef_set_recipe_last_updated (this, "2025-08-11"); chef_set_sources_last_updated (this, "2024-12-18"); chef_set_chef (this, "@ccmywish"); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@BingChunMoLi"); /* 支持 bundler. 不支持 gem */ chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://rubygems.org/", "https://rubygems.org/gems/nokogiri-1.15.0-java.gem"}, {&RubyChina, "https://gems.ruby-china.com/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/rubygems/", DelegateToMirror} // {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/rubygems/", DelegateToMirror}, // {&Bfsu, "https://mirrors.bfsu.edu.cn/rubygems/", DelegateToMirror}, // {&Tencent, "https://mirrors.tencent.com/rubygems/", DelegateToMirror}, // {&Tencent_Intra, "https://mirrors.tencentyun.com/rubygems/", DelegateToMirror}, // {&Ali, "https://mirrors.aliyun.com/rubygems/", DelegateToMirror}, // {&Huawei, "https://mirrors.huaweicloud.com/repository/rubygems/", DelegateToMirror}, def_sources_end() } void pl_ruby_getsrc (char *option) { chsrc_run ("gem sources", RunOpt_Default); chsrc_run ("bundle config get mirror.https://rubygems.org", RunOpt_Default); } bool pl_ruby_remove_gem_source (const char *source) { char *cmd = NULL; if (hp_is_url (source)) { cmd = xy_2strcat ("gem sources -r ", source); chsrc_run (cmd, RunOpt_Default); } return false; } /** * @consult https://gitee.com/RubyMetric/rbenv-cn */ void pl_ruby_setsrc (char *option) { chsrc_ensure_program ("gem"); chsrc_use_this_source (pl_ruby); char *cmd = NULL; // step1 xy_run_iter_lines ("gem sources -l", 0, pl_ruby_remove_gem_source); cmd = xy_2strcat ("gem source -a ", source.url); chsrc_run (cmd, RunOpt_Default); // 我们在 step1 中,把源全部清空了,但是现在 RubyGems 的行为是: 当清空会自动给你把默认源给加回来 // 所以我们在这一步,最后一次删除默认源,确保它不存在 pl_ruby_remove_gem_source (this->sources[0].url); chsrc_ensure_program ("bundle"); char *where = " --global "; if (chsrc_in_project_scope_mode()) { where = " --local "; } cmd = xy_strcat (4, "bundle config", where, "'mirror.https://rubygems.org' ", source.url); chsrc_run (cmd, RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void pl_ruby_resetsrc (char *option) { pl_ruby_setsrc (option); } ================================================ FILE: src/recipe/lang/Rust/Cargo.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(pl_rust_cargo, "rust/cargo/crate/crates"); void pl_rust_cargo_prelude (void) { chef_prep_this (pl_rust_cargo, gsr); chef_set_recipe_created_on (this, "2023-08-30"); chef_set_recipe_last_updated (this, "2025-12-31"); chef_set_sources_last_updated (this, "2026-01-21"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@Mikachu2333", "@ccmywish"); chef_set_sauciers (this, 1, "@happy-game"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_But_Not_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english (this); chef_allow_user_define (this); // 以下都支持稀疏索引,我们换源时都将默认添加 `sparse+`。链接末尾的 `/` 不能缺少 /** * @warning 2025-12-29: * 许多镜像站的 dl 字段指向 static.crates.io,因此测速链接也指向 static.crates.io,原API失效 * 见: https://github.com/RubyMetric/chsrc/pull/330 */ #define url_postfix "api/v1/crates/windows/0.62.2/download" def_sources_begin() {&UpstreamProvider, "https://crates.io/", "https://static.crates.io/crates/windows/windows-0.62.2.crate"}, {&MirrorZ, "https://mirrors.cernet.edu.cn/crates.io-index/", NULL}, {&RsProxyCN, "https://rsproxy.cn/index/", "https://rsproxy.cn/" url_postfix}, {&Ali, "https://mirrors.aliyun.com/crates.io-index/", "https://mirrors.aliyun.com/crates/" url_postfix}, {&Zju, "https://mirrors.zju.edu.cn/crates.io-index/", NULL}, /* 注释原因: (2025-06-17) 镜像同步失败,多数包都不可用 */ // {&Nju, "https://mirror.nju.edu.cn/git/crates.io-index.git/", FeedByPrelude}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/crates.io-index/", "https://mirror.sjtu.edu.cn/crates.io/crates/windows/windows-0.58.0.crate"}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/", NULL}, {&Bfsu, "https://mirrors.bfsu.edu.cn/crates.io-index/", NULL}, {&Ustc, "https://mirrors.ustc.edu.cn/crates.io-index/", "https://crates-io.proxy.ustclug.org/" url_postfix}, /* 注释原因: (2025-06-17) 镜像同步失败,多数包都不可用 */ // {&Hust, "https://mirrors.hust.edu.cn/crates.io-index/", FeedByPrelude}, {&Cqu, "https://mirrors.cqu.edu.cn/crates.io-index/", NULL} def_sources_end() #undef url_postfix } void pl_rust_cargo_note_get_src_default () { if (ENGLISH) chsrc_note2 ("No source configured in Cargo, showing default upstream source:"); else chsrc_note2 ("Cargo 中未自定义源,显示默认源:"); Source_t default_source = chsrc_yield_source (&pl_rust_cargo_target, "upstream"); say (default_source.url); } void pl_rust_cargo_note_get_src_mirror (char *url, bool sparse) { chsrc_note2 (ENGLISH ? "Custom source found: " : "已找到自定义源:"); say (xy_2strcat (url, sparse ? " (sparse)" : "")); } void pl_rust_cargo_getsrc (char *option) { char *cargo_config_file = xy_normalize_path ("~/.cargo/config.toml"); char *raw_content = xy_file_read (cargo_config_file); char *formatted_content = xy_str_gsub (raw_content, " ", ""); formatted_content = xy_str_gsub (formatted_content, "'", "\""); XyStrFindResult_t result_has_mirror = xy_str_find (formatted_content, "replace-with"); if (result_has_mirror.found) { char *mirror_name = xy_str_next_nonempty_line (formatted_content + result_has_mirror.end + 1); mirror_name = xy_str_delete_prefix (mirror_name, "=\""); mirror_name = xy_str_delete_suffix (mirror_name, "\""); XyStrFindResult_t result_mirror = xy_str_find (formatted_content, xy_strcat (3, "[source.", mirror_name, "]")); if (!result_mirror.found) { pl_rust_cargo_note_get_src_default(); return; } char *mirror_url = xy_str_next_nonempty_line (formatted_content + result_mirror.end + 1); mirror_url = xy_str_delete_prefix (mirror_url, "registry=\""); mirror_url = xy_str_delete_suffix (mirror_url, "\""); if (xy_str_find (mirror_url, "sparse+").found) { pl_rust_cargo_note_get_src_mirror (xy_str_delete_prefix (mirror_url, "sparse+"), true); } } else { pl_rust_cargo_note_get_src_default(); } } void pl_write_rust_config (const char *path, const char *url) { remove (path); char *content = RAWSTR_pl_rust_cargo_config; content = xy_str_gsub (content, "@url@", url); chsrc_overwrite_file (content, path); } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/crates.io-index/ * @consult https://help.mirrors.cernet.edu.cn/crates.io-index */ void pl_rust_cargo_setsrc (char *option) { chsrc_ensure_program ("cargo"); chsrc_use_this_source (pl_rust_cargo); char *default_content = RAWSTR_pl_rust_cargo_config; char *cargo_config_dir = "~/.cargo/"; char *cargo_config_file = xy_2strcat (cargo_config_dir, "config.toml"); chsrc_ensure_dir (cargo_config_dir); cargo_config_file = xy_normalize_path (cargo_config_file); if (xy_file_exist (cargo_config_file)) { chsrc_backup (cargo_config_file); char *raw_content = xy_file_read (cargo_config_file); XyStrFindResult_t result_has_mirror = xy_str_find (raw_content, "replace-with"); if (!result_has_mirror.found) { pl_write_rust_config (cargo_config_file, source.url); goto finish; } char *mirror_name = xy_str_next_nonempty_line (raw_content + result_has_mirror.end + 1); mirror_name = xy_str_gsub (mirror_name, " ", ""); mirror_name = xy_str_gsub (mirror_name, "'", "\""); mirror_name = xy_str_delete_prefix (mirror_name, "=\""); mirror_name = xy_str_delete_suffix (mirror_name, "\""); XyStrFindResult_t result_mirror = xy_str_find (raw_content, xy_strcat (3, "[source.", mirror_name, "]")); if (!result_mirror.found) { pl_write_rust_config (cargo_config_file, source.url); goto finish; } char *mirror_url = xy_str_next_nonempty_line (raw_content + result_mirror.end + 1); mirror_url = xy_str_gsub (mirror_url, " ", ""); if (!xy_str_find (mirror_url, "registry").found) { pl_write_rust_config (cargo_config_file, source.url); goto finish; } mirror_url = xy_str_delete_prefix (mirror_url, "registry=\""); mirror_url = xy_str_delete_suffix (mirror_url, "\""); char *final_content = xy_str_gsub (raw_content, mirror_url, xy_2strcat ("sparse+", source.url)); chsrc_overwrite_file (final_content, cargo_config_file); goto finish; } pl_write_rust_config (cargo_config_file, source.url); goto finish; finish: chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void pl_rust_cargo_resetsrc (char *option) { pl_rust_cargo_setsrc (option); } ================================================ FILE: src/recipe/lang/Rust/common.h ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Authors : Aoran Zeng * Contributors : Nil Null * Created On : <2024-10-02> * Last Modified : <2025-07-11> * ------------------------------------------------------------*/ static MirrorSite_t RsProxyCN = { IS_DedicatedMirrorSite, "rsproxycn", "RsProxy.cn", "字节跳动基础架构Dev Infra", "https://rsproxy.cn/", {NotSkip, NA, NA, "https://rsproxy.cn/api/v1/crates/windows/0.58.0/download", ACCURATE} }; ================================================ FILE: src/recipe/lang/Rust/rustup.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ // Size: 20MB #define PL_Rustup_Speed_URL_Suffix def_target(pl_rust_rustup, "rustup"); void pl_rust_rustup_prelude (void) { chef_prep_this (pl_rust_rustup, gsr); chef_set_recipe_created_on (this, "2024-10-02"); chef_set_recipe_last_updated (this, "2025-08-07"); chef_set_sources_last_updated (this, "2025-08-07"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 2, "@Yangmoooo", "@Mikachu2333"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english (this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://static.rust-lang.org", FeedByPrelude}, {&MirrorZ, "https://mirrors.cernet.edu.cn/rustup", FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/rustup", FeedByPrelude}, {&Ustc, "https://mirrors.ustc.edu.cn/rust-static", FeedByPrelude}, {&Nju, "https://mirror.nju.edu.cn/rustup", FeedByPrelude}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/rust-static", FeedByPrelude}, {&Zju, "https://mirrors.zju.edu.cn/rustup", FeedByPrelude}, {&Iscas, "https://mirror.iscas.ac.cn/rustup", FeedByPrelude}, {&Ali, "https://mirrors.aliyun.com/rustup", FeedByPrelude}, {&RsProxyCN, "https://rsproxy.cn", FeedByPrelude} def_sources_end() // 20MB大小 chef_set_rest_smURL_with_postfix (this, "/dist/2025-06-26/cargo-1.88.0-x86_64-unknown-illumos.tar.gz"); } void pl_rust_rustup_getsrc (char *option) { chsrc_view_env ("RUSTUP_UPDATE_ROOT", "RUSTUP_DIST_SERVER", NULL); } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/rustup/ */ void pl_rust_rustup_setsrc (char *option) { chsrc_use_this_source (pl_rust_rustup); #ifdef XY_Build_On_Windows char *cmd1 = xy_strcat (3, "setx RUSTUP_DIST_SERVER \"", source.url, "\""); char *cmd2 = xy_strcat (3, "setx RUSTUP_UPDATE_ROOT \"", source.url, "/rustup\""); char *cmd = xy_strcat (3, cmd1, " & ", cmd2); chsrc_run (cmd, RunOpt_Dont_Notify_On_Success|RunOpt_No_Last_New_Line); #else char *w1 = xy_strcat (3, "export RUSTUP_DIST_SERVER=\"", source.url, "\"\n"); char *w2 = xy_strcat (3, "export RUSTUP_UPDATE_ROOT=\"", source.url, "/rustup\"\n"); char *w = xy_2strcat (w1, w2); char *bashrc = xy_bashrc; if (xy_file_exist (bashrc)) { chsrc_backup (bashrc); chsrc_append_to_file (w, bashrc); } char *zshrc = xy_zshrc; if (xy_file_exist (zshrc)) { chsrc_backup (zshrc); chsrc_append_to_file (w, zshrc); } char *fishrc = xy_fishrc; if (xy_file_exist (fishrc)) { char *w1 = xy_strcat (3, "set -x RUSTUP_DIST_SERVER ", source.url, "\n"); char *w2 = xy_strcat (3, "set -x RUSTUP_UPDATE_ROOT ", source.url, "/rustup\n"); char *w = xy_2strcat (w1, w2); chsrc_backup (fishrc); chsrc_append_to_file (w, fishrc); } #endif chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); chsrc_alert2 ("请重启终端使rustup环境变量生效"); } void pl_rust_rustup_resetsrc (char *option) { pl_rust_rustup_setsrc (option); } ================================================ FILE: src/recipe/lang/rawstr4c.h ================================================ #pragma once /** * Generated by rawstr4c v1.0.0-2025/08/09 */ char RAWSTR_pl_js_bun_config[] = "\133\151\156\163\164\141\154\154\135\012\162\145\147\151\163\164\162\171\040\075\040\042\100\165\162\154\100\042"; char RAWSTR_pl_java_maven_config[] = "\074\155\151\162\162\157\162\076\012\040\040\040\040\074\151\144\076\100\061\100\074\057\151\144\076\012\040\040\040\040\074\155\151\162\162\157\162\117\146\076\052\074\057\155\151\162\162\157\162\117\146\076\012\040\040\040\040\074\156\141\155\145\076\100\156\141\155\145\100\074\057\156\141\155\145\076\012\040\040\040\040\074\165\162\154\076\100\165\162\154\100\074\057\165\162\154\076\012\074\057\155\151\162\162\157\162\076"; char RAWSTR_pl_java_build_gradle[] = "\141\154\154\160\162\157\152\145\143\164\163\040\173\012\040\040\040\040\162\145\160\157\163\151\164\157\162\151\145\163\040\173\012\040\040\040\040\040\040\040\040\155\141\166\145\156\040\173\040\165\162\154\040\047\100\165\162\154\100\047\040\175\012\040\040\040\040\040\040\040\040\155\141\166\145\156\114\157\143\141\154\050\051\012\040\040\040\040\040\040\040\040\155\141\166\145\156\103\145\156\164\162\141\154\050\051\012\040\040\040\040\175\012\175"; char RAWSTR_pl_rust_cargo_config[] = "\133\163\157\165\162\143\145\056\143\162\141\164\145\163\055\151\157\135\012\162\145\160\154\141\143\145\055\167\151\164\150\040\075\040\042\155\151\162\162\157\162\042\012\012\133\163\157\165\162\143\145\056\155\151\162\162\157\162\135\012\162\145\147\151\163\164\162\171\040\075\040\042\163\160\141\162\163\145\053\100\165\162\154\100\042\012"; char RAWSTR_pl_haskell_cabal_config[] = "\162\145\160\157\163\151\164\157\162\171\040\155\151\162\162\157\162\012\040\040\165\162\154\072\040\100\165\162\154\100\012\040\040\163\145\143\165\162\145\072\040\124\162\165\145"; char RAWSTR_pl_haskell_stackage_yaml[] = "\160\141\143\153\141\147\145\055\151\156\144\145\170\072\012\040\040\144\157\167\156\154\157\141\144\055\160\162\145\146\151\170\072\040\100\165\162\154\100\012\040\040\150\141\143\153\141\147\145\055\163\145\143\165\162\151\164\171\072\012\040\040\040\040\153\145\171\151\144\163\072\012\040\040\040\040\040\040\055\040\060\141\065\143\067\145\141\064\067\143\144\061\142\061\065\146\060\061\146\065\146\065\061\141\063\063\141\144\144\141\067\145\066\065\065\142\143\060\146\060\142\060\066\061\065\142\141\141\070\145\062\067\061\146\064\143\063\063\065\061\145\062\061\144\012\040\040\040\040\040\040\055\040\061\145\141\071\142\141\063\062\143\065\062\066\144\061\143\143\071\061\141\142\065\145\065\142\144\063\066\064\145\143\065\145\071\145\070\143\142\066\067\061\067\071\141\064\067\061\070\067\062\146\066\145\062\066\146\060\141\145\067\067\063\144\064\062\012\040\040\040\040\040\040\055\040\062\070\060\142\061\060\061\065\063\141\065\062\062\066\070\061\061\066\063\066\065\070\143\142\064\071\146\066\063\062\143\144\145\063\146\063\070\144\067\066\070\142\067\063\066\144\144\142\143\071\060\061\144\071\071\141\061\141\067\067\062\070\063\063\012\040\040\040\040\040\040\055\040\062\141\071\066\142\061\070\070\071\144\143\062\062\061\143\061\067\062\071\066\146\143\143\062\142\142\063\064\142\071\060\070\143\141\071\067\063\064\063\067\066\146\060\146\063\066\061\066\066\060\062\060\060\071\063\065\071\061\066\145\146\062\060\061\012\040\040\040\040\040\040\055\040\062\143\066\143\063\066\062\067\142\144\066\143\071\070\062\071\071\060\062\063\071\064\070\067\146\061\141\142\144\060\062\145\060\070\141\060\062\145\066\143\146\061\066\145\144\142\061\060\065\141\070\060\061\062\144\064\064\064\144\070\067\060\143\063\012\040\040\040\040\040\040\055\040\065\061\146\060\061\066\061\142\071\060\066\060\061\061\142\065\062\143\066\066\061\063\063\067\066\142\061\141\145\071\063\067\066\067\060\144\141\066\071\063\062\062\061\061\063\141\062\064\066\141\060\071\146\070\060\067\143\066\062\146\066\071\062\061\012\040\040\040\040\040\040\055\040\067\067\062\145\071\146\064\143\067\144\142\063\063\144\062\065\061\144\065\143\066\145\063\065\067\061\071\071\143\070\061\071\145\065\066\071\144\061\063\060\070\065\067\144\143\062\062\065\065\064\071\142\064\060\070\064\065\146\146\060\070\071\060\144\012\040\040\040\040\040\040\055\040\141\141\063\061\065\062\070\066\145\066\141\144\062\070\061\141\144\066\061\061\070\062\062\063\065\065\063\063\143\064\061\145\070\060\066\145\065\141\067\070\067\145\060\142\066\144\061\145\067\145\145\146\063\146\060\071\144\061\063\067\144\062\145\071\012\040\040\040\040\040\040\055\040\146\145\063\063\061\065\060\062\066\060\066\070\060\062\146\145\141\143\061\065\145\065\061\064\144\071\142\071\145\141\070\063\146\145\145\070\142\066\146\146\145\146\067\061\063\063\065\064\067\071\141\062\145\066\070\144\070\064\141\144\143\066\142\060\012\040\040\040\040\153\145\171\055\164\150\162\145\163\150\157\154\144\072\040\063\040\043\040\156\165\155\142\145\162\040\157\146\040\153\145\171\163\040\162\145\161\165\151\162\145\144\012\040\040\040\040\043\040\151\147\156\157\162\145\040\145\170\160\151\162\141\164\151\157\156\040\144\141\164\145\054\040\163\145\145\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\143\157\155\155\145\162\143\151\141\154\150\141\163\153\145\154\154\057\163\164\141\143\153\057\160\165\154\154\057\064\066\061\064\012\040\040\040\040\151\147\156\157\162\145\055\145\170\160\151\162\171\072\040\156\157"; char RAWSTR_pl_clojure_project_clj[] = "\050\144\145\146\160\162\157\152\145\143\164\040\155\171\141\160\160\040\042\061\056\060\056\060\042\012\040\040\040\072\144\145\163\143\162\151\160\164\151\157\156\040\042\115\171\040\101\160\160\154\151\143\141\164\151\157\156\042\012\040\040\040\072\144\145\160\145\156\144\145\156\143\151\145\163\040\133\133\154\151\142\061\040\042\061\056\060\056\060\042\135\012\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\133\154\151\142\062\040\042\062\056\060\056\060\042\135\012\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\133\154\151\142\063\040\042\063\056\060\056\060\042\135\135\012\040\040\040\072\155\151\162\162\157\162\163\040\173\042\143\154\157\152\141\162\163\042\040\173\072\156\141\155\145\040\042\155\151\162\162\157\162\042\012\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\040\072\165\162\154\040\042\100\165\162\154\100\042\175\175\012\040\040\040\072\155\141\151\156\040\154\145\151\156\151\156\147\145\156\056\167\145\142\051"; char RAWSTR_pl_clojure_projfiles_clj[] = "\072\165\163\145\162\040\173\072\162\145\160\157\163\151\164\157\162\151\145\163\040\133\133\042\143\154\157\152\141\162\163\042\040\173\072\165\162\154\040\042\100\165\162\154\100\042\175\135\135\012\040\040\040\040\040\040\040\073\073\040\157\164\150\145\162\040\072\165\163\145\162\040\160\162\157\146\151\154\145\040\163\145\164\164\151\156\147\163\012\040\040\040\040\040\040\175"; ================================================ FILE: src/recipe/lang/rawstr4c.md ================================================ # rawstr4c input for PL - prefix = `RAWSTR_pl` - output = `:global-variable-only-header` - translate = `:oct` - no-postfix = `true`
## Node.js - namespace = `nodejs` ### Bun - name = `bun_config` ```toml [install] registry = "@url@" ```
## Java - namespace = `java` ### maven config ```xml @1@ * @name@ @url@ ``` ### build.gradle ```groovy allprojects { repositories { maven { url '@url@' } mavenLocal() mavenCentral() } } ```
## Rust - namespace = `rust` - name = `cargo_config` ```toml [source.crates-io] replace-with = "mirror" [source.mirror] registry = "sparse+@url@" ```
## Haskell - namespace = `haskell` ### cabal config ``` repository mirror url: @url@ secure: True ``` ### stackage - name = `stackage` - no-postfix = `false` - postfix = `yaml` ```yaml package-index: download-prefix: @url@ hackage-security: keyids: - 0a5c7ea47cd1b15f01f5f51a33adda7e655bc0f0b0615baa8e271f4c3351e21d - 1ea9ba32c526d1cc91ab5e5bd364ec5e9e8cb67179a471872f6e26f0ae773d42 - 280b10153a522681163658cb49f632cde3f38d768b736ddbc901d99a1a772833 - 2a96b1889dc221c17296fcc2bb34b908ca9734376f0f361660200935916ef201 - 2c6c3627bd6c982990239487f1abd02e08a02e6cf16edb105a8012d444d870c3 - 51f0161b906011b52c6613376b1ae937670da69322113a246a09f807c62f6921 - 772e9f4c7db33d251d5c6e357199c819e569d130857dc225549b40845ff0890d - aa315286e6ad281ad61182235533c41e806e5a787e0b6d1e7eef3f09d137d2e9 - fe331502606802feac15e514d9b9ea83fee8b6ffef71335479a2e68d84adc6b0 key-threshold: 3 # number of keys required # ignore expiration date, see https://github.com/commercialhaskell/stack/pull/4614 ignore-expiry: no ```
## Clojar - namespace = `clojure` ### project.clj ```clojure (defproject myapp "1.0.0" :description "My Application" :dependencies [[lib1 "1.0.0"] [lib2 "2.0.0"] [lib3 "3.0.0"]] :mirrors {"clojars" {:name "mirror" :url "@url@"}} :main leiningen.web) ``` ### projfiles.clj ```clojure :user {:repositories [["clojars" {:url "@url@"}]] ;; other :user profile settings } ``` ================================================ FILE: src/recipe/menu.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Authors : 曾奥然 * Contributors : Mikachu2333 * | BingChunMoLi * | * Created On : <2023-09-01> * Major Revision : 5 * Last Modified : <2025-08-22> * ------------------------------------------------------------*/ #include "lang/rawstr4c.h" #include "lang/Ruby/Ruby.c" #include "lang/Python/common.h" #include "lang/Python/pip.c" #include "lang/Python/Poetry.c" #include "lang/Python/PDM.c" #include "lang/Python/Rye.c" #include "lang/Python/uv.c" #include "lang/Python/Python.c" #include "lang/JavaScript/common.h" #include "lang/JavaScript/npm.c" #include "lang/JavaScript/pnpm.c" #include "lang/JavaScript/Yarn.c" #include "lang/JavaScript/JavaScript.c" #include "lang/JavaScript/Bun.c" #include "lang/JavaScript/nvm.c" #include "lang/Perl.c" #include "lang/PHP.c" #include "lang/Lua.c" #include "lang/Go.c" #include "lang/Java.c" #include "lang/Rust/common.h" #include "lang/Rust/rustup.c" #include "lang/Rust/Cargo.c" #include "lang/Dart/common.h" #include "lang/Dart/Pub.c" #include "lang/Dart/Flutter.c" #include "lang/Haskell.c" #include "lang/OCaml.c" #include "lang/R.c" #include "lang/Julia.c" #include "lang/NuGet.c" #include "lang/Clojure.c" #include "os/rawstr4c.h" #include "os/APT/common.h" // Debian-based #include "os/APT/Debian.c" #include "os/APT/Ubuntu.c" #include "os/APT/Kali-Linux.c" #include "os/APT/Raspberry-Pi-OS.c" // Ubuntu-based #include "os/APT/Linux-Mint.c" #include "os/APT/Trisquel.c" #include "os/APT/Linux-Lite.c" // Debian-based and Ubuntu-based #include "os/APT/Armbian.c" // Independent #include "os/APT/ROS.c" #include "os/APT/Termux.c" #include "os/APT/openKylin.c" #include "os/APT/deepin.c" #include "os/YUM/common.h" #include "os/YUM/Fedora-Linux.c" #include "os/YUM/AlmaLinux.c" #include "os/YUM/Rocky-Linux.c" #include "os/YUM/openEuler.c" #include "os/YUM/Anolis-OS.c" #include "os/pacman/Arch-Linux.c" #include "os/pacman/Manjaro-Linux.c" #include "os/pacman/MSYS2.c" #include "os/openSUSE.c" #include "os/Alpine-Linux.c" #include "os/Gentoo-Linux.c" #include "os/Void-Linux.c" #include "os/Solus.c" #include "os/OpenWrt.c" #include "os/BSD/FreeBSD.c" #include "os/BSD/NetBSD.c" #include "os/BSD/OpenBSD.c" #include "ware/TeX-Live.c" #include "ware/Emacs.c" #include "ware/WinGet.c" #include "ware/Homebrew/Homebrew.c" #include "ware/CocoaPods.c" #include "ware/Nix.c" #include "ware/Guix.c" #include "ware/Flatpak.c" #include "ware/Docker/Docker.c" #include "ware/Anaconda/Anaconda.c" void chsrc_init_menu () { #define add(t) xy_seq_push(ProgStore.pl, &pl_##t##_target); (&pl_##t##_target)->preludefn = pl_##t##_prelude add (ruby); add (python_group); add (python_pip); add (python_poetry); add (python_pdm); add (python_rye); add (python_uv); add (js_group); add (js_bun); add (js_npm); add (js_yarn); add (js_pnpm); add (js_nvm); add (perl); add (php); add (lua); add (go); add (rust_cargo); add (rust_rustup); add (java); add (clojure); add (dart); add (dart_flutter); add (nuget); add (haskell); add (ocaml); add (r); add (julia); #undef add #define add(t) xy_seq_push(ProgStore.os, &os_##t##_target); (&os_##t##_target)->preludefn = os_##t##_prelude add (ubuntu); add (linuxmint); add (debian); add (fedora); add (opensuse); add (kali); add (msys2); add (arch); add (archlinuxcn); add (manjaro); add (gentoo); add (rockylinux); add (almalinux); add (alpine); add (voidlinux); add (solus); add (trisquel); add (linuxlite); add (ros); add (raspberrypi); add (armbian); add (openwrt); add (termux); add (openkylin); add (openeuler); add (anolis); add (deepin); add (freebsd); add (netbsd); add (openbsd); #undef add #define add(t) xy_seq_push(ProgStore.wr, &wr_##t##_target); (&wr_##t##_target)->preludefn = wr_##t##_prelude add (winget); add (homebrew); add (cocoapods); add (docker); add (flatpak); add (nix); add (guix); add (emacs); add (tex); add (anaconda); #undef add // Internal Target 不会由用户使用,需要特殊对待 pl_js_nodejs_binary_target.preludefn = pl_js_nodejs_binary_prelude; } /** * @sync https://github.com/RubyMetric/chsrc/wiki */ static MirrorSite_t* chsrc_available_mirrors[] = { /* ------------------------ 教育网(通用镜像站) ------------------------ */ &MirrorZ, &Tuna, &Sjtug_Zhiyuan, &Sjtug_Siyuan, &Bfsu, &Ustc, &Zju, &Jlu, &Lzuoss, &Pku, &Bjtu, &Sustech, &Nju, &Xjtu, &Hust, &Iscas, &Hit, &Scau, &NJTech, &Nyist, &Sdu, &Qlu, &Cqupt, &Cqu, &Neosoft, /* ------------------------ 商业公司(通用镜像站) ------------------------ */ &Ali, // &Ali_ECS_VPC, &Ali_ECS_classic, &Tencent, // &Tencent_Intra, &Huawei, &HuaweiCDN, &Volcengine, &Netease, &Sohu, &Api7, &Fit2Cloud, &DaoCloud, /* ------------------------ 专用镜像站 ------------------------ */ &RubyChina, &EmacsChina, &NpmMirror, &GoProxyIO, &GoProxyCN, &RsProxyCN, &FlutterCN, /* 暂不支持 &NugetOrg */ /* ------------------------ chsrc 内部实现 ------------------------ */ /* 不要列出 &UpstreamProvider 和 &UserDefinedProvider */ }; ================================================ FILE: src/recipe/os/APT/Armbian.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_armbian, "armbian"); void os_armbian_prelude () { chef_prep_this (os_armbian, gsr); chef_set_recipe_created_on (this, "2024-06-14"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-11-21"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@livelycode36"); chef_set_sauciers (this, 2, "@ccmywish", "@Yangmoooo"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://apt.armbian.com", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/armbian", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/armbian", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/armbian", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/armbian", DelegateToMirror}, {&Sustech, "https://mirrors.sustech.edu.cn/armbian", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/armbian", DelegateToMirror}, {&Nju, "https://mirrors.nju.edu.cn/armbian", DelegateToMirror}, {&Ali, "https://mirrors.aliyun.com/armbian", DelegateToMirror} def_sources_end() } void os_armbian_getsrc (char *option) { if (chsrc_check_file (OS_Armbian_SourceList)) { chsrc_view_file (OS_Armbian_SourceList); return; } char *msg = ENGLISH ? "Source list config file missing! Path: " OS_Armbian_SourceList : "缺少源配置文件!路径:" OS_Armbian_SourceList; chsrc_error2 (msg); } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/armbian */ void os_armbian_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_armbian); chsrc_backup (OS_Armbian_SourceList); char *cmd = xy_strcat (3, "sed -E -i 's@https?[^ ]*armbian/?[^ ]*@", source.url, "@g' " OS_Armbian_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void os_armbian_resetsrc (char *option) { os_armbian_setsrc (option); } ================================================ FILE: src/recipe/os/APT/Debian.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_debian, "debian"); void os_debian_prelude () { chef_prep_this (os_debian, gsr); chef_set_recipe_created_on (this, "2023-09-02"); chef_set_recipe_last_updated (this, "2025-11-09"); chef_set_sources_last_updated (this, "2025-07-11"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@ccmywish", "@G_I_Y"); chef_set_sauciers (this, 1, "@Yangmoooo"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://deb.debian.org/debian", FeedByPrelude}, {&MirrorZ, "https://mirrors.cernet.edu.cn/debian", FeedByPrelude}, {&Ali, "https://mirrors.aliyun.com/debian", FeedByPrelude}, {&Volcengine, "https://mirrors.volces.com/debian", FeedByPrelude}, {&Bfsu, "https://mirrors.bfsu.edu.cn/debian", FeedByPrelude}, {&Ustc, "https://mirrors.ustc.edu.cn/debian", FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/debian", FeedByPrelude}, {&Tencent, "https://mirrors.tencent.com/debian", FeedByPrelude} // {&Tencent_Intra, "https://mirrors.tencentyun.com/debian", FeedByPrelude}, /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/debian", FeedByPrelude}, /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com/debian", FeedByPrelude} def_sources_end() chef_set_rest_smURL_with_postfix (this, "/dists/bookworm/main/Contents-all.gz"); } void os_debian_getsrc (char *option) { if (chsrc_check_file (OS_Debian_SourceList_DEB822)) { chsrc_view_file (OS_Debian_SourceList_DEB822); return; } if (chsrc_check_file (OS_Debian_old_SourceList)) { chsrc_view_file (OS_Debian_old_SourceList); return; } char *msg = ENGLISH ? "Source list file missing! However, you can still run `chsrc set debian` to add and use new sources" : "缺少源配置文件!但仍可直接通过 chsrc set debian 来添加使用新的源"; chsrc_error2 (msg); return; } static bool os_debian_does_old_sourcelist_use_cdrom (void) { /* 我们只检查旧版sourcelist,因为 common.h 中的填充只支持旧版 */ char *cmd = xy_2strcat ("grep -q '^deb cdrom:' ", OS_Debian_old_SourceList); int ret = xy_run_get_status (cmd); bool use_cdrom = ret == 0; return use_cdrom; } void os_debian_setsrc_for_deb822 (char *option) { chsrc_use_this_source (os_debian); chsrc_backup (OS_Debian_SourceList_DEB822); char *cmd = xy_strcat (3, "sed -E -i 's@https?://.*/debian/?@", source.url, "@g' " OS_Debian_SourceList_DEB822); chsrc_run (cmd, RunOpt_Default); /* debian-security 源和其他源不一样 */ cmd = xy_strcat (3, "sed -E -i 's@https?://.*/debian-security/?@", source.url, "-security@g' " OS_Debian_SourceList_DEB822); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } /** * 处理旧版(非DEB822) sourcelist 的换源 * * Debian 10 Buster 以上版本默认支持 HTTPS 源。如果遇到无法拉取 HTTPS 源的情况,请先使用 HTTP 源并安装 * apt install apt-transport-https ca-certificates */ void os_debian_setsrc (char *option) { chsrc_ensure_root (); if (chsrc_check_file (OS_Debian_SourceList_DEB822)) { chsrc_note2 ("将基于新格式(DEB822)换源"); os_debian_setsrc_for_deb822 (option); return; } chsrc_alert2 ("将基于旧格式(非DEB822)换源"); /* Docker环境下,Debian镜像可能不存在该文件 */ bool sourcelist_exist = ensure_debian_or_ubuntu_old_sourcelist (OS_Is_Debian_Literally); /** * 处理带有CDROM源的sourcelist * * https://github.com/RubyMetric/chsrc/issues/185#issuecomment-2746072917 */ if (sourcelist_exist) { bool use_cdrom = os_debian_does_old_sourcelist_use_cdrom(); if (use_cdrom) { chsrc_backup (OS_Debian_old_SourceList); int rm_status = xy_run_get_status ("rm " OS_Debian_old_SourceList); if (rm_status != 0) { /* Placate -Wunused-result */ } chsrc_warn2 ("旧版源配置文件中使用了 CDROM 源,已删除(但备份)该配置文件,重新配置"); /* 现在的情况是:系统中已经没有配置文件了 */ sourcelist_exist = ensure_debian_or_ubuntu_old_sourcelist (OS_Is_Debian_Literally); } } chsrc_use_this_source (os_debian); chsrc_alert2 ("如果遇到无法拉取 HTTPS 源的情况,请手动运行:"); say ("apt install apt-transport-https ca-certificates"); /* 不存在的时候,用的是我们生成的用来填充占位的无效文件,不要备份 */ if (sourcelist_exist) { chsrc_backup (OS_Debian_old_SourceList); } char *cmd = xy_strcat (3, "sed -E -i \'s@https?://.*/debian/?@", source.url, "@g\' " OS_Debian_old_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void os_debian_resetsrc (char* option) { os_debian_setsrc (option); } ================================================ FILE: src/recipe/os/APT/Kali-Linux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_kali, "kali"); void os_kali_prelude () { chef_prep_this (os_kali, gsr); chef_set_recipe_created_on (this, "2023-09-29"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-06-20"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@G_I_Y"); chef_set_sauciers (this, 2, "@Yangmoooo", "@happy-game"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://http.kali.org/kali", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/kali", DelegateToMirror}, {&Ali, "https://mirrors.aliyun.com/kali", DelegateToMirror}, {&Volcengine, "https://mirrors.volces.com/kali", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/kali", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/kali", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/kali", DelegateToMirror} /* 不启用原因: 未与上游同步 */ // {&Huawei, "https://mirrors.huaweicloud.com/kali", DelegateToMirror}, def_sources_end() } void os_kali_getsrc (char *option) { chsrc_view_file (OS_Apt_SourceList); } void os_kali_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_kali); chsrc_backup (OS_Apt_SourceList); char *cmd = xy_strcat (3, "sed -E -i \'s@https?://.*/kali/?@", source.url, "@g\' " OS_Apt_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } void os_kali_resetsrc (char *option) { os_kali_setsrc (option); } ================================================ FILE: src/recipe/os/APT/Linux-Lite.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_linuxlite, "linuxlite"); void os_linuxlite_prelude () { chef_prep_this (os_linuxlite, gsr); chef_set_recipe_created_on (this, "2023-09-29"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-11-21"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@Yangmoooo"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://repo.linuxliteos.com/linuxlite/", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/linuxliteos/", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/linuxliteos/", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/linuxliteos/", DelegateToMirror} def_sources_end() } void os_linuxlite_getsrc (char *option) { chsrc_view_file (OS_Apt_SourceList); } /** * @consult https://help.mirrors.cernet.edu.cn/linuxliteos/ */ void os_linuxlite_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_linuxlite); chsrc_backup (OS_Apt_SourceList); char *cmd = xy_strcat (3, "sed -E -i 's@https?://.*/.*/?@", source.url, "@g' " OS_Apt_SourceList); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void os_linuxlite_resetsrc (char *option) { os_linuxlite_setsrc (option); } ================================================ FILE: src/recipe/os/APT/Linux-Mint.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_linuxmint, "linuxmint"); void os_linuxmint_prelude () { chef_prep_this (os_linuxmint, gs); chef_set_recipe_created_on (this, "2023-09-29"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-11-21"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@happy-game"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); /* @note 实际上镜像站里的内容和Ubuntu的不太一样 */ def_sources_begin() {&UpstreamProvider, "http://packages.linuxmint.com", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/linuxmint/", DelegateToMirror}, {&Ali, "http://mirrors.aliyun.com/linuxmint-packages/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/linuxmint/", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/linuxmint/", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/linuxmint/", DelegateToMirror}, {&Jlu, "https://mirrors.jlu.edu.cn/linuxmint/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/linuxmint/", DelegateToMirror}, {&Bjtu, "https://mirror.bjtu.edu.cn/linuxmint/", DelegateToMirror}, {&Zju, "https://mirrors.zju.edu.cn/linuxmint/", DelegateToMirror}, {&Sustech, "https://mirrors.sustech.edu.cn/linuxmint/", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/linuxmint/", DelegateToMirror}, {&Scau, "https://mirrors.scau.edu.cn/linuxmint/", DelegateToMirror} /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/linuxmint/packages/", DelegateToMirror} def_sources_end() } void os_linuxmint_getsrc (char *option) { chsrc_view_file (OS_LinuxMint_SourceList); } /** * @consult https://help.mirrors.cernet.edu.cn/linuxmint/ */ void os_linuxmint_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_linuxmint); chsrc_backup (OS_LinuxMint_SourceList); // deb xxx wilma main upstream import backport 为mint主要源, wilma为版本代号 // 暂不实现自动替换基于debian或ubuntu的基础源 char *version_codename = xy_run ("sed -nr 's/^VERSION_CODENAME=([^\"]+)/\1/p' " ETC_OS_RELEASE, 0); // sed -i '// s|http[^ ]*||g' OS_LinuxMint_SourceList char* cmd = xy_strcat (5, "sed -i '/", version_codename, "/ s|http[^ ]*|", source.url, "|g' " OS_LinuxMint_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); chsrc_warn2 ("完成后请不要再使用 mintsources(自带的图形化软件源设置工具)进行任何操作,因为在操作后,无论是否有按“确定”,mintsources 均会覆写我们刚才换源的内容"); chsrc_warn2 ("已自动更换mint主要源, 但mint也使用基于debian或ubuntu的基础源, 可参考对应的debian或ubuntu换源方法进行手动换源"); } ================================================ FILE: src/recipe/os/APT/ROS.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_ros, "ros/ros2"); void os_ros_prelude () { chef_prep_this (os_ros, gs); chef_set_recipe_created_on (this, "2023-09-03"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-04-18"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@G_I_Y"); chef_set_sauciers (this, 2, "@ccmywish", "@zouri"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, "该换源方案中,URL存在拼凑,因此不能手动使用某URL来换源", "In this switching method, URLs are constructed, so manual URL specification is not supported"); def_sources_begin() {&UpstreamProvider, "http://packages.ros.org", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com", DelegateToMirror}, // {&Tencent_Intra, "https://mirrors.tencentyun.com", DelegateToMirror}, {&Huawei, "https://mirrors.huaweicloud.com", DelegateToMirror} /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com", DelegateToMirror} def_sources_end() } void os_ros_getsrc (char *option) { chsrc_view_file (OS_ROS_SourceList); } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/ros/ */ void os_ros_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_ros); chsrc_backup (OS_ROS_SourceList); char *cmd = NULL; cmd = xy_strcat(3, "sed -E -i \'s@https?://.*/ros/ubuntu/?@", source.url, "/ros/ubuntu@g\' " OS_ROS_SourceList); chsrc_run(cmd, RunOpt_Default); cmd = "apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654"; chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/APT/Raspberry-Pi-OS.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * Raspberry Pi OS 树莓派操作系统,以前称为 Raspbian * ------------------------------------------------------------*/ def_target(os_raspberrypi, "raspi/raspberrypi"); void os_raspberrypi_prelude () { chef_prep_this (os_raspberrypi, gsr); chef_set_recipe_created_on (this, "2023-09-29"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2023-09-29"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@Yangmoooo"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() // https://archive.raspberrypi.org/ until Debian "bullseye" release {&UpstreamProvider, "https://archive.raspberrypi.com/", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/raspberrypi/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/raspberrypi/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/raspberrypi/", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/raspberrypi/", DelegateToMirror}, {&Sustech, "https://mirrors.sustech.edu.cn/raspberrypi/", DelegateToMirror} def_sources_end() } void os_raspberrypi_getsrc (char *option) { chsrc_view_file (OS_RaspberryPi_SourceList); } void os_raspberrypi_setsrc (char *option) { chsrc_ensure_root(); // HELP: 不确定是否需要 chsrc_use_this_source (os_raspberrypi); chsrc_backup (OS_RaspberryPi_SourceList); char *cmd = xy_strcat (3, "sed -E -i 's@https?://.*/.*/?@", source.url, "@g' " OS_RaspberryPi_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } void os_raspberrypi_resetsrc (char *option) { os_raspberrypi_setsrc (option); } ================================================ FILE: src/recipe/os/APT/Termux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_termux, "termux"); void os_termux_prelude () { chef_prep_this (os_termux, gsr); chef_set_recipe_created_on (this, "2025-03-04"); chef_set_recipe_last_updated (this, "2026-01-21"); chef_set_sources_last_updated (this, "2025-03-04"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, "该 recipe 存在对应的 bootstrapper", "This recipe has a corresponding bootstrapper"); def_sources_begin() {&UpstreamProvider, "https://packages.termux.org/apt/termux-main/", DelegateToUpstream}, {&Ustc, "https://mirrors.ustc.edu.cn/termux/", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/termux/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/termux/", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/termux/", DelegateToMirror}, {&Pku, "https://mirrors.pku.edu.cn/termux/", DelegateToMirror}, {&Nyist, "https://mirror.nyist.edu.cn/termux/", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/termux/", DelegateToMirror}, {&Sustech, "https://mirrors.sustech.edu.cn/termux/", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/termux/", DelegateToMirror}, {&Zju, "https://mirrors.zju.edu.cn/termux/", DelegateToMirror}, {&Sdu, "https://mirrors.sdu.edu.cn/termux/", DelegateToMirror}, {&Cqupt, "https://mirrors.cqupt.edu.cn/termux/", DelegateToMirror} def_sources_end() } void os_termux_getsrc (char *option) { chsrc_view_file (OS_Termux_SourceList); } /** * @consult https://help.mirrors.cernet.edu.cn/termux/ * * chsrc set termux */ void os_termux_setsrc (char *option) { // chsrc_ensure_root (); Termux下禁止使用root chsrc_use_this_source (os_termux); char *cmd = xy_strcat (3, "sed -i 's@^\\(deb.*stable main\\)$@#\\1\\ndeb ", source.url, "apt/termux-main stable main@' " OS_Termux_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_Default); chsrc_run ("apt-get upgrade", RunOpt_Default); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void os_termux_resetsrc (char *option) { os_termux_setsrc (option); } ================================================ FILE: src/recipe/os/APT/Trisquel.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * Trisquel基于Ubuntu开发,不含任何专有软件及专有固件,内核使用 Linux-libre * ------------------------------------------------------------*/ def_target(os_trisquel, "trisquel"); void os_trisquel_prelude () { chef_prep_this (os_trisquel, gsr); chef_set_recipe_created_on (this, "2023-09-29"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-11-21"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://archive.trisquel.info/trisquel/", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/trisquel/", DelegateToMirror}, {&Ali, "https://mirrors.aliyun.com/trisquel/", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/trisquel/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/trisquel/", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/trisquel/", DelegateToMirror} def_sources_end() } void os_trisquel_getsrc (char *option) { chsrc_view_file (OS_Apt_SourceList); } /** * @consult https://help.mirrors.cernet.edu.cn/trisquel/ */ void os_trisquel_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_trisquel); chsrc_backup (OS_Apt_SourceList); char *cmd = xy_strcat (3, "sed -E -i 's@https?://.*/trisquel/?@", source.url, "@g' /etc/apt/sources.list"); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } void os_trisquel_resetsrc (char *option) { os_trisquel_setsrc (option); } ================================================ FILE: src/recipe/os/APT/Ubuntu.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ /* Zorin OS 完全使用 Ubuntu 的换源方法,二者兼容 */ def_target(os_ubuntu, "ubuntu/zorinos"); void os_ubuntu_prelude () { chef_prep_this (os_ubuntu, gsr); chef_set_recipe_created_on (this, "2023-08-30"); chef_set_recipe_last_updated (this, "2026-01-21"); chef_set_sources_last_updated (this, "2026-01-21"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@ccmywish", "@G_I_Y"); chef_set_sauciers (this, 2, "@XUANJI233", "@usernameisnull"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://archive.ubuntu.com/ubuntu", FeedByPrelude}, /* 不支持https */ {&MirrorZ, "https://mirrors.cernet.edu.cn/ubuntu",FeedByPrelude}, {&Ali, "https://mirrors.aliyun.com/ubuntu",FeedByPrelude}, {&Volcengine, "https://mirrors.volces.com/ubuntu",FeedByPrelude}, {&Bfsu, "https://mirrors.bfsu.edu.cn/ubuntu",FeedByPrelude}, {&Ustc, "https://mirrors.ustc.edu.cn/ubuntu",FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/ubuntu",FeedByPrelude}, {&Tencent, "https://mirrors.tencent.com/ubuntu", FeedByPrelude}, // {&Tencent_Intra, "https://mirrors.tencentyun.com/ubuntu",FeedByPrelude}, {&Huawei, "https://mirrors.huaweicloud.com/ubuntu",FeedByPrelude} /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/ubuntu", FeedByPrelude}, /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com/ubuntu", FeedByPrelude} def_sources_end() char *arch = chsrc_get_cpuarch (); // Ubuntu 非 x86_64 架构的默认源地址有所不同 if (strncmp (arch, "x86_64", 6)!=0) { chef_set_repoURL (this, &UpstreamProvider, "http://ports.ubuntu.com/ubuntu"); } chef_set_rest_smURL_with_postfix (this, "/dists/noble/Contents-amd64.gz"); } void os_ubuntu_getsrc (char *option) { if (chsrc_check_file (OS_Ubuntu_SourceList_DEB822)) { chsrc_view_file (OS_Ubuntu_SourceList_DEB822); return; } if (chsrc_check_file (OS_Ubuntu_old_SourceList)) { chsrc_view_file (OS_Ubuntu_old_SourceList); return; } char *msg = ENGLISH ? "Source list file missing! However, you can still run `chsrc set ubuntu` to add and use new sources" : "缺少源配置文件!但仍可直接通过 chsrc set ubuntu 来添加使用新的源"; chsrc_error2 (msg); return; } /** * 此函数基本和 os_ubuntu_setsrc() 一致 */ void os_ubuntu_setsrc_for_deb822 (char *option) { chsrc_use_this_source (os_ubuntu); chsrc_backup (OS_Ubuntu_SourceList_DEB822); char *arch = chsrc_get_cpuarch (); char *cmd = NULL; if (strncmp (arch, "x86_64", 6)==0) { cmd = xy_strcat (3, "sed -E -i \'s@https?://.*/ubuntu/?@", source.url, "@g\' " OS_Ubuntu_SourceList_DEB822); } else { cmd = xy_strcat (3, "sed -E -i \'s@https?://.*/ubuntu-ports/?@", source.url, "-ports@g\' " OS_Ubuntu_SourceList_DEB822); } chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } /** * @note 不同架构下换源不一样 */ void os_ubuntu_setsrc (char *option) { chsrc_ensure_root (); if (chsrc_check_file (OS_Ubuntu_SourceList_DEB822)) { char *msg = ENGLISH ? "Will change source based on new format" : "将基于新格式(DEB822)换源"; chsrc_note2 (msg); os_ubuntu_setsrc_for_deb822 (option); return; } chsrc_alert2 ("将基于旧格式(非DEB822)换源"); bool sourcelist_exist = ensure_debian_or_ubuntu_old_sourcelist (OS_Is_Ubuntu); chsrc_use_this_source (os_ubuntu); /* 不存在的时候,用的是我们生成的无效文件,不要备份 */ if (sourcelist_exist) { chsrc_backup (OS_Ubuntu_old_SourceList); } char *arch = chsrc_get_cpuarch(); char *cmd = NULL; if (0==strncmp (arch, "x86_64", 6)) { cmd = xy_strcat (3, "sed -E -i \'s@https?://.*/ubuntu/?@", source.url, "@g\' " OS_Ubuntu_old_SourceList); } else { cmd = xy_strcat (3, "sed -E -i \'s@https?://.*/ubuntu-ports/?@", source.url, "-ports@g\' " OS_Ubuntu_old_SourceList); } chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void os_ubuntu_resetsrc (char *option) { os_ubuntu_setsrc (option); } ================================================ FILE: src/recipe/os/APT/common.h ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Authors : Aoran Zeng * Contributors : happy game * | * Created On : <2024-06-14> * Major Revision : 3 * Last Modified : <2025-07-14> * ------------------------------------------------------------*/ #include "rawstr4c.h" #define OS_Apt_SourceList "/etc/apt/sources.list" #define OS_Apt_SourceList_D "/etc/apt/sources.list.d/" /** * @note 从 Debian 12 (bookworm) 开始,Debain 的软件源配置文件变更为 DEB822 格式, * 路径为: /etc/apt/sources.list.d/debian.sources" * * @note 从 Ubuntu 24.04 开始,Ubuntu 的软件源配置文件变更为 DEB822 格式, * 路径为: /etc/apt/sources.list.d/ubuntu.sources */ #define OS_Debian_SourceList_DEB822 "/etc/apt/sources.list.d/debian.sources" #define OS_Ubuntu_SourceList_DEB822 "/etc/apt/sources.list.d/ubuntu.sources" #define OS_Debian_old_SourceList OS_Apt_SourceList #define OS_Ubuntu_old_SourceList OS_Apt_SourceList #define ETC_OS_RELEASE "/etc/os-release" #define OS_Is_Debian_Literally 1 #define OS_Is_Ubuntu 2 // independent #define OS_ROS_SourceList OS_Apt_SourceList_D "ros-latest.list" #define OS_Termux_SourceList "/data/data/com.termux/files/usr" OS_Apt_SourceList // Ubuntu based #define OS_LinuxMint_SourceList OS_Apt_SourceList_D "official-package-repositories.list" // Debian based #define OS_Armbian_SourceList OS_Apt_SourceList_D "armbian.list" #define OS_RaspberryPi_SourceList OS_Apt_SourceList_D "raspi.list" /** * 当不存在该文件时,我们只能拼凑一个假的出来,但该函数目前只适用于 Ubuntu 和 Debian * 因为其它的 Debian 变体可能不使用 OS_Apt_SourceList,也可能并不适用 `VERSION_CODENAME` * * @return 文件是否存在 */ bool ensure_debian_or_ubuntu_old_sourcelist (int debian_type) { bool exist = chsrc_check_file (OS_Apt_SourceList); if (exist) { return true; } else { char *msg = ENGLISH ? "Will generate a new source list file" : "将生成新的源配置文件"; chsrc_note2 (msg); } /* 反向引用需要escape */ char *codename = xy_run ("sed -nr 's/VERSION_CODENAME=(.*)/\\1/p' " ETC_OS_RELEASE, 0); char *version_id = xy_run ("sed -nr 's/VERSION_ID=\"(.*)\"/\\1/p' " ETC_OS_RELEASE, 0); double version = atof (version_id); char *makeup = NULL; if (debian_type == OS_Is_Ubuntu) { makeup = xy_str_gsub (RAWSTR_os_apt_based_ubuntu_old_source_list, "@v@", Chsrc_Version); makeup = xy_str_gsub (makeup, "@1@", Chsrc_Maintain_URL); makeup = xy_str_gsub (makeup, "@2@", codename); } else { if (version >= 12) /* bookworm */ { makeup = xy_str_gsub (RAWSTR_os_apt_based_debian_12_source_list, "@v@", Chsrc_Version); makeup = xy_str_gsub (makeup, "@1@", Chsrc_Maintain_URL); makeup = xy_str_gsub (makeup, "@2@", codename); } else if (version >= 11) /* bullseye */ { makeup = xy_str_gsub (RAWSTR_os_apt_based_debian_11_source_list, "@v@", Chsrc_Version); makeup = xy_str_gsub (makeup, "@1@", Chsrc_Maintain_URL); makeup = xy_str_gsub (makeup, "@2@", codename); } else if (version >= 10) /* buster */ { makeup = xy_str_gsub (RAWSTR_os_apt_based_debian_10_source_list, "@v@", Chsrc_Version); makeup = xy_str_gsub (makeup, "@1@", Chsrc_Maintain_URL); makeup = xy_str_gsub (makeup, "@2@", codename); } else { char *msg = ENGLISH ? "Your Debian version is too low (<10) for chsrc to support" : "你的 Debian 版本过低 (<10),暂不支持换源"; chsrc_error (msg); exit (Exit_Unsupported); } } FILE *f = fopen (OS_Apt_SourceList, "w"); fwrite (makeup, strlen (makeup), 1, f); fclose (f); return false; } ================================================ FILE: src/recipe/os/APT/deepin.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_deepin, "deepin"); void os_deepin_prelude () { chef_prep_this (os_deepin, gsr); chef_set_recipe_created_on (this, "2023-09-26"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-09-14"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@G_I_Y"); chef_set_sauciers (this, 1, "@Yangmoooo"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "https://community-packages.deepin.com/deepin", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/deepin", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/deepin", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/deepin", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/deepin", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com/deepin", DelegateToMirror} // {&Tencent_Intra, "https://mirrors.tencentyun.com/deepin", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/deepin", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com/deepin", DelegateToMirror} def_sources_end() } void os_deepin_getsrc(char *option) { chsrc_view_file (OS_Apt_SourceList); } /** * HELP: 未经测试 */ void os_deepin_setsrc (char *option) { chsrc_ensure_root(); chsrc_use_this_source (os_deepin); chsrc_backup (OS_Apt_SourceList); char *cmd = xy_strcat (3, "sed -E -i \'s@https?://.*/deepin/?@", source.url, "@g\' " OS_Apt_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } void os_deepin_resetsrc (char *option) { os_deepin_setsrc (option); } ================================================ FILE: src/recipe/os/APT/openKylin.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * openKylin直接基于Linux内核开发,属于和Debian、openSUSE、Fedora、Arch * 同一级别的、根社区发布的系统 * ------------------------------------------------------------*/ def_target(os_openkylin, "openkylin"); void os_openkylin_prelude () { chef_prep_this (os_openkylin, gsr); chef_set_recipe_created_on (this, "2023-09-06"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2023-09-29"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@G_I_Y"); chef_set_sauciers (this, 1, "@ccmywish"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "https://archive.openkylin.top/openkylin/", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/openkylin/", DelegateToMirror}, {&Netease, "https://mirrors.163.com/openkylin/", DelegateToMirror} def_sources_end() } void os_openkylin_getsrc (char *option) { chsrc_view_file (OS_Apt_SourceList); } void os_openkylin_setsrc (char *option) { chsrc_ensure_root(); chsrc_use_this_source (os_openkylin); chsrc_backup (OS_Apt_SourceList); char *cmd = xy_strcat (3, "sed -E -i 's@https?://.*/openkylin/?@", source.url, "@g'" OS_Apt_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apt-get update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } void os_openkylin_resetsrc (char *option) { os_openkylin_setsrc (option); } ================================================ FILE: src/recipe/os/APT/rawstr4c.h ================================================ #pragma once /** * Generated by rawstr4c v1.0.0-2025/08/09 */ char RAWSTR_os_apt_based_ubuntu_old_source_list[] = "\x23\x20\x47\x65\x6e\x65\x72\x61\x74\x65\x64\x20\x62\x79\x20\x63\x68\x73\x72\x63\x20\x40\x76\x40\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x75\x62\x75\x6e\x74\x75\x20\x40\x32\x40\x20\x6d\x61\x69\x6e\x20\x72\x65\x73\x74\x72\x69\x63\x74\x65\x64\x20\x75\x6e\x69\x76\x65\x72\x73\x65\x20\x6d\x75\x6c\x74\x69\x76\x65\x72\x73\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x75\x62\x75\x6e\x74\x75\x20\x40\x32\x40\x2d\x75\x70\x64\x61\x74\x65\x73\x20\x6d\x61\x69\x6e\x20\x72\x65\x73\x74\x72\x69\x63\x74\x65\x64\x20\x75\x6e\x69\x76\x65\x72\x73\x65\x20\x6d\x75\x6c\x74\x69\x76\x65\x72\x73\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x75\x62\x75\x6e\x74\x75\x20\x40\x32\x40\x2d\x62\x61\x63\x6b\x70\x6f\x72\x74\x73\x20\x6d\x61\x69\x6e\x20\x72\x65\x73\x74\x72\x69\x63\x74\x65\x64\x20\x75\x6e\x69\x76\x65\x72\x73\x65\x20\x6d\x75\x6c\x74\x69\x76\x65\x72\x73\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x75\x62\x75\x6e\x74\x75\x20\x40\x32\x40\x2d\x73\x65\x63\x75\x72\x69\x74\x79\x20\x6d\x61\x69\x6e\x20\x72\x65\x73\x74\x72\x69\x63\x74\x65\x64\x20\x75\x6e\x69\x76\x65\x72\x73\x65\x20\x6d\x75\x6c\x74\x69\x76\x65\x72\x73\x65"; char RAWSTR_os_apt_based_debian_12_source_list[] = "\x23\x20\x47\x65\x6e\x65\x72\x61\x74\x65\x64\x20\x62\x79\x20\x63\x68\x73\x72\x63\x20\x40\x76\x40\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x2d\x66\x69\x72\x6d\x77\x61\x72\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x2d\x75\x70\x64\x61\x74\x65\x73\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x2d\x66\x69\x72\x6d\x77\x61\x72\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x2d\x62\x61\x63\x6b\x70\x6f\x72\x74\x73\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x2d\x66\x69\x72\x6d\x77\x61\x72\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x2d\x73\x65\x63\x75\x72\x69\x74\x79\x20\x40\x32\x40\x2d\x73\x65\x63\x75\x72\x69\x74\x79\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x2d\x66\x69\x72\x6d\x77\x61\x72\x65"; char RAWSTR_os_apt_based_debian_11_source_list[] = "\x23\x20\x47\x65\x6e\x65\x72\x61\x74\x65\x64\x20\x62\x79\x20\x63\x68\x73\x72\x63\x20\x40\x76\x40\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x2d\x75\x70\x64\x61\x74\x65\x73\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x2d\x62\x61\x63\x6b\x70\x6f\x72\x74\x73\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x2d\x73\x65\x63\x75\x72\x69\x74\x79\x20\x40\x32\x40\x2d\x73\x65\x63\x75\x72\x69\x74\x79\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65"; char RAWSTR_os_apt_based_debian_10_source_list[] = "\x23\x20\x47\x65\x6e\x65\x72\x61\x74\x65\x64\x20\x62\x79\x20\x63\x68\x73\x72\x63\x20\x40\x76\x40\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x2d\x75\x70\x64\x61\x74\x65\x73\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x20\x40\x32\x40\x2d\x62\x61\x63\x6b\x70\x6f\x72\x74\x73\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65\x0a\x64\x65\x62\x20\x40\x31\x40\x2f\x64\x65\x62\x69\x61\x6e\x2d\x73\x65\x63\x75\x72\x69\x74\x79\x20\x40\x32\x40\x2f\x75\x70\x64\x61\x74\x65\x73\x20\x6d\x61\x69\x6e\x20\x63\x6f\x6e\x74\x72\x69\x62\x20\x6e\x6f\x6e\x2d\x66\x72\x65\x65"; ================================================ FILE: src/recipe/os/APT/rawstr4c.md ================================================ # rawstr4c input - prefix = `RAWSTR_os_apt_based` - output = `:global-variable-only-header` - translate = `:hex` - no-postfix = `true` ## Ubuntu old source list ```sh # Generated by chsrc @v@ deb @1@/ubuntu @2@ main restricted universe multiverse deb @1@/ubuntu @2@-updates main restricted universe multiverse deb @1@/ubuntu @2@-backports main restricted universe multiverse deb @1@/ubuntu @2@-security main restricted universe multiverse ``` ## Debian 12 source list ```sh # Generated by chsrc @v@ deb @1@/debian @2@ main contrib non-free non-free-firmware deb @1@/debian @2@-updates main contrib non-free non-free-firmware deb @1@/debian @2@-backports main contrib non-free non-free-firmware deb @1@/debian-security @2@-security main contrib non-free non-free-firmware ``` 从 Debian 12 开始,开始有一项 `non-free-firmware` - https://wiki.debian.org/SourcesList - https://mirrors.tuna.tsinghua.edu.cn/help/debian/ ## Debian 11 source list ```sh # Generated by chsrc @v@ deb @1@/debian @2@ main contrib non-free deb @1@/debian @2@-updates main contrib non-free deb @1@/debian @2@-backports main contrib non-free deb @1@/debian-security @2@-security main contrib non-free ``` ## Debian 10 source list ```sh # Generated by chsrc @v@ deb @1@/debian @2@ main contrib non-free deb @1@/debian @2@-updates main contrib non-free deb @1@/debian @2@-backports main contrib non-free deb @1@/debian-security @2@/updates main contrib non-free ``` 上述 debian-security 这种写法是和 Debian 11 不同的,我们只支持到这里 ================================================ FILE: src/recipe/os/Alpine-Linux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_alpine, "alpine"); void os_alpine_prelude () { chef_prep_this (os_alpine, gs); chef_set_recipe_created_on (this, "2023-09-24"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-09-14"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@Yangmoooo"); chef_set_os_scope (this); chef_allow_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://dl-cdn.alpinelinux.org/alpine", FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/alpine", FeedByPrelude}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/alpine", FeedByPrelude}, {&Sustech, "https://mirrors.sustech.edu.cn/alpine", FeedByPrelude}, {&Zju, "https://mirrors.zju.edu.cn/alpine", FeedByPrelude}, {&Lzuoss, "https://mirror.lzu.edu.cn/alpine", FeedByPrelude}, {&Ali, "https://mirrors.aliyun.com/alpine", FeedByPrelude}, {&Tencent, "https://mirrors.cloud.tencent.com/alpine", FeedByPrelude}, {&Huawei, "https://mirrors.huaweicloud.com/alpine", FeedByPrelude} def_sources_end() chef_set_rest_smURL_with_postfix (this, "/latest-stable/releases/x86_64/alpine-standard-3.21.0-x86_64.iso"); } void os_alpine_getsrc (char *option) { chsrc_view_file ("/etc/apk/repositories"); } /** * @consult https://help.mirrors.cernet.edu.cn/alpine/ */ void os_alpine_setsrc (char *option) { // chsrc_ensure_root(); // HELP: 不确定是否需要root chsrc_use_this_source (os_alpine); char* cmd = xy_strcat (3, "sed -i 's#https\\?://dl-cdn.alpinelinux.org/alpine#", source.url, "#g' /etc/apk/repositories" ); chsrc_run (cmd, RunOpt_Default); chsrc_run ("apk update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/BSD/FreeBSD.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_freebsd, "freebsd"); void os_freebsd_prelude () { chef_prep_this (os_freebsd, s); chef_set_recipe_created_on (this, "2023-09-03"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2023-09-27"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@ccmywish", "@G_I_Y"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); // 2023-09-24: 以下三个USTC, NJU, Netease 均维护了 freebsd-pkg freebsd-ports // 2023-09-27: 请务必保持Nju前面有至少一个镜像,原因请查看 freebsd 的换源函数 def_sources_begin() {&UpstreamProvider, "pkg.freebsd.org", DelegateToUpstream}, {&Ustc, "mirrors.ustc.edu.cn", DelegateToMirror}, {&Nju, "mirror.nju.edu.cn", DelegateToMirror}, {&Netease, "mirrors.163.com", DelegateToMirror}, def_sources_end() } /** * @consult * 1. https://book.bsdcn.org/di-3-zhang-ruan-jian-yuan-ji-bao-guan-li-qi/di-3.2-jie-freebsd-huan-yuan-fang-shi.html * 2. https://help.mirrors.cernet.edu.cn/FreeBSD-ports/ * * 据 @ykla, * FreeBSD 有五类源:pkg、ports、port、portsnap、update,其中 portsnap 在 FreeBSD 14 已经被移除了 */ void os_freebsd_setsrc (char *option) { // 据 @ykla,FreeBSD不自带sudo,但是我们依然要保证是root权限 chsrc_ensure_root (); chef_use_this (os_freebsd); int index = use_specific_mirror_or_auto_select (option, this); Source_t source = this->sources[index]; chsrc_confirm_source(&source); chsrc_log2 ("1. 添加 freebsd-pkg 源 (二进制安装包)"); chsrc_ensure_dir ("/usr/local/etc/pkg/repos"); char *conf = xy_strcat (3, "/usr/local/etc/pkg/repos/", source.mirror->code, ".conf"); char *pkg_content = xy_strcat (4, source.mirror->code, ": { \n" " url: \"http://", source.url, "/freebsd-pkg/${ABI}/latest\",\n" "}\n" "FreeBSD: { enabled: no }" ); chsrc_overwrite_file (pkg_content, conf); chsrc_note2 ( xy_strcat (3, "若要使用季度分支,请在", conf ,"中将latest改为quarterly")); chsrc_alert2 ("若要使用HTTPS源,请先安装securtiy/ca_root_ns,并将'http'改成'https',最后使用'pkg update -f'刷新缓存即可\n"); br(); chsrc_log2 ("2. 修改 freebsd-ports 源"); // @ccmywish: 2023-09-27 据 @ykla , NJU的freebsd-ports源没有设置 Git, // 但是我认为由于使用Git还是要比非Git方便许多,我们尽可能坚持使用Git // 而 gitup 又要额外修改它自己的配置,比较麻烦 bool git_exist = query_program_exist (xy_quiet_cmd ("git version"), "git", Noisy_When_Exist|Noisy_When_NonExist); if (git_exist) { if (xy_streql("nju",source.mirror->code)) { source = this->sources[index-1]; // 使用NJU的前一个源,即USTC源 } char *git_cmd = xy_strcat (3, "git clone --depth 1 https://", source.url, "/freebsd-ports/ports.git /usr/ports"); chsrc_run (git_cmd, RunOpt_Default); source = this->sources[index]; // 恢复至选中的源 chsrc_alert2 ("下次更新请使用 git -C /usr/ports pull 而非使用 gitup"); } else { char *fetch = xy_strcat (3, "fetch https://", source.url, "/freebsd-ports/ports.tar.gz"); // 70多MB char *unzip = "tar -zxvf ports.tar.gz -C /usr/ports"; char *delete = "rm ports.tar.gz"; chsrc_run (fetch, RunOpt_Default); chsrc_run (unzip, RunOpt_Default); chsrc_run (delete, RunOpt_Default); chsrc_log2 ("下次更新请重新下载内容至 /usr/ports"); } chsrc_log2 ("3. 指定 port 源"); // https://help.mirrors.cernet.edu.cn/FreeBSD-ports/ chsrc_backup ("/etc/make.conf"); char *ports = xy_strcat (3, "MASTER_SITE_OVERRIDE?=http://", source.url, "/freebsd-ports/distfiles/${DIST_SUBDIR}/\n"); chsrc_append_to_file (ports, "/etc/make.conf"); /* 不再换 portsnap */ /* chsrc_backup ("/etc/portsnap.conf"); char *portsnap =xy_strcat(3,"s@(.*)SERVERNAME=[\\.|a-z|A-Z]*@\\1SERVERNAME=", source.url, "@g < /etc/portsnap.conf.bak"); chsrc_overwrite_file (portsnap, "/etc/portsnap.conf"); chsrc_log2 ("portsnap sources changed"); chsrc_log2 ("获取portsnap更新使用此命令: 'portsnap fetch extract'"); */ // HELP: 暂时没有源提供 chsrc_alert2 ("4. 抱歉,目前境内无 freebsd-update 源,若存在请报告issue,谢谢"); /* chsrc_log2 ("3. 修改 freebsd-update 源"); char *update_cp = "cp /etc/freebsd-update.conf /etc/freebsd-update.conf.bak"; chsrc_run (update_cp, RunOpt_Default); char *update =xy_strcat (3,"s@(.*)SERVERNAME [\\.|a-z|A-Z]*@\\1SERVERNAME ", source.url, "@g < /etc/freebsd-update.conf.bak"); chsrc_overwrite_file (update, "/etc/freebsd-update.conf"); */ chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/BSD/NetBSD.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_netbsd, "netbsd"); void os_netbsd_prelude () { chef_prep_this (os_netbsd, gs); chef_set_recipe_created_on (this, "2023-09-05"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-07-31"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@ccmywish", "@G_I_Y"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); def_sources_begin() {&UpstreamProvider, "http://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/pkgsrc/packages/NetBSD/", DelegateToMirror}, {&Ali, "https://mirrors.aliyun.com/pkgsrc/packages/NetBSD/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/pkgsrc/packages/NetBSD/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/pkgsrc/packages/NetBSD/", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/pkgsrc/packages/NetBSD/", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/pkgsrc/packages/NetBSD/", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com/pkgsrc/packages/NetBSD/", DelegateToMirror}, // {&Tencent_Intra, "https://mirrors.tencentyun.com/pkgsrc/packages/NetBSD/", DelegateToMirror}, {&Netease, "https://mirrors.163.com/pkgsrc/packages/NetBSD/", DelegateToMirror}, {&Sohu, "https://mirrors.sohu.com/pkgsrc/packages/NetBSD/", DelegateToMirror} def_sources_end() } void os_netbsd_getsrc (char *option) { chsrc_view_file ("/usr/pkg/etc/pkgin/repositories.conf"); } /** * @consult * 1. https://mirrors.tuna.tsinghua.edu.cn/help/pkgsrc/ * 2. https://book.bsdcn.org/di-27-zhang-netbsd/di-27.2-jie-huan-yuan-yu-bao-guan-li-qi.html * * 根据 @ykla * NetBSD 默认状态下没有 pkgsrc,用户可能安装了也可能没安装 */ void os_netbsd_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_netbsd); chsrc_backup ("/usr/pkg/etc/pkgin/repositories.conf"); char *arch = chsrc_get_cpuarch (); char *vercmd = "cat /etc/os-release | grep \"VERSION=\" | grep -Po \"[8-9].[0-9]+\""; char *version = xy_run (vercmd, 0); char *url = xy_strcat (5, hp_ensure_trailing_slash (source.url), arch, "/", version, "/All"); chsrc_overwrite_file (url, "/usr/pkg/etc/pkgin/repositories.conf"); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/BSD/OpenBSD.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_openbsd, "openbsd"); void os_openbsd_prelude () { chef_prep_this (os_openbsd, gs); chef_set_recipe_created_on (this, "2023-09-03"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-07-31"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@G_I_Y", "@ccmywish"); chef_set_sauciers (this, 1, "@hezonglun"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); def_sources_begin() {&UpstreamProvider, "https://cdn.openbsd.org/pub/OpenBSD/", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/OpenBSD/", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/OpenBSD/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/OpenBSD/", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/OpenBSD/", DelegateToMirror}, {&Nyist, "https://mirror.nyist.edu.cn/OpenBSD/", DelegateToMirror}, {&Sjtug_Siyuan, "https://mirror.sjtu.edu.cn/OpenBSD/", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/OpenBSD/", DelegateToMirror}, {&Cqupt, "https://mirrors.cqupt.edu.cn/openbsd/", DelegateToMirror}, {&Ali, "https://mirrors.aliyun.com/OpenBSD/", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com/OpenBSD/", DelegateToMirror}, // {&Tencent_Intra, "https://mirrors.tencentyun.com/OpenBSD/", DelegateToMirror}, {&Netease, "https://mirrors.163.com/OpenBSD/", DelegateToMirror}, {&Sohu, "https://mirrors.sohu.com/OpenBSD/", DelegateToMirror} def_sources_end() } void os_openbsd_getsrc (char *option) { chsrc_view_file ("/etc/installurl"); } /** * @consult * 1. https://mirrors.tuna.tsinghua.edu.cn/help/openbsd/ * 2. https://book.bsdcn.org/di-26-zhang-openbsd/di-26.2-jie-pei-zhi.html */ void os_openbsd_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_openbsd); chsrc_backup ("/etc/installurl"); chsrc_overwrite_file (source.url, "/etc/installurl"); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/Gentoo-Linux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_gentoo, "gentoo"); void os_gentoo_prelude () { chef_prep_this (os_gentoo, s); chef_set_recipe_created_on (this, "2023-09-05"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-06-20"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@G_I_Y"); chef_set_sauciers (this, 1, "@ccmywish"); chef_set_os_scope (this); chef_allow_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "rsync://rsync.gentoo.org/gentoo-portage", DelegateToUpstream}, {&Ali, "mirrors.aliyun.com", DelegateToMirror}, {&Bfsu, "mirrors.bfsu.edu.cn", DelegateToMirror}, {&Ustc, "mirrors.ustc.edu.cn", DelegateToMirror}, {&Tuna, "mirrors.tuna.tsinghua.edu.cn", DelegateToMirror}, {&Tencent, "mirrors.tencent.com", DelegateToMirror} def_sources_end() } /** * HELP: 未经测试 */ void os_gentoo_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_gentoo); chsrc_backup ("/etc/portage/repos.conf/gentoo.conf"); char *cmd = xy_strcat (3, "sed -i \"s#rsync://.*/gentoo-portage#rsync://", source.url, "gentoo-portage#g"); chsrc_run (cmd, RunOpt_Default); char *w = xy_strcat (3, "GENTOO_MIRRORS=\"https://", source.url, "gentoo\"\n"); chsrc_append_to_file (w, "/etc/portage/make.conf"); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/OpenWrt.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_openwrt, "openwrt/opkg/LEDE"); void os_openwrt_prelude () { chef_prep_this (os_openwrt, gsr); chef_set_recipe_created_on (this, "2024-08-08"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-12-14"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 2, "@Yangmoooo", "@happy-game"); chef_set_os_scope (this); chef_allow_english(this); chef_allow_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "https://downloads.openwrt.org", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/openwrt", DelegateToMirror}, {&Ali, "https://mirrors.aliyun.com/openwrt", DelegateToMirror}, {&Tencent, "https://mirrors.cloud.tencent.com/openwrt", DelegateToMirror}, {&Tuna, "https://mirror.tuna.tsinghua.edu.cn/openwrt", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/openwrt", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/openwrt", DelegateToMirror}, {&Pku, "https://mirrors.pku.edu.cn/openwrt", DelegateToMirror}, {&Sustech, "https://mirrors.sustech.edu.cn/openwrt", DelegateToMirror} def_sources_end() chef_set_provider_smURL (&UpstreamProvider, "https://downloads.openwrt.org/releases/23.05.5/targets/x86/64/openwrt-sdk-23.05.5-x86-64_gcc-12.3.0_musl.Linux-x86_64.tar.xz"); } #define OS_OpenWRT_SourceConfig "/etc/opkg/distfeeds.conf" void os_openwrt_getsrc (char *option) { chsrc_view_file (OS_OpenWRT_SourceConfig); } /** * @consult * 1. https://mirror.tuna.tsinghua.edu.cn/help/openwrt/ * 2. https://github.com/RubyMetric/chsrc/issues/153#issuecomment-2543077933 */ void os_openwrt_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_openwrt); chsrc_backup (OS_OpenWRT_SourceConfig); char *cmd = xy_strcat (3, "sed -E -i 's@https?://.*/releases@", source.url, "/releases@g' " OS_OpenWRT_SourceConfig); chsrc_run (cmd, RunOpt_No_Last_New_Line); chsrc_run ("opkg update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void os_openwrt_resetsrc (char *option) { os_openwrt_setsrc (option); } ================================================ FILE: src/recipe/os/Solus.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_solus, "solus"); void os_solus_prelude () { chef_prep_this (os_solus, s); chef_set_recipe_created_on (this, "2023-09-29"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2023-09-29"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_os_scope (this); chef_deny_english(this); chef_allow_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "https://packages.getsol.us/shannon/eopkg-index.xml.xz", DelegateToUpstream}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/solus/packages/shannon/eopkg-index.xml.xz", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/solus/packages/shannon/eopkg-index.xml.xz", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/solus/packages/shannon/eopkg-index.xml.xz", DelegateToMirror} def_sources_end() } /** * @consult https://help.mirrors.cernet.edu.cn/solus/ */ void os_solus_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_solus); char *cmd = xy_2strcat ("eopkg add-repo Solus ", source.url); chsrc_run (cmd, RunOpt_Default); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/Void-Linux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_voidlinux, "voidlinux"); void os_voidlinux_prelude () { chef_prep_this (os_voidlinux, gsr); chef_set_recipe_created_on (this, "2023-09-24"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-12-18"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@Yangmoooo"); chef_set_os_scope (this); chef_deny_english(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "https://repo-default.voidlinux.org", FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/voidlinux", FeedByPrelude}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/voidlinux", FeedByPrelude}, {&Bfsu, "https://mirrors.bfsu.edu.cn/voidlinux", FeedByPrelude} def_sources_end() chef_set_rest_smURL_with_postfix (this, "/live/20240314/void-live-x86_64-musl-20240314-xfce.iso"); } void os_voidlinux_getsrc (char *option) { char* cmd = "xbps-query -L"; chsrc_run (cmd, RunOpt_No_Last_New_Line); } /** * @consult https://help.mirrors.cernet.edu.cn/voidlinux/ */ void os_voidlinux_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_voidlinux); chsrc_ensure_dir ("/etc/xbps.d"); char *cmd = "cp /usr/share/xbps.d/*-repository-*.conf /etc/xbps.d/"; chsrc_run (cmd, RunOpt_Default); cmd = xy_strcat (3, "sed -i 's|https://repo-default.voidlinux.org|", source.url, "|g' /etc/xbps.d/*-repository-*.conf" ); chsrc_run (cmd, RunOpt_Default); cmd = xy_strcat (3, "sed -i 's|https://alpha.de.repo.voidlinux.org|", source.url, "|g' /etc/xbps.d/*-repository-*.conf" ); chsrc_note2 ("若报错可尝试使用以下命令:"); p (cmd); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } void os_voidlinux_resetsrc (char *option) { os_voidlinux_setsrc (option); } ================================================ FILE: src/recipe/os/YUM/AlmaLinux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_almalinux, "alma/almalinux"); void os_almalinux_prelude () { chef_prep_this (os_almalinux, s); chef_set_recipe_created_on (this, "2024-06-12"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-08-22"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@Yangmoooo"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://repo.almalinux.org/almalinux", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/almalinux", FeedByPrelude}, {&Volcengine, "https://mirrors.volces.com/almalinux", FeedByPrelude}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/almalinux", FeedByPrelude}, {&Zju, "https://mirrors.zju.edu.cn/almalinux", FeedByPrelude}, {&Nju, "https://mirror.nju.edu.cn/almalinux", FeedByPrelude} def_sources_end() #define link "/9.6/isos/x86_64/AlmaLinux-9-latest-x86_64-minimal.iso" chef_set_rest_smURL_with_postfix (this, link); chef_set_provider_smURL (&UpstreamProvider, "https://raw.repo.almalinux.org/almalinux" link); #undef link } /** * @consult: https://developer.aliyun.com/mirror/almalinux */ void os_almalinux_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_almalinux); char *cmd = xy_strcat (3, "sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#\\s*baseurl=https://repo.almalinux.org/almalinux|baseurl=", source.url, "|g' -i.bak /etc/yum.repos.d/almalinux*.repo"); chsrc_run (cmd, RunOpt_Default); chsrc_run ("dnf makecache", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/YUM/Anolis-OS.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_anolis, "anolis/openanolis"); void os_anolis_prelude () { chef_prep_this (os_anolis, s); chef_set_recipe_created_on (this, "2023-09-24"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2024-06-12"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "https://mirrors.openanolis.cn/anolis", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/anolis", DelegateToMirror}, {&Hust, "https://mirrors.hust.edu.cn/anolis", DelegateToMirror} def_sources_end() } /** * @consult: https://mirrors.hust.edu.cn/docs/anolis */ void os_anolis_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_anolis); char *cmd = xy_strcat (3, "sed -i.bak -E 's|https?://(mirrors\\.openanolis\\.cn/anolis)|", source.url, "|g' /etc/yum.repos.d/*.repo"); chsrc_run (cmd, RunOpt_Default); chsrc_run ("dnf makecache", RunOpt_Default); chsrc_run ("dnf update", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/YUM/Fedora-Linux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_fedora, "fedora"); void os_fedora_prelude () { chef_prep_this (os_fedora, sr); chef_set_recipe_created_on (this, "2023-09-26"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-06-20"); chef_set_chef (this, "@happy-game"); chef_set_cooks (this, 2, "@G_I_Y", "@happy-game"); chef_set_sauciers (this, 1, "@ccmywish"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "http://download.example/pub/fedora/linux", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/fedora", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/fedora", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/fedora", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/fedora", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com/fedora", DelegateToMirror} // {&Tencent_Intra, "https://mirrors.tencentyun.com/fedora", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/fedora", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com/fedora", DelegateToMirror}" def_sources_end() } /** * @note fedora 38 及以下版本暂不支持 * * @consult https://mirrors.ustc.edu.cn/help/fedora.html */ void os_fedora_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_fedora); chsrc_alert2 ("Fedora 38 及以下版本暂不支持"); chsrc_backup ("/etc/yum.repos.d/fedora.repo"); chsrc_backup ("/etc/yum.repos.d/fedora-updates.repo"); // 取消对 baseurl 的注释 char* cmd = xy_strcat (5, "sed ", "-i 's|^#baseurl=|baseurl=", "|g' ", "/etc/yum.repos.d/fedora.repo ", "/etc/yum.repos.d/fedora-updates.repo"); chsrc_run (cmd, RunOpt_Default); // 替换 // (1) baseurl=<>/releases/... // (2) baseurl=<>/updates/... cmd = xy_strcat (7, "sed ", "-i -E 's!^baseurl=.*?/(releases|updates)/!baseurl=", source.url, "/\\1/", "!g' ", "/etc/yum.repos.d/fedora.repo ", "/etc/yum.repos.d/fedora-updates.repo"); chsrc_run (cmd, RunOpt_Default); chsrc_alert2 ("已更换baseurl, 但Fedora默认会优先使用metalink来匹配最快的源, 若在获取metadata时速度较慢可自行将其注释:"); chsrc_log2 ("(1) /etc/yum.repos.d/fedora.repo"); chsrc_log2 ("(2) /etc/yum.repos.d/fedora-updates.repo"); chsrc_run ("dnf makecache", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void os_fedora_resetsrc (char *option) { os_fedora_setsrc (option); } ================================================ FILE: src/recipe/os/YUM/Rocky-Linux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_rockylinux, "rocky/rockylinux"); void os_rockylinux_prelude () { chef_prep_this (os_rockylinux, sr); chef_set_recipe_created_on (this, "2023-09-24"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-06-20"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@happy-game"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); def_sources_begin() {&UpstreamProvider, "https://dl.rockylinux.org", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/rocky", DelegateToMirror}, {&Ali, "https://mirrors.aliyun.com/rockylinux", DelegateToMirror}, {&Volcengine, "https://mirrors.volces.com/rockylinux", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/rocky", DelegateToMirror}, {&Sustech, "https://mirrors.sustech.edu.cn/rocky-linux", DelegateToMirror}, {&Zju, "https://mirrors.zju.edu.cn/rocky", DelegateToMirror}, {&Lzuoss, "https://mirror.lzu.edu.cn/rocky", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/rocky", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com/Rocky", DelegateToMirror} def_sources_end() } /** * @consult https://help.mirrors.cernet.edu.cn/rocky/ */ void os_rockylinux_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_rockylinux); char *version_str = xy_run ("sed -nr 's/ROCKY_SUPPORT_PRODUCT_VERSION=\"(.*)\"/\\1/p' " ETC_OS_RELEASE, 0); double version = atof (version_str); char *cmd = NULL; if (version < 9) { cmd = xy_strcat (3, "sed -e 's|^mirrorlist=|#mirrorlist=|g' " "-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=", source.url, "|g' " "-i.bak /etc/yum.repos.d/Rocky-*.repo" ); // Rocky-AppStream.repo // Rocky-BaseOS.repo // Rocky-Extras // Rocky-PowerTools } else { cmd = xy_strcat (3, "sed -e 's|^mirrorlist=|#mirrorlist=|g' " "-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=", source.url, "|g' " "-i.bak /etc/yum.repos.d/rocky-extras.repo /etc/yum.repos.d/rocky.repo" ); } chsrc_run (cmd, RunOpt_Default); chsrc_run ("dnf makecache", RunOpt_No_Last_New_Line); chsrc_conclude (&source); } void os_rockylinux_resetsrc (char *option) { os_rockylinux_setsrc (option); } ================================================ FILE: src/recipe/os/YUM/common.h ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Authors : Aoran Zeng * Contributors : Nil Null * | * Created On : <2024-08-16> * Last Modified : <2024-12-18> * ------------------------------------------------------------*/ // #define OS_Yum_SourceList "/etc/yum.repos" #define OS_Yum_SourceList_D "/etc/yum.repos.d/" #define OS_openEuler_SourceList OS_Yum_SourceList_D "openEuler.repo" ================================================ FILE: src/recipe/os/YUM/openEuler.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_openeuler, "openeuler"); void os_openeuler_prelude () { chef_prep_this (os_openeuler, gs); chef_set_recipe_created_on (this, "2023-09-06"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-06-20"); chef_set_cooks (this, 1, "@G_I_Y"); chef_set_chef (this, NULL); chef_set_sauciers (this, 3, "@ccmywish", "@Yangmoooo", "@happy-game"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "https://repo.openeuler.org/", FeedByPrelude}, {&Ali, "https://mirrors.aliyun.com/openeuler/", FeedByPrelude}, {&Bfsu, "https://mirrors.bfsu.edu.cn/openeuler/", FeedByPrelude}, {&Ustc, "https://mirrors.ustc.edu.cn/openeuler/", FeedByPrelude}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/openeuler/", FeedByPrelude}, {&Tencent, "https://mirrors.tencent.com/openeuler/", FeedByPrelude} // {&Tencent_Intra, "https://mirrors.tencentyun.com/openeuler/", FeedByPrelude}, /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/openeuler/", FeedByPrelude} /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com/openeuler/", FeedByPrelude} def_sources_end() chef_set_rest_smURL_with_postfix (this, "https://repo.openeuler.org/openEuler-24.03-LTS/ISO/x86_64/openEuler-24.03-LTS-netinst-x86_64-dvd.iso"); } /** * chsrc get openeuler */ void os_openeuler_getsrc (char *option) { chsrc_view_file (OS_openEuler_SourceList); } /** * chsrc set openeuler */ void os_openeuler_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_openeuler); chsrc_backup (OS_openEuler_SourceList); // 替换 baseurl=<>/openEuler-xx.xx/... // openEuler-xx.xx 为 openEuler 版本号 // sed -E 's!^baseurl=.*?/openEuler-([^/]+)!baseurl=$(source.url)/openEuler-\1/!g' OS_openEuler_SourceList char* cmd = xy_strcat (6, "sed ", "-i -E 's!^baseurl=.*?/openEuler-([^/]+)!baseurl=", source.url, "openEuler-\\1", "!g' ", OS_openEuler_SourceList); chsrc_run (cmd, RunOpt_Default); chsrc_run ("dnf makecache", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } /** * chsrc reset openeuler */ void os_openeuler_resetsrc (char *option) { os_openeuler_setsrc (option); } ================================================ FILE: src/recipe/os/openSUSE.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_opensuse, "opensuse/opensuse-leap/opensuse-tumbleweed"); void os_opensuse_prelude () { chef_prep_this (os_opensuse, s); chef_set_recipe_created_on (this, "2023-09-17"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-06-20"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@ccmywish", "@G_I_Y"); chef_set_sauciers (this, 0); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note(this, NULL, NULL); def_sources_begin() {&UpstreamProvider, "https://download.opensuse.org/", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/opensuse", DelegateToMirror}, {&Volcengine, "https://mirrors.volces.com/opensuse", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/opensuse", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/opensuse", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/opensuse", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com/opensuse", DelegateToMirror} def_sources_end() } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/opensuse/ */ void os_opensuse_setsrc (char *option) { // chsrc_ensure_root (); chsrc_use_this_source (os_opensuse); while (1) { chsrc_note2 ("请选择你的操作系统为:"); printf ("%s", "1. openSUSE Leap\n" "2. openSUSE Tumbleweed\n" "\n" "==> "); int choice = 0; /* 接受到一个数字时返回1,非法为0,流结束为-1 */ if (scanf ("%d", &choice) != 1) { /* 清除输入缓冲区 */ int ch; while ((ch = getchar()) != '\n' && ch != EOF); chsrc_error2 ("输入无效!请输入数字"); continue; } if (choice == 1) { char *script = xy_str_gsub (RAWSTR_os_openSUSE_leap_in_bash, "@url@", source.url); chsrc_run_as_bash_file (script); break; } if (choice == 2) { char *script = xy_str_gsub (RAWSTR_os_openSUSE_tumbleweed_in_bash, "@url@", source.url); chsrc_run_as_bash_file (script); break; } else { chsrc_error2 ("无效的选择,请输入 1 或 2 "); } } chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/pacman/Arch-Linux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_arch, "arch/archlinux"); #define OS_Pacman_MirrorList "/etc/pacman.d/mirrorlist" #define OS_Pacman_ArchLinuxCN_MirrorList "/etc/pacman.conf" void os_arch_prelude () { chef_prep_this (os_arch, gs); chef_set_recipe_created_on (this, "2023-09-05"); chef_set_recipe_last_updated (this, "2025-10-30"); chef_set_sources_last_updated (this, "2025-06-20"); chef_set_chef (this, "@happy-game"); chef_set_cooks (this, 2, "@ccmywish", "@G_I_Y"); chef_set_sauciers (this, 2, "@happy-game", "@Young-Lord"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note (this, "可额外使用 chsrc set archlinuxcn 来更换 Arch Linux CN Repository 源", "You can additionally use chsrc set archlinuxcn to change Arch Linux CN Repository source"); /** * @note 不要给后面加 / ,因为ARM情况下,还要额外加一个 arm 后缀 */ def_sources_begin() {&UpstreamProvider, "https://repo.archlinux.org", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/archlinux", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/archlinux", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/archlinux", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/archlinux", DelegateToMirror}, {&Sjtug_Siyuan, "https://mirror.sjtu.edu.cn/archlinux", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com/archlinux", DelegateToMirror}, // {&Tencent_Intra, "https://mirrors.tencentyun.com/archlinux", DelegateToMirror}, {&Huawei, "https://mirrors.huaweicloud.com/archlinux", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/archlinux", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com/archlinux", DelegateToMirror} def_sources_end() } void os_arch_getsrc (char *option) { chsrc_view_file (OS_Pacman_MirrorList); } /** * @consult * 1. https://mirrors.tuna.tsinghua.edu.cn/help/archlinux/ * 2. https://mirrors.tuna.tsinghua.edu.cn/help/archlinuxarm/ */ void os_arch_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_arch); chsrc_backup (OS_Pacman_MirrorList); bool is_x86 = false; char *to_write = NULL; char *arch = chsrc_get_cpuarch (); if (strncmp(arch, "x86_64", 6)==0) { is_x86 = true; to_write = xy_strcat (3, "Server = ", source.url, "/$repo/os/$arch\n"); } else { is_x86 = false; to_write = xy_strcat (3, "Server = ", source.url, "arm/$arch/$repo\n"); } /* 配置文件中,越前面的优先级越高 */ chsrc_prepend_to_file (to_write, OS_Pacman_MirrorList); if (is_x86) { chsrc_run ("pacman -Syyu", RunOpt_No_Last_New_Line); } else { chsrc_run ("pacman -Syy", RunOpt_No_Last_New_Line); } chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } /** ------------------------------------------------------------ * archlinuxcn target * ------------------------------------------------------------*/ def_target(os_archlinuxcn, "archlinuxcn/archcn"); void os_archlinuxcn_prelude () { chef_prep_this (os_archlinuxcn, gs); chef_set_recipe_created_on (this, "2023-09-05"); chef_set_recipe_last_updated (this, "2025-09-12"); chef_set_sources_last_updated (this, "2024-07-03"); chef_set_chef (this, NULL); chef_set_cooks (this, 2, "@ccmywish", "@G_I_Y"); chef_set_sauciers (this, 2, "@happy-game", "@Young-Lord"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); chef_set_note (this, "可额外使用 chsrc set arch 来更换 Arch Linux 源", "You can additionally use chsrc set arch to change Arch Linux source"); def_sources_begin() {&UpstreamProvider, "https://repo.archlinuxcn.org/", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/archlinuxcn/", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/archlinuxcn/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/archlinuxcn/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/archlinux-cn/", DelegateToMirror}, {&Tencent, "https://mirrors.cloud.tencent.com/archlinuxcn/", DelegateToMirror}, // {&Tencent_Intra, "https://mirrors.cloud.tencentyun.com/archlinuxcn/", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/archlinux-cn/", DelegateToMirror} def_sources_end() } void os_archlinuxcn_getsrc (char *option) { chsrc_view_file (OS_Pacman_MirrorList); } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/archlinuxcn/ */ void os_archlinuxcn_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (os_archlinuxcn); chsrc_backup (OS_Pacman_ArchLinuxCN_MirrorList); char *arch = chsrc_get_cpuarch (); /* 检查是否已存在 archlinuxcn 配置段 */ char *check_cmd = "grep -q '\\[archlinuxcn\\]' " OS_Pacman_ArchLinuxCN_MirrorList; int ret = xy_run_get_status (check_cmd); if (ret == 0) { char *sed_cmd = xy_strcat (4, "sed -i '/\\[archlinuxcn\\]/{n;s|^Server = .*|Server = ", source.url, "$arch|;}' ", OS_Pacman_ArchLinuxCN_MirrorList); chsrc_run (sed_cmd, RunOpt_Default); } else { char *archlinuxcn_config = xy_strcat (3, "\n[archlinuxcn]\nServer = ", source.url, "$arch\n"); chsrc_append_to_file (archlinuxcn_config, OS_Pacman_ArchLinuxCN_MirrorList); } chsrc_run ("pacman-key --lsign-key \"farseerfc@archlinux.org\"", RunOpt_Dont_Abort_On_Failure); // 此命令可能会失败, 但对换源没有影响 chsrc_run ("pacman -Sy archlinuxcn-keyring", RunOpt_Default); chsrc_run ("pacman -Syy", RunOpt_No_Last_New_Line); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/pacman/MSYS2.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_msys2, "msys2/msys"); void os_msys2_prelude () { chef_prep_this (os_msys2, s); chef_set_recipe_created_on (this, "2023-09-06"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_sources_last_updated (this, "2025-06-20"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@G_I_Y"); chef_set_sauciers (this, 2, "@ccmywish", "@hezonglun"); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); def_sources_begin() {&UpstreamProvider, "https://mirror.msys2.org/", DelegateToUpstream}, {&Ali, "https://mirrors.aliyun.com/msys2", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/msys2", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/msys2", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/msys2", DelegateToMirror}, {&Tencent, "https://mirrors.tencent.com/msys2", DelegateToMirror}, {&Huawei, "https://mirrors.huaweicloud.com/msys2", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Netease, "https://mirrors.163.com/msys2", DelegateToMirror}, /* 不启用原因:过慢 */ // {&Sohu, "https://mirrors.sohu.com/msys2", DelegateToMirror} def_sources_end() } /** * HELP: 未经测试 */ void os_msys2_setsrc (char *option) { chsrc_use_this_source (os_msys2); chsrc_backup ("/etc/pacman.d/mirrorlist.mingw32"); chsrc_backup ("/etc/pacman.d/mirrorlist.mingw64"); chsrc_backup ("/etc/pacman.d/mirrorlist.msys"); char *prev = xy_strcat (3, "请针对你的架构下载安装此目录下的文件:", source.url, "distrib/<架构>/"); chsrc_note2 (prev); char *cmd = xy_strcat (3, "sed -i \"s#https\?://mirror.msys2.org/#", source.url, "#g\" /etc/pacman.d/mirrorlist* "); chsrc_run (cmd, RunOpt_Default); chsrc_conclude (&source); } ================================================ FILE: src/recipe/os/pacman/Manjaro-Linux.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(os_manjaro, "manjaro"); void os_manjaro_prelude () { chef_prep_this (os_manjaro, s); chef_set_recipe_created_on (this, "2023-09-06"); chef_set_recipe_last_updated (this, "2025-08-10"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@G_I_Y"); chef_set_sauciers (this, 0); chef_set_os_scope (this); chef_deny_english(this); chef_deny_user_define(this); def_sources_begin() {&UpstreamProvider, NULL, DelegateToUpstream} /* Manjaro uses GUI tool, no manual sources needed */ def_sources_end() } /** * 似乎会弹出GUI,待确定 */ void os_manjaro_setsrc (char *option) { chsrc_ensure_root (); char *cmd = "pacman-mirrors -i -c China -m rank"; chsrc_run (cmd, RunOpt_Default); chsrc_run ("pacman -Syy", RunOpt_No_Last_New_Line); chsrc_conclude (NULL); } ================================================ FILE: src/recipe/os/rawstr4c.h ================================================ #pragma once /** * Generated by rawstr4c v1.0.0-2025/08/09 */ char RAWSTR_os_openSUSE_leap_in_bash[] = "\x23\x20\xe9\xa6\x96\xe5\x85\x88\xe7\xa6\x81\xe7\x94\xa8\xe5\xae\x98\xe6\x96\xb9\xe8\xbd\xaf\xe4\xbb\xb6\xe6\xba\x90\x0a\x7a\x79\x70\x70\x65\x72\x20\x6d\x72\x20\x2d\x64\x61\x0a\x0a\x7a\x79\x70\x70\x65\x72\x20\x61\x72\x20\x2d\x63\x66\x67\x20\x27\x40\x75\x72\x6c\x40\x2f\x64\x69\x73\x74\x72\x69\x62\x75\x74\x69\x6f\x6e\x2f\x6c\x65\x61\x70\x2f\x24\x72\x65\x6c\x65\x61\x73\x65\x76\x65\x72\x2f\x72\x65\x70\x6f\x2f\x6f\x73\x73\x2f\x27\x20\x6d\x69\x72\x72\x6f\x72\x2d\x6f\x73\x73\x0a\x7a\x79\x70\x70\x65\x72\x20\x61\x72\x20\x2d\x63\x66\x67\x20\x27\x40\x75\x72\x6c\x40\x2f\x64\x69\x73\x74\x72\x69\x62\x75\x74\x69\x6f\x6e\x2f\x6c\x65\x61\x70\x2f\x24\x72\x65\x6c\x65\x61\x73\x65\x76\x65\x72\x2f\x72\x65\x70\x6f\x2f\x6e\x6f\x6e\x2d\x6f\x73\x73\x2f\x27\x20\x6d\x69\x72\x72\x6f\x72\x2d\x6e\x6f\x6e\x2d\x6f\x73\x73\x0a\x7a\x79\x70\x70\x65\x72\x20\x61\x72\x20\x2d\x63\x66\x67\x20\x27\x40\x75\x72\x6c\x40\x2f\x75\x70\x64\x61\x74\x65\x2f\x6c\x65\x61\x70\x2f\x24\x72\x65\x6c\x65\x61\x73\x65\x76\x65\x72\x2f\x6f\x73\x73\x2f\x27\x20\x6d\x69\x72\x72\x6f\x72\x2d\x75\x70\x64\x61\x74\x65\x0a\x7a\x79\x70\x70\x65\x72\x20\x61\x72\x20\x2d\x63\x66\x67\x20\x27\x40\x75\x72\x6c\x40\x2f\x75\x70\x64\x61\x74\x65\x2f\x6c\x65\x61\x70\x2f\x24\x72\x65\x6c\x65\x61\x73\x65\x76\x65\x72\x2f\x6e\x6f\x6e\x2d\x6f\x73\x73\x2f\x27\x20\x6d\x69\x72\x72\x6f\x72\x2d\x75\x70\x64\x61\x74\x65\x2d\x6e\x6f\x6e\x2d\x6f\x73\x73\x0a\x0a\x7a\x79\x70\x70\x65\x72\x20\x61\x72\x20\x2d\x63\x66\x67\x20\x27\x40\x75\x72\x6c\x40\x2f\x75\x70\x64\x61\x74\x65\x2f\x6c\x65\x61\x70\x2f\x24\x72\x65\x6c\x65\x61\x73\x65\x76\x65\x72\x2f\x73\x6c\x65\x2f\x27\x20\x6d\x69\x72\x72\x6f\x72\x2d\x73\x6c\x65\x2d\x75\x70\x64\x61\x74\x65\x0a\x7a\x79\x70\x70\x65\x72\x20\x61\x72\x20\x2d\x63\x66\x67\x20\x27\x40\x75\x72\x6c\x40\x2f\x75\x70\x64\x61\x74\x65\x2f\x6c\x65\x61\x70\x2f\x24\x72\x65\x6c\x65\x61\x73\x65\x76\x65\x72\x2f\x62\x61\x63\x6b\x70\x6f\x72\x74\x73\x2f\x27\x20\x6d\x69\x72\x72\x6f\x72\x2d\x62\x61\x63\x6b\x70\x6f\x72\x74\x73\x2d\x75\x70\x64\x61\x74\x65"; char RAWSTR_os_openSUSE_tumbleweed_in_bash[] = "\x23\x20\xe9\xa6\x96\xe5\x85\x88\xe7\xa6\x81\xe7\x94\xa8\xe5\xae\x98\xe6\x96\xb9\xe8\xbd\xaf\xe4\xbb\xb6\xe6\xba\x90\x0a\x7a\x79\x70\x70\x65\x72\x20\x6d\x72\x20\x2d\x64\x61\x0a\x0a\x7a\x79\x70\x70\x65\x72\x20\x61\x72\x20\x2d\x63\x66\x67\x20\x27\x40\x75\x72\x6c\x40\x2f\x74\x75\x6d\x62\x6c\x65\x77\x65\x65\x64\x2f\x72\x65\x70\x6f\x2f\x6f\x73\x73\x2f\x27\x20\x6d\x69\x72\x72\x6f\x72\x2d\x6f\x73\x73\x0a\x7a\x79\x70\x70\x65\x72\x20\x61\x72\x20\x2d\x63\x66\x67\x20\x27\x40\x75\x72\x6c\x40\x2f\x74\x75\x6d\x62\x6c\x65\x77\x65\x65\x64\x2f\x72\x65\x70\x6f\x2f\x6e\x6f\x6e\x2d\x6f\x73\x73\x2f\x27\x20\x6d\x69\x72\x72\x6f\x72\x2d\x6e\x6f\x6e\x2d\x6f\x73\x73\x0a\x0a\x23\x20\xe5\x88\xb7\xe6\x96\xb0\xe8\xbd\xaf\xe4\xbb\xb6\xe6\xba\x90\x0a\x7a\x79\x70\x70\x65\x72\x20\x72\x65\x66"; ================================================ FILE: src/recipe/os/rawstr4c.md ================================================ # rawstr4c input for OS - prefix = `RAWSTR_os` - output = `:global-variable-only-header` - translate = `:hex` ## openSUSE - namespace = `openSUSE` ### Leap `openSUSE Leap` 换源脚本 ```bash # 首先禁用官方软件源 zypper mr -da zypper ar -cfg '@url@/distribution/leap/$releasever/repo/oss/' mirror-oss zypper ar -cfg '@url@/distribution/leap/$releasever/repo/non-oss/' mirror-non-oss zypper ar -cfg '@url@/update/leap/$releasever/oss/' mirror-update zypper ar -cfg '@url@/update/leap/$releasever/non-oss/' mirror-update-non-oss zypper ar -cfg '@url@/update/leap/$releasever/sle/' mirror-sle-update zypper ar -cfg '@url@/update/leap/$releasever/backports/' mirror-backports-update ``` ### Tumbleweed `openSUSE Tumbleweed` 换源脚本 ```bash # 首先禁用官方软件源 zypper mr -da zypper ar -cfg '@url@/tumbleweed/repo/oss/' mirror-oss zypper ar -cfg '@url@/tumbleweed/repo/non-oss/' mirror-non-oss # 刷新软件源 zypper ref ``` ================================================ FILE: src/recipe/recipe-template.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Name : recipe-template.c * File Authors : 曾奥然 * Contributors : Mikachu2333 * | * Created On : <2024-08-09> * Last Modified : <2026-02-24> * ------------------------------------------------------------- * 本文件作为一个通用模板: * * 为一个【换源目标(target)】定义具体的【换源方法(recipe)】 * * 模版中: * 为该换源目标的名称 * 为该换源目标的类别,仅有3类: pl, os, wr * 分别对应3个子目录: lang, os, ware * ------------------------------------------------------------*/ /* 模版文件内容从下方第30行正式开始 */ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(_); /** * 定义专服务于该target的镜像站,该例数据为虚拟填充 */ static MirrorSite_t RubyMetric = { IS_DedicatedMirrorSite, /* 镜像站类型 */ "rbmt", /* 该镜像站的 code, 可以这么使用: chsrc set rbmt */ /* 该镜像站的英文缩写 | 该镜像站的全名 | 镜像站首页 */ "RubyMetric", "RubyMetric 镜像站", "https://rubymetirc.com", /* 是否跳过测速 | 跳过原因(中文) | 跳过原因(英文) */ {NotSkip, NA, NA, /* 镜像站某个较大的可下载物的下载链接,用于测速 */ "https://rubymetirc.com/target/aws/aws-sdk-go/@v/v1.45.2.zip", /* 是否为精准测速,若使用间接URL来测速,则填ROUGH */ ACCURATE }; } void __prelude (void) { // op 可以为 NOOP|s|sr|gsr|gs, 代表支持 Get Set Reset 三种操作 chef_prep_this (_, op); chef_set_recipe_created_on (this, "2024-08-09"); // 文件创建日期 chef_set_recipe_last_updated (this, "2025-08-12"); // 文件最后一次更新日期 chef_set_sources_last_updated (this, "2025-08-11"); // 镜像源最后一次更新日期 chef_set_chef (this, "@ccmywish"); // recipe 负责人 chef_set_cooks (this, 2, "@ccmywish", "@nilnull"); // recipe 核心作者 // 做了贡献?将自己的信息加在这里! chef_set_sauciers (this, 2, "@nulnone", "@someone"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, UserScope, ScopeCap_Able_But_NotImplemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); // chef_allow_english(this); // 项目是否支持英文 chef_deny_english(this); // chef_allow_user_define(this); // 是否支持用户自定义镜像源 chef_deny_user_define(this); chef_set_note ("中文备注说明...", "English note..."); def_sources_begin() {&UpstreamProvider, "上游默认源链接, 若维护者暂时未知, 可填NULL, 这个主要用于reset", DelegateToUpstream} {&RubyMetric, "https://rubymetirc.com/target", DelegateToMirror}, {&RubyInstaller, "https://rubyinstaller.cn/target", DelegateToMirror}, {&Gitee, "https://gitee.com/RubyMetric/chsrc", DelegateToMirror}, {&GitHub, "https://github.com/RubyMetric/chsrc", "https://一个精准测速链接"} def_sources_end() } /** * @required 非必需 * * 用于 chsrc get */ void __getsrc (char *option) { // chsrc get } /** * @required 必需 * @consult 写明换源实现的参考地址 * * 用于 chsrc set */ void __setsrc (char *option) { /* 下面这行是必须的,注入 source 变量 */ chsrc_use_this_source (_); /* 如果是 target group,你可能想要指定不同的 target 来使用它的源 */ // Source_t source = chsrc_yield_source_and_confirm (&pl_js_group_target, option); /* 具体的换源步骤,如调用第三方命令... */ /* 最后总结输出 */ chsrc_determine_chgtype (ChgType_xxx); chsrc_conclude (&source); } /** * @required 非必需 * * 用于 chsrc reset */ void __resetsrc (char *option) { /* 往往统一在 _setsrc() 中实现,直接调用即可 */ // __setsrc (option); } // 最后,请将自己的文件加入到 menu.c 和 chsrc-main.c 对应的位置 // 形式可以参考其附近的其他食谱 ================================================ FILE: src/recipe/ware/Anaconda/Anaconda.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ #include "rawstr4c.h" def_target(wr_anaconda, "conda/anaconda"); void wr_anaconda_prelude () { chef_prep_this (wr_anaconda, gs); chef_set_recipe_created_on (this, "2023-09-10"); chef_set_recipe_last_updated (this, "2025-08-09"); chef_set_sources_last_updated (this, "2025-07-14"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 2, "@Yangmoooo", "@xyx1926885268"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_deny_user_define(this); def_sources_begin() {&UpstreamProvider, "https://repo.anaconda.com", DelegateToUpstream}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn", DelegateToMirror}, {&Bjtu, "https://mirror.bjtu.edu.cn", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn", DelegateToMirror}, {&Zju, "https://mirrors.zju.edu.cn", DelegateToMirror}, {&Pku, "https://mirrors.pku.edu.cn", DelegateToMirror}, {&NJTech, "https://mirrors.njtech.edu.cn", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn", DelegateToMirror}, {&Sjtug_Siyuan, "https://mirror.sjtu.edu.cn", DelegateToMirror}, {&Lzuoss, "https://mirror.lzu.edu.cn", DelegateToMirror} def_sources_end() } void wr_anaconda_getsrc (char *option) { chsrc_view_file ("~/.condarc"); } /** * @consult https://help.mirrors.cernet.edu.cn/anaconda/ */ void wr_anaconda_setsrc (char *option) { chsrc_use_this_source (wr_anaconda); char *w = xy_str_gsub (RAWSTR_wr_anaconda_condarc, "@1@", source.url); /* Windows 也是在这里 */ char *configfile = xy_2strcat (xy_os_home, "/.condarc"); if (xy.on_windows) { if (xy_file_exist (configfile)) { chsrc_alert2 ("配置文件不存在,将使用 conda 命令创建"); bool conda_exist = chsrc_check_program ("conda"); if (!conda_exist) { chsrc_error ("未找到 conda 命令,请检查是否存在"); exit (Exit_UserCause); } chsrc_run ("conda config --set show_channel_urls yes", RunOpt_Default); } } chsrc_note2 (xy_strcat (3, "请向 ", configfile, " 中手动添加:")); println (w); chsrc_note2 ("然后运行 conda clean -i 清除索引缓存,保证用的是镜像站提供的索引"); chsrc_note2 ("若还需要添加其他第三方源, 可参考: https://help.mirrors.cernet.edu.cn/anaconda/"); chsrc_determine_chgtype (ChgType_SemiAuto); chsrc_conclude (&source); } ================================================ FILE: src/recipe/ware/Anaconda/rawstr4c.h ================================================ #pragma once /** * Generated by rawstr4c v1.0.0-2025/08/09 */ char RAWSTR_wr_anaconda_condarc[] = "\143\150\141\156\156\145\154\163\072\012\040\040\055\040\144\145\146\141\165\154\164\163\012\163\150\157\167\137\143\150\141\156\156\145\154\137\165\162\154\163\072\040\164\162\165\145\012\144\145\146\141\165\154\164\137\143\150\141\156\156\145\154\163\072\012\040\040\055\040\100\061\100\057\141\156\141\143\157\156\144\141\057\160\153\147\163\057\155\141\151\156\012\040\040\055\040\100\061\100\057\141\156\141\143\157\156\144\141\057\160\153\147\163\057\162\012\040\040\055\040\100\061\100\057\141\156\141\143\157\156\144\141\057\160\153\147\163\057\155\163\171\163\062\012\143\165\163\164\157\155\137\143\150\141\156\156\145\154\163\072\012\040\040\143\157\156\144\141\055\146\157\162\147\145\072\040\100\061\100\057\141\156\141\143\157\156\144\141\057\143\154\157\165\144\012\040\040\160\171\164\157\162\143\150\072\040\100\061\100\057\141\156\141\143\157\156\144\141\057\143\154\157\165\144"; ================================================ FILE: src/recipe/ware/Anaconda/rawstr4c.md ================================================ # rawstr4c input - output = `:global-variable-only-header` - translate = `:oct` ## Anaconda - prefix = `RAWSTR_wr_anaconda` - no-postfix = `true` - name = `condarc` ```yaml channels: - defaults show_channel_urls: true default_channels: - @1@/anaconda/pkgs/main - @1@/anaconda/pkgs/r - @1@/anaconda/pkgs/msys2 custom_channels: conda-forge: @1@/anaconda/cloud pytorch: @1@/anaconda/cloud ``` ================================================ FILE: src/recipe/ware/CocoaPods.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(wr_cocoapods, "cocoa/cocoapods/cocoapod"); void wr_cocoapods_prelude () { chef_prep_this (wr_cocoapods, s); chef_set_recipe_created_on (this, "2024-06-08"); chef_set_recipe_last_updated (this, "2025-08-09"); chef_set_sources_last_updated (this, "2025-07-13"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://github.com/CocoaPods/Specs.git", DelegateToUpstream}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/git/CocoaPods/Specs.git", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/git/CocoaPods/Specs.git", DelegateToMirror}, {&Nyist, "https://mirror.nyist.edu.cn/git/CocoaPods/Specs.git", DelegateToMirror} def_sources_end() } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/CocoaPods/ */ void wr_cocoapods_setsrc (char *option) { chsrc_use_this_source (wr_cocoapods); chsrc_note2 ("请手动执行以下命令:"); p("cd ~/.cocoapods/repos"); p("pod repo remove master"); char *git_cmd = xy_strcat (3, "git clone ", source.url, " master"); p(git_cmd); br(); chsrc_note2 ("最后进入项目工程目录,在Podfile中第一行加入:"); char *source_str = xy_strcat (3, "source '", source.url, "'"); p(source_str); chsrc_determine_chgtype (ChgType_Manual); chsrc_conclude (&source); } ================================================ FILE: src/recipe/ware/Docker/Docker.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ #include "rawstr4c.h" static MirrorSite_t DaoCloud = { IS_DedicatedMirrorSite, "daocloud", "DaoCloud","上海道客网络科技有限公司", "https://www.daocloud.io/", // 没有找到 DaoCloud 合适的下载链接,先随便给一个,以规避 chsrc 自动测速时所有 Docker Hub 镜像站都没有测速链接带来的 Bug {NotSkip, NA, NA, "https://qiniu-download-public.daocloud.io/DaoCloud_Enterprise/dce5/offline-community-v0.18.0-amd64.tar", ACCURATE} }, Fit2Cloud = { IS_DedicatedMirrorSite, "fit2cloud", "FIT2CLOUD", "杭州飞致云信息科技有限公司", "https://www.fit2cloud.com/", {SKIP, ToFill, ToFill, NULL, ROUGH} }; def_target(wr_docker, "docker/dockerhub"); void wr_docker_prelude () { chef_prep_this (wr_docker, gs); chef_set_recipe_created_on (this, "2024-06-08"); chef_set_recipe_last_updated (this, "2025-08-09"); chef_set_sources_last_updated (this, "2025-07-14"); chef_set_chef (this, "@happy-game"); chef_set_cooks (this, 2, "@happy-game", "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://hub.docker.com/", DelegateToUpstream}, {&DaoCloud, "https://docker.m.daocloud.io", DelegateToMirror}, {&Fit2Cloud, "https://docker.1panel.live", DelegateToMirror} def_sources_end() } #define WR_Docker_ConfigFile "/etc/docker/daemon.json" void wr_docker_getsrc (char *option) { if (xy.on_linux || xy.on_bsd) { chsrc_view_file (WR_Docker_ConfigFile); } else { chsrc_note2 ("请打开 Docker Desktop 设置"); chsrc_note2 ("选择 'Docker Engine' 选项卡,在该选项卡中找到 'registry-mirrors' 一栏查看"); } } /** * @consult * 1. https://mirrors.ustc.edu.cn/help/dockerhub.html * 2. https://www.cnblogs.com/yuzhihui/p/17461781.html */ void wr_docker_setsrc (char *option) { chsrc_ensure_root (); chsrc_use_this_source (wr_docker); if (xy.on_linux || xy.on_bsd) { char *to_add = xy_str_gsub (RAWSTR_wr_docker_insert_content, "@1@", source.url); if (chsrc_check_file (WR_Docker_ConfigFile)) { chsrc_note2 ("已找到Docker配置文件,将自动换源"); chsrc_backup (WR_Docker_ConfigFile); if (chsrc_check_program_quietly ("jq")) { /* 检查是否已经存在 source.url */ char *cmd = xy_str_gsub (RAWSTR_wr_docker_check_cmd, "@1@", source.url); cmd = xy_str_gsub (cmd, "@2@", WR_Docker_ConfigFile); char *result = xy_run (cmd, 0); if (result && !xy_streql (result, "null")) { chsrc_alert2 ("已存在源,无需重复添加"); } else { cmd = xy_str_gsub (RAWSTR_wr_docker_insert_cmd, "@1@", source.url); cmd = xy_str_gsub (cmd, "@2@", WR_Docker_ConfigFile); chsrc_run (cmd, RunOpt_Default); chsrc_succ2 ("源已添加"); } } else { chsrc_alert2 ("未找到 jq 命令, 将使用 sed 换源"); char *cmd = xy_str_gsub (RAWSTR_wr_docker_sed_command, "@1@", source.url); cmd = xy_str_gsub (cmd, "@2@", WR_Docker_ConfigFile); chsrc_run (cmd, RunOpt_Default); } } else { /* 不存在 /etc/docker/daemon.json 时可以直接写入文件 */ chsrc_alert2 ("未找到Docker配置文件, 将自动创建"); chsrc_ensure_dir ("/etc/docker"); chsrc_run ( xy_2strcat ("touch ", WR_Docker_ConfigFile), RunOpt_Default); chsrc_append_to_file (to_add, WR_Docker_ConfigFile); } if (xy.on_linux) { /* 由于 systemctl restart docker 会导致所有容器停止,所以不自动重启 */ chsrc_alert2 ("请自行运行: sudo systemctl restart docker"); chsrc_alert2 ("该命令会重启所有容器, 请在合适的时机执行"); } else { chsrc_alert2 ("然后请手动重启 docker 服务"); } } else { chsrc_note2 ("请打开 Docker Desktop 设置"); chsrc_note2 ("选择 'Docker Engine' 选项卡,在该选项卡中找到 'registry-mirrors' 一栏,添加镜像地址:"); println (source.url); } chsrc_determine_chgtype (ChgType_SemiAuto); chsrc_conclude (&source); } ================================================ FILE: src/recipe/ware/Docker/README.md ================================================ # Docker - USTC 与 SJTUG 于 2024-06-06 停止支持 DockerHub - NJU 于 2024-06-07 停止支持 DockerHub ================================================ FILE: src/recipe/ware/Docker/rawstr4c.h ================================================ #pragma once /** * Generated by rawstr4c v1.0.0-2025/08/09 */ char RAWSTR_wr_docker_insert_content[] = "\x7b\x0a\x20\x20\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x3a\x20\x5b\x22\x40\x31\x40\x22\x5d\x0a\x7d"; char RAWSTR_wr_docker_check_cmd[] = "\x6a\x71\x20\x27\x2e\x5b\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x5d\x20\x7c\x20\x69\x6e\x64\x65\x78\x28\x22\x40\x31\x40\x22\x29\x27\x20\x40\x32\x40"; char RAWSTR_wr_docker_insert_cmd[] = "\x6a\x71\x20\x27\x2e\x5b\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x5d\x20\x7c\x3d\x20\x5b\x22\x40\x31\x40\x22\x5d\x20\x2b\x20\x2e\x27\x20\x40\x32\x40\x2e\x62\x61\x6b\x20\x3e\x20\x20\x40\x32\x40"; char RAWSTR_wr_docker_sed_command[] = "\x73\x65\x64\x20\x2d\x7a\x20\x2d\x69\x20\x27\x73\x2f\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x3a\x5b\x5e\x5d\x5d\x2a\x5d\x2f\x22\x72\x65\x67\x69\x73\x74\x72\x79\x2d\x6d\x69\x72\x72\x6f\x72\x73\x22\x3a\x5b\x22\x40\x31\x40\x22\x5d\x2f\x27\x20\x40\x32\x40"; ================================================ FILE: src/recipe/ware/Docker/rawstr4c.md ================================================ # rawstr4c input - prefix = `RAWSTR_wr_docker` - output = `:global-variable-only-header` - translate = `:hex` - no-postfix = `true` ## auto insert content - name = `insert_content` ```json { "registry-mirrors": ["@1@"] } ``` ## jq command1 检查配置文件中是否已存在即将要换的源 - name = `check_cmd` ```sh jq '.["registry-mirrors"] | index("@1@")' @2@ ``` ## jq command2 插入新的源到配置文件中 - name = `insert_cmd` ```sh jq '.["registry-mirrors"] |= ["@1@"] + .' @2@.bak > @2@ ``` 注释: `|=` 为赋值, `+ .` 表示把原数组加过来 ## sed command 没有 `jq` 时,用 `sed` 换源,写入到配置文件中 ```sh sed -z -i 's/"registry-mirrors":[^]]*]/"registry-mirrors":["@1@"]/' @2@ ``` 注释: `[^]]*` 即不是 `]` 的所有字符, 后面再跟一个 `]` 由于没有前面的 `[`,因此可被解析为普通字符 `]`,没有特殊含义 ================================================ FILE: src/recipe/ware/Emacs.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ static MirrorSite_t EmacsChina = { IS_DedicatedMirrorSite, "emacschina", "EmacsChina", "Emacs China 社区", "https://elpamirror.emacs-china.org/", {SKIP, ToFill, ToFill, NULL, ROUGH} }; def_target(wr_emacs, "emacs/elpa"); void wr_emacs_prelude () { chef_prep_this (wr_emacs, s); chef_set_recipe_created_on (this, "2023-10-10"); chef_set_recipe_last_updated (this, "2025-08-09"); chef_set_sources_last_updated (this, "2025-07-13"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_deny_user_define(this); chef_set_note (this, "Emacs用户往往只需要一次性换源,只会极少次调用 chsrc,我们只给用户提供文档", "Emacs users typically only need to switch sources once and rarely call chsrc, so we only provide documentation to users"); def_sources_begin() {&UpstreamProvider, "https://elpa.gnu.org/", DelegateToUpstream}, {&MirrorZ, "https://help.mirrors.cernet.edu.cn/elpa/", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/docs/emacs-elpa", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/help/elpa/", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/help/elpa/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/help/elpa.html", DelegateToMirror}, {&Zju, "https://mirrors.zju.edu.cn/docs/elpa/", DelegateToMirror}, {&EmacsChina, "https://elpamirror.emacs-china.org/", DelegateToMirror} def_sources_end() } void wr_emacs_setsrc (char *option) { chsrc_use_this_source (wr_emacs); chsrc_note2 ("Emacs换源涉及Elisp, 需要手动查阅并换源:"); p (source.url); chsrc_determine_chgtype (ChgType_Manual); chsrc_conclude (&source); } ================================================ FILE: src/recipe/ware/Flatpak.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(wr_flatpak, "flatpak/flathub"); void wr_flatpak_prelude () { chef_prep_this (wr_flatpak, gsr); chef_set_recipe_created_on (this, "2023-09-11"); chef_set_recipe_last_updated (this, "2025-08-09"); chef_set_sources_last_updated (this, "2025-05-27"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@jialinlvcn"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); chef_set_note (this, "对Flathub目标进行测速的文件非常小,测速效果严重失真,若你知道可供测速的URL,欢迎参与贡献: chsrc issue", "The test file for Flathub is very small, causing inaccurate speed test results. If you know a URL suitable for speed testing, welcome to contribute: chsrc issue"); def_sources_begin() {&UpstreamProvider, "https://flathub.org/repo", DelegateToUpstream}, {&Sjtug_Siyuan, "https://mirror.sjtu.edu.cn/flathub", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/flathub", DelegateToMirror}, def_sources_end() chef_set_provider_smURL (&UpstreamProvider, "https://flathub.org/repo/flathub.gpg"); /* upstream 默认是 ACCURATE 的,但是我们给了一个超小的文件,测速效果严重失真,所以改为 ROUGH */ chef_set_provider_sm_accuracy (&UpstreamProvider, ROUGH); /** * @note 下述上海交大两个镜像站都可使用,但实际使用时出现过无法访问的情况 (GitHub-#178), * 所以额外定义两个镜像站以更改测速链接为一个很小的文件: flathub.gpg * 若无速度,则证明无法访问。 * 注意,这会使得测速的效果严重失真。 */ chef_set_provider_smURL (&Sjtug_Siyuan, "https://mirror.sjtu.edu.cn/flathub/flathub.gpg"); chef_set_provider_smURL (&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/flathub/flathub.gpg"); /* 由于实在找不到其他可测文件,所以这也只能是 ROUGH */ chef_set_provider_sm_accuracy (&Sjtug_Siyuan, ROUGH); chef_set_provider_sm_accuracy (&Sjtug_Zhiyuan, ROUGH); } void wr_flatpak_getsrc (char *option) { chsrc_run ("flatpak remotes", RunOpt_Default); chsrc_run ("flatpak remote-info flathub", RunOpt_Default); } /** * @consult https://mirrors.sjtug.sjtu.edu.cn/docs/flathub */ void wr_flatpak_setsrc (char *option) { chsrc_use_this_source (wr_flatpak); chsrc_alert2 ("若出现问题,可先调用以下命令:"); char *note = xy_strcat (3, "wget ", source.url, "/flathub.gpg\n" "flatpak remote-modify --gpg-import=flathub.gpg flathub" ); say (note); char *repo_note = "Flathub 中部分软件由于重分发授权问题,需要从官方服务器下载,无法使用镜像站加速\n" "比如 NVIDIA 驱动、JetBrains 系列软件等\n" "尝试运行 flatpak remote-modify flathub --url=https://flathub.org/repo"; say (repo_note); char *cmd = xy_2strcat ("flatpak remote-modify flathub --url=", source.url); chsrc_run (cmd, RunOpt_Default); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void wr_flatpak_resetsrc (char *option) { wr_flatpak_setsrc (option); } ================================================ FILE: src/recipe/ware/Guix.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(wr_guix, "guix"); void wr_guix_prelude () { chef_prep_this (wr_guix, s); chef_set_recipe_created_on (this, "2023-09-11"); chef_set_recipe_last_updated (this, "2025-08-09"); chef_set_sources_last_updated (this, "2025-07-13"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_deny_user_define(this); chef_set_note (this, "目前只有一个源, guixcn 的源不知道是否可用", "Currently only one source available, guixcn source availability unknown"); def_sources_begin() {&UpstreamProvider, "https://git.savannah.gnu.org/git/guix.git", DelegateToUpstream}, {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/git/guix.git", DelegateToMirror} def_sources_end() } /** * @consult https://mirrors.sjtug.sjtu.edu.cn/docs/guix */ void wr_guix_setsrc (char *option) { chsrc_use_this_source (wr_guix); char *file = xy_strcat (3, "(list (channel\n" " (inherit (car %default-channels))\n" " (url \"", source.url, "\")))"); chsrc_note2 ("为防止扰乱配置文件,请手动写入以下内容到 ~/.config/guix/channels.scm 文件中"); p(file); chsrc_determine_chgtype (ChgType_Manual); chsrc_conclude (&source); } ================================================ FILE: src/recipe/ware/Homebrew/Homebrew.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ #include "rawstr4c.h" def_target(wr_homebrew, "brew/homebrew"); void wr_homebrew_prelude () { chef_prep_this (wr_homebrew, gs); chef_set_recipe_created_on (this, "2023-09-10"); chef_set_recipe_last_updated (this, "2025-09-12"); chef_set_sources_last_updated (this, "2025-07-13"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@Word2VecT"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_allow_english(this); chef_deny_user_define(this); chef_set_note (this, "该换源通过写入环境变量实现,若多次换源,请手动清理profile文件", "This source switching is implemented by writing environment variables. If switching sources multiple times, please manually clean the profile file"); def_sources_begin() {&UpstreamProvider, "https://github.com/Homebrew/brew.git", DelegateToUpstream}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/", DelegateToMirror}, {&Nyist, "https://mirror.nyist.edu.cn/", DelegateToMirror} def_sources_end() } void wr_homebrew_getsrc (char *option) { int status = xy_run_get_status (RAWSTR_wr_homebrew_read_config_cmd); if (status != 0) { xy_noop(); } } /** * @consult https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/ * * 自brew 4.0.0 (2023 年 2 月 16日) 起, * HOMEBREW_INSTALL_FROM_API 会成为默认行为,无需设置。大部分用户无需再克隆 homebrew-core 仓库,故无需设置 HOMEBREW_CORE_GIT_REMOTE 环境变量; * 但是为了以防万一,我们还是为用户设置该环境变量 */ void wr_homebrew_setsrc (char *option) { chsrc_use_this_source (wr_homebrew); char *w = xy_str_gsub (RAWSTR_wr_homebrew_config_in_bash, "@1@", source.url); char *zshrc = xy_zshrc; chsrc_backup (zshrc); chsrc_append_to_file (w, zshrc); char *bashrc = xy_bashrc; if (xy_file_exist (bashrc)) { chsrc_backup (bashrc); chsrc_append_to_file (w, bashrc); } char *fishrc = xy_fishrc; if (xy_file_exist (fishrc)) { char *w = xy_str_gsub (RAWSTR_wr_homebrew_config_in_fish, "@1@", source.url); chsrc_backup (fishrc); chsrc_append_to_file (w, fishrc); } chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); chsrc_alert2 ("请重启终端使Homebrew环境变量生效"); } ================================================ FILE: src/recipe/ware/Homebrew/rawstr4c.h ================================================ #pragma once /** * Generated by rawstr4c v1.0.0-2025/08/09 */ char RAWSTR_wr_homebrew_read_config_cmd[] = "echo HOMEBREW_API_DOMAIN=$HOMEBREW_API_DOMAIN;\necho HOMEBREW_BOTTLE_DOMAIN=$HOMEBREW_BOTTLE_DOMAIN;\necho HOMEBREW_BREW_GIT_REMOTE=$HOMEBREW_BREW_GIT_REMOTE;\necho HOMEBREW_CORE_GIT_REMOTE=$HOMEBREW_CORE_GIT_REMOTE;"; char RAWSTR_wr_homebrew_config_in_bash[] = "# ------ chsrc BLOCK BEGIN for Homebrew ------\nexport HOMEBREW_BREW_GIT_REMOTE=\"@1@/git/homebrew/brew.git\"\nexport HOMEBREW_CORE_GIT_REMOTE=\"@1@/git/homebrew/homebrew-core.git\"\n# For Bottles\nexport HOMEBREW_API_DOMAIN=\"@1@/homebrew-bottles/api\"\nexport HOMEBREW_BOTTLE_DOMAIN=\"@1@/homebrew-bottles\"\n# ------ chsrc BLOCK ENDIN for Homebrew ------"; char RAWSTR_wr_homebrew_config_in_fish[] = "# ------ chsrc BLOCK BEGIN for Homebrew ------\nset -x HOMEBREW_BREW_GIT_REMOTE \"@1@/git/homebrew/brew.git\"\nset -x HOMEBREW_CORE_GIT_REMOTE \"@1@/git/homebrew/homebrew-core.git\"\n# For Bottles\nset -x HOMEBREW_API_DOMAIN \"@1@/homebrew-bottles/api\"\nset -x HOMEBREW_BOTTLE_DOMAIN \"@1@/homebrew-bottles\"\n# ------ chsrc BLOCK ENDIN for Homebrew ------"; ================================================ FILE: src/recipe/ware/Homebrew/rawstr4c.md ================================================ # rawstr4c input 该文件尽可能测试 `rawstr4c` 的多个功能,以下三个变量的输出,将分别为: 1. `RAWSTR_wr_homebrew_read_config_cmd` 2. `RAWSTR_wr_homebrew_config_in_bash` 3. `RAWSTR_wr_homebrew_config_in_fish` 若不同,则生成有问题。 - prefix = `RAWSTR_wr_homebrew` - postfix = `:use-language` - translate = `:escape` - output = `:global-variable-only-header` ## Homebrew ### read user env 下面的命令对于 `Bash`、`Zsh` 和 `Fish` 都适用 - name = `RAWSTR_wr_homebrew_read_config_cmd` - name-literally = `true` 测试一下 name-literally 功能 ```bash echo HOMEBREW_API_DOMAIN=$HOMEBREW_API_DOMAIN; echo HOMEBREW_BOTTLE_DOMAIN=$HOMEBREW_BOTTLE_DOMAIN; echo HOMEBREW_BREW_GIT_REMOTE=$HOMEBREW_BREW_GIT_REMOTE; echo HOMEBREW_CORE_GIT_REMOTE=$HOMEBREW_CORE_GIT_REMOTE; ``` ### Bash config - no-prefix = `false` - no-postfix = `true` - name = `config_in_bash` 测试一下 no-postfix 功能 ```bash # ------ chsrc BLOCK BEGIN for Homebrew ------ export HOMEBREW_BREW_GIT_REMOTE="@1@/git/homebrew/brew.git" export HOMEBREW_CORE_GIT_REMOTE="@1@/git/homebrew/homebrew-core.git" # For Bottles export HOMEBREW_API_DOMAIN="@1@/homebrew-bottles/api" export HOMEBREW_BOTTLE_DOMAIN="@1@/homebrew-bottles" # ------ chsrc BLOCK ENDIN for Homebrew ------ ``` ### Fish config - name = `config` - language = `fish` `Fish` 没有高亮,所以我们这里的 code block 用 `bash` 指示 ,我们刚好测试一下 language 功能 ```bash # ------ chsrc BLOCK BEGIN for Homebrew ------ set -x HOMEBREW_BREW_GIT_REMOTE "@1@/git/homebrew/brew.git" set -x HOMEBREW_CORE_GIT_REMOTE "@1@/git/homebrew/homebrew-core.git" # For Bottles set -x HOMEBREW_API_DOMAIN "@1@/homebrew-bottles/api" set -x HOMEBREW_BOTTLE_DOMAIN "@1@/homebrew-bottles" # ------ chsrc BLOCK ENDIN for Homebrew ------ ``` ================================================ FILE: src/recipe/ware/Nix.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(wr_nix, "nix"); /** * 运行时检查到底是哪种安装方式 * * https://github.com/RubyMetric/chsrc/issues/337 */ bool wr_nix_is_multi_user_installation_mode () { if (xy_file_exist ("/nix/var/nix/daemon-socket/socket")) { return true; } else { return false; } } void wr_nix_prelude () { chef_prep_this (wr_nix, s); chef_set_recipe_created_on (this, "2023-09-26"); chef_set_recipe_last_updated (this, "2026-02-24"); chef_set_sources_last_updated (this, "2025-07-13"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 0); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Able_And_Implemented); if (wr_nix_is_multi_user_installation_mode ()) { chef_set_default_scope (this, SystemScope); } else { chef_set_default_scope (this, UserScope); } chef_deny_english(this); chef_deny_user_define(this); def_sources_begin() {&UpstreamProvider, "https://channels.nixos.org/", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/nix-channels/", DelegateToMirror}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/nix-channels/", DelegateToMirror}, {&Nju, "https://mirror.nju.edu.cn/nix-channels/", DelegateToMirror}, {&Ustc, "https://mirrors.ustc.edu.cn/nix-channels/", DelegateToMirror}, {&Sjtug_Siyuan, "https://mirror.sjtu.edu.cn/nix-channels/", DelegateToMirror}, {&Bfsu, "https://mirrors.bfsu.edu.cn/nix-channels/", DelegateToMirror}, {&Iscas, "https://mirror.iscas.ac.cn/nix-channels/", DelegateToMirror} def_sources_end() } void wr_nix_check_cmd () { chsrc_ensure_program ("nix-channel"); } /** * @consult * 1. https://help.mirrors.cernet.edu.cn/nix-channels/ * 2. https://gitee.com/RubyMetric/chsrc/issues/I83894 * 3. https://github.com/RubyMetric/chsrc/issues/337 */ void wr_nix_setsrc (char *option) { wr_nix_check_cmd (); chsrc_use_this_source (wr_nix); char *user_scope_nix_config = "~/.config/nix/nix.conf"; char *system_scope_nix_config = "/etc/nix/nix.conf"; char *config_file = NULL; if (wr_nix_is_multi_user_installation_mode ()) { config_file = system_scope_nix_config; } else { config_file = user_scope_nix_config; } char *cmd = xy_strcat (3, "nix-channel --add ", source.url, "nixpkgs-unstable nixpkgs"); chsrc_run (cmd, RunOpt_Default); char *w = xy_strcat (3, "substituters = ", source.url, "store https://cache.nixos.org/"); chsrc_append_to_file (w, config_file); chsrc_run ("nix-channel --update", RunOpt_Default); chsrc_note2 ("若你使用的是NixOS,请确认你的系统版本(如22.11),并手动运行:"); cmd = xy_strcat (3, "nix-channel --add ", source.url, "nixpkgs- nixpkgs"); p(cmd); cmd = xy_strcat (3, "nix.settings.substituters = [ \"", source.url, "store\" ];"); chsrc_note2 ("若你使用的是NixOS,请额外添加下述内容至 configuration.nix 中"); p(cmd); chsrc_determine_chgtype (ChgType_SemiAuto); chsrc_conclude (&source); } ================================================ FILE: src/recipe/ware/TeX-Live.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * MiKTeX 和 TeX Live 都是流行的 LaTeX 发行版,但它们是不同的实现 * ------------------------------------------------------------*/ def_target(wr_tex, "latex/ctan/tex/texlive/miktex/tlmgr/mpm"); void wr_tex_prelude () { chef_prep_this (wr_tex, gs); chef_set_recipe_created_on (this, "2023-09-10"); chef_set_recipe_last_updated (this, "2025-08-09"); chef_set_sources_last_updated (this, "2025-07-28"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@Mikachu2333"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://ctan.org/tex-archive/systems/texlive/tlnet", DelegateToUpstream}, {&MirrorZ, "https://mirrors.cernet.edu.cn/CTAN/systems/texlive/tlnet", DelegateToMirror}, {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/ctan/systems/texlive/tlnet", "https://mirrors.sustech.edu.cn/CTAN/systems/texlive/tlnet/archive/fandol.tar.xz"}, {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet", "https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet/archive/fandol.tar.xz"}, {&Bfsu, "https://mirrors.bfsu.edu.cn/CTAN/systems/texlive/tlnet", "https://mirrors.bfsu.edu.cn/CTAN/systems/texlive/tlnet/archive/fandol.tar.xz"}, {&Bjtu, "https://mirror.bjtu.edu.cn/ctan/systems/texlive/tlnet", "https://mirror.bjtu.edu.cn/ctan/systems/texlive/tlnet/archive/fandol.tar.xz"}, {&Lzuoss, "https://mirror.lzu.edu.cn/CTAN/systems/texlive/tlnet", "https://mirror.lzu.edu.cn/CTAN/systems/texlive/tlnet/archive/fandol.tar.xz"}, {&Jlu, "https://mirrors.jlu.edu.cn/CTAN/systems/texlive/tlnet", "https://mirrors.jlu.edu.cn/CTAN/systems/texlive/tlnet/archive/fandol.tar.xz"}, {&Sustech, "https://mirrors.sustech.edu.cn/CTAN/systems/texlive/tlnet", "https://mirrors.sustech.edu.cn/CTAN/systems/texlive/tlnet/archive/fandol.tar.xz"} def_sources_end() } void wr_tex_check_cmd (bool *tlmgr_exist, bool *mpm_exist) { *tlmgr_exist = chsrc_check_program ("tlmgr"); *mpm_exist = chsrc_check_program ("mpm"); if (!*tlmgr_exist && !*mpm_exist) { chsrc_error ("未找到 tlmgr 或 mpm 命令,请检查是否存在(其一)"); exit (Exit_UserCause); } } void wr_tex_getsrc (char *option) { bool tlmgr_exist, mpm_exist; wr_tex_check_cmd (&tlmgr_exist, &mpm_exist); if (tlmgr_exist) { chsrc_run("tlmgr option repository", RunOpt_Default); } if (mpm_exist) { chsrc_run("mpm --get-repository", RunOpt_Default); } } /** * @consult https://help.mirrors.cernet.edu.cn/CTAN/ */ void wr_tex_setsrc (char *option) { bool tlmgr_exist, mpm_exist; wr_tex_check_cmd (&tlmgr_exist, &mpm_exist); chsrc_use_this_source (wr_tex); char *cmd = NULL; if (tlmgr_exist) { cmd = xy_2strcat ("tlmgr option repository ", source.url); chsrc_run (cmd, RunOpt_Default); } if (mpm_exist) { char *miktex_url = xy_2strcat (xy_str_delete_suffix (source.url, "texlive/tlnet"), "win32/miktex/tm/packages/"); cmd = xy_2strcat ("mpm --set-repository=", miktex_url); chsrc_run (cmd, RunOpt_Default); } chsrc_determine_chgtype (ChgType_Untested); chsrc_conclude (&source); } ================================================ FILE: src/recipe/ware/WinGet.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------*/ def_target(wr_winget, "winget"); void wr_winget_prelude () { chef_prep_this (wr_winget, gsr); chef_set_recipe_created_on (this, "2024-06-07"); chef_set_recipe_last_updated (this, "2025-08-17"); chef_set_sources_last_updated (this, "2025-07-13"); chef_set_chef (this, NULL); chef_set_cooks (this, 1, "@ccmywish"); chef_set_sauciers (this, 1, "@Mikachu2333"); chef_set_scope_cap (this, ProjectScope, ScopeCap_Unable); chef_set_scope_cap (this, UserScope, ScopeCap_Able_And_Implemented); chef_set_scope_cap (this, SystemScope, ScopeCap_Unable); chef_set_default_scope (this, UserScope); chef_deny_english(this); chef_allow_user_define(this); def_sources_begin() {&UpstreamProvider, "https://cdn.winget.microsoft.com/cache", NULL}, {&MirrorZ, "https://mirrors.cernet.edu.cn/winget-source", NULL}, {&Ustc, "https://mirrors.ustc.edu.cn/winget-source", NULL}, {&Nju, "https://mirror.nju.edu.cn/winget-source", NULL}, {&Nyist, "https://mirror.nyist.edu.cn/winget-source", NULL}, {&Xjtu, "https://mirrors.xjtu.edu.cn/winget-source", NULL} def_sources_end() } void wr_winget_getsrc (char *option) { chsrc_run ("winget source list", RunOpt_Default); } /** * @consult https://mirrors.ustc.edu.cn/help/winget-source.html */ void wr_winget_setsrc (char *option) { chsrc_use_this_source (wr_winget); // 2025.8.17 此前用户可能隐式使用默认源导致 remove 失败,故使用 Dont_Abort chsrc_run ("winget source remove winget", RunOpt_Dont_Abort_On_Failure); // 2025.8.18 执行两次相同的命令后继续设置,无报错,换源成功。具体原因不明 chsrc_run ("winget source remove winget", RunOpt_Dont_Abort_On_Failure); chsrc_run (xy_strcat (3, "winget source add winget ", source.url, " --trust-level trusted"), RunOpt_Default); chsrc_determine_chgtype (ChgType_Auto); chsrc_conclude (&source); } void wr_winget_resetsrc (char *option) { chsrc_run ("winget source reset winget", RunOpt_Default); chsrc_determine_chgtype (ChgType_Reset); chsrc_conclude (NULL); } ================================================ FILE: src/resource/chsrc.rc ================================================ // chsrc.rc - Windows Resource Script // SPDX-License-Identifier: GPL-3.0-or-later #include #include "../framework/version.h" // 图标资源 IDI_ICON1 ICON DISCARDABLE "logo.ico" // 版本信息 VS_VERSION_INFO VERSIONINFO FILEVERSION Chsrc_Version_Major,Chsrc_Version_Minor,Chsrc_Version_Patch,Chsrc_Version_Pre // 经实验,该值会被下面的 ProductVersion 替换 // PRODUCTVERSION Chsrc_Version_Major,Chsrc_Version_Minor,Chsrc_Version_Patch,Chsrc_Version_Pre FILEFLAGSMASK VS_FFI_FILEFLAGSMASK #ifdef _DEBUG FILEFLAGS VS_FF_DEBUG #else FILEFLAGS 0x0L #endif FILEOS VOS_NT_WINDOWS32 FILETYPE VFT_APP FILESUBTYPE VFT2_UNKNOWN BEGIN BLOCK "StringFileInfo" BEGIN BLOCK "040904b0" BEGIN VALUE "CompanyName", "RubyMetric" VALUE "FileDescription", "Change Source everywhere for every software" // 经实验,该值会被上面的 FILEVERSION 替换 // VALUE "FileVersion", Chsrc_Version VALUE "InternalName", "chsrc" VALUE "LegalCopyright", "Copyright (c) 2023-2026 RubyMetric" VALUE "OriginalFilename", "chsrc.exe" VALUE "ProductName", "chsrc" VALUE "ProductVersion", Chsrc_Version VALUE "Comments", "Released on " Chsrc_Release_Date " (GPLv3+)" VALUE "LegalTrademarks", "RubyMetric" END END BLOCK "VarFileInfo" BEGIN VALUE "Translation", 0x0804, 1200, 0x0409, 1200 // 简体中文和英语 END END ================================================ FILE: test/cli.pl ================================================ #!/usr/bin/env perl # --------------------------------------------------------------- # Test File : cli.pl # Test Authors : 曾奥然 # Contributors : Nil Null # Created On : <2024-06-05> # Last Modified : <2026-02-24> # # 测试 chsrc 可执行文件 # --------------------------------------------------------------- =encoding utf8 本文件可以使用 $ podchecker .test\cli.pl 来检查 Pod 语法 $ pod2html .\test\cli.pl > CLI.html 来生成 HTML 文件 $ pod2markdown .\test\cli.pl > CLI.md 来生成 Markdown 文件 =over =item C<=end> 的时候前面必须给一个空行,否则 podchecker 不认 =item 每一个 Pod block 必须用 C<=cut> 来终结,否则 VS Code 会把后续内容全部认为是 Pod =back =cut use v5.42; use utf8; # v5.38 还不用强制 utf8,现在代码里(包括注释)只要有其他字符,都必须强制使用 utf8 use Test::More; # `` 执行命令以后,返回的全部是字节流,而不是字符串, # chsrc 已经输出的是 UTF-8 字符串了,所以我们在这里需要 # 设置该选项,将这些字节流编码为 UTF-8 才能得到我们想要的结果 use open qw(:std :encoding(UTF-8)); =begin comment 注意: `./chsrc` 在 Windows 上也是可以正常执行的,Perl 应该是内部进行了转换。然而,下面这一行却会报错: `./chsrc get -no-color 2>&1` 2>&1重定向虽然在 Windows CMD 中是正确的,但是在 Perl 中执行,却会反而把 ./chsrc 的问题给报出来。 因此,我们需要对执行的命令进行处理 =end comment =cut my $CHSRC = ($^O eq 'MSWin32') ? '.\chsrc' : './chsrc'; my $version_str_cn = qr|chsrc .*\n\nCopyright .*\n许可证 GPLv3\+:GNU GPL 第 3 版或更高版本|; like `$CHSRC -v`, $version_str_cn, 'chsrc -v'; like `$CHSRC --version`, $version_str_cn, 'chsrc --version'; like `$CHSRC version`, $version_str_cn, 'chsrc version'; my $version_str_en = qr|chsrc .*\n\nCopyright .*\nLicense GPLv3\+: GNU GPL version 3 or later|; like `$CHSRC -v -en`, $version_str_en, 'chsrc -v -en'; like `$CHSRC -en -v`, $version_str_en, 'chsrc -en -v'; like `$CHSRC --version -en`, $version_str_en, 'chsrc --version -en'; like `$CHSRC version -en`, $version_str_en, 'chsrc version -en'; my $help_str = qr/^ (help|list|get|set|reset)/m; like `$CHSRC -h`, $help_str, 'chsrc -h'; like `$CHSRC --help`, $help_str, 'chsrc --help'; like `$CHSRC help`, $help_str, 'chsrc help'; like `$CHSRC`, $help_str, 'chsrc'; =begin comment 测试 chsrc list =end comment =cut my $list_str = qr/mirrorz\s*MirrorZ\s*.*\ntuna\s*TUNA/; like `$CHSRC ls`, $list_str, 'chsrc ls'; like `$CHSRC list mirrors`, $list_str, 'chsrc list mirrors'; like `$CHSRC list os`, qr/netbsd\s*openbsd/, 'chsrc list os'; like `$CHSRC list ware`, qr/brew\s*homebrew/, 'chsrc list ware'; =begin comment 测试 chsrc get =end comment =cut my $get_null = qr/chsrc: 请提供想要查看源的目标名/u; like `$CHSRC get -no-color 2>&1`, $get_null, 'chsrc get -no-color'; my $fake_target_name = qr/暂不支持的换源目标/; like `$CHSRC get fake_target_name 2>&1`, $fake_target_name, 'chsrc get fake_target_name'; if ((defined $ARGV[0]) && ($ARGV[0] eq 'fastcheck')) { say "Fast checking, done testing."; done_testing; exit 0; } my $has_ruby = system 'ruby -v'; if ($has_ruby == 0) { say "Ruby exists. Go on testing."; } else { say "No Ruby. End testing."; done_testing; exit 0; } my $get_ruby = qr/gem sources/; like `$CHSRC get ruby`, $get_ruby, 'chsrc get ruby'; =begin comment 测试 chsrc measure =end comment =cut my $measure_ruby = qr/Ruby China 社区/; like `$CHSRC measure ruby`, $measure_ruby, 'chsrc measure ruby'; =begin comment 测试 chsrc set 以及 chsrc reset =end comment =cut my $set_ruby_abcd = qr/镜像站.*不存在/; my $set_ruby_first = qr/全自动换源完成, 感谢镜像提供方/; my $set_ruby = qr/全自动换源完成, 感谢镜像提供方/; my $reset_ruby = qr/选中镜像站.*Upstream.*已重置为上游默认源/s; my $set_ruby_rubychina = qr/Ruby China 社区/; my $set_ruby_locally = qr/bundle config --local/; like `$CHSRC set ruby abcd 2>&1`, $set_ruby_abcd, 'chsrc set ruby abcd'; like `$CHSRC set ruby first`, $set_ruby_first, 'chsrc set ruby first'; like `$CHSRC set ruby`, $set_ruby, 'chsrc set ruby'; like `$CHSRC reset ruby`, $reset_ruby, 'chsrc reset ruby'; like `$CHSRC set ruby rubychina`, $set_ruby_rubychina, 'chsrc set ruby rubychina'; like `$CHSRC set -scope=project ruby first`, $set_ruby_locally, 'chsrc set -scope=project ruby first'; done_testing; ================================================ FILE: test/fw.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: GPL-3.0-or-later * ------------------------------------------------------------- * File Authors : @ccmywish * Contributors : @Mikachu2333 * Created On : <2024-12-14> * Last Modified : <2025-12-29> * * 该文件最好启用 DEBUG mode 编译 * ------------------------------------------------------------*/ #define Chsrc_Version "Frameworker" #include "../src/framework/core.c" int main (int argc, char const *argv[]) { if (argc == 2 && xy_streql (argv[1], "--version")) { say (Chsrc_Version); return 0; } xy_init (); br(); chsrc_log ("chsrc_log"); chsrc_succ ("chsrc_succ"); chsrc_info ("chsrc_info"); chsrc_warn ("chsrc_warn"); chsrc_error ("chsrc_error"); chsrc_debug ("fw", "chsrc_debug"); chsrc_verbose ("chsrc_verbose"); chsrc_succ2 ("chsrc_succ2"); chsrc_log2 ("chsrc_log2"); chsrc_note2 ("chsrc_note2"); chsrc_alert2 ("chsrc_alert2"); chsrc_warn2 ("chsrc_warn2"); chsrc_error2 ("chsrc_error2"); chsrc_debug2 ("fw", "chsrc_debug2"); chsrc_verbose2 ("chsrc_verbose2"); // 现在 chsrc_check_program() 无法检测本目录文件了 assert (chsrc_check_program ("curl")); assert (chsrc_check_program_quietly ("curl")); assert (chsrc_check_program_quietly_when_exist ("curl")); chsrc_ensure_program ("curl"); chsrc_ensure_dir ("test"); char *tmpfile_content = "Line2\n\nLine3\n"; char *tmpfile_name = "test"; char *tmpfile_ext = ".txt"; char *tmpfile = ""; FILE *tmp = chsrc_make_tmpfile (tmpfile_name, tmpfile_ext, true, &tmpfile); fwrite (tmpfile_content, sizeof (char), strlen (tmpfile_content), tmp); fclose (tmp); char *tmpfile_bk = xy_2strcat (tmpfile, ".bak"); assert (xy_file_exist (tmpfile)); chsrc_backup (tmpfile);\ assert (chsrc_check_file (tmpfile_bk)); remove (tmpfile_bk); chsrc_append_to_file ("Line4\n", tmpfile); assert_str (xy_file_read (tmpfile), "Line2\n\nLine3\nLine4\n"); chsrc_prepend_to_file ("Line1 \n", tmpfile); assert_str (xy_file_read (tmpfile), "Line1 \nLine2\n\nLine3\nLine4\n"); chsrc_overwrite_file ("Line999 \nLine998\nLine997\n", tmpfile); assert_str (xy_file_read (tmpfile), "Line999 \nLine998\nLine997\n"); chsrc_log (xy_2strcat ("CPU arch = ", chsrc_get_cpuarch ())); print ("chsrc: CPU cores = "); println (chsrc_get_cpucore ()); remove (tmpfile); // chsrc_run_in_inline_pwsh_shell ("Write-Host \"Hello from inline PowerShell\""); if (xy.on_windows) { chsrc_view_env ("OS", "windir", NULL); chsrc_run_as_pwsh_file ("Write-Host \"Hello from PowerShell (v7) file\""); chsrc_run_as_powershellv5_file ("Write-Host \"Hello from PowerShell v5 file\""); chsrc_run_as_powershell_file ("Write-Host \"Hello from PowerShell file\""); } else { chsrc_view_env ("PWD", NULL); chsrc_run_as_sh_file ("echo Hello from sh file"); chsrc_run_as_bash_file ("echo Hello from Bash file"); } chsrc_run ("echo " Chsrc_Version " test pass!", RunOpt_Dont_Notify_On_Success); return 0; } ================================================ FILE: test/xy.c ================================================ /** ------------------------------------------------------------ * SPDX-License-Identifier: MIT * ------------------------------------------------------------- * Test File : xy.c * Test Authors : 曾奥然 * Contributors : Mikachu2333 * Created On : <2023-08-30> * Last Modified : <2025-08-27> * * Test xy.h * ------------------------------------------------------------*/ #include "xy.h" void callback_print_str_for_seq (void *str, void *DUMMY) { print ((char *) str); } void callback_print_str_for_map (const char *key, void *value, void *DUMMY) { println (xy_strcat (4, " ", key, " => ", (char *) value)); } int main (int argc, char const *argv[]) { xy_init (); // 关闭颜色 // xy.enable_color = false; println (xy_os_depend_str ("Hello, Windows!", "Hello, Unix!")); println (3); double dbl = 3.1415; println (dbl); say (xy_2strcat ("Xi", "'an")); say (xy_strcat (2, "Xi", "'an")); say (xy_strcat (3, "屈身守分,", "以待天时,", "不可与命争也")); say (xy_strcat (4, "水落鱼梁浅,", "天寒梦泽深。", "羊公碑字在,", "读罢泪沾襟。")); say (xy_strcat (6, "楚山横地出,", "汉水接天回。", "冠盖非新里,", "章华即旧台。", "习池风景异,", "归路满尘埃。")); print (xy_str_to_bold ("粗体")); print (xy_str_to_faint ("浅体")); print (xy_str_to_italic ("斜体")); print (xy_str_to_underline ("下划线")); print (xy_str_to_blink ("闪烁")); print (xy_str_to_cross ("删除线")); print (xy_str_to_red ("红色")); print (xy_str_to_green ("绿色")); print (xy_str_to_yellow ("黄色")); print (xy_str_to_blue ("蓝色")); print (xy_str_to_magenta ("紫色")); print (xy_str_to_purple ("紫色")); print (xy_str_to_cyan ("青色")); br(); xy_log ("普通", "输出普通内容"); xy_succ ("成功", "输出成功内容"); xy_info ("信息", "输出信息内容"); xy_warn ("警告", "输出警告内容"); xy_error ("错误", "输出错误内容"); xy_log_brkt ("xy.h", "普通", "咸阳油泼面筋道十足辣子香"); xy_succ_brkt ("xy.h", "成功", "西安花干鸡蛋肉夹馍已出炉"); xy_info_brkt ("xy.h", "信息", "襄阳牛肉面搭配黄酒更美味"); xy_warn_brkt ("xy.h", "警告", "兰州牛肉面,而非兰州拉面"); xy_error_brkt ("xy.h", "错误", "西安肉丸胡辣汤里没有肉丸"); assert (xy_streql ("abc", "abc")); assert (xy_streql_ic ("abc", "abc")); assert (false == xy_streql ("abc", "abC")); assert (true == xy_streql_ic ("abc", "abC")); assert (false == xy_str_end_with ("abcdef", "abcdefg")); assert (xy_str_end_with ("abcdef", "def")); assert (xy_str_end_with ("abcdef", "bcdef")); assert (xy_str_end_with ("abcdef", "abcdef")); assert (xy_str_end_with ("abcdef", "")); assert (false == xy_str_start_with ("abcdef", "abcdefg")); assert (xy_str_start_with ("abcdef", "abc")); assert (xy_str_start_with ("abcdef", "abcde")); assert (xy_str_start_with ("abcdef", "abcdef")); assert (xy_str_start_with ("abcdef", "")); assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "cdef")); assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "cdefgh")); assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "")); assert_str ("abcd", xy_str_delete_suffix ("abcdefg", "efg")); assert_str ("abcdefg", xy_str_delete_prefix ("abcdefg", "cdef")); assert_str ("abcdefg", xy_str_delete_prefix ("abcdefg", "0abcde")); assert_str ("abcdefg", xy_str_delete_prefix ("abcdefg", "")); assert_str ("defg", xy_str_delete_prefix ("abcdefg", "abc")); assert_str ("defdef", xy_str_gsub ("abcdefabcdef", "abc", "")); // 删除 assert_str ("6def6def", xy_str_gsub ("abcdefabcdef", "abc", "6")); // 缩小 assert_str ("XIANGdefXIANGdef", xy_str_gsub ("abcdefabcdef", "abc", "XIANG")); // 扩张 assert_str ("DEFdefDEFdef", xy_str_gsub ("abcdefabcdef", "abc", "DEF")); // 等量 assert (xy_file_exist ("./doc/image/chsrc.png")); assert (xy_dir_exist ("~")); if (xy.on_windows) { say (xy_normalize_path ("~")); say (xy_normalize_path ("~/")); // xy_parent_dir() 得到的结果一定是不包含尾斜杠的目录名 assert_str (xy_parent_dir (xy_normalize_path ("~")), "C:\\Users"); assert_str (xy_parent_dir (xy_normalize_path ("~/")), "C:\\Users"); assert (xy_dir_exist ("C:\\Users")); say (xy_win_powershell_profile); say (xy_win_powershellv5_profile); assert (xy_file_exist (xy_win_powershell_profile)); assert (true == xy_file_exist (xy_win_powershellv5_profile)); assert_str (xy_normalize_path ("C:\\a bc\\def\\"), "C:\\a bc\\def\\"); assert_str (xy_normalize_path ("a/b c/d"), "a\\b c\\d"); assert_str (xy_normalize_path ("a/b c/d/"), "a\\b c\\d\\"); assert_str (xy_parent_dir ("a/b c/d"), "a\\b c"); assert_str (xy_parent_dir ("a/b c\\d/"), "a\\b c"); } else { /** * debuild 过程会创建虚拟的 HOME 环境,导致检查 .bashrc 的测试会失败,所以我们先检查一下 .profile * 如果没有,则大概率也没有 .bashrc */ if (xy_file_exist ("~/.profile")) { assert (xy_file_exist (xy_bashrc)); } assert (xy_dir_exist ("/etc")); } println (xy_normalize_path (" \n ~/haha/test/123 \n\r ")); assert_str (xy_normalize_path ("~/haha/test"), xy_parent_dir (" ~/haha/test/123")); XySeq_t *seq = xy_seq_new (); xy_seq_push (seq, "Hello"); xy_seq_push (seq, "World"); assert_str ("Hello", xy_seq_at (seq, 1)); assert_str ("World", xy_seq_at (seq, 2)); xy_seq_each (seq, callback_print_str_for_seq, NULL); br(); xy_seq_pop (seq); assert (1 == xy_seq_len (seq)); XyMap_t *map = xy_map_new (); xy_map_set (map, "Hello", "World"); xy_map_set (map, "你好", "世界"); assert_str ("World", xy_map_get (map, "Hello")); assert_str ("世界", xy_map_get (map, "你好")); xy_map_set (map, "Hello", "chsrc"); assert_str ("chsrc", xy_map_get (map, "Hello")); assert (2 == xy_map_len (map)); echo ("{"); xy_map_each (map, callback_print_str_for_map, NULL); echo ("}"); xy_succ ("测试完成", "xy.h 测试全部通过"); // xy_unimplemented(); // xy_unsupported(); // xy_unreached(); return 0; } ================================================ FILE: tool/README.md ================================================ # 工具说明 1. `installer.sh` Bash 编写的一键安装工具 2. `installer.ps1` PowerShell 编写的一键安装工具 3. `download-pre-on-GitHub.ps1` 从 GitHub `pre` release 上下载所有附件
## `rawstr4c` C语言字符串生成工具,本身是为了本项目而开发,是放在本仓库下的一个完整子项目。但为了维护更方便,拆分到新的仓库: ================================================ FILE: tool/download-pre-on-GitHub.ps1 ================================================ # --------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # --------------------------------------------------------------- # File Name : download-pre-on-GitHub.ps1 # File Authors : Aoran Zeng # Created On : <2023-09-21> # Last Modified : <2025-08-07> # # Download all files from the `pre` release on GitHub: # https://github.com/RubyMetric/chsrc/releases/tag/pre # --------------------------------------------------------------- $destination = "~\Desktop\chsrc-pre-on-GitHub" Write-Output "=> Mkdir $destination" mkdir -Force $destination | Out-Null $destination = (Resolve-Path $destination).Path $names = @( 'chsrc-x64-windows.exe' 'chsrc-x86-windows.exe' 'chsrc-aarch64-macos' 'chsrc-x64-macos' 'chsrc-x64-linux' 'chsrc-aarch64-linux' 'chsrc-riscv64-linux' 'chsrc-armv7-linux' 'chsrc_latest-1_amd64.deb' 'chsrc-arm64-android' ) # Like https://github.com/RubyMetric/chsrc/releases/download/latest/chsrc-x64-windows.exe $url_prefix = "https://github.com/RubyMetric/chsrc/releases/download/pre/" Write-Output "=> Starting downloads..." $names | ForEach-Object -Parallel { $name = $_ $url = $using:url_prefix + $name $dest = $using:destination Write-Output " - Downloading $name" # -s 阻止输出,避免并行输出混乱 # 此处必须使用绝对路径 curl -s -LO $url --output-dir $dest Write-Output " √ Completed $name" } -ThrottleLimit 5 # 限制同时下载5个文件 Write-Output "=> All downloads completed!" Write-Output "=> Downloaded to $destination" ================================================ FILE: tool/git-ignore-vscode-settings.ps1 ================================================ # --------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # --------------------------------------------------------------- # File Name : git-ignore-vscode-settings.ps1 # File Authors : Aoran Zeng # Created On : <2025-08-17> # Last Modified : <2025-08-17> # # VS Code 的某些操作/插件会频繁地修改我们的 .vscode/settings.json # 文件,非常非常烦人,但是该文件又应该被加入仓库,所以我们使用以下 # 命令来忽略它在本地的变化 # --------------------------------------------------------------- git update-index --skip-worktree .\.vscode\settings.json # 恢复方法是: # git update-index --no-skip-worktree .\.vscode\settings.json ================================================ FILE: tool/installer.ps1 ================================================ # --------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # --------------------------------------------------------------- # File Name : installer.ps1 # File Authors : xuan # | ChatGPT # | 曾奥然 # Contributors : GitHub Copilot # | # Created On : <2024-10-26> # Last Modified : <2026-01-06> # # chsrc installer for Windows # --------------------------------------------------------------- param( [switch] $Help, $Directory = $null, $Version = "pre" ) $script:binary_name = "chsrc" $script:platform = "Windows" $script:install_dir = "" $script:arch = "" $script:target_version = "" $script:url = "" $script:create_dir_flag = $false function Help { Write-Host @" chsrc-installer: Install chsrc on ${script:platform}. Usage: install.ps1 [options] Options: -h Print this help information -d Specify installation directory, default is current directory -v Specify chsrc version "@ } # 检查当前操作系统是否为 macOS 或 Linux if ($IsMacOS -or $IsLinux) { Write-Host @" Sorry, This installer is only available for ${script:platform}. If you're looking for installation instructions for your operating system, please visit the following link: https://github.com/RubyMetric/chsrc "@ exit } if ($Help) { Help exit } function Output_Info () { Write-Host "[INFO] $args" } function Output_Error () { Write-Host "[ERROR] $args" exit 1 } # https://github.com/RubyMetric/chsrc/issues/332 function Get_System_Downloads_Dir { # 尝试从注册表获取实际的 Downloads 文件夹位置 try { $shellFoldersKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" $downloadsGuid = "{374DE290-123F-4565-9164-39C4925E467B}" $downloadsPath = (Get-ItemProperty -Path $shellFoldersKey -Name $downloadsGuid -ErrorAction Stop).$downloadsGuid # 展开环境变量 (例如 %USERPROFILE% -> C:\Users\xxx) $downloadsPath = [System.Environment]::ExpandEnvironmentVariables($downloadsPath) if (Test-Path -Path $downloadsPath -PathType Container) { return $downloadsPath } } catch { # 如果注册表读取失败,不输出错误信息,继续使用后备方案 } # 后备方案:返回 null,稍后使用当前目录 return $null } function Set_Install_Dir { # 如果用户未指定目录,则自动检测 if ($null -eq $Directory) { # 尝试获取实际的 Downloads 目录 $detectedDownloads = Get_System_Downloads_Dir if ($null -ne $detectedDownloads) { $Directory = $detectedDownloads Output_Info "Detected Downloads directory: $Directory" } else { # 使用当前目录作为默认值 $Directory = $PWD.Path Output_Info "Using current directory: $Directory" } } # 检查目录是否存在 if (-not (Test-Path -Path $Directory -PathType Container)) { # 如果目录不存在,执行下面的代码块 try { New-Item -Path $Directory -ItemType Directory -Force | Out-Null Output_Info "Directory created: $Directory" $script:create_dir_flag = $true } catch { Output_Error "Failed to create directory: $_" } } $script:install_dir=$Directory # 输出最终路径 Output_Info "Set install dir to: $script:install_dir" } function Set_Version { $pattern = '^(0\.[1-9]\.[0-9]|pre)$' if ($Version -notmatch $pattern) { Output_Error "Invalid version '$Version'. Please provide a valid version: 0.x.y (>=0.1.4) or 'pre'" } # 设置版本号 $script:target_version=$Version Output_Info "Set chsrc version: $script:target_version" } function Set_URL { # 获取 CPU 型号 $cpuArchitecture = Get-WmiObject Win32_Processor ` | Select-Object -First 1 -ExpandProperty Architecture switch ($cpuArchitecture) { 0 { $script:arch = 'x86' } 9 { # 如果是 64 位操作系统,选择 x64 安装包,否则选择 x86 if ([Environment]::Is64BitOperatingSystem) { $script:arch = "x64" } else { $script:arch = "x86" } } default { Output_Error "Unsupported architecture '$cpuArchitecture'. Only x86 or x64 architectures are supported." } } Output_Info "Get my CPU architecture: $script:arch" # Set URL if ($script:target_version -eq "pre") { $script:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + ` "${script:target_version}/chsrc-${script:arch}-windows.exe" } else { $script:url = "https://gitee.com/RubyMetric/chsrc/releases/download/" + ` "v" + "${script:target_version}/chsrc-${script:arch}-windows.exe" } Output_Info "Set download URL: $script:url" } function Install { try { # 设置安全协议为 TLS 1.2 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # 检查 URL 是否可访问 $response = Invoke-WebRequest -Uri $script:url -Method Head -ErrorAction Stop if ($response.StatusCode -ne 200) { Output_Error "Unable to access $script:url. Status code: $($response.StatusCode)" } } catch { Write-Host "Unable to download ${script:binary_name}. Please check your internet connection." exit 1 } try { $outfile = "\${script:binary_name}.exe" Output_Info "Downloading $script:binary_name (architecture: $script:arch, platform: $script:platform, version: $script:target_version) to $script:install_dir" Invoke-WebRequest -OutFile ($script:install_dir + $outfile) -Uri $script:url -ErrorAction Stop # 🎉 这个符号会变成 ??? 不要添加 Output_Info "Installation completed, destination:" ($script:install_dir + $outfile) } catch { Output_Error "Unable to download $script:binary_name. Error: $_" } } function Cleanup { if ($script:create_dir_flag -eq $true) { if (Test-Path -Path $script:install_dir) { Remove-Item -Path $script:install_dir -Recurse -Force # 删除路径及其内容 Output_Info "Deleted the directory: $script:install_dir" } } } $null = Register-EngineEvent PowerShell.Exiting -Action { Cleanup } # main Set_Install_Dir Set_Version Set_URL Install ================================================ FILE: tool/installer.sh ================================================ #!/usr/bin/env bash # --------------------------------------------------------------- # SPDX-License-Identifier: GPL-3.0-or-later # --------------------------------------------------------------- # File Name : installer.sh # File Authors : GnixAij # | xuan # | ChatGPT # | Aoran Zeng # Contributors : GitHub Copilot # | DeepSeek # | # Created On : <2024-10-25> # Last Modified : <2025-07-12> # # chsrc installer for POSIX (mainly Linux & macOS) # --------------------------------------------------------------- binary_name="chsrc" # 用户指定的安装目录,函数set_install_dir()将填充/校验该变量 userOpt_install_dir="" # 默认安装目录 default_install_dir_4root="/usr/local/bin" default_install_dir_4nonroot="$HOME/.local/bin" # 若用户指定的目录不存在则会创建该目录 tmp_created_install_dir="" userOpt_version="pre" userOpt_help=0 userOpt_lang="zh" info() { echo "[INFO] $*" } warn() { echo "[WARN] $*" } error() { echo "[ERROR] $*" } # 用户自己输入 typeit() { echo "[->] $ $*" } error_exit() { echo -e "[ERROR] $*" >&2 exit 1 } is_zh() { if [ "$userOpt_lang" = "zh" ]; then return 0 else return 1 fi } help() { if is_zh; then echo "chsrc-installer: 在类Unix操作系统上安装 chsrc" echo echo "使用: install.sh [选项]" echo "选项:" echo "-h 打印此帮助信息" echo "-d 指定安装目录,默认为 $default_install_dir_4root 或 $default_install_dir_4nonroot" echo "-v 指定 chsrc 版本" echo "-l 指定输出语言,支持 zh 和 en " echo else echo "chsrc-installer: Install chsrc on any Unix-like OS" echo echo "Usage: install.sh [options]" echo "Options:" echo "-h Print this help information" echo "-d Specify installation directory, default is $default_install_dir_4root OR $default_install_dir_4nonroot" echo "-v Specify chsrc version" echo "-l Specify output language, supports zh and en" echo fi } let_user_build() { if command -v make &>/dev/null; then # 检测是否是 GNU make,如果是则用 make,如果不是则用C编译器来编译 if make --version | grep -q "GNU Make"; then typeit "make" else if is_zh; then warn "检测到非 GNU make,本项目的Makefile仅支持GNU make" else warn "Non-GNU make was detected, the Makefile for this project only supports GNU make" fi typeit "cc/gcc/clang -Iinclude -Ilib src/chsrc-main.c -o chsrc" fi else if is_zh; then warn "未检测到GNU make,需手动执行编译命令:" else warn "GNU make was not detected, so we need to run the compile command manually:" fi typeit "cc/gcc/clang -Iinclude -Ilib src/chsrc-main.c -o chsrc" fi } let_user_use_bootstrap() { info "请参考: https://github.com/RubyMetric/chsrc/tree/main/bootstrap 查找是否存在已有的 boostrapper" } let_user_compile() { source_zip_url="https://gitee.com/RubyMetric/chsrc/repository/archive/main.zip" echo "" if command -v git &>/dev/null; then typeit "git clone https://gitee.com/RubyMetric/chsrc.git" typeit "cd chsrc" let_user_build exit 1 elif command -v curl &>/dev/null; then typeit "curl -LO $source_zip_url" elif command -v wget &>/dev/null; then typeit "wget $source_zip_url" else info "Please download the source code: $source_zip_url" fi typeit "unzip ./main.zip " typeit "cd ./chsrc-main" let_user_build exit 1; } get_arch() { echo $(uname -m | tr '[:upper:]' '[:lower:]') } get_platform() { echo $(uname -s | awk '{print tolower($0)}') } # Linux -> GNU/Linux # Android -> Android get_os() { echo $(uname -o | awk '{print tolower($0)}') } set_arch() { arch=$(get_arch) case "$arch" in x86_64) arch="x64" ;; aarch64|arm64) arch="aarch64" ;; riscv64) arch="riscv64" ;; armv7*) arch="armv7" ;; *) if is_zh; then warn "抱歉, 暂无预编译二进制文件供你所在的平台架构: ${arch} 使用。请使用 chsrc-bootstrap 或 自行编译:" else warn "Sorry, No precompiled binaries for your arch: ${arch}. Please compile it on your own:" fi let_user_use_bootstrap let_user_compile exit 1 ;; esac } set_platform() { platform=$(get_platform) case "$platform" in linux) platform="linux" whatos=$(get_os) if [ "$whatos" = "android" ]; then if is_zh; then info "使用 chsrc-bootstrap: Termux" else info "Use chsrc-bootstrap: Termux" fi wget -O - https://gitee.com/RubyMetric/chsrc/raw/main/bootstrap/Termux.bash | bash exit 1 fi ;; darwin) platform="macos" ;; bsd|dragonfly) platform="bsd" if is_zh; then info "抱歉, 暂无预编译二进制文件供BSD使用。请使用 chsrc-bootstrap 或 自行编译:" else info "Sorry, No precompiled binaries for BSD! Please compile it on your own:" fi let_user_use_bootstrap let_user_compile exit 1 ;; *) if is_zh; then error_exit "抱歉,暂无预编译二进制文件供你所在的平台: ${platform} 使用。请使用 chsrc-bootstrap 或 自行编译:" else error_exit "Sorry, No precompiled binaries for your platform: ${platform}. Please compile it on your own:" fi let_user_use_bootstrap let_user_compile exit 1 ;; esac } set_binary_version() { if [[ ! "$userOpt_version" =~ ^(pre|0\.([1-9])\.([0-9]))$ ]]; then # version 不符合条件,报错 if is_zh; then error_exit "不支持的版本: ${userOpt_version},版本号必须为 0.x.y (>=0.1.4) 或 'pre'" else error_exit "Unsupported version: ${userOpt_version}. Version number must be 0.x.y (>=0.1.4) or 'pre'" fi fi binary_version="${userOpt_version}" # version 版本不是 'pre',添加'v'前缀 if [[ "$userOpt_version" =~ ^(0\.([1-9])\.([0-9]))$ ]]; then binary_version="v${userOpt_version}" fi } # # 1. 若用户指定了安装目录,则安装至那里 # # 若安装目录不存在,该脚本会为用户创建 # # 2. 若用户没有指定安装目录: # # 2.1 若 chsrc 已存在,自动安装到该位置 # 2.2 若 chsrc 不存在,安装到两个预定义的默认安装位置 # set_install_dir() { if [ -n "$userOpt_install_dir" ]; then # 扩展 ~ 符号 userOpt_install_dir="${userOpt_install_dir/#\~/$HOME}" # 检查目录是否存在,如果不存在则创建该目录 if [ ! -d "$userOpt_install_dir" ]; then if is_zh; then echo "目录 $userOpt_install_dir 不存在,正在创建..." else echo "Directory $userOpt_install_dir does not exist. Creating..." fi if ! mkdir -p "$userOpt_install_dir"; then if is_zh; then echo "创建目录失败,请重试" else echo "Failed to create directory, please try again" fi exit 1 fi tmp_created_install_dir="$userOpt_install_dir" # 记录临时安装目录 fi elif existing_path=$(command -v "$binary_name" 2>/dev/null); then if is_zh; then info "$binary_name 已安装,更新路径: ${existing_path}" else info "$binary_name is already installed, updating path: ${existing_path}" fi userOpt_install_dir=$(dirname "$existing_path") if [ ! -w "$userOpt_install_dir" ]; then if is_zh; then error_exit "目录 $userOpt_install_dir 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它目录安装" else error_exit "Directory $userOpt_install_dir is not writable. Please run the script with sudo; or specify another dir using the -d option." fi fi else # 检查默认安装目录 if [ -d "$default_install_dir_4root" ] && [ -w "$default_install_dir_4root" ]; then userOpt_install_dir="$default_install_dir_4root" elif [ -d "$default_install_dir_4nonroot" ] && [ -w "$default_install_dir_4nonroot" ]; then userOpt_install_dir="$default_install_dir_4nonroot" else if is_zh; then error_exit "默认安装目录 $default_install_dir_4root 和 $default_install_dir_4nonroot 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它目录安装" else error_exit "Default install dir $default_install_dir_4root and $default_install_dir_4nonroot is not writable. Please run the script with sudo; or specify another dir using the -d option." fi fi fi } # # @param $1: 下载链接 # @param $2: 保存路径(带文件名) # download() { if command -v curl &>/dev/null; then curl -sL "$1" -o "$2" return $? # 只能单独 return fi if command -v wget &>/dev/null; then # follow 是wget默认行为 wget -qO "$2" "$1" return $? fi if is_zh; then error_exit "缺乏必要的下载工具(curl或wget),无法下载文件" else error_exit "Missing necessary download tools (curl or wget) to download the file!" fi } install() { set_binary_version set_arch set_platform set_install_dir url="https://gitee.com/RubyMetric/chsrc/releases/download/${binary_version}/${binary_name}-${arch}-${platform}" path_to_executable="${userOpt_install_dir}/${binary_name}" if is_zh; then info "下载 ${binary_name} (架构: ${arch}, 平台: ${platform}, 版本: ${binary_version}) 到 ${path_to_executable}" else info "Downloading ${binary_name} (arch: ${arch}, platform: ${platform}, version: ${binary_version}) to ${path_to_executable}" fi if download $url "$path_to_executable"; then chmod +x "$path_to_executable" if is_zh; then info "🎉 安装完成,路径: $path_to_executable" else info "🎉 Installation completed, path: $path_to_executable" fi else if is_zh; then error_exit "下载失败,请检查网络连接和代理设置: ${url}" else error_exit "Download failed, please check your network connection and proxy settings: ${url}" fi fi } cleanup() { if [ -n "$tmp_created_install_dir" ] && [ -d "$tmp_created_install_dir" ]; then if is_zh; then echo "清理创建的目录: $tmp_created_install_dir" else echo "Cleaning up created directory: $tmp_created_install_dir" fi rm -rf "$tmp_created_install_dir" fi } trap cleanup EXIT # main while getopts ":hd:v:l:" option; do case $option in h) userOpt_help=1 ;; d) userOpt_install_dir=${OPTARG} ;; v) userOpt_version=${OPTARG} ;; l) userOpt_lang=${OPTARG} ;; \?) echo "无效的命令行选项,请使用 -h 查看帮助" exit 1 ;; esac done if [[ "$userOpt_lang" != "zh" && "$userOpt_lang" != "en" ]]; then error_exit "无效的语言选项: $userOpt_lang,支持的选项为 zh 和 en" fi if [ "$userOpt_help" -eq 1 ]; then help exit 0; fi install