Rake is make for ruby, but it’s so damn good I use it on every project no matter what language i’m in. Ruby is the perfect glue language for getting stuff done and Rake makes it easy to write a set of tasks for each project. It’s also convenient because it’s already installed on Mac OS X!
Today I’m going to show how we can create tasks to build spritesheets from images, build the app and publish it to TestFlight for beta testers.
I like to build my spritesheets based on directory structures. This makes things easy to manage, just group sprite files into directories and they get combined into spritesheets. Something like this:
├── spritesheets │ ├── charlie_walk │ │ ├── charlie_walk.00.png │ │ ├── charlie_walk.01.png │ │ ├── charlie_walk.02.png │ │ ├── charlie_walk.03.png │ │ ├── charlie_walk.04.png │ │ └── charlie_walk.05.png │ ├── charlie_run │ │ ├── charlie_run.00.png │ │ ├── charlie_run.01.png │ │ ├── charlie_run.02.png │ │ ├── charlie_run.03.png │ │ ├── charlie_run.04.png │ │ └── charlie_run.05.png
In this example I’m combining animation frames into spritesheets. I use TexturePacker to create spritesheets. Using command line tools like this is great, it keeps things flexible and easy to change.
So here’s a Rake task which extracts path information and uses that to build the spritesheets:
This will create cocos2d specific spritesheets but can be modified for other formats. It also creates the retina spritesheet at full resolution and a half scaled one for older devices. Now first lets see how we can list out the available tasks:
⚡ rake -T rake spritesheets:update # Recreates spritesheets that are out of date
Cool, so it tells us that we have a spritesheets:update task, let’s try it out!
⚡ rake spritesheets:update Writing sprite sheet to assets/spritesheets/charlie_walk-hd.png Writing data for cocos2d to assets/spritesheets/charlie_walk-hd.plist Resulting sprite sheet is 256x1024. Writing sprite sheet to assets/spritesheets/charlie_run-hd.png Writing data for cocos2d to assets/spritesheets/charlie_run-hd.plist Resulting sprite sheet is 256x1024.
Alright! Now if we want to change the spritesheets, it’s just a matter of regrouping the sprite files into directories and running that task.k.
Auto build and publish to TestFlight
Another great aspect of using Ruby is you get access to thousands of gem packages which are really easy to install and use. We’re going to use wox to help us out now. First you need to install the gem. This is as simple as:
⚡ gem install wox Successfully installed wox-0.0.8 1 gem installed
Now we can require this gem from our Rakefile and use it to configure the build:
# Rakefile require 'wox' # make sure the info_plist argument points to the right file! Wox::Tasks.create :info_plist => 'assets/Info.plist' do end
This will create a few tasks for us, let’s have a look:
⚡ rake -T rake info:configurations # List available configurations rake info:sdks # List available sdks rake info:targets # List project targets rake spritesheets:update # Recreates spritesheets that are out of date
There’s a few info tasks there, let’s see what they tell us:
⚡ rake info:configurations Debug Release ⚡ rake info:sdks macosx10.6 iphoneos4.3 iphonesimulator3.2 iphonesimulator4.0 iphonesimulator4.1 iphonesimulator4.2 iphonesimulator4.3 ⚡ rake info:targets chickenpox cocos2d tests
Ok, so lets create a task which will build the chickenpox target in Release mode with iphoneos4.3 sdk. Wox makes this fairly simple:
Wox::Tasks.create :info_plist => 'assets/Info.plist' do build :release, :configuration => 'Release', :sdk => 'iphoneos4.3', :target => 'chickenpox' end ⚡ rake build:release Building chickenpox 0.2 configuration:Release Success. Results in build/build-Release.log
Great! Now we have a build we can run from the command line which we could easily call from a Continuous Integration service like Jenkins.
The last step is to create the ipa file and have it publish to TestFlight. In order to do this we’ll need a couple of things. For the ipa we need to know which developer certificate and provisioning profile we want to use to sign the app bundle.
For the TestFlight publish you need your API token and Team token. Grab the API token from the Account page. The Team token you can get from clicking edit next to the team on the top left.
Now change the wox section of your Rakefile to this:
Now we have some extra tasks:
⚡ rake -T rake build:release # Build chickenpox 0.2 with Release configuration rake ipa:adhoc # Creates build/chickenpox-0.2-Release-adhoc.ipa rake testflight:publish # Publishes build/chickenpox-0.2-Release-adhoc.ipa to testflight
Let’s try it out!
⚡ rake testflight:publish Building chickenpox 0.2 configuration:Release Success. Results in build/build-Release.log Creating build/chickenpox-0.2-Release-adhoc.ipa Success. Results in build/ipa.log Publishing to TestFlight File: build/chickenpox-0.2-Release-adhoc.ipa (1.0 megabyte) Accessible To: Internal After publish will notify team members ######################################################################## 100.0% Success. Results in build/testflight.log
This one command line now builds the app, creates an ipa file, uploads it and sends emails to all of your beta testers with a link to install the new version. TestFlight is an amazing service, if you’re not signed up you’re missing out!
That’s it for now, feel free to share any more automation tips in the comments.