/ scripts / setup
setup
 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  }