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-diagnostics.sh

117 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
DIAGNOSTICS_DIR="${ROOT_DIR}/diagnostics"
BUILD_DIR="${ROOT_DIR}/build/diagnostics"
usage() {
cat <<'EOF_USAGE'
Usage:
./render-diagnostics.sh REPORT.json "Author Name" [theme]
./render-diagnostics.sh --all "Author Name" [theme]
Examples:
./render-diagnostics.sh diagnostics/windows_system_report_PC.json "Jane Doe"
./render-diagnostics.sh diagnostics/windows_system_report_PC.json "Jane Doe" cic
./render-diagnostics.sh --all "Jane Doe"
./render-diagnostics.sh --all "Jane Doe" eridani
The theme is optional and defaults to "default".
Available themes: default, cic, eridani
EOF_USAGE
}
die() {
echo "error: $*" >&2
exit 1
}
render_diagnostics() {
local input="$1"
local author="$2"
local theme="$3"
[[ -f "$input" ]] || die "diagnostics JSON not found: $input"
[[ "$input" == *.json ]] || die "diagnostics input must be a .json file: $input"
local input_abs
input_abs="$(realpath "$input")"
[[ "$input_abs" == "$ROOT_DIR/"* ]] \
|| die "diagnostics JSON must be inside the repository: $input"
local input_typst="${input_abs#"$ROOT_DIR/"}"
local relative_output
if [[ "$input_abs" == "$DIAGNOSTICS_DIR/"* ]]; then
relative_output="${input_abs#"$DIAGNOSTICS_DIR/"}"
else
relative_output="$(basename -- "$input_abs")"
fi
relative_output="${relative_output%.*}.pdf"
local output="${BUILD_DIR}/${relative_output}"
mkdir -p "$(dirname -- "$output")"
echo "Rendering: $input_typst"
echo " -> ${output#"$ROOT_DIR/"}"
(
cd "$ROOT_DIR"
typst compile \
--input "diagnostics=${input_typst}" \
--input "author=${author}" \
--input "theme=${theme}" \
diagnostics.typ \
"$output"
)
}
render_all() {
local author="$1"
local theme="$2"
local found=0
[[ -d "$DIAGNOSTICS_DIR" ]] \
|| die "diagnostics directory not found: $DIAGNOSTICS_DIR"
while IFS= read -r -d '' input; do
found=1
render_diagnostics "$input" "$author" "$theme"
done < <(find "$DIAGNOSTICS_DIR" -type f -name '*.json' -print0 | sort -z)
if [[ "$found" -eq 0 ]]; then
echo "No diagnostics .json reports found in $DIAGNOSTICS_DIR"
return 0
fi
echo "All diagnostics rendered successfully."
}
main() {
command -v typst >/dev/null 2>&1 || die "typst was not found"
[[ $# -ge 2 && $# -le 3 ]] || { usage; exit 1; }
local target="$1"
local author="$2"
local theme="${3:-default}"
[[ -n "$author" ]] \
|| die "author is required; pass it as the second argument"
if [[ "$target" == "--all" ]]; then
render_all "$author" "$theme"
else
if [[ -f "$target" ]]; then
render_diagnostics "$target" "$author" "$theme"
elif [[ -f "${ROOT_DIR}/$target" ]]; then
render_diagnostics "${ROOT_DIR}/$target" "$author" "$theme"
else
die "diagnostics JSON not found: $target"
fi
fi
}
main "$@"