File size: 2,625 Bytes
74c37a6
4db1e05
 
 
 
 
 
 
74c37a6
 
 
 
 
 
4db1e05
74c37a6
 
 
 
4db1e05
 
 
 
 
 
74c37a6
 
 
4db1e05
 
74c37a6
 
4db1e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74c37a6
 
 
 
 
 
4db1e05
 
 
 
 
 
 
 
74c37a6
 
 
4db1e05
74c37a6
 
4db1e05
74c37a6
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/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