From 13e94bc3f81255e78b6c00de25704bf677588615 Mon Sep 17 00:00:00 2001 From: Clemens Koza Date: Tue, 4 Jun 2024 00:19:04 +0200 Subject: [PATCH] add package script and Just commands --- Justfile | 10 +++++++ scripts/package | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 scripts/package diff --git a/Justfile b/Justfile index 5dcb261..73384e9 100644 --- a/Justfile +++ b/Justfile @@ -18,5 +18,15 @@ test *args: update *args: typst-test update {{ args }} +# package the library into the specified destination folder +package target: + ./scripts/package "{{target}}" + +# install the library with the "@local" prefix +install: (package "@local") + +# install the library with the "@preview" prefix (for pre-release testing) +install-preview: (package "@preview") + # run ci suite ci: test doc diff --git a/scripts/package b/scripts/package new file mode 100755 index 0000000..61cd9e3 --- /dev/null +++ b/scripts/package @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +set -eu + +# adapted from https://github.com/johannes-wolf/cetz/blob/35c0868378cea5ad323cc0d9c2f76de8ed9ba5bd/scripts/package +# licensed under Apache License 2.0 + +# List of all files that get packaged +files=( + docs/manual.pdf + src/ + CHANGELOG.md + LICENSE + README.md + typst.toml +) + +# Local package directories per platform +if [[ "$OSTYPE" == "linux"* ]]; then + DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}" +elif [[ "$OSTYPE" == "darwin"* ]]; then + DATA_DIR="$HOME/Library/Application Support" +else + DATA_DIR="${APPDATA}" +fi + +if (( $# < 1 )) || [[ "${1:-}" == "help" ]]; then + echo "package TARGET" + echo "" + echo "Packages all relevant files into a directory named '/'" + echo "at TARGET. If TARGET is set to @local or @preview, the local Typst package" + echo "directory will be used so that the package gets installed for local use." + echo "The name and version are read from 'typst.toml' in the project root." + echo "" + echo "Local package prefix: $DATA_DIR/typst/package/local" + echo "Local preview package prefix: $DATA_DIR/typst/package/preview" + exit 1 +fi + +function read-toml() { + local file="$1" + local key="$2" + # Read a key value pair in the format: = "" + # stripping surrounding quotes. + perl -lne "print \"\$1\" if /^${key}\\s*=\\s*\"(.*)\"/" < "$file" +} + +ROOT="$(cd "$(dirname "$0")"; pwd -P)/.." # macOS has no realpath +TARGET="${1:?Missing target path, @local or @preview}" +PKG_PREFIX="$(read-toml "$ROOT/typst.toml" "name")" +VERSION="$(read-toml "$ROOT/typst.toml" "version")" + +if [[ "$TARGET" == "@local" ]]; then + TARGET="${DATA_DIR}/typst/packages/local" + echo "Install dir: $TARGET" +elif [[ "$TARGET" == "@preview" ]]; then + TARGET="${DATA_DIR}/typst/packages/preview" + echo "Install dir: $TARGET" +fi + +TMP="$(mktemp -d)" + +for f in "${files[@]}"; do + if [[ -e "$f" ]]; then + mkdir -p "$TMP/$(dirname "$f")" 2>/dev/null + cp -r "$ROOT/$f" "$TMP/$f" + fi +done + +TARGET="${TARGET:?}/${PKG_PREFIX:?}/${VERSION:?}" +echo "Packaged to: $TARGET" +if rm -r "${TARGET:?}" 2>/dev/null; then + echo "Overwriting existing version." +fi +mkdir -p "$TARGET" +mv "$TMP"/* "$TARGET"