Xcode Run Scripts

June 20, 2013

Xcode provides a mechanism to run custom scripts when building a target.

  1. Select your project file
  2. Select your app's target
  3. Select "Build Phases"
  4. Tap "Add Build Phase"
  5. 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:

  1. Absolute path to the source file
  2. Line and column number
  3. 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

Quick Sample

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.

Very awesome!

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