/ xcode

Must have build scripts for iOS developers

In this post I'll show you the best build scripts for Xcode and a brand new way to integrate them into your projects. Let's dive in with a few basic scripts that you might have heard about.


R.swift

Get strong typed, autocompleted resources like images, fonts and segues in Swift projects

If you are familiar with resource files in Android, you might know what's this all about. You can install R.swift in many different ways, but the preferred one is to set it up as a build script with the help of CocoaPods.

"$PODS_ROOT/R.swift/rswift" "$SRCROOT"

This little script will generate a R.generated.swift file and from now on you can use your image assets like this: R.image.starIcon. Yeah, it's not just for image assets, but for nibs, colors, etc. Really cool stuff!


SwiftLint

A tool to enforce Swift style and conventions.

Style guides are really nice, but if you have to enforce them that's a greater challenge. This is where SwiftLint comes in. You can install it via homebrew, and all you have to do is to setup a new build phase script.

if which swiftlint >/dev/null; then
  swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

You should really watch your language, but if you don't like SwiftLint, you can try out my experimental linter which is made only with some shell scripts.


Iconer

App icon generator for Apple platforms.

I created this tool, because I was sick & tired of the various icon sizes that you have to provide for a single application. If you install iconer, you can provide one "AppIcon.png" file and this script will take care about the rest:

iconer "${SRCROOT}/${PROJECT_NAME}/AppIcon.png" -t "${SRCROOT}/${PROJECT_NAME}" -x -p "ios"

Can you feel the difference? Only one icon file to rule them all!


Build version bumper

Automating Version and Build Numbers

You don't have to install anything to auto increment your build version numbers in Xcode. You just have to create a new build phase script. There are lots of variants for the same purpose, but my personal favorite is this one:

BUILD_COUNT=$(git rev-list --all --count)

/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_COUNT" "${PROJECT_DIR}/${INFOPLIST_FILE}"

Other alternatives:


Sourcery

Meta-programming for Swift, stop writing boilerplate code.

Honestly I just tried Sourcery once. I am planning to use it later on, but I simply had no time to check it from a really close point of view. It looks like a super handy tool, you just have to install it through CocoaPods, and add a build script to your project:

$PODS_ROOT/Sourcery/bin/sourcery ${SRCROOT} ${SRCROOT}/Templates ${SRCROOT}/Sourcery

From now on, you can do write some nice meta programs inside stencil files in your Template directory and Sourcery will generate the Swift source code inside the target folder. It's like C macros with superpowers!!!


Conclusions?

We need a build script manager

This is why I made Xphase. It's very simple to use. Let me show you quickly:

Installation

sudo gem install xphase

xphase.json file next to your Xcode project

{
  "*": {
    "phases": [
      {
        "spec": "swiftlint"
      }
    ]
  },
  "ios-target": {
    "phases": [
      {
        "spec": "build-version",
        "params": {
          "counter": "git rev-list --all --count"
        }
      }
    ]
  }
}

Run the install command to install the scripts

xphase install

Now all your targets will contain the swiftlint build script, and the ios-target will also have a "custom build version bumper build phase" - lol this phrase... - Simple, isn't it? If this little introduction grabbed your attention, you can read more about the usage on the official github page:

![](/content/images/2017/02/CoreKit.png)
### CoreKit/Xphase

Special thanks to Matthew Newberry for giving away his github / twitter handles to support the CoreKit framework! You are amazing!!! ;)

CoreKit logo is made by akosveber.