Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a postflight script using pkgbuild and productbuild on Mac building an installation package

I want to build an installation package for Mac OS X that contains 4 sub packages. The sub packages are build with pkgbuild. The final package is build with productbuild using a Distribution.xml for welcome and license text and install location choices.

Now I want to run a script after the installation has succeeded. In particular after the receipts of all packaged are written by the installer.

As far as I understand the man pages and documentation and other useful links I should use a postflight script for that. Unfortunately I can't get it to work. I named it postflight and added to the sub package with pkgbuild using the --script option but it seems pkgbuild does not support postflight scripts. When I name it postinstall it runs but unfortunately before the receipts are written.

I need to run after the receipts are written because I want to write a xcconfig file for XCode constisting of the location of the other packages' install locations using the pkgutil tool.

I already tried to use the expand-flatten hack by adding a Scripts/ folder to the expanded pkg but that seems to be ignored after I flatten it.

Is there a way to accomplish my need? (Writing the chosen install locations of my packages to a file in the installation location of a sub package)

If possible I want to avoid using the deprecated packagemaker tool, at least if there is another better way.

Thanks in advance

like image 769
ecreif Avatar asked Nov 04 '22 01:11

ecreif


1 Answers

As I had to find out finally, the postflight script is also executed before the receipts are written. So my solution to this problem is now the following:

I add postinstall scripts to my packages that read the DSTROOT environment variable set by the Mac OS installer and write them to a file in the shared temp folder:

echo "${DSTROOT}" > "${SHARED_INSTALLER_TEMP}/my_install_location"

Then in my package, that needs these information, I read the old receipts via a postinstall script (in case the sub package is already installed and the user "reinstalls" leaving out some of the sub packages) and then the temp file from the other package.

So, if the package is already installed and not reinstalled it uses the current install location otherwise it uses the new install location via the temp files. If nothing is present the required package was not installed so I use default values and write them to the xcconfig file (that probably won't work):

pkgutil --pkg-info-plist my.package.bundle > "${SHARED_INSTALLER_TEMP}/tmp.plist"
if [ -e "${SHARED_INSTALLER_TEMP}/tmp.plist" ];
then
    MY_PACKAGE_VOL=`/usr/libexec/PlistBuddy -c "Print :volume" "${SHARED_INSTALLER_TEMP}/tmp.plist"`
    MY_PACKAGE_DIR=`/usr/libexec/PlistBuddy -c "Print :install-location" "${SHARED_INSTALLER_TEMP}/tmp.plist"`      
    MY_PACKAGE_DIR="${MY_PACKAGE_VOL}${MY_PACKAGE_DIR}"
fi

After reading the "old" install location, I check if I have a new install location using the previously written temp file from the other packages:

if [ -e "${SHARED_INSTALLER_TEMP}/my_install_location" ];
then
    MY_PACKAGE_DIR=`cat "${SHARED_INSTALLER_TEMP}/my_install_location"`
fi 

...
Write the xcconfig file...
...

If someone has a better solution let me know.

like image 122
ecreif Avatar answered Nov 09 '22 10:11

ecreif