/ scripts / package
package
 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"