模板
模板 {#templates}
Section titled “模板 {#templates}”在架构已定型的项目中,开发人员可能希望引导符合项目规范的新组件或功能。使用 tuist scaffold,你可以从模板生成文件。你可以定义自己的模板或使用 Tuist 自带的模板。以下是脚手架可能有用的一些场景:
- 创建遵循给定架构的新功能:
tuist scaffold viper --name MyFeature。 - 创建新项目:
tuist scaffold feature-project --name Home
::: info 非强制意见 Tuist 对模板的内容以及你用它们做什么没有强制意见。它们只需要放在特定的目录中。 :::
定义模板 {#defining-a-template}
Section titled “定义模板 {#defining-a-template}”要定义模板,你可以运行 tuist editTuist/Templates 下创建一个名为 name_of_template 的目录来表示你的模板。模板需要一个清单文件 name_of_template.swift 来描述模板。因此,如果你要创建一个名为 framework 的模板,你应该在 Tuist/Templates 下创建一个新目录 framework,其中包含一个可能如下所示的清单文件 framework.swift:
import ProjectDescription
let nameAttribute: Template.Attribute = .required("name")
let template = Template( description: "Custom template", attributes: [ nameAttribute, .optional("platform", default: "ios"), ], items: [ .string( path: "Project.swift", contents: "My template contents of name \(nameAttribute)" ), .file( path: "generated/Up.swift", templatePath: "generate.stencil" ), .directory( path: "destinationFolder", sourcePath: "sourceFolder" ), ])使用模板 {#using-a-template}
Section titled “使用模板 {#using-a-template}”定义模板后,我们可以从 scaffold 命令使用它:
tuist scaffold name_of_template --name Name --platform macos::: info
由于 platform 是一个可选参数,我们也可以不带 --platform macos 参数调用命令。
:::
如果 .string 和 .files 提供的灵活性不够,你可以利用 Stencil 模板语言通过 .file _case。此外,你还可以使用此处定义的其他过滤器。
使用字符串插值,上面的 \(nameAttribute) 将解析为 {{ name }}。如果你想在模板定义中使用 Stencil 过滤器,你可以手动使用该插值并添加你喜欢的任何过滤器。例如,你可以使用 { { name | lowercase } } 而不是 \(nameAttribute) 来获取名称属性的小写值。
你也可以使用 .directory,它可以将整个文件夹复制到给定路径。
::: tip 项目描述辅助模块
模板支持使用