Skip to content

Commit 888011f

Browse files
committed
extract setup script that provides useful package metadata as environment variables. use ${BASH_SOURCE[0]} instead of $0 in that script, for when the script is sourced from outside the scripts directory (e.g. in CI)
1 parent 41d6e49 commit 888011f

File tree

3 files changed

+59
-72
lines changed

3 files changed

+59
-72
lines changed

scripts/package

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ set -eu
44
# adapted from https://github.com/johannes-wolf/cetz/blob/35c0868378cea5ad323cc0d9c2f76de8ed9ba5bd/scripts/package
55
# licensed under Apache License 2.0
66

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+
725
# ignore rules
826
readarray -t ignores < <(grep -v '^#' .typstignore | grep '[^[:blank:]]')
927

@@ -49,49 +67,6 @@ function enumerate {
4967
readarray -t files < <(enumerate ".")
5068
# declare -p files >&2
5169

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-
9570
TMP="$(mktemp -d)"
9671

9772
for f in "${files[@]}"; do

scripts/setup

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# source this script to prepare some common environment variables
2+
3+
# adapted from https://github.com/johannes-wolf/cetz/blob/35c0868378cea5ad323cc0d9c2f76de8ed9ba5bd/scripts/package
4+
# licensed under Apache License 2.0
5+
6+
# Local package directories per platform
7+
if [[ "$OSTYPE" == "linux"* ]]; then
8+
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}"
9+
elif [[ "$OSTYPE" == "darwin"* ]]; then
10+
DATA_DIR="$HOME/Library/Application Support"
11+
else
12+
DATA_DIR="${APPDATA}"
13+
fi
14+
15+
function read-toml() {
16+
local file="$1"
17+
local key="$2"
18+
# Read a key value pair in the format: <key> = "<value>"
19+
# stripping surrounding quotes.
20+
perl -lne "print \"\$1\" if /^${key}\\s*=\\s*\"(.*)\"/" < "$file"
21+
}
22+
23+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)/.." # macOS has no realpath
24+
PKG_PREFIX="$(read-toml "$ROOT/typst.toml" "name")"
25+
VERSION="$(read-toml "$ROOT/typst.toml" "version")"
26+
27+
function resolve-target() {
28+
local target="$1"
29+
30+
if [[ "$target" == "@local" ]]; then
31+
echo "${DATA_DIR}/typst/packages/local"
32+
elif [[ "$target" == "@preview" ]]; then
33+
echo "${DATA_DIR}/typst/packages/preview"
34+
else
35+
echo "$target"
36+
fi
37+
}

scripts/uninstall

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@ set -eu
44
# adapted from https://github.com/johannes-wolf/cetz/blob/35c0868378cea5ad323cc0d9c2f76de8ed9ba5bd/scripts/package
55
# licensed under Apache License 2.0
66

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
7+
. "$(dirname "${BASH_SOURCE[0]}")/setup"
158

169
if (( $# < 1 )) || [[ "${1:-}" == "help" ]]; then
1710
echo "uninstall TARGET"
1811
echo ""
19-
echo "removes the package installed into a directory named '<name>/<version>'"
12+
echo "Removes the package installed into a directory named '<name>/<version>'"
2013
echo "at TARGET. If TARGET is set to @local or @preview, the local Typst package"
2114
echo "directory will be used so that the package gets installed for local use."
2215
echo "The name and version are read from 'typst.toml' in the project root."
@@ -26,26 +19,8 @@ if (( $# < 1 )) || [[ "${1:-}" == "help" ]]; then
2619
exit 1
2720
fi
2821

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
22+
TARGET="$(resolve-target "${1:?Missing target path, @local or @preview}")"
23+
echo "Install dir: $TARGET"
4924

5025
TARGET="${TARGET:?}/${PKG_PREFIX:?}/${VERSION:?}"
5126
echo "Package to uninstall: $TARGET"

0 commit comments

Comments
 (0)