Tuesday, November 10, 2015

Introduction to pkgbuild

Since Apple's PackageMaker went away, people have been looking for a good solid tool to build installer packages.  JAMF's Composer gets a lot of attention and works for many.  However it's not right in all situations, cost being one factor.

Apple supplies pkgbuild with developer tools and, while being a command line tool, is not as hard as one might think.  While pkgbuild is amazingly powerful, I'll only be covering an introduction to it here so you get get a basic app out the door.

Start by building a directory structure.

THE SETUP....
PACKAGEROOT --
   ---ROOTofINSTALLFILES
   ---SCRIPTS

PACKAGEROOT is your folder that keeps all the junk about this stuff in there. I might keep the command line string to build the installer in a .txt file for later reference. 

ROOTofINSTALLFILES is the base installation folder.  If you are installing an Application to the Applications folder, then you either put the Application in this folder and set the install-location to /Applications   or you create a folder called Applications with your program inside it and set the install-location to /.    I personally do that latter, so that if needed, I can later easily add a /Library folder with any LaunchDaemon that I might need to add.  Be sure permissions are correct before building your installer.

SCRIPTS folder contains any postinstall or preinstall scripts and is optional (if you don't have any scripts, ignore it).  These are BASH scripts named simply as postinstall or preinstall with no file extensions and they are executed either before or after the installation takes place.  It's useful if you need to backup data or check for a certain condition before installing anything.  If either scripts exits with a non-zero status, the installer will report as failed.


OTHER INFO YOU'LL NEED...
An Identifier:  you'll need a unique identifier for this package.  Generally a reverse domain name with the package name appended will ensure it is unique.  "com.companyname.packagename.pkg" is a good example.
A Version number: Increment this when you distribute a new version of the installer.  This way computers will know if they are upgrading an existing installer, or repeating something already done.



IF YOU BUILD IT....
Run pkgbuild to build your installer.  Open terminal and 'cd' (change directory) into your PACKAGEROOT.  The easiest way to do that is open terminal and type 'cd' followed by a space.  Then drag the PACKAGEROOT folder into the Terminal window and it'll auto-type the full path for you.

Then type the command:
pgkbuild 
 --root <drag ROOTofINSTALLFILES folder here> 
 --identifier "com.companyname.packagename.pkg" 
 --version "1.0" 
 --install-location "/" 
 --scripts "<drag SCRIPTS folder here>  
  NewInstallerPackageName.pkg

I wrote a little tool to encourage users to update their Adobe CC applications.  Here's an example of my installer build.
pkgbuild 
   --root /Users/todd/Desktop/AdobeCCSoftware/AdobeCCUpdater
   --identifier "edu.school.department.AdobeCCUpdater.pkg" 
   --version "1.0" 
   --install-location "/" 
   --scripts /Users/todd/Desktop/AdobeCCSoftware/Scripts AdobeCCUpdateTool.pkg

Note that these commands are all on one line, only separated here for better formatting. 



TROUBLESHOOTING....Go ahead and run your new installer.  If you have an error, especially with scripts involved, be sure to use the 'echo' command in your script.  While the installer is open, from the Window menu, choose Installer Log and set the PopUp to "Show All Logs".  You can then see exactly what your installer is doing.  I also recommend you test your installer on a test box, not your own machine. 

Also be sure to increment your version number when building a new installer. 



COMING UP....
Another great tool is Apple's pkgutil which allows you to manipulate existing packages.  It's great when you want to see what a postinstall script looks like or what else is included in an installer.