103 lines
5.6 KiB
Bash
Executable File
103 lines
5.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Validate and render a Reportist report or a Typst-transformed plugin input.
|
|
set -euo pipefail
|
|
root=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
|
|
usage() { echo "Usage: $0 INPUT [--plugin DIR] [--plugin-input KEY=VALUE] [--output PDF] [--emit-ir JSON] [--typst COMMAND] [--force]" >&2; exit 2; }
|
|
fail() { echo "error: $*" >&2; exit 1; }
|
|
input=${1:-}; [[ -n "$input" ]] || usage; shift
|
|
output= ir_output= typst_command=typst force=false plugin= plugin_inputs=()
|
|
while (($#)); do
|
|
case $1 in
|
|
--output|--emit-ir|--typst|--plugin) (($# >= 2)) || usage
|
|
case $1 in --output) output=$2;; --emit-ir) ir_output=$2;; --typst) typst_command=$2;; --plugin) plugin=$2;; esac; shift 2;;
|
|
--plugin-input) (($# >= 2)) || usage
|
|
[[ $2 =~ ^[a-z][a-z0-9-]*= ]] || fail "plugin input must be KEY=VALUE with a lowercase key"
|
|
plugin_inputs+=(--input "plugin-$2"); shift 2;;
|
|
--force) force=true; shift;;
|
|
*) usage;;
|
|
esac
|
|
done
|
|
case $input in *.json) format=json;; *.yaml|*.yml) format=yaml;; *) fail "expected a .yaml, .yml, or .json input";; esac
|
|
[[ -z "$plugin" || "$format" == json ]] || fail "plugin input must be JSON"
|
|
[[ -n "$plugin" || ${#plugin_inputs[@]} -eq 0 ]] || fail "--plugin-input requires --plugin"
|
|
command -v "$typst_command" >/dev/null 2>&1 || fail "Typst executable not found: $typst_command"
|
|
input=$(realpath "$input"); [[ -f "$input" ]] || fail "$input: input does not exist"
|
|
if [[ -z "$output" ]]; then output=${input%.*}.pdf; fi
|
|
output_parent=$(dirname "$output"); mkdir -p "$output_parent"; output=$(cd "$output_parent" && pwd -P)/$(basename "$output")
|
|
[[ "$input" != "$output" ]] || fail "input and output paths must differ"
|
|
[[ ! -e "$output" || "$force" == true ]] || fail "$output: output exists; use --force to replace it"
|
|
if [[ -n "$ir_output" ]]; then
|
|
ir_parent=$(dirname "$ir_output"); mkdir -p "$ir_parent"; ir_output=$(cd "$ir_parent" && pwd -P)/$(basename "$ir_output")
|
|
[[ "$ir_output" != "$input" && "$ir_output" != "$output" ]] || fail "IR output must differ from input and PDF"
|
|
[[ ! -e "$ir_output" || "$force" == true ]] || fail "$ir_output: output exists; use --force to replace it"
|
|
fi
|
|
stage=$(mktemp -d "${TMPDIR:-/tmp}/reportist.XXXXXXXX")
|
|
trap 'rm -rf "$stage"' EXIT
|
|
cp -- "$root/templates/report.typ" "$stage/report.typ"
|
|
cp -- "$root/tools/report-value.typ" "$stage/report-value.typ"
|
|
if [[ -n "$plugin" ]]; then
|
|
plugin=$(realpath "$plugin")
|
|
[[ -f "$plugin/plugin.json" && -f "$plugin/transform.typ" ]] || fail "invalid plugin directory: $plugin"
|
|
mkdir -p "$stage/plugin"
|
|
cp -- "$plugin/transform.typ" "$stage/plugin/transform.typ"
|
|
cp -- "$plugin/plugin.json" "$stage/plugin/plugin.json"
|
|
cp -- "$root/tools/plugin-manifest.typ" "$stage/plugin-manifest.typ"
|
|
"$typst_command" eval --root "$stage" --in "$stage/plugin-manifest.typ" 'query(<valid-plugin>).first().value' >/dev/null || fail "invalid plugin manifest"
|
|
if [[ -d "$plugin/runtime" ]]; then
|
|
[[ -z $(find "$plugin/runtime" -type l -print -quit) ]] || fail "plugin runtime contains a symlink"
|
|
cp -R -- "$plugin/runtime" "$stage/plugin/runtime"
|
|
fi
|
|
cp -- "$root/tools/resolve-plugin.typ" "$stage/resolve-plugin.typ"
|
|
cp -- "$input" "$stage/source.json"
|
|
"$typst_command" eval --root "$stage" --input source=source.json "${plugin_inputs[@]}" \
|
|
--in "$stage/resolve-plugin.typ" 'query(<report-ir>).first().value' >"$stage/report.json" || fail "plugin transform failed"
|
|
"$root/tools/validate.sh" "$stage/report.json" >/dev/null || fail "plugin returned an invalid report"
|
|
format=json
|
|
else
|
|
"$root/tools/validate.sh" "$input" >/dev/null || fail "invalid report"
|
|
cp -- "$input" "$stage/report.$format"
|
|
fi
|
|
logo_path=
|
|
logo=$(
|
|
"$typst_command" eval --root "$stage" --input "report=report.$format" --input "format=$format" \
|
|
--in "$stage/report-value.typ" 'query(<report-logo>).first().value' 2>/dev/null
|
|
) || fail "unable to read report input"
|
|
if [[ "$logo" != null ]]; then
|
|
logo=${logo#\"}; logo=${logo%\"}
|
|
logo_path=$logo
|
|
[[ -n "$logo" && "$logo" != /* && "$logo" != *\\* && "$logo" != *$'\n'* ]] || fail "/branding/logo: invalid relative asset path"
|
|
IFS=/ read -r -a parts <<<"$logo"
|
|
source_dir=$(dirname "$input"); cursor=$source_dir; stage_cursor=$stage
|
|
for part in "${parts[@]}"; do
|
|
[[ -n "$part" && "$part" != . && "$part" != .. ]] || fail "/branding/logo: invalid relative asset path"
|
|
cursor=$cursor/$part; stage_cursor=$stage_cursor/$part
|
|
[[ ! -L "$cursor" && ! -L "$stage_cursor" ]] || fail "/branding/logo: symlinks are not accepted: $logo"
|
|
done
|
|
if [[ ! -f "$stage_cursor" ]]; then
|
|
[[ -f "$cursor" ]] || fail "/branding/logo: asset does not exist: $logo"
|
|
mkdir -p "$stage/$(dirname "$logo")"; cp -- "$cursor" "$stage/$logo"
|
|
fi
|
|
fi
|
|
if ! "$typst_command" compile --root "$stage" --input "report=report.$format" --input "format=$format" \
|
|
"$stage/report.typ" "$stage/report.pdf"; then fail "invalid report or Typst rendering failed"; fi
|
|
if [[ -n "$ir_output" ]]; then
|
|
if [[ -n "$logo_path" ]]; then
|
|
ir_asset="$ir_parent/$logo_path"
|
|
if [[ -e "$ir_asset" ]]; then
|
|
if ! cmp -s -- "$stage/$logo_path" "$ir_asset"; then
|
|
[[ "$force" == true ]] || fail "$ir_asset: IR asset differs; use --force to replace it"
|
|
cp -- "$stage/$logo_path" "$ir_asset"
|
|
fi
|
|
else
|
|
mkdir -p "$(dirname "$ir_asset")"
|
|
cp -- "$stage/$logo_path" "$ir_asset"
|
|
fi
|
|
fi
|
|
if [[ "$format" == json ]]; then cp -- "$stage/report.json" "$ir_output"; else
|
|
"$typst_command" eval --root "$stage" --input "report=report.$format" --input "format=$format" \
|
|
--in "$stage/report-value.typ" 'query(<report-data>).first().value' >"$ir_output"
|
|
fi
|
|
fi
|
|
mv -f -- "$stage/report.pdf" "$output"
|
|
echo "Rendered PDF: $output"
|