|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -eu |
| 3 | + |
| 4 | +# adapted from https://github.com/johannes-wolf/cetz/blob/35c0868378cea5ad323cc0d9c2f76de8ed9ba5bd/scripts/package |
| 5 | +# licensed under Apache License 2.0 |
| 6 | + |
| 7 | +# List of all files that get packaged |
| 8 | +files=( |
| 9 | + docs/manual.pdf |
| 10 | + src/ |
| 11 | + CHANGELOG.md |
| 12 | + LICENSE |
| 13 | + README.md |
| 14 | + typst.toml |
| 15 | +) |
| 16 | + |
| 17 | +# Local package directories per platform |
| 18 | +if [[ "$OSTYPE" == "linux"* ]]; then |
| 19 | + DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}" |
| 20 | +elif [[ "$OSTYPE" == "darwin"* ]]; then |
| 21 | + DATA_DIR="$HOME/Library/Application Support" |
| 22 | +else |
| 23 | + DATA_DIR="${APPDATA}" |
| 24 | +fi |
| 25 | + |
| 26 | +if (( $# < 1 )) || [[ "${1:-}" == "help" ]]; then |
| 27 | + echo "package TARGET" |
| 28 | + echo "" |
| 29 | + echo "Packages all relevant files into a directory named '<name>/<version>'" |
| 30 | + echo "at TARGET. If TARGET is set to @local or @preview, the local Typst package" |
| 31 | + echo "directory will be used so that the package gets installed for local use." |
| 32 | + echo "The name and version are read from 'typst.toml' in the project root." |
| 33 | + echo "" |
| 34 | + echo "Local package prefix: $DATA_DIR/typst/package/local" |
| 35 | + echo "Local preview package prefix: $DATA_DIR/typst/package/preview" |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +function read-toml() { |
| 40 | + local file="$1" |
| 41 | + local key="$2" |
| 42 | + # Read a key value pair in the format: <key> = "<value>" |
| 43 | + # stripping surrounding quotes. |
| 44 | + perl -lne "print \"\$1\" if /^${key}\\s*=\\s*\"(.*)\"/" < "$file" |
| 45 | +} |
| 46 | + |
| 47 | +ROOT="$(cd "$(dirname "$0")"; pwd -P)/.." # macOS has no realpath |
| 48 | +TARGET="${1:?Missing target path, @local or @preview}" |
| 49 | +PKG_PREFIX="$(read-toml "$ROOT/typst.toml" "name")" |
| 50 | +VERSION="$(read-toml "$ROOT/typst.toml" "version")" |
| 51 | + |
| 52 | +if [[ "$TARGET" == "@local" ]]; then |
| 53 | + TARGET="${DATA_DIR}/typst/packages/local" |
| 54 | + echo "Install dir: $TARGET" |
| 55 | +elif [[ "$TARGET" == "@preview" ]]; then |
| 56 | + TARGET="${DATA_DIR}/typst/packages/preview" |
| 57 | + echo "Install dir: $TARGET" |
| 58 | +fi |
| 59 | + |
| 60 | +TMP="$(mktemp -d)" |
| 61 | + |
| 62 | +for f in "${files[@]}"; do |
| 63 | + if [[ -e "$f" ]]; then |
| 64 | + mkdir -p "$TMP/$(dirname "$f")" 2>/dev/null |
| 65 | + cp -r "$ROOT/$f" "$TMP/$f" |
| 66 | + fi |
| 67 | +done |
| 68 | + |
| 69 | +TARGET="${TARGET:?}/${PKG_PREFIX:?}/${VERSION:?}" |
| 70 | +echo "Packaged to: $TARGET" |
| 71 | +if rm -r "${TARGET:?}" 2>/dev/null; then |
| 72 | + echo "Overwriting existing version." |
| 73 | +fi |
| 74 | +mkdir -p "$TARGET" |
| 75 | +mv "$TMP"/* "$TARGET" |
0 commit comments