Xcode provides a mechanism to run custom scripts when building a target.
- Select your project file
- Select your app’s target
- Select “Build Phases”
- Tap “Add Build Phase”
- Select “Run Script”
Code Convention Violation Script
My current client project executes a custom shell script that scans all source files looking for code convention violations. If a violation is found, the script fails the build and notifies the developer of the offending code. The greatest part is that Xcode allows us to quickly jump to the offending code, just like a compiler error.
Three Strings To Echo
There are three main pieces needed for Xcode to fail and provide “jump to” capability:
- Absolute path to the source file
- Line and column number
- Custom message
Here is a complete line example that may be echoed in a script:
/Users/briancoyner/Development/Private/MediaTiming/MediaTiming/BTSViewController.m:13:17: Some Custom Message
You can see how this works by simply echoing a few lines in the “Run Script” console.
Here is an example screenshot:
When you build the target, Xcode fails the build. You are now able to jump between each “code convention violation” error just like a compiler error.
A Word of Warning
You can also add
warning: to the string to generate a warning instead of an error.
/Users/briancoyner/Development/Private/MediaTiming/MediaTiming/BTSViewController.m:13:17: warning: Some Custom Warning Message