Genesis

SPM Git Version license

Genesis 是一个模板和脚手架工具。

Genesis 模板是以 yaml 或 json 格式编写的选项和文件清单。选项值可以通过多种方式提供,否则您将被交互式地要求输入。模板文件以 Stencil 编写。文件列表可以使用动态路径,并根据输入格式生成多次。

非常简单的例子

options:
  - name: project
    description: The name of the project
    question: What is the name of your project?
    required: true
    type: string
files:
  - template: project.stencil
    path: "{{ project }}.project"

然后像这样运行

$ genesis generate template.yml
What is the name of your project? MyProject
Generated files:
  MyProject.project

或者您可以通过参数提供所需的选项

$ genesis generate template.yml --options name:MyProject
Generated files:
  MyProject.project

可以使用更强大的模板。请参阅 此处 获取更完整的文档

安装

请确保先安装 Xcode 13。

Mint

$ mint install yonaskolb/genesis

Make

$ git clone https://github.com/yonaskolb/Genesis.git
$ cd Genesis
$ make

Swift Package Manager

使用 CLI

$ git clone https://github.com/yonaskolb/Genesis.git
$ cd Genesis
$ swift run mint

用作依赖项

将以下内容添加到您的 Package.swift 文件的依赖项中

.package(url: "https://github.com/yonaskolb/Genesis.git", from: "0.1.0"),

然后导入 GenesisKit。有关更多信息,请参阅 GenesisKit

用法

运行 genesis help 获取用法说明

Usage: genesis generate <templatePath> [options]

Options:
  -d, --destination <value>    Path to the directory where output will be generated. Defaults to the current directory
  -h, --help                   Show help information for this command
  -n, --non-interactive        Do not prompt for required options
  -o, --options <value>        Provide option overrides, in the format --options "option1: value 2, option2: value 2.
  -p, --option-path <value>    Path to a yaml or json file containing options

提供选项

选项将按照以下顺序传递给模板,并在传递过程中合并,重复的选项将覆盖之前的选项。

1. 环境变量

在调用 generate 之前设置任何环境变量,或者使用开发环境中已有的环境变量

name="My Name" genesis generate template.yml

2. 选项文件

使用 --option-file 传递 json 或 yaml 文件的路径。这可以包含更多结构和复杂的数据。例如

name: MyProject
targets:
  - name: MyTarget
    type: application
  - name: MyFramework
    type: framework  

3. 选项参数

使用 --options 传递特定选项,像这样

-- options "option1: value 2, option2: value 2"

4. 交互式输入

如果缺少任何必需的选项,Genesis 将会询问您。您可以使用 --non-interactive 关闭交互式输入。

5. 默认值

每个选项都可以有一个默认的 value,用作后备值。

缺少值

如果某个选项是必需的,但仍然没有从任何地方接收到值,则生成将失败。

模板

Genesis 模板是一个 yaml 或 json 文件,其中包含 optionsfiles 列表

选项

选项是 Stencil 模板的结构化输入。它们用作文档,并允许 Genesis 交互式地询问输入。

文件

Stencil 模板

每个 Stencil 模板都拥有 StencilSwiftKit 中提供的所有过滤器,并可以访问所有选项。有关标签的更多信息,请参阅 Stencil

小例子

{% if name %}
name: {{ name }}
{% endif %}

GenesisKit

GenesisKit 可用于在您自己的工具中轻松提供生成功能。

import GenesisKit

// create a context
let context: [String: Any] = ["name": "hello"]

// create a template, either from a file or programmatically
let template = try GenesisTemplate(path: "template.yml")

// Create the generator
let generator = try TemplateGenerator(template: template)

// generate the files
let generationResult = try generator.generate(context: context, interactive: false)

// write the files to disk
try generationResult.writeFiles(path: "destination")

贡献

欢迎提交 Pull Request 和 Issue

许可证

Genesis 在 MIT 许可证下获得许可。有关更多信息,请参阅 LICENSE