#!/bin/bash
# Generate HTML API documentation for the pure layers.
#
# The stable contracts live in core/, domain/ and ports/. The app/adapters
# graph pulls chronos, whose nimdoc support is broken on Nim 2.2.8
# (asyncproc/handles), so documenting it would fail on upstream code. This
# script documents the pure layers, which compile cleanly under nimdoc.
#
# Usage: ./scripts/generate_docs.sh [--serve]
#
# --serve: Start a local HTTP server to view the docs
#
# Requirements: Nim compiler with docgen support
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
OUTDIR="${PROJECT_ROOT}/htmldocs"
ENTRY="${PROJECT_ROOT}/src/donkey_api_docs.nim"
cleanup() {
rm -f "$ENTRY"
}
trap cleanup EXIT
cd "$PROJECT_ROOT"
echo "==> Generating Donkey API documentation..."
echo " Source: src/donkey/{core,domain,ports}"
echo " Output: $OUTDIR/"
# The entry point must live inside src/ so nimdoc treats the imported modules
# as part of the project (an entry outside src only documents itself).
{
echo '## API reference for the pure layers (core, domain, ports).'
echo '##'
echo '## Generated by scripts/generate_docs.sh; do not edit.'
echo
while IFS= read -r file; do
module="${file#"$PROJECT_ROOT/src/"}"
module="${module%.nim}"
alias="$(printf '%s' "$module" | tr '/.' '__')"
echo "import $module as $alias"
echo "export $alias"
done < <(find "$PROJECT_ROOT/src/donkey/core" \
"$PROJECT_ROOT/src/donkey/domain" \
"$PROJECT_ROOT/src/donkey/ports" \
-name '*.nim' | sort)
} > "$ENTRY"
## A checkout without `origin` (a tarball, a fresh clone of a fork) falls
## back to the canonical URL instead of tripping `set -e`/`pipefail`.
GIT_URL="$(git remote get-url origin 2>/dev/null | sed -E 's#^git@github\.com:#https://github.com/#; s#\.git$##')" || GIT_URL=""
GIT_COMMIT="$(git rev-parse --short HEAD 2>/dev/null || echo main)"
GIT_URL="${GIT_URL:-https://github.com/manelsen/donkey}"
rm -rf "$OUTDIR"
nim doc \
--project \
--index:on \
--outdir:"$OUTDIR" \
--git.url:"$GIT_URL" \
--git.commit:"$GIT_COMMIT" \
"$ENTRY"
if [ ! -f "$OUTDIR/donkey_api_docs.html" ] || [ ! -f "$OUTDIR/theindex.html" ]; then
echo "documentation output is incomplete" >&2
exit 1
fi
echo ""
echo "==> Documentation generated successfully!"
echo " Open: $OUTDIR/donkey_api_docs.html"
echo ""
if [ "${1:-}" = "--serve" ]; then
echo "==> Starting HTTP server on http://localhost:8080"
echo " Press Ctrl+C to stop"
python3 -m http.server 8080 --directory "$OUTDIR"
fi