Skip to content

Commit

Permalink
fixed templates for user scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Feb 4, 2024
1 parent 8c27d08 commit 95491e7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ applecrate build \

This will create a native macOS installer for the tool `dist/mytool` which will install it to `/usr/local/bin/mytool-1.0.0`. The installer will create a symlink to the tool at `/usr/local/bin/mytool` and will also create an uninstaller to remove the tool.

## How It Works

AppleCrate is a Python application that uses the `pkgbuild` and `productbuild` command 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 uses [Jinja2](https://jinja.palletsprojects.com/en/3.0.x/) templates 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.

## Usage

<!--[[[cog
Expand Down
33 changes: 10 additions & 23 deletions applecrate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
create_build_dirs,
stage_install_files,
)
from .template_utils import create_html_file, get_template, render_template
from .template_utils import create_html_file, get_template, render_template, render_template_from_file
from .utils import copy_and_create_parents, set_from_defaults


Expand Down Expand Up @@ -120,15 +120,13 @@ def cli():
"--pre-install",
"-p",
type=click.Path(dir_okay=False, exists=True),
help="Path to pre-install shell script; "
"if not provided, a pre-install script will be created for you.",
help="Path to pre-install shell script; " "if not provided, a pre-install script will be created for you.",
)
@click.option(
"--post-install",
"-P",
type=click.Path(dir_okay=False, exists=True),
help="Path to post-install shell script; "
"if not provided, a post-install script will be created for you.",
help="Path to post-install shell script; " "if not provided, a post-install script will be created for you.",
)
def build(**kwargs):
"""applecrate: A Python package for creating macOS installer packages."""
Expand Down Expand Up @@ -184,13 +182,10 @@ def build(**kwargs):

# Render the welcome and conclusion templates
echo("Creating welcome.html")
create_html_file(
welcome, BUILD_DIR / "Resources" / "welcome.html", data, "welcome.md"
)
create_html_file(welcome, BUILD_DIR / "Resources" / "welcome.html", data, "welcome.md")

echo("Creating conclusion.html")
create_html_file(
conclusion, BUILD_DIR / "Resources" / "conclusion.html", data, "conclusion.md"
)
create_html_file(conclusion, BUILD_DIR / "Resources" / "conclusion.html", data, "conclusion.md")

echo("Copying license file")
copy_and_create_parents(license, BUILD_DIR / "Resources" / "LICENSE.txt")
Expand All @@ -202,17 +197,9 @@ def build(**kwargs):
# Render the uninstall script
if not no_uninstall:
echo("Creating uninstall script")
target = (
BUILD_DIR
/ "darwinpkg"
/ "Library"
/ "Application Support"
/ app
/ version
/ "uninstall.sh"
)
target = BUILD_DIR / "darwinpkg" / "Library" / "Application Support" / app / version / "uninstall.sh"
if uninstall:
copy_and_create_parents(uninstall, target)
render_template(uninstall, data, target)
else:
template = get_template("uninstall.sh")
render_template(template, data, target)
Expand Down Expand Up @@ -241,13 +228,13 @@ def build(**kwargs):

if pre_install:
target = BUILD_DIR / "scripts" / "custom_preinstall"
copy_and_create_parents(pre_install, target)
render_template_from_file(pre_install, data, target)
pathlib.Path(target).chmod(0o755)
echo(f"Created {target}")

if post_install:
target = BUILD_DIR / "scripts" / "custom_postinstall"
copy_and_create_parents(post_install, target)
render_template_from_file(post_install, data, target)
pathlib.Path(target).chmod(0o755)
echo(f"Created {target}")

Expand Down
10 changes: 10 additions & 0 deletions applecrate/template_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import os
import pathlib

import markdown2
Expand Down Expand Up @@ -46,6 +47,15 @@ def render_template(template: Template, data: dict[str, str], output: pathlib.Pa
file.write(rendered)


def render_template_from_file(filepath: str | os.PathLike, data: dict[str, str], output: pathlib.Path):
"""Render and save a Jinja2 template to a file."""
template = load_template_from_file(filepath)
rendered = template.render(**data)
output.parent.mkdir(parents=True, exist_ok=True)
with open(output, "w") as file:
file.write(rendered)


def create_html_file(
input_path: pathlib.Path | None,
output_path: pathlib.Path,
Expand Down

0 comments on commit 95491e7

Please sign in to comment.