SuperAPI AI 工具集

cargo-generate

编程与开发

查看项目

更新于 10-16 06:22

技能介绍

用途:cargo-generate 把 Git 仓库中的真实文件展开为新项目,替换文件内容和路径名里的 Liquid 表达式,是 Rust 生态标准脚手架工具,Bevy、wasm-pack 及多数框架起始模板都提供支持。它并非 Rust 专用,模板无需 Cargo.toml,生成 TypeScript、Python 或纯配置文件项目同样可行。

安装:cargo install cargo-generate --locked,随后用 cargo generate --version 验证。它既是 cargo 子命令(cargo generate),也是独立二进制(cargo-generate)。

生成项目:cargo generate --git https://github.com/TheBevyFlock/bevy_new_2d --name my_game;本地模板用 cargo generate --path ./templates/my-template --name my-project;可用 --branch/--tag/--revision 固定版本,--init 展开到当前目录。

非交互使用:cargo generate --path ./templates/my-template --name my-project --define 'display_name=My Project' --define variant=app --vcs none --silent。--define 可重复,--template-values-file 从 TOML 提供全部占位符,--silent 不提示、缺值即失败,--vcs none 跳过自动 git init,--destination 指定输出父目录。

编写模板:模板根目录放 cargo-generate.toml,含 [template](ignore、include/exclude)、[placeholders](string、text、editor、bool、array,支持 default、choices、regex)、[conditional.'<expr>'] 与 [hooks](init/pre/post 的 Rhai 脚本)。内置变量有 project-name、crate_name、crate_type、authors、username、os-arch、is_init、within_cargo_project,并提供 kebab_case、snake_case、pascal_case 等大小写过滤器;路径名也会被渲染。

限制与注意:Liquid 会吞掉其他 {{ }} 语法(GitHub Actions、Handlebars、Jinja、Vue),需用 exclude 或 {% raw %} 处理;variable::set 只接受字符串;项目名在钩子运行前已被 kebab 化;.liquid 是魔法后缀。资料未提供收费、价格或平台信息。