sourcery_使用Sourcery进行Swift元编程

sourcery

Every iOS developer already came up with the problem of boilerplate code once. Whether it is related to verbose architectures or protocol conformances such as Codable, Equatable or simply a init method. The problem exists and can be very time consuming.

每个iOS开发人员都已经提出了样板代码问题。 它是否与冗长的体系结构或协议一致性(例如,Codable,Equatable或仅是init方法)相关。 该问题存在并且可能非常耗时。

This article is a simple tutorial of using Sourcery to generate boilerplate code for us.

本文是使用Sourcery为我们生成样板代码的简单教程。

那么……什么是Sourcery? (So… What is Sourcery?)

Sourcery is a code generator tool for Swift. It works by providing templates and creating protocols that tell Sourcery which template will be applied to each class/struct.

Sourcery是Swift的代码生成器工具。 它通过提供模板并创建协议来工作,该协议告诉Sourcery哪个模板将应用于每个类/结构。

它是如何工作的? (How does it work?)

Before trying anything at all, install Sourcery through cocoapods or brew. After that, we need to specify the path where Sourcery will look for the templates, swift files, and where it will output the generated code. So, on your project target, create a new Run Script Phase and add a script like this:

在尝试任何操作之前,请通过可可豆或brew安装Sourcery。 之后,我们需要指定Sourcery在其中寻找模板,swift文件以及在其中输出生成的代码的路径。 因此,在您的项目目标上,创建一个新的“运行脚本阶段”并添加如下脚本:

Image for post
sourcery --sources YourProject --templates YourProject/Templates --output YourProject/Autogenerated

There are many more options we can add, but for the sake of simplicity, let's keep it like that. You can also create a .yml file and specify more than one source and template paths. Check it out here.

我们可以添加更多选项,但是为了简单起见,让我们保持这种状态。 您还可以创建一个.yml文件,并指定多个源和模板路径。 在这里查看

Now, let's start with a simple example:

现在,让我们从一个简单的示例开始:

平等的 (Equatable)

Sourcery templates can be written in Stencil and swiftemplate. For this example, we will use an equatable template that already comes out of the box with sourcery. All we have to do is declare an AutoEquatable protocol, and every class or struct that conforms to it will get the equatable method implemented.

源模板可以用Stencil和swiftemplate编写。 在此示例中,我们将使用随货提供的现成的可等同模板。 我们要做的就是声明一个AutoEquatable协议,每个符合该协议的类或结构都将实现equatable方法。

In my project, for example, I've declared the following protocols:

例如,在我的项目中,我声明了以下协议:

I’ve also declared three structs, simulating some components. As you can see, they conform to the protocols that I’ve mentioned above.

我还声明了三个结构,模拟了一些组件。 如您所见,它们符合我上面提到的协议。

Once this is done, all you have to do is build your project and import the files generated by Sourcery(only for the first time).

完成此操作后,您要做的就是构建项目并导入Sourcery生成的文件(仅第一次)。

结果 (Outcome)

All the AutoEquatable classes/structs will have their own implementation declared in one file. Other files will be created as you declare more protocols and make them conform to your data.

所有AutoEquatable类/结构将在一个文件中声明其自己的实现。 当您声明更多协议并使它们符合您的数据时,将创建其他文件。

This equatable template written in stencil:

用模板写的这个相等的模板:

Generates this code:

生成此代码:

自定义init和自定义可解码 (Custom init and custom decodable)

Custom templates can also be written in swiftemplate. We can basically declare whatever we want to be generated. Here is the complete documentation.

自定义模板也可以用swiftemplate编写。 我们基本上可以声明要生成的任何内容。 是完整的文档。

In my example project, I’ve written a custom template to consider nullable variables and variables with default values as optionals in the init method. I’ve also used the provided codable template from Sourcery and created a custom decodable template. Check it out.

在示例项目中,我编写了一个自定义模板,以将可空变量和具有默认值的变量视为init方法中的可选变量。 我还使用了Sourcery提供的可编码模板,并创建了一个自定义可解码模板。 看看这个。

从这往哪儿走 (Where to go from here)

  • Lecture from Krzysztof Zabłocki, in CocoaHeads

    讲座由克日什托夫·Zabłocki,在CocoaHeads

  • This tutorial is a very good way to start writting custom templates

    教程是开始编写自定义模板的非常好的方法

翻译自: https://medium.com/swlh/swift-metaprogramming-using-sourcery-c23e3bda240e

sourcery

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值