|
| 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 | +# ignore rules |
| 8 | +readarray -t ignores < <(grep -v '^#' .typstignore | grep '[^[:blank:]]') |
| 9 | + |
| 10 | +# recursively print all files that are not excluded via .typstignore |
| 11 | +function enumerate { |
| 12 | + local root="$1" |
| 13 | + if [[ -f "$root" ]]; then |
| 14 | + echo "$root" |
| 15 | + else |
| 16 | + local files |
| 17 | + readarray -t files < <(find "$root" \ |
| 18 | + -mindepth 1 -maxdepth 1 \ |
| 19 | + -not -name .git \ |
| 20 | + -not -name .typstignore) |
| 21 | + # declare -p files >&2 |
| 22 | + |
| 23 | + local f |
| 24 | + for f in "${files[@]}"; do |
| 25 | + local include |
| 26 | + include=1 |
| 27 | + |
| 28 | + local ignore |
| 29 | + for ignore in "${ignores[@]}"; do |
| 30 | + if [[ "$ignore" =~ ^! ]]; then |
| 31 | + ignore="${ignore:1}" |
| 32 | + if [[ "$f" == ./$ignore ]]; then |
| 33 | + # echo "\"$f\" matched \"!$ignore\"" >&2 |
| 34 | + include=1 |
| 35 | + fi |
| 36 | + elif [[ "$f" == ./$ignore ]]; then |
| 37 | + # echo "\"$f\" matched \"$ignore\"" >&2 |
| 38 | + include=0 |
| 39 | + fi |
| 40 | + done |
| 41 | + if [[ "$include" == 1 ]]; then |
| 42 | + enumerate "$f" |
| 43 | + fi |
| 44 | + done |
| 45 | + fi |
| 46 | +} |
| 47 | + |
| 48 | +# List of all files that get packaged |
| 49 | +readarray -t files < <(enumerate ".") |
| 50 | +# declare -p files >&2 |
| 51 | + |
| 52 | +# Local package directories per platform |
| 53 | +if [[ "$OSTYPE" == "linux"* ]]; then |
| 54 | + DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}" |
| 55 | +elif [[ "$OSTYPE" == "darwin"* ]]; then |
| 56 | + DATA_DIR="$HOME/Library/Application Support" |
| 57 | +else |
| 58 | + DATA_DIR="${APPDATA}" |
| 59 | +fi |
| 60 | + |
| 61 | +if (( $# < 1 )) || [[ "${1:-}" == "help" ]]; then |
| 62 | + echo "package TARGET" |
| 63 | + echo "" |
| 64 | + echo "Packages all relevant files into a directory named '<name>/<version>'" |
| 65 | + echo "at TARGET. If TARGET is set to @local or @preview, the local Typst package" |
| 66 | + echo "directory will be used so that the package gets installed for local use." |
| 67 | + echo "The name and version are read from 'typst.toml' in the project root." |
| 68 | + echo "" |
| 69 | + echo "Local package prefix: $DATA_DIR/typst/package/local" |
| 70 | + echo "Local preview package prefix: $DATA_DIR/typst/package/preview" |
| 71 | + exit 1 |
| 72 | +fi |
| 73 | + |
| 74 | +function read-toml() { |
| 75 | + local file="$1" |
| 76 | + local key="$2" |
| 77 | + # Read a key value pair in the format: <key> = "<value>" |
| 78 | + # stripping surrounding quotes. |
| 79 | + perl -lne "print \"\$1\" if /^${key}\\s*=\\s*\"(.*)\"/" < "$file" |
| 80 | +} |
| 81 | + |
| 82 | +ROOT="$(cd "$(dirname "$0")"; pwd -P)/.." # macOS has no realpath |
| 83 | +TARGET="${1:?Missing target path, @local or @preview}" |
| 84 | +PKG_PREFIX="$(read-toml "$ROOT/typst.toml" "name")" |
| 85 | +VERSION="$(read-toml "$ROOT/typst.toml" "version")" |
| 86 | + |
| 87 | +if [[ "$TARGET" == "@local" ]]; then |
| 88 | + TARGET="${DATA_DIR}/typst/packages/local" |
| 89 | + echo "Install dir: $TARGET" |
| 90 | +elif [[ "$TARGET" == "@preview" ]]; then |
| 91 | + TARGET="${DATA_DIR}/typst/packages/preview" |
| 92 | + echo "Install dir: $TARGET" |
| 93 | +fi |
| 94 | + |
| 95 | +TMP="$(mktemp -d)" |
| 96 | + |
| 97 | +for f in "${files[@]}"; do |
| 98 | + mkdir -p "$TMP/$(dirname "$f")" 2>/dev/null |
| 99 | + cp -r "$ROOT/$f" "$TMP/$f" |
| 100 | +done |
| 101 | + |
| 102 | +TARGET="${TARGET:?}/${PKG_PREFIX:?}/${VERSION:?}" |
| 103 | +echo "Packaged to: $TARGET" |
| 104 | +if rm -r "${TARGET:?}" 2>/dev/null; then |
| 105 | + echo "Overwriting existing version." |
| 106 | +fi |
| 107 | +mkdir -p "$TARGET" |
| 108 | +mv "$TMP"/* "$TARGET" |
0 commit comments