-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage
86 lines (73 loc) · 2.22 KB
/
package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env bash
set -eu
# adapted from https://github.com/johannes-wolf/cetz/blob/35c0868378cea5ad323cc0d9c2f76de8ed9ba5bd/scripts/package
# licensed under Apache License 2.0
. "$(dirname "${BASH_SOURCE[0]}")/setup"
if (( $# < 1 )) || [[ "${1:-}" == "help" ]]; then
echo "package TARGET"
echo ""
echo "Packages all relevant files into a directory named '<name>/<version>'"
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
TARGET="$(resolve-target "${1:?Missing target path, @local or @preview}")"
echo "Install dir: $TARGET"
# ignore rules
readarray -t ignores < <(grep -v '^#' .typstignore | grep '[^[:blank:]]')
# recursively print all files that are not excluded via .typstignore
function enumerate {
local root="$1"
if [[ -f "$root" ]]; then
echo "$root"
else
local files
readarray -t files < <(find "$root" \
-mindepth 1 -maxdepth 1 \
-not -name .git \
-not -name .typstignore)
# declare -p files >&2
local f
for f in "${files[@]}"; do
local include
include=1
local ignore
for ignore in "${ignores[@]}"; do
if [[ "$ignore" =~ ^! ]]; then
ignore="${ignore:1}"
if [[ "$f" == ./$ignore ]]; then
# echo "\"$f\" matched \"!$ignore\"" >&2
include=1
fi
elif [[ "$f" == ./$ignore ]]; then
# echo "\"$f\" matched \"$ignore\"" >&2
include=0
fi
done
if [[ "$include" == 1 ]]; then
enumerate "$f"
fi
done
fi
}
# List of all files that get packaged
readarray -t files < <(enumerate ".")
# declare -p files >&2
TMP="$(mktemp -d)"
for f in "${files[@]}"; do
mkdir -p "$TMP/$(dirname "$f")" 2>/dev/null
cp -r "$ROOT/$f" "$TMP/$f"
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"
# include hidden files by setting dotglob
shopt -s dotglob
mv "$TMP"/* "$TARGET"