Using SwiftLint with XcodeGen
Recently I was working on a project using Xcodegen and we needed to install SwiftLint to enforce the code style. This is usually a straightforward procedure but since Xcodegen and CI/CD process were involved it got quite tricky.
Since I started using Swift Package Manager I find it hard to go back to CocoaPods for any reason, but unfortunately, SPM wouldn't support it. I found a way to make it work but it wasn't as stable as I thought it would be.
So I decided to insert the swiftlint
executable into the project and run it using a build phase script.
The steps
1) Download the latest swiftlint
from Github (it's named portable_swiftlint.zip
), unzip it, and place it in your project directory. I prefer to place it in /Scripts/SwiftLint/
.
2) Now we need to run swiftlint
locally and on CI/CD to make sure that the style code is enforced. So we have to add a script in the build phase to run it using XcodeGen on each build. We will do that by adding the following code snippet in the project.yml
(I will assume that you are already familiar with XcodeGen and you already built your project.yml
):
targets:
MyProject:
postCompileScripts:
- script: |
./Scripts/SwiftLint/swiftlint lint --quiet
name: Swiftlint
That's it! Run xcodegen
and you will notice the script added to the build phase and is ready to run on the next build.
Happy coding!