Building an Installer — Part 1

A Step-by-Step process on how we created the C4 Xcode Templates installer for OSX

C4
The C4 Dev

--

Intro

Building an installer for OSX is a right pain in the ass. There’s little documentation and hardly anyone has a good, up to date tutorial that covers the subjects we need for installing the Xcode templates for C4. The best resource we found was published by Shane Kirk, which we used as a basis for figuring out how to do everything else that we needed.

There used to be a program called Package Maker that Apple provided. But, they canned that app and shifted the process of building installers to the command line. If you’re not used to reading the OSX Man Pages, and you don’t work in the command-line a lot, then the process of learning to build an installer is less than fun.

The rest of this doc is an outline of how I built the installer for C4. It took me about a day to learn how to run scripts on the command line so I could build an installer from a couple of shell files.

A Basic Installer

The first thing I had to do was figure out how to build an installer (i.e. a .pkg file) from terminal using the pkgbuild command.

On your Desktop…

1. Create a folder called files and put an image called test.jpg in it.

2. Open Terminal and run:

cd Desktop

3. We are going to build the most simple installer possible. We do this by running the following line in Terminal:

pkgbuild --root ./files --identifier com.test.travis --version 1 --install-location /Desktop/newFiles test.pkg
This is what the terminal window looks like with the command pasted

Dissecting the command we expect the following:

--root ./files

… should pull content from the files folder on the desktop.

--identifier com.test.travis

… basically gives the installer a unique name.

--version 1

… sets the version number for the installer.

--install-location /Desktop/newFiles

… should set the target for the installer to create a folder on the desktop called newFiles, into which we assume the installer’s files will go.

test.pkg

… is the name of the file we’re creating.

4. Running the installer seems to do nothing! We expected a new folder called newFiles to appear on the desktop but it didn’t… If you search for the file in Spotlight, you’ll see that the folder is actually in your root directory.

Macintosh HD/Desktop/files/

But! It had the test.jpg image in it!

5. Next, we want the newFiles folder to appear on the desktop, so we have to change the variable for the install-location to:

--install-location /Users/$USER/Desktop/newFiles

6. Run the pkg again and a new folder will pop up on the desktop with the test image inside it.

The final command looks like:

pkgbuild --root ./files --identifier com.test.travis --version 1 --install-location /Users/$USER/Desktop/files test.pkg
This is the terminal window after running the command

Running From a Script

Right, so we don’t want to have to copy and paste all that nonsense into Terminal every time we want to run something, because doing so makes modifying variables is a pain in the ass. So, we want to run command-line code from scripts. It’s much easier.

Create a new text file and call it basicInstaller.sh (you can use whichever text editor you have, I use TextMate). Inside the file add the line of code from above.

This is what the script file looks like, with only a single line of code

With this file on the desktop, go back to Terminal and run:

sh basicInstaller.sh

Which looks cleaner, is easier to read and allows us to edit the contents of the script without having to copy / paste a bunch of ugly stuff into Terminal.

A little clearer than the previous terminal window with a long command in it

The script part of this is a real gem. It means that we can add a bunch of commands to a single file and run them from one simple line in Terminal.

--

--

C4
The C4 Dev

Code, Creatively. An open-source API for iOS.