From 8d7e6f846da153283b976f44b87f6411e4db6807 Mon Sep 17 00:00:00 2001 From: Paul Tsouchlos Date: Mon, 16 Sep 2024 21:59:31 -0600 Subject: [PATCH] build: add useful scripts for installing template Can now install it locally (or anywhere else) quite easily. This is the first step for being able to auto create PRs when we do a release --- .typstignore | 7 ++++ scripts/package | 86 +++++++++++++++++++++++++++++++++++++++++++++++ scripts/setup | 37 ++++++++++++++++++++ scripts/uninstall | 33 ++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 .typstignore create mode 100755 scripts/package create mode 100644 scripts/setup create mode 100644 scripts/uninstall diff --git a/.typstignore b/.typstignore new file mode 100644 index 0000000..182b454 --- /dev/null +++ b/.typstignore @@ -0,0 +1,7 @@ +.github +.fork +scripts +template/*.pdf +.gitignore +.issuetracker +modern-cv-docs.* diff --git a/scripts/package b/scripts/package new file mode 100755 index 0000000..57013fb --- /dev/null +++ b/scripts/package @@ -0,0 +1,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 '/'" + 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" diff --git a/scripts/setup b/scripts/setup new file mode 100644 index 0000000..cb6a174 --- /dev/null +++ b/scripts/setup @@ -0,0 +1,37 @@ +# source this script to prepare some common environment variables + +# adapted from https://github.com/johannes-wolf/cetz/blob/35c0868378cea5ad323cc0d9c2f76de8ed9ba5bd/scripts/package +# licensed under Apache License 2.0 + +# Local package directories per platform +if [[ "$OSTYPE" == "linux"* ]]; then + DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}" +elif [[ "$OSTYPE" == "darwin"* ]]; then + DATA_DIR="$HOME/Library/Application Support" +else + DATA_DIR="${APPDATA}" +fi + +function read-toml() { + local file="$1" + local key="$2" + # Read a key value pair in the format: = "" + # stripping surrounding quotes. + perl -lne "print \"\$1\" if /^${key}\\s*=\\s*\"(.*)\"/" < "$file" +} + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)/.." # macOS has no realpath +PKG_PREFIX="$(read-toml "$ROOT/typst.toml" "name")" +VERSION="$(read-toml "$ROOT/typst.toml" "version")" + +function resolve-target() { + local target="$1" + + if [[ "$target" == "@local" ]]; then + echo "${DATA_DIR}/typst/packages/local" + elif [[ "$target" == "@preview" ]]; then + echo "${DATA_DIR}/typst/packages/preview" + else + echo "$target" + fi +} \ No newline at end of file diff --git a/scripts/uninstall b/scripts/uninstall new file mode 100644 index 0000000..d57ae16 --- /dev/null +++ b/scripts/uninstall @@ -0,0 +1,33 @@ +#!/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 "uninstall TARGET" + echo "" + echo "Removes the package installed into a directory named '/'" + 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" + +TARGET="${TARGET:?}/${PKG_PREFIX:?}/${VERSION:?}" +echo "Package to uninstall: $TARGET" +if [[ ! -e "${TARGET:?}" ]]; then + echo "Package was not found." +elif rm -r "${TARGET:?}" 2>/dev/null; then + echo "Successfully removed." +else + echo "Removal failed." +fi