Repository navigation
Expand file tree
/
Copy pathbuild-docs.sh
More file actions
executable file
·80 lines (66 loc) · 2.28 KB
/
Copy pathbuild-docs.sh
File metadata and controls
executable file
·80 lines (66 loc) · 2.28 KB
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
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Usage: $0 [--offline] [--out DIR]" >&2
exit 1
}
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OUT="$ROOT/site"
OFFLINE=false
while [[ $# -gt 0 ]]; do
case "$1" in
--offline) OFFLINE=true; shift ;;
--out) [[ $# -ge 2 ]] || usage; OUT="$2"; shift 2 ;;
*) usage ;;
esac
done
CDN="https://cdn.usebruno.com/api-docs"
GIT_URL="$(git -C "$ROOT" remote get-url origin 2>/dev/null || true)"
# The Bruno CLI parses every folder and .yml file in the collection root, so
# non-collection folders would show up in the docs. Build from a clean copy,
# which also keeps private environments out of the published page.
STAGE="$(mktemp -d)"
trap 'rm -rf "$STAGE"' EXIT
rsync -a \
--include environments/Public.yml --exclude 'environments/*' \
--exclude .git --exclude .github --exclude site \
--exclude scripts --exclude branding --exclude node_modules \
"$ROOT/" "$STAGE/"
mkdir -p "$OUT"
(cd "$STAGE" && bru docs generate --envs Public -o "$OUT/index.html")
cp "$ROOT/branding/logo.png" "$ROOT/branding/theme.css" "$OUT/"
if $OFFLINE; then
curl -sfL "$CDN/api-docs.js" -o "$OUT/api-docs.js"
curl -sfL "$CDN/api-docs.css" -o "$OUT/api-docs.css"
ASSET_BASE=""
else
ASSET_BASE="$CDN/"
fi
python3 - "$OUT/index.html" "$ASSET_BASE" "$GIT_URL" <<'EOF'
import json, re, sys
path, asset_base, git_url = sys.argv[1:]
html = open(path, encoding="utf-8").read()
html = html.replace(
'<link rel="stylesheet" href="https://cdn.usebruno.com/api-docs/api-docs.css">',
f'<link rel="stylesheet" href="{asset_base}api-docs.css">\n'
' <link rel="stylesheet" href="theme.css">\n'
' <link rel="icon" href="logo.png">',
)
html = html.replace(
'<script src="https://cdn.usebruno.com/api-docs/api-docs.js">',
f'<script src="{asset_base}api-docs.js">',
)
options = 'logo: "logo.png",'
if git_url:
html = re.sub(r'\s*gitCollectionUrl:[^\n]*', '', html)
options += f'\n gitCollectionUrl: {json.dumps(git_url)},'
html, count = re.subn(
r'(opencollection: collectionData,)',
lambda m: f'{m.group(1)}\n {options}',
html,
)
if count != 1:
sys.exit("Could not inject branding: generated HTML format changed")
open(path, "w", encoding="utf-8").write(html)
EOF
echo "Documentation built at $OUT/index.html"