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 install yonaskolb/genesis
$ git clone https://github.com/yonaskolb/Genesis.git
$ cd Genesis
$ make
使用 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
选项将按照以下顺序传递给模板,并在传递过程中合并,重复的选项将覆盖之前的选项。
在调用 generate 之前设置任何环境变量,或者使用开发环境中已有的环境变量
name="My Name" genesis generate template.yml
使用 --option-file
传递 json 或 yaml 文件的路径。这可以包含更多结构和复杂的数据。例如
name: MyProject
targets:
- name: MyTarget
type: application
- name: MyFramework
type: framework
使用 --options
传递特定选项,像这样
-- options "option1: value 2, option2: value 2"
如果缺少任何必需的选项,Genesis 将会询问您。您可以使用 --non-interactive
关闭交互式输入。
每个选项都可以有一个默认的 value
,用作后备值。
如果某个选项是必需的,但仍然没有从任何地方接收到值,则生成将失败。
Genesis 模板是一个 yaml 或 json 文件,其中包含 options
和 files
列表
选项是 Stencil
模板的结构化输入。它们用作文档,并允许 Genesis 交互式地询问输入。
string
,但可以是以下任何一种string
一个简单的字符串boolean
一个布尔值choice
来自选项列表的字符串。需要定义 choices
array
其他选项的数组。如果定义了子 options
,则它将是选项数组,否则它将按命令拆分字符串,删除任何空格array
类型包含对象,则使用Stencil
标签以使其动态化。path
中的标签来区分它们。{% if type == 'framework' %}
,而是写 type == 'framework'
每个 Stencil 模板都拥有 StencilSwiftKit 中提供的所有过滤器,并可以访问所有选项。有关标签的更多信息,请参阅 Stencil。
小例子
{% if name %}
name: {{ name }}
{% endif %}
库 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。