Skip to content

Commit 85a71fc

Browse files
authored
Merge pull request typst-community#8 from SillyFreak/install-uninstall
Add commands for packaging and local install/uninstall
2 parents e50b971 + fe4f791 commit 85a71fc

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

.typstignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# this is not a "standard" ignore file, it's specific to this template's `scripts/package` script
2+
# list any files here that should not be uploaded to Universe when releasing this package
3+
4+
# if you are used to ignore files, be aware that .typstignore is a bit more limited:
5+
# - only this file is used; .typstignore files in subdirectories are not considered
6+
# - patterns must match file/directory names from the beginning: `x.typ` will not match `src/x.typ`
7+
# - `*` in patterns works, but also matches directory separators: `*.typ` _will_ match `src/x.typ`
8+
# .git and .typstignore are excluded automatically
9+
10+
.github
11+
scripts
12+
tests
13+
Justfile
14+
# PDF manuals should be included so that they can be linked, but not their sources
15+
docs/*
16+
!docs/*.pdf

Justfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,25 @@ test *args:
1818
update *args:
1919
typst-test update {{ args }}
2020

21+
# package the library into the specified destination folder
22+
package target:
23+
./scripts/package "{{target}}"
24+
25+
# install the library with the "@local" prefix
26+
install: (package "@local")
27+
28+
# install the library with the "@preview" prefix (for pre-release testing)
29+
install-preview: (package "@preview")
30+
31+
[private]
32+
remove target:
33+
./scripts/uninstall "{{target}}"
34+
35+
# uninstalls the library from the "@local" prefix
36+
uninstall: (remove "@local")
37+
38+
# uninstalls the library from the "@preview" prefix (for pre-release testing)
39+
uninstall-preview: (remove "@preview")
40+
2141
# run ci suite
2242
ci: test doc

scripts/package

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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"

scripts/uninstall

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
# Local package directories per platform
8+
if [[ "$OSTYPE" == "linux"* ]]; then
9+
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}"
10+
elif [[ "$OSTYPE" == "darwin"* ]]; then
11+
DATA_DIR="$HOME/Library/Application Support"
12+
else
13+
DATA_DIR="${APPDATA}"
14+
fi
15+
16+
if (( $# < 1 )) || [[ "${1:-}" == "help" ]]; then
17+
echo "uninstall TARGET"
18+
echo ""
19+
echo "removes the package installed into a directory named '<name>/<version>'"
20+
echo "at TARGET. If TARGET is set to @local or @preview, the local Typst package"
21+
echo "directory will be used so that the package gets installed for local use."
22+
echo "The name and version are read from 'typst.toml' in the project root."
23+
echo ""
24+
echo "Local package prefix: $DATA_DIR/typst/package/local"
25+
echo "Local preview package prefix: $DATA_DIR/typst/package/preview"
26+
exit 1
27+
fi
28+
29+
function read-toml() {
30+
local file="$1"
31+
local key="$2"
32+
# Read a key value pair in the format: <key> = "<value>"
33+
# stripping surrounding quotes.
34+
perl -lne "print \"\$1\" if /^${key}\\s*=\\s*\"(.*)\"/" < "$file"
35+
}
36+
37+
ROOT="$(cd "$(dirname "$0")"; pwd -P)/.." # macOS has no realpath
38+
TARGET="${1:?Missing target path, @local or @preview}"
39+
PKG_PREFIX="$(read-toml "$ROOT/typst.toml" "name")"
40+
VERSION="$(read-toml "$ROOT/typst.toml" "version")"
41+
42+
if [[ "$TARGET" == "@local" ]]; then
43+
TARGET="${DATA_DIR}/typst/packages/local/"
44+
echo "Install dir: $TARGET"
45+
elif [[ "$TARGET" == "@preview" ]]; then
46+
TARGET="${DATA_DIR}/typst/packages/preview/"
47+
echo "Install dir: $TARGET"
48+
fi
49+
50+
TARGET="${TARGET:?}/${PKG_PREFIX:?}/${VERSION:?}"
51+
echo "Package to uninstall: $TARGET"
52+
if [[ ! -e "${TARGET:?}" ]]; then
53+
echo "Package was not found."
54+
elif rm -r "${TARGET:?}" 2>/dev/null; then
55+
echo "Successfully removed."
56+
else
57+
echo "Removal failed."
58+
fi

0 commit comments

Comments
 (0)