首先,您必须在您的项目中安装 Templar。
$ templar init
或者,如果您的项目使用 Xcode
$ templar init --xcodeproj
Templar 将创建 YAML 配置文件 .templar
和 .templates
目录。
让我们来谈谈每个新生成的文件
.templar
包含有关您的项目和模板目录路径的信息。
例如,我们将讨论使用 Xcode 的项目
# Templar file
version: 1.0.0 # config version
templateFolder: .templates # path to templates directory
kind: # Information about your project
xcodeproj:
name: templar.xcodeproj # your project file name
targets: # list of targets for generation
- templar
templates: # list of templates name
- mvvm
安装 Templar 后,您可以使用命令 templar template new [templateName]
来生成一个带有空白模板的目录。 每个模板都将包含在个人文件夹中。
模板目录将包含 [templateName].templar
,看起来像这样
# Template file
version: 1.0.0 # template version.
summary: ENTER_YOUR_SUMMORY # Bit of information about your template and what it do.
author: ENTER_YOUR # Authour name or links
root: Sources/templar # Path to folder where templar will generate your templates.
files: # Template files
- path: View/ViewController.swift # Path for place where file will contains after process
templatePath: View/ViewController.swift.templar # Path to template
replaceRules: # Keys for replacing
- pattern: __CLASS_NAME__ # Key for replace. You can use regexp here.
question: 'Name of your class:' # Question will display in terminal and answer will use for replace pattern
$ templar generate [templateName] [moduleName]
Templar 将从您的模板配置文件中询问用户问题,并将每个模式替换为答案。 很简单,对吧?
首先,您要创建模板文件并设置要替换的模式。 例如,__CLASS_NAME__
class __CLASS_NAME__ViewController: UIViewController {
// Other code
}
之后,您应该将模板文件的路径和完成目录的路径添加到您的模板配置中。
# Template file
varsion: 1.0.0
...
files:
- path: View/ViewController.swift
templatePath: View/ViewController.swift.templar
好的,就是这样。 当您生成模板时,Templar 将使用 [moduleName]
作为文件名来创建文件。 例如 View/MyPrettyViewController.swift
。
Templar 将使用用户从问题中获得的答案来替换每个模式。
下一章
模式可以使用以下分隔的键进行修改
注意: 所有修饰符都可以用于每个模式一次。 并且修饰符区分大小写!
带有修饰符的示例
class __CLASS_NAME__Presenter {
func __CLASS_NAME__=firstLowercased=ViewControllerDidLoad() {
// Code
}
}
Templar 中有一些用于替换的默认模式。
__NAME__
- 从参数 [moduleName] 获取的模块名称__FILE__
- 文件名__PROJECT__
- 从 templar config -> xcodeproj -> name
获取的项目名称,或者为 nil。 可以使用模板设置 template -> settings -> projectName
进行修改__AUTHOR__
- 作者姓名,从 template -> author
获取。 如果模板中不存在作者姓名,则不会替换此键。__YEAR__
- 当前年份。__DATE__
- 当前日期,格式为 dd/MM/YYYY。 可以使用模板设置 template -> settings -> dateFormat
进行修改__COMPANY_NAME__
- 您的公司名称,从 templar -> companyName
获取,或者为 nil。__LICENSE__
- 将密钥替换为项目根目录中的许可证文件。 路径可以使用模板设置 template -> settings -> licensePath
进行修改。 或者获取许可证模板并使用默认模式替换每个键。设置将包含用于替换的规范,或者将来会添加一些操作。
# Template file
version: 1.0.0
...
settings:
dateFormat: dd/MM/YY
projectName: Templar
licensePath: LICENSE #will add in future release
...
是的,Templar 支持脚本。 当模板成功完成时,脚本将运行,并使用 bash 执行。
要创建脚本,只需使用命令: templar template new [templateName] --use-scripts
或手动将 scripts:
参数设置为您的模板。
例如,为您的 SPM 项目生成一个新的 xcodeproj
# Template file
...
scripts:
- swift package generate-xcodeproj
使用 Makefile
$ git clone https://github.com/SpectralDragon/Templar.git
$ cd Templar
$ make
使用 Mint 🌱
$ mint install SpectralDragon/Templar
$ git clone https://github.com/SpectralDragon/Templar.git
$ cd Templar
$ swift build -c release -Xswiftc -static-stdlib
$ cd .build/release
$ cp -f templar /usr/local/bin/templar
Vladislav Prusakov, twitter
Templar 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。