This repository has been archived on 2026-09-16. You can view files and clone it, but cannot push or open issues or pull requests.
typst_reporter/render.sh

144 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPORT_DIR="${ROOT_DIR}/reports"
BUILD_DIR="${ROOT_DIR}/build"
ENTRYPOINT="${ROOT_DIR}/report.typ"
usage() {
cat <<'EOF'
Usage:
./render.sh PATH
./render.sh --all
Examples:
./render.sh reports/2026-09-04-clevo-handoff.yaml
./render.sh --all
Outputs PDFs into:
./build/
Requirements:
- typst must be available in PATH
EOF
}
die() {
echo "error: $*" >&2
exit 1
}
check_dependencies() {
command -v typst >/dev/null 2>&1 \
|| die "'typst' was not found in PATH."
}
render_report() {
local input="$1"
[[ -f "$input" ]] \
|| die "report not found: $input"
case "$input" in
*.yaml|*.yml)
;;
*)
die "unsupported report format: $input (expected .yaml or .yml)"
;;
esac
local filename
local basename
local output
filename="$(basename -- "$input")"
basename="${filename%.*}"
output="${BUILD_DIR}/${basename}.pdf"
mkdir -p "$BUILD_DIR"
echo "Rendering: $input"
echo " -> $output"
(
cd "$ROOT_DIR"
# Pass the report path relative to the repository root when possible.
local report_path="$input"
if [[ "$input" == "$ROOT_DIR/"* ]]; then
report_path="${input#"$ROOT_DIR/"}"
fi
typst compile \
--input "report=${report_path}" \
"$ENTRYPOINT" \
"$output"
)
echo "Done: $output"
}
render_all() {
[[ -d "$REPORT_DIR" ]] \
|| die "report directory not found: $REPORT_DIR"
mkdir -p "$BUILD_DIR"
local found=0
while IFS= read -r -d '' report; do
found=1
render_report "$report"
done < <(
find "$REPORT_DIR" \
-type f \
\( -name '*.yaml' -o -name '*.yml' \) \
-print0 |
sort -z
)
if [[ "$found" -eq 0 ]]; then
echo "No YAML reports found in $REPORT_DIR"
return 0
fi
echo
echo "All reports rendered successfully."
}
main() {
check_dependencies
[[ $# -eq 1 ]] || {
usage
exit 1
}
case "$1" in
--all)
render_all
;;
-h|--help)
usage
;;
*)
# Accept paths relative to either:
# - the current working directory
# - the repository root
if [[ -f "$1" ]]; then
render_report "$(realpath "$1")"
elif [[ -f "${ROOT_DIR}/$1" ]]; then
render_report "$(realpath "${ROOT_DIR}/$1")"
else
die "report not found: $1"
fi
;;
esac
}
main "$@"