#scaffold #generate #templating #cargo #toml-config #git-repository #config-file

bin+lib cargo-scaffold

使用简单的 toml 配置文件和 'handlebars' 模板生成整个堆栈的脚手架和生成工具

37 个版本 (13 个破坏性版本)

0.14.0 2024年5月16日
0.12.0 2024年2月15日
0.9.0 2023年12月13日
0.8.14 2023年11月10日
0.2.0 2020年9月1日

#19模板引擎

Download history 275/week @ 2024-04-22 276/week @ 2024-04-29 277/week @ 2024-05-06 577/week @ 2024-05-13 856/week @ 2024-05-20 1163/week @ 2024-05-27 500/week @ 2024-06-03 1019/week @ 2024-06-10 984/week @ 2024-06-17 856/week @ 2024-06-24 478/week @ 2024-07-01 172/week @ 2024-07-08 326/week @ 2024-07-15 513/week @ 2024-07-22 476/week @ 2024-07-29 368/week @ 2024-08-05

1,689 每月下载量
troll-rs 中使用

自定义许可协议

8.5MB
881

Cargo-scaffold

cargo-scaffold 是一款灵活且易于使用的开发者工具,让您能够快速搭建项目。它无需编写任何代码即可完全配置。它通过友好的 CLI 生成各种类型的项目。

特性

  • 几秒钟内搭建项目
  • 声明式
  • 自动生成用户交互
  • 不仅适用于 Rust crate/project。它完全与语言无关

安装

cargo install cargo-scaffold

使用

您可以从位于本地目录或 git 仓库中的任何 cargo-template 脚手架处搭建项目

# Locally
cargo scaffold your_template_dir

# From git repository
cargo scaffold https://github.com/username/template.git

# From git repository based on a specific commit
cargo scaffold https://github.com/username/template.git -t deed14dcbf17ba87f6659ea05755cf94cb1464ab

# From git repository based on a specific branch
cargo scaffold https://github.com/username/template.git -t main

以下是 cargo scaffold 的可用选项

USAGE:
    cargo-scaffold scaffold [FLAGS] [OPTIONS] <template>

FLAGS:
    -a, --append        Append files in the target directory, create directory with the project name if it doesn't
                        already exist but doesn't overwrite existing file (use force for that kind of usage)
    -f, --force         Override target directory if it exists
    -h, --help          Prints help information
    -p, --passphrase    Specify if your SSH key is protected by a passphrase
    -V, --version       Prints version information

OPTIONS:
    -t, --git_ref <git_ref>
            Full commit hash, tag or branch from which the template is cloned (i.e.: "deed14dcbf17ba87f6659ea05755cf94cb1464ab" or "v0.5.0" or "main")
    -n, --name <name>
            Specify the name of your generated project (and so skip the prompt asking for it)
        --param <parameters>...                  Supply parameters via the command line in <name>=<value> format
    -k, --private_key_path <private-key-path>
            Specify if your private SSH key is located in another location than $HOME/.ssh/id_rsa

    -r, --path <repository_template_path>
            Specify your template location in the repository if it's not located at the root of your repository

    -d, --target_directory <target_directory>    Specify the target directory

ARGS:
    <template>    Specify your template location

编写自己的模板

为了让您能够搭建和生成不同的项目,唯一必需的部分是在模板目录的根目录下有一个 .scaffold.toml 文件。此文件用于记录和添加用户交互以供模板使用。在模板的目录中,每个文件和目录都将复制/粘贴到生成的项目中,但使用 Handlebars 模板 进行更新。

模板描述

以下是一个 .scaffold.toml 文件的示例

# Basic template informations
[template]
name = "test"
author = "Benjamin Coenen <[email protected]>"
version = "0.1.0"

# Exclude paths you do not want copy/pasted in the generated project
exclude = [
    "./target"
]

# Notes to display at the end of the generation
notes = """
Have fun using this template called {{name}} ! Here is the description: {{description}}
"""

[hooks]
# Commands to be executed before scaffolding, from within the generated project
pre = [
    "bash -c some_pre_script.sh"
]
# Commands to be executed after scaffolding, from within the generated project
post = [
    "cargo vendor",
    "bash -c some_post_script.sh"
]

# Parameters are basically all the variables needed to generate your template using templating.
# It will be displayed as prompt to interact with user (thanks to the message subfield).
# All the parameters will be available in your templates as variables (example: `{{description}}`).
[parameters]
    # [parameters.name] is already reserved
    [parameters.feature]
    type = "string"
    message = "What is the name of your feature ?"
    required = true

    [parameters.gender]
    type = "select"
    message = "Which kind of API do you want to scaffold ?"
    values = ["REST", "graphql"]

    [parameters.dependencies]
    type = "multiselect"
    message = "Which dependencies do you want to use ?"
    values = ["serde", "anyhow", "regex", "rand", "tokio"]

    [parameters.description]
    type = "string"
    message = "What is the description of your feature ?"
    default = "Here is my default description"

    [parameters.show_description]
    type = "boolean"
    message = "Do you want to display the description ?"

    [parameters.limit]
    type = "integer"
    message = "What is the limit ?"

以下是您可以使用参数的不同类型的列表: stringintegerfloatbooleanselectmultiselect

模板化

在模板目录内的任何文件中,您都可以使用Handlebars模板。请参考该文档以获取有关模板的所有语法信息。如果您在Handlerbars中寻找自定义助手,可以在此文档中查看。以下是一个基本示例,如果您想显示名为description的参数,并且布尔参数show_description设置为上一节中描述的true

{{#if show_description}} {{description}} {{/if}}
{{#forRange 5}}
Repeat this line 5 times with the current {{@index}}{{/forRange}}

您还可以将模板放入目录路径或文件名中(例如:一个名为{{name}}.rs的文件将被生成为正确的名称)。

致谢

感谢@Arlune提供的这个令人惊叹的标志以及所有审阅者。

替代方案

依赖项

~17–31MB
~568K SLoC