Skip to content

Commit c70c957

Browse files
committed
add uninstall script and Just commands
1 parent 0512414 commit c70c957

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Justfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,15 @@ install: (package "@local")
2828
# install the library with the "@preview" prefix (for pre-release testing)
2929
install-preview: (package "@preview")
3030

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+
3141
# run ci suite
3242
ci: test doc

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)