Code generation using Swift Package Plugins

Code generation using Swift Package Plugins

A while ago I was working on a new service at work which consisted of a Swift Package which exposed a protocol similar to Decodable for the rest of our app to use. In fact, the protocol inherited from Decodable itself and looked like this:

[…]

Note that build tool plugins for Xcode projects are not yet available in Xcode 14 Beta 2 as the release notes specify, but will be made available in a future version of Xcode 14.

[…]

As with all Swift Packages, the simplest way to get started is by running swift package init on the command line.

[…]

After creating the executableTarget and giving it both dependencies, this is what the Package.swift looks like:

[…]

In the same way as for the executable, a .plugin target must be added to the Package.swift and a .swift file must be created containing the plugin's implementation (Plugins/SourceKitPlugin/SourceKitPlugin.swift).

[…]