AD-HOC Builder and Packager
I recently had a lot of trouble with building and distributing AD-HOC builds (see this post), with that in mind I set out to not to fall into the same trap twice (lots of time wasted) and automate the process using a shell script. This would be able to compile the desired build configuration, grab any documentation I wanted to go along with said build and include a valid mobileprovision (preferable the one it was built with). The script would then package up all this data into an object that could be directly distributed to the test group.
The result is a script that can be found at the bottom of this post ( or download it here ).
Note : The Package script creates both a zip and a dmg, they both contain the same files, I wanted to know a little bit more about creating dmg and zip files via a shell script.
——————————————————————————————————————————————————-
Setting it up
The following is a step by step guide to adding the package script into your xCode project, the steps are valid for xCode 3.14 and may differ in later version.
1) Download and Add
Download the shell script ( or copy the script into a file ), simply drag the downloaded file into your Resource directory.
I created a group inside the Resource folder to separate things out, also if you notice I also added a ReadMe.txt file to project. This is the documentation that will also be included in the package, please create the read me file now and add your instructions for the recipient of the package (mine tells them how to install the AD-HOC build in iTunes, download it here ).
Now select the Script that you have added into your project and find the below lines:
CONFIGURATION=AD_HOC # string containing the build configuration name LICENCE_STRING=AD_HOC # string containing the mobile provision name
These specify the build configuration to compile and the mobile provision file to include in the package, now is the time to change them. Please change the CONFIGURATION to the name of your AD-HOC build configuration, this can be found in by double clicking on the project file and selecting the configurations tab ( if you don’t have an AD-HOC configuration add one now ). Please also change the LICENCE_STRING to the name of your AD_HOC mobileprovision, this can be found in the device Organizer.
2) Add a New Target
The next step is to add a new target to your project, this will be used to run the script that you have just added to the project. Locate your Targets, this can be found near the bottom of your Groups & Files window.
Right Click on the Targets to bring up the context menu.
In the menu navigate right on the Add and then select the New Target Option.
The New Target Window should now appear.
Select the Other option on the left and then select the Shell Script Target.

Give the new target a name, in this case I have called it Create_AD_HOC_Package.
You should now see the following window.
Please close it.
Check the targets to see if it has been added.
3) Hook up the Script
At this time the new Build Target does nothing, the next step of the process is to point the Target at the shell script in your project.
Expand the new Target to Expose the Run Scripts, then Double Click it.
The following window should appear, this is where we will add the link to run our script.
Now replace the line with the # with the following “exec $SOURCE_ROOT/Create_AD_HOC_Package.sh”.
Please close the window.
4) Building the Package
Now that you have successfully linked the Shell Target to the script inside your project, you are ready to build the target.
Select the Target to be your Active Target.
Now just Build the Target like you would do normally.
Down the left hand side of the main window you should see the above line.
Once the script has completed you should see the following, if it has not you will see an error or a warning instead.

Navigate to your Build output directory.
You should find a dmg and zip file in the directory, double click the dmg to inspect it’s contents.
In this case you should find a mobile provision file, a read me file and the application file.
You can now mail the package to your testers.
——————————————————————————————————————————————————-
Package Script
#!/bin/sh
# Create_AD_HOC_Package.sh
# Created by Robert on 25/09/2009.
set -x
# Determine the project name and version
VERS=$(agvtool mvers -terse1)
# Derived names
VOLNAME=${PROJECT}${VERS}
DISK_IMAGE=$BUILD_DIR/$VOLNAME
DISK_IMAGE_FILE=$BUILD_DIR/$VOLNAME.dmg
ZIP_IMAGE_FILE=$BUILD_DIR/$VOLNAME.zip
CONFIGURATION=AD_HOC # string containing the build configuration name (UPDATE ME)
LICENCE_STRING=AD_HOC # string containing the mobile provision name (UPDATE ME)
##############################################
# Remove old targets
##############################################
rm -f $DISK_IMAGE_FILE
rm -f $ZIP_IMAGE_FILE
test -d $DISK_IMAGE && chmod -R +w $DISK_IMAGE && rm -rf $DISK_IMAGE
mkdir -p $DISK_IMAGE
##############################################
##############################################
# iPhone Target Build
##############################################
xcodebuild -target ${PROJECT} -configuration $CONFIGURATION install\
ARCHS=armv6 SKIP_INSTALL=NO\
DSTROOT=$BUILD_DIR INSTALL_PATH=/$VOLNAME || exit 2
##############################################
##############################################
# Copy the Documentation
##############################################
cp -p $SOURCE_ROOT/Documents/ReadMe.txt $DISK_IMAGE
##############################################
##############################################
# Copy the AD_HOC Licence
##############################################
RESULT=$( grep $LICENCE_STRING -l -r "/Users/"${USER}"/Library/MobileDevice/Provisioning Profiles/" | head -n 1 )
cp "${RESULT}" $DISK_IMAGE/Licence.mobileprovision
##############################################
##############################################
# Write out the disk image
##############################################
hdiutil create -volname $VOLNAME -srcfolder $DISK_IMAGE $DISK_IMAGE_FILE
##############################################
##############################################
# Write out the contents in a zip
##############################################
cd $DISK_IMAGE
zip -r9 $ZIP_IMAGE_FILE * -x "*.DS_Store"
##############################################
##############################################
# remove the old folder
##############################################
rm -rf $DISK_IMAGE
##############################################



