Package: applecrate
AppleCratePackage your command line tools into a native macOS installer.AppleCrate is a tool for creating native macOS installers for your command line tools. It's useful for creating installers for command line tools written in any language. Tools written in interpreted languages like Python will need to be first processed with a tool likepyinstallerorOfek Lev'sexcellentPyAppto create a standalone executable. If you're starting a new Python command line project, I highly recommend using PyApp by to create the standalone executable.InstallationSee thelatest releasefor the AppleCrate installer package for your platform. This installer includes a signed binary for theapplecratecommand line tool built withPyAppand packaged with AppleCrate itself into a signed macOS installer package.Note: I've not yet figured out how to create a universal binary with PyApp so you'll need to download the correct installer package for your platform, e.g.applecrate-0.1.7-x86_64-installer.pkgfor Intel Macs andapplecrate-0.1.7-arm64-installer.pkgfor Apple Silicon Macs.The installer package will add theapplecratecommand line tool to/usr/local/bin. You can upgrade to a new version of AppleCrate by runningapplecrate update.Alternatively, you can install AppleCrate withpiporpipx:AppleCrate requires Python 3.9 or later.pipinstallapplecrateorpipxinstallapplecrateUpgradingAppleCrate includes a self-update command to upgrade your installation to the latest version of AppleCrate. This works for both the binary installer package and thepiporpipxinstallation.applecrateupdateIf you are interested in implementing this in your own Python package, see theimplementation.Simple Exampleapplecratebuild\--appmytool\--version1.0.0\--licenseLICENSE\--installdist/mytool"/usr/local/bin/{{ app }}-{{ version }}"\--link"/usr/local/bin/{{ app }}-{{ version }}""/usr/local/bin/mytool"This will create a native macOS installer for the tooldist/mytoolwhich will install it to/usr/local/bin/mytool-1.0.0. The installer will create a symlink to the tool at/usr/local/bin/mytooland will also create an uninstaller to remove the tool.You can also use applecrate from your own Python code to create installers programmatically:"""Create a macOS installer package programmatically."""fromapplecrateimportbuild_installerif__name__=="__main__":build_installer(app="MyApp",version="1.0.0",license="LICENSE",install=[("dist/myapp","/usr/local/bin/myapp")],output="build/{{ app }}-{{ version }}.pkg",verbose=print,)How It WorksAppleCrate is a Python application that uses thepkgbuildandproductbuildcommand line tools to create a macOS installer package. AppleCrate does not do anything that you couldn't do yourself with these tools, but it automates the process and provide a simple command line interface. Creating a macOS installer package is a multi-step process that requires the generation of multiple files such as HTML files for the welcome screen, pre and post install scripts, Distribution XML file, etc. AppleCrate takes care of all of this for you but also allows you to customize the installer by providing your own files for these steps.AppleCrate usesJinja2templates to generate the files required for the installer. This allows you to use template variables in your files or command line parameters to customize the installer. For example, you can use{{ app }}and{{ version }}in your files to refer to the app name and version you provide on the command line.UsageUsage: applecrate build [OPTIONS]

  applecrate: A Python package for creating macOS installer packages.

Options:
  -a, --app TEXT                  App name.
  -v, --version TEXT              App version.
  -I, --identifier TEXT           Unique package identifier. The OS X Installer
                                  recognizes a package as being an upgrade to an
                                  already-installed package only if the package
                                  identifiers match, so it is advisable to set a
                                  meaningful, consistent identifier when you
                                  build the package. If not set, the identifier
                                  will be set to 'org.opensource.{{ app }}'.
  -l, --license FILE              Path to license file. If provided, the
                                  installer will include a click-through license
                                  agreement.
  -w, --welcome FILE              Path to welcome markdown or HTML file
  -c, --conclusion FILE           Path to conclusion markdown or HTML file
  -u, --uninstall FILE            Path to uninstall script; if not provided, an
                                  uninstall script will be created for you. See
                                  also '--no-uninstall'
  -U, --no-uninstall              Do not include an uninstall script in the
                                  package
  -L, --url NAME URL              Links to additional resources to include in
                                  conclusion HTML shown after installation. For
                                  example, the project website or documentation.
  -b, --banner FILE               Path to optional PNG banner image for
                                  installer package.
  -i, --install FILE_OR_DIR DEST  Install FILE_OR_DIR to destination DEST; DEST
                                  must be an absolute path, for example
                                  '/usr/local/bin/app'. DEST may include
                                  template variables {{ app }} and {{ version
                                  }}. For example: `--install dist/app
                                  "/usr/local/bin/{{ app }}-{{ version }}"` will
                                  install the file 'dist/app' to
                                  '/usr/local/bin/app-1.0.0' if --app=app and
                                  --version=1.0.0.
  -k, --link SRC TARGET           Create a symbolic link from SRC to TARGET
                                  after installation. SRC and TARGET must be
                                  absolute paths and both may include template
                                  variables {{ app }} and {{ version }}. For
                                  example: `--link "/Library/Application
                                  Support/{{ app }}/{{ version }}/app"
                                  "/usr/local/bin/{{ app }}-{{ version }}"`
  -p, --pre-install FILE          Path to pre-install shell script; if not
                                  provided, a pre-install script will be created
                                  for you.
  -P, --post-install FILE         Path to post-install shell script; if not
                                  provided, a post-install script will be
                                  created for you. If provided, the installer
                                  will run this script after other post-install
                                  actions.
  -m, --chmod MODE PATH           Change the mode of PATH to MODE after
                                  installation. PATH must be an absolute path.
                                  PATH may contain template variables {{ app }}
                                  and {{ version }}. MODE must be an octal
                                  number, for example '755'.
  -s, --sign APPLE_DEVELOPER_CERTIFICATE_ID
                                  Sign the installer package with a developer
                                  ID. If APPLE_DEVELOPER_CERTIFICATE_ID starts
                                  with '$', it will be treated as an environment
                                  variable and the value of the environment
                                  variable will be used as the developer ID.
  -d, --build-dir DIRECTORY       Build directory to use for building the
                                  installer package. Default is
                                  build/applecrate/darwin if not provided.
  -o, --output FILE               Path to save the installer package.
  --help                          Show this message and exit.ConfigurationThe command line tool applecrate can be configured viapyproject.tomlorapplecrate.tomlin the current working directory or via command line options. The command line arguments will always take precedence over the configuration files. If present,applecrate.tomlwill take precedence overpyproject.toml. The configuration file should be in the following format:pyproject.toml:[tool.applecrate]app="mytool"version="1.0.0"license="LICENSE"install=[["dist/mytool","/usr/local/bin/{{ app }}-{{ version }}"],]sign="$APPLE_DEVELOPER_CERTIFICATE_ID"applecrate.toml:app="mytool"version="1.0.0"license="LICENSE"install=[["dist/mytool","/usr/local/bin/{{ app }}-{{ version }}"],]sign="$APPLE_DEVELOPER_CERTIFICATE_ID"Any command line option is a valid key in the configuration file. For example, the--appoption can be set in the configuration file asapp = "mytool". Command line options with a dash (-) should be converted to underscores (_) in the configuration file. For example, the--pre-installoption should be set in the configuration file aspre_install = "scripts/preinstall.sh".If the value ofsignbegins with a$, as in the example above, it will be treated as an environment variable and the value of the environment variable will be used as the certificate ID.Template VariablesDestination paths, the package identifier, the welcome and conclusion HTML files, and the pre and post install scripts can include template variables. The following template variables are available:app: The name of the app.version: The version of the app.identifier: The package identifier.uninstall: The path to the uninstall shell script.url: A list of URLs to include in the installer package.install: A list of tuples of source and destination paths to install.banner: The path to the banner image.link: A list of tuples of source and target paths to create symlinks post-installation.post_install: The path to the post-install shell script.pre_install: The path to the pre-install shell script.chmod: A list of tuples of mode and path to change the mode of files post-installation.build_dir: The build directory.output: The path to the installer package.machine: The machine architecture, for examplex86_64orarm64.For example, the default post-install script includes the following to create a symlink:{%iflink%}# Create links if needed{%forsource,targetinlink%}ln-s"{{ source }}""{{ target }}"{%endfor%}{%endif%}The Jinja2 template engine will replace{{ source }}and{{ target }}with the source and target paths provided on the command line and remove theifandforblocks.See theJinja2 template documentationfor more information on how to use template variables.The package identifier and path arguments such as the build path and the output file path may also be templates but they will only have access to theapp,version, andmachinevariables. Your scripts and welcome/conclusion files will have access to all of the variables.See thetemplatesdirectory in the AppleCrate source code for examples of how to use these variables in your own scripts or welcome/conclusion files.UtilitiesAppleCrate includes a few utilities for working with installer packages:fromapplecrate.pkgutilimportpkg_info,pkg_files,extract_pkgpkg_inforeturns a dictionary of information about the package:pkg_filesreturns a list of files in the package.extract_pkgextracts the package to a directory.>>>importos>>>fromapplecrate.pkgutilimportpkg_info,pkg_files,extract_pkg>>>pkg_info("dist/applecrate-0.1.6-x86_64-installer.pkg"){'overwrite-permissions': 'true', 'relocatable': 'false', 'identifier': 'org.rhettbull.applecrate', 'postinstall-action': 'none', 'version': '0.1.6', 'format-version': '2', 'generator-version': 'InstallCmds-830.2 (22G90)', 'auth': 'root'}>>>pkg_files("dist/applecrate-0.1.6-x86_64-installer.pkg")['Distribution', 'Resources/conclusion.html', 'Resources/welcome.html', 'applecrate.pkg/postinstall', 'applecrate.pkg/Bom', 'applecrate.pkg/custom_preinstall', 'applecrate.pkg/Payload', 'applecrate.pkg/Scripts', 'applecrate.pkg/preinstall', 'applecrate.pkg/PackageInfo', 'applecrate.pkg/usr/local/bin/applecrate', 'applecrate.pkg/Library/Application Support/applecrate/0.1.6/uninstall.sh']>>>os.mkdir("temp")>>>extract_pkg("dist/applecrate-0.1.6-x86_64-installer.pkg","temp")>>>To DoAdd support for signing the installer with a developer certificateAdd python API to create installers programmaticallyTestsAddapplecrate checkcommand to check the configuration without building the installerDocumentation (set up mkdocs)Add support for notarizing the installerCreditsHeavily inspired bymacOS Installer BuilderbyKosala Herath. AppleCrate is a complete rewrite in Python but borrows many ideas from macOS Installer Builder and is thus licensed under the same Apache License, Version 2.0.LicenseCopyright Rhet Turnbull, 2024. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the Licensehere.ContributingContributions of all kinds are welcome! Please seeCONTRIBUTING.mdfor more information.See AlsomacOS-Pkg-Builder, another Python package for creating macOS installer packages. macOS-Pkg-Builder provides a programmatic interface for creating installer packages and is more flexible than AppleCrate. AppleCrate is designed to be simpler and easier to use for common use cases. I wasn't aware of macOS-Pkg-Builder when I started AppleCrate, but I might have used it to implement AppleCrate had I known about it.