#template #template-generator #text #generator #engine #code

bin+lib yagenerator

使用 tinytemplate 引擎生成文本文件的程序。如果您有一组结构化数据,并需要从中生成多种类型的文件,这个工具可以帮助您节省一些时间。

4 个版本

0.1.3 2020年11月1日
0.1.2 2020年11月1日
0.1.1 2020年10月31日
0.1.0 2020年10月31日

#1155 in 开发工具

MIT 许可协议

190KB
370

又一个生成器

Rust 应用程序,如果您有一组结构化数据,需要从中生成多种类型的文件,这个工具可以帮助您节省一些时间。它是一个文本生成工具,而不是代码生成工具,二者的区别虽小但很重要。它不了解所生成内容的语义,对于应用程序来说,一切都是文本。

建议首先阅读 tiny template 文档 以了解其带来的好处。

可以直接使用 cargo 进行安装

cargo install yagenerator

示例

查看 示例

详细信息

给定

使用示例

yagenerator -d data.yaml -c template_config.yaml

它通过将数据文件中的数据“原样”及相同结构传递给模板(s)来工作。模板语法必须符合 TinyTemplate rust crate 的要求。

它使用 未转义格式化器,并且还实现了以下自定义值格式化器

  • to_camel_case
  • to_class_case
  • to_kebab_case
  • to_screaming_snake_case
  • to_sentence_case
  • to_snake_case
  • to_title_case
  • to_train_case
  • to_uppercase

格式化器的文档可以在 Inflector crate 中找到

数据文件

数据文件可以是json或yaml格式,是一个哈希表(也称为字典),其中每个键必须是一个字符串,并用作数据ID。值可以是任意的json或yaml数据,并将直接传递给模板。

模板配置文件

是一个具有良好定义模式的对象列表/数组

每个配置所需字段

  • data_to_use_id:在数据文件中使用的字典键。
  • file_path:要使用的模板文件。
  • destination_path:生成模板的位置。
  • 操作
    • 创建:从头开始使用模板创建文件,它将覆盖现有文件。
    • 追加:将生成的模板追加到文件的末尾,如果文件不存在,将抛出错误并停止处理。
    • 插入:在指定的标记上方插入生成的模板。标记可以是任何任意的文本字符串,并且可以在同一文件上有多个标记。
  • enabled:如果您想生成包括配置在内的文件,则设置为true;如果不包括,则设置为false

简化模式

$id: http://example.com/example.json
$schema: https://json-schema.fullstack.org.cn/draft-07/schema
title: The root schema
type: array
additionalItems: true
default: []
items:
    $id: '#/items'
    anyOf:
    -   $id: '#/items/anyOf/0'
        additionalProperties: true
        title: The first anyOf schema
        type: object
        properties:
            data_to_use_id:
                $id: '#/items/anyOf/0/properties/data_to_use_id'
                default: ''
                title: The data_to_use_id schema
                type: string
            destination_path:
                $id: '#/items/anyOf/0/properties/destination_path'
                default: ''
                title: The destination_path schema
                type: string
            enabled:
                $id: '#/items/anyOf/0/properties/enabled'
                default: false
                title: The enabled schema
                type: boolean
            file_path:
                $id: '#/items/anyOf/0/properties/file_path'
                default: ''
                title: The file_path schema
                type: string
            operation:
                $id: '#/items/anyOf/0/properties/operation'
                default: ''
                title: The operation schema
                type: string
        required:
        - data_to_use_id
        - file_path
        - destination_path
        - operation
        - enabled

详细模式

$id: http://example.com/example.json
$schema: https://json-schema.fullstack.org.cn/draft-07/schema
additionalItems: true
default: []
description: The root schema comprises the entire JSON document.
examples:
-   -   data_to_use_id: IODevice
        destination_path: /tmp/IODevice.hpp
        enabled: true
        file_path: ./tests/templates/Interface.hpp.txt
        operation: Create
    -   data_to_use_id: TimeDate
        destination_path: /tmp/TimeDate.h
        enabled: true
        file_path: ./tests/templates/Class.h.txt
        operation: Create
items:
    $id: '#/items'
    anyOf:
    -   $id: '#/items/anyOf/0'
        additionalProperties: true
        default: {}
        description: An explanation about the purpose of this instance.
        examples:
        -   data_to_use_id: IODevice
            destination_path: /tmp/IODevice.hpp
            enabled: true
            file_path: ./tests/templates/Interface.hpp.txt
            operation: Create
        properties:
            data_to_use_id:
                $id: '#/items/anyOf/0/properties/data_to_use_id'
                default: ''
                description: An explanation about the purpose of this instance.
                examples:
                - IODevice
                title: The data_to_use_id schema
                type: string
            destination_path:
                $id: '#/items/anyOf/0/properties/destination_path'
                default: ''
                description: An explanation about the purpose of this instance.
                examples:
                - /tmp/IODevice.hpp
                title: The destination_path schema
                type: string
            enabled:
                $id: '#/items/anyOf/0/properties/enabled'
                default: false
                description: An explanation about the purpose of this instance.
                examples:
                - true
                title: The enabled schema
                type: boolean
            file_path:
                $id: '#/items/anyOf/0/properties/file_path'
                default: ''
                description: An explanation about the purpose of this instance.
                examples:
                - ./tests/templates/Interface.hpp.txt
                title: The file_path schema
                type: string
            operation:
                $id: '#/items/anyOf/0/properties/operation'
                default: ''
                description: An explanation about the purpose of this instance.
                examples:
                - Create
                title: The operation schema
                type: string
        required:
        - data_to_use_id
        - file_path
        - destination_path
        - operation
        - enabled
        title: The first anyOf schema
        type: object
title: The root schema
type: array

依赖关系

~5.5–7.5MB
~134K SLoC