Templar - 一种新的模板生成方式

Build Status License: MIT Swift Version

它是如何工作的?

首先,您必须在您的项目中安装 Templar。

$ templar init

或者,如果您的项目使用 Xcode

$ templar init --xcodeproj

Templar 将创建 YAML 配置文件 .templar.templates 目录。

让我们来谈谈每个新生成的文件

1) .templar

.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
2) Templates (模板)

安装 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 中有一些用于替换的默认模式。

模板设置

设置将包含用于替换的规范,或者将来会添加一些操作。

# 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

使用 Swift Package Manager 🛠

$ 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 文件。