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