SwiftLint is a great tool to enforce Swift style and conventions. Thanks to the Xcode Build Phases, integrating SwiftLint in an Xcode project is simple: a Build Phase automatically triggers
swiftlint when compiling your project.
Sadly at the moment, you can’t easily integrate SwiftLint with Swift Packages: A Swift Package has no Build Phases and no way to automatically runs scripts.
This article explains how to use a post action script in Xcode to automatically trigger SwiftLint afer a successful Swift Package compilation.
- Download the script SucceedsPostAction.sh on your Mac.
- Ensure that the script has the proper permissions, i.e. run
chmod 755 SucceedsPostAction.sh.
- If you want to use custom SwiftLint rules, add the
.swiftlint.ymlfile next to the script.
- Launch Xcode 13.0 or later
- Open Preferences > Locations and ensure that
Command Line Toolsis set to an Xcode version
- Open Preferences > Behaviors > Succeeds
- Choose the script
That’s it: each time you compile a Swift Package,
SucceedsPostAction.sh will run SwiftLint.
A post action script running in Xcode can’t add a log, a warning or an error to the Xcode build results. As a consequence,
SucceedsPostAction.sh opens a text file as a new window in Xcode with the list of SwiftLint reports. There is no deep integration to easily jump to the SwiftLint warnings.
Note that Swift 5.6 (not yet available at the time of writing) might help thanks to SE-0303: Package Manager Extensible Build Tools. Once SE-0303 is integrated, this script shouldn’t be needed anymore.
You can find the latest version of the script
SucceedsPostAction.sh on Github.