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.

SucceedsPostAction.sh

SucceedsPostAction.sh is a bash script to be used as a ‘Succeeds’ post action in Xcode. This convenient script will automatically trigger SwiftLint when you compile a Swift Package.

Installation

  1. Download the script SucceedsPostAction.sh on your Mac.
  2. Ensure that the script has the proper permissions, i.e. run chmod 755 SucceedsPostAction.sh.
  3. If you want to use custom SwiftLint rules, add the .swiftlint.yml file next to the script.
  4. Launch Xcode 13.0 or later
  5. Open Preferences > Behaviors > Succeeds
  6. Choose the script SucceedsPostAction.sh

That’s it: each time you compile a Swift Package, SucceedsPostAction.sh will run SwiftLint.

Demo

Limitations

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.

Swift 5.6

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.

Downloads

You can find the latest version of the script SucceedsPostAction.sh on Github.