Skip to content

Instantly share code, notes, and snippets.

@timsutton
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timsutton/b02f6fcfc77917adfe68 to your computer and use it in GitHub Desktop.
Save timsutton/b02f6fcfc77917adfe68 to your computer and use it in GitHub Desktop.
Dead-simple Omnibus-style installer package builder for OS X using Homebrew.
#!/bin/sh -e
#
# make_brew_omnibus_osx_pkg.sh
# Tim Sutton, 2014
#
# Dead-simple Omnibus-style installer package builder for OS X using Homebrew. It
# takes no command-line options or arguments: all configuration is done using
# environment variables:
#
# - FORMULA: (required) name of Homebrew formula
# - PREFIX: prefix to use in conjunction with FORMULA, to use as a root for the package.
# For example, a PREFIX of "/opt" and a FORMULA of "tree" will make a root of
# /opt/tree, meaning the final binaries will be in /opt/tree/bin/ (or sbin, etc.)
# default: /opt
# - REVERSE_DOMAIN: reverse-domain-style prefix for the installer package identifier
# default: org.homebrew
# - BREW_GIT_SHA: optional Git SHA-1 hash to which the Brew installation's HEAD will
# be checked out. Useful if you want to 'pin' to a specific known state
# for the Formula.
# default: (none, use the tip of master branch)
# - OUTPUT_DIR: optional output directory for the built package
# default: (current working directory)
FORMULA=${FORMULA:-""}
PREFIX=${PREFIX:-"/opt"}
REVERSE_DOMAIN=${REVERSE_DOMAIN:-"org.homebrew"}
BREW_GIT_SHA=${BREW_GIT_SHA:-""}
OUTPUT_DIR=${OUTPUT_DIR:-"$(pwd)"}
if [ -z "${FORMULA}" ]; then
echo "FORMULA must be defined as an environment variable to this script." 1>&2
exit 1
fi
# Clear any existing $PREFIX and install Homebrew to our root
[ -d "${PREFIX}" ] && rm -rf "${PREFIX}"
mkdir "${PREFIX}"
root="${PREFIX}/${FORMULA}"
git clone https://github.com/homebrew/homebrew "${root}"
cd "${root}"
# Optionally revert to BREW_GIT_SHA
if [ -n "${BREW_GIT_SHA}" ]; then
git checkout "${BREW_GIT_SHA}"
fi
# Brew install $FORMULA, figure out what version was installed
bin/brew install "${FORMULA}"
version_path="${root}/Cellar/${FORMULA}"
version=$(ls "${version_path}")
if [ -z "${version}" ]; then
echo "Could not derive version directory expected at path: ${version_path}" 1>&2
exit 1
fi
# Build a package using our root, with the same path as the install destination.
# Includes a short list of exclude filters, which should leave Cellar and the
# LSB directories.
# This pkgbuild filter list was tailored for exactly one formula: wimlib,
# so it's likely there are things missing or that eventually it will need to be
# updated to follow Homebrew.
pkgbuild \
--version "${version}" \
--identifier "${REVERSE_DOMAIN}.${FORMULA}" \
--root "${root}" \
--install-location "${root}" \
--filter '/.git.*$' \
--filter '/.yardopts' \
--filter '/.*.md$' \
--filter '/.*.txt$' \
--filter '/Library$' \
"${OUTPUT_DIR}/${FORMULA}-${version}.pkg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment