Xcode project Templates Vol. 3: How to automate the configuration of swiftlint and swiftgen
As the title implies, in this article I'll show you how to add run scripts and ready-made swiftlint and swiftgen configuration to your project via a project template.
In this tutorial I will show you how to add build scripts to a project via a project template. Scripts in projects usually look very inconsistent depending on the number of people setting up projects. However, if you always use the same template there is nowhere to go wrong. This is what our template looked like in the previous article about Xcode Templates.
You may have noticed that I added a new section, which you can see below ⬇️
This is due to the new Xcode 13. However, today we will add a brand new Targets section to the template.
The Targets key specifies a list of project targets, but we will only use one in specific, the default main target of the application we are creating. To do this, you need to specify a target ID, which is:
Then we enter the build phases key, which is again a list from the buildphases tab of the project.
Enter a name for the script in the Name variable. You can also be little creative with the name and enter various emoji, which will immediately guide you visually to the script.
- SortOrder determines the order of the scripts.
- Class must be with standard ShellScript sh scripts.
- ShellPath by default also /bin/sh
- ShellScript launches the library and specifies the path to the configuration file.
Within scripts, SWIFT_LINT_CONFIG specifies the path to the configuration file, as well as ANY_LINT_CONFIG and SWIFT_GEN_CONFIG.
Swiftgen localizations should be set only after selecting the localization and the base language of the app. But the probable setup is still listed in the template.
You can write Swiftlint by yourself, but even if you do not enter anything in the configuration file, swiftlint will take the default configuration.
Then we add the swiftgen files and the swiflint configuration file to the nodes:
We will add to the definitions:
The result looks like this:
This is what the finished project and its buildphases we created today can look like ⬇️
Voilà. After setting up the SPM dependency and installing the pods, we have generated assets and a swiftlint is running over the project.
Such scripts can be packaged very easily by adding additional build-phases. You can find a fully developed template as well as previous templates on our github. You will also find templates that are more advanced than these, on the main branch, so don't hesitate and give it a try.