Repository navigation
Expand file tree
/
Copy pathcapture-screenshots.sh
More file actions
executable file
·293 lines (254 loc) · 10.5 KB
/
Copy pathcapture-screenshots.sh
File metadata and controls
executable file
·293 lines (254 loc) · 10.5 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env bash
set -euo pipefail
# ============================================================================
# Documentation Screenshot Capture
#
# Boots the same stack as `execute-e2e-local.sh` (Docker + Spring server +
# client production bundle) and runs the Playwright screenshot pipeline in
# `client/screenshots/`. Every "scene" registered under
# `client/screenshots/scenes/` produces a PNG in
# `documentation/static/img/screenshots/`.
#
# Services are left running after the capture so re-runs (especially with
# --grep) don't pay the full startup cost every time. Use --stop to shut
# everything down.
#
# Usage:
# ./capture-screenshots.sh Capture every scene
# ./capture-screenshots.sh --grep <regex> Only capture scenes matching regex
# ./capture-screenshots.sh --ui Open Playwright UI mode
# ./capture-screenshots.sh --headed Show the browser while capturing
# ./capture-screenshots.sh --stop Stop the services this script started
#
# Logs:
# Server log: .capture-server.log
# Client log: .capture-client.log
# ============================================================================
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
CLIENT_DIR="$ROOT_DIR/client"
SERVER_DIR="$ROOT_DIR/server"
PID_DIR="$ROOT_DIR/.capture-pids"
OUTPUT_DIR="$ROOT_DIR/documentation/static/img/screenshots"
CONFIG_REL="screenshots/playwright.config.ts"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
NC='\033[0m'
log() { echo -e "${CYAN}[capture]${NC} $*"; }
ok() { echo -e "${GREEN}[capture]${NC} $*"; }
warn() { echo -e "${YELLOW}[capture]${NC} $*"; }
err() { echo -e "${RED}[capture]${NC} $*"; }
if ! command -v pnpm >/dev/null 2>&1; then
log "pnpm not on PATH — enabling Corepack..."
corepack enable >/dev/null 2>&1 || {
err "Failed to enable Corepack. Install pnpm manually: npm install -g pnpm"
exit 1
}
fi
# ---------------------------------------------------------------------------
# Helpers (kept in sync with execute-e2e-local.sh so the two scripts don't
# drift — if you change one, update the other).
# ---------------------------------------------------------------------------
is_port_open() {
if command -v ss >/dev/null 2>&1; then
ss -tlnp "sport = :$1" 2>/dev/null | grep -q LISTEN
else
lsof -iTCP:"$1" -sTCP:LISTEN -P -n -t >/dev/null 2>&1
fi
}
wait_for_url() {
local url="$1" label="$2" max_wait="${3:-120}"
log "Waiting for $label ..."
for i in $(seq 1 "$max_wait"); do
if curl -sf "$url" >/dev/null 2>&1; then
ok "$label is ready (${i}s)"
return 0
fi
sleep 1
done
err "$label did not start within ${max_wait}s"
return 1
}
save_pid() { mkdir -p "$PID_DIR"; echo "$2" > "$PID_DIR/$1.pid"; }
read_pid() {
local f="$PID_DIR/$1.pid"
[[ -f "$f" ]] && cat "$f" || echo ""
}
kill_pid() {
local pid
pid=$(read_pid "$1")
if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null; then
pkill -P "$pid" 2>/dev/null || true
kill "$pid" 2>/dev/null || true
wait "$pid" 2>/dev/null || true
ok "Stopped $1 (PID $pid)"
fi
rm -f "$PID_DIR/$1.pid"
}
wait_for_port_release() {
local port="$1"
for i in $(seq 1 30); do
is_port_open "$port" || return 0
sleep 1
done
err "Port $port still in use after 30s — aborting"
exit 1
}
# PIDs of the process(es) listening on $1, using whichever tool is available (mirrors
# is_port_open's detection). Empty when none can be determined.
listener_pids() {
local port="$1"
if command -v lsof >/dev/null 2>&1; then
lsof -tiTCP:"$port" -sTCP:LISTEN -P -n 2>/dev/null
elif command -v ss >/dev/null 2>&1; then
ss -tlnpH "sport = :$port" 2>/dev/null | grep -oE 'pid=[0-9]+' | cut -d= -f2 | sort -u
fi
}
# Stops a process holding $1 that we did NOT start via a pidfile (a stray `serve`, a manual
# `pnpm dev`, or a run whose pidfile was removed). Sends SIGTERM and waits briefly; returns 0
# once the port is free. Fails immediately with an actionable error if the owner can't be
# identified or won't release the port — never a blind SIGKILL on a process we don't own.
stop_port_listener() {
local port="$1" pids
pids=$(listener_pids "$port")
if [[ -z "$pids" ]]; then
err "Port $port is in use but its owner could not be identified (need lsof or ss). Free port $port and re-run."
exit 1
fi
warn "Stopping untracked process(es) holding port $port: $(echo "$pids" | tr '\n' ' ')"
# shellcheck disable=SC2086
kill $pids 2>/dev/null || true
for _ in $(seq 1 10); do
is_port_open "$port" || return 0
sleep 1
done
err "Could not free port $port (still held by PID(s): $(echo "$pids" | tr '\n' ' ')). Stop it manually and re-run."
exit 1
}
# ---------------------------------------------------------------------------
# --stop: Shut down all services and exit
# ---------------------------------------------------------------------------
stop_all() {
log "Stopping capture services..."
kill_pid "client"
kill_pid "server"
(cd "$ROOT_DIR" && docker compose stop 2>/dev/null) || true
rm -rf "$PID_DIR"
ok "All services stopped."
exit 0
}
# ---------------------------------------------------------------------------
# Parse command-line arguments
# ---------------------------------------------------------------------------
PLAYWRIGHT_ARGS=()
for arg in "$@"; do
case "$arg" in
--stop) stop_all ;;
--ui) PLAYWRIGHT_ARGS+=(--ui) ;;
--headed) PLAYWRIGHT_ARGS+=(--headed) ;;
*) PLAYWRIGHT_ARGS+=("$arg") ;;
esac
done
# ---------------------------------------------------------------------------
# 1. Docker services (PostgreSQL + Keycloak)
# ---------------------------------------------------------------------------
# Reuse existing Docker services when they're already up — a full reset here
# is wasteful for a documentation task and would trash any state a human was
# curating on the local instance. Users who want a clean slate can run
# `docker compose down -v` explicitly beforehand.
if is_port_open 5444 && is_port_open 8181; then
log "Docker services already running — reusing them."
else
log "Starting Docker services..."
(cd "$ROOT_DIR" && docker compose up -d) 2>&1 | while IFS= read -r line; do echo " $line"; done
fi
wait_for_url "http://localhost:8181/realms/thesis-management" "Keycloak" 90
# ---------------------------------------------------------------------------
# 2. Server (Spring Boot with dev profile)
# ---------------------------------------------------------------------------
# Reuse an already-running server. If nothing is listening on 8180, start one.
if is_port_open 8180; then
log "Server already running on port 8180 — reusing it."
else
log "Starting server (dev profile)..."
(cd "$SERVER_DIR" && exec ./gradlew bootRun --args='--spring.profiles.active=dev' \
> "$ROOT_DIR/.capture-server.log" 2>&1) &
save_pid "server" $!
fi
# ---------------------------------------------------------------------------
# 3. Client static bundle
# ---------------------------------------------------------------------------
# Same production-bundle strategy as e2e: it eliminates the dev overlay that
# occasionally intercepts pointer events, so screenshots stay clean.
need_client_start=1
if is_port_open 3100; then
# A client is already listening. Only reuse it when it was built with AI features enabled —
# staff-19-request-changes-ai-drafts needs the "Generate with AI" button, which the client
# renders only when AI_FEATURES_ENABLED=true. The generated runtime-env.js carries that flag,
# so probe it; if AI is not confirmed, restart the client with the required environment rather
# than capturing against an incompatible build.
if curl -sf "http://localhost:3100/runtime-env.js" 2>/dev/null | grep -q '"AI_FEATURES_ENABLED":"true"'; then
log "Client already running on port 3100 with AI features enabled — reusing it."
need_client_start=0
else
warn "Client on port 3100 was not built with AI_FEATURES_ENABLED=true — restarting it so AI screenshots render..."
kill_pid "client"
# kill_pid only stops a client THIS script started (tracked via .capture-pids/client.pid).
# If a foreign process still holds 3100, stop it too — otherwise wait_for_port_release would
# just hang for 30s before aborting.
if is_port_open 3100; then
stop_port_listener 3100
fi
wait_for_port_release 3100
fi
fi
if [[ "$need_client_start" == "1" ]]; then
# The AI feedback screenshot scenes mock the endpoints but still need the buttons rendered,
# which the client only does when AI features are enabled (matching the dev server profile).
export AI_FEATURES_ENABLED=true
log "Building client (production)..."
(cd "$CLIENT_DIR" && pnpm build > "$ROOT_DIR/.capture-client-build.log" 2>&1) || {
err "Client build failed. See $ROOT_DIR/.capture-client-build.log"
exit 1
}
log "Generating runtime-env.js..."
(cd "$CLIENT_DIR/build" && node ../public/generate-runtime-env.js)
log "Starting static client server (serve)..."
(cd "$CLIENT_DIR" && exec pnpm dlx serve@14 -s build -l 3100 -c ../serve.e2e.json --no-clipboard --no-port-switching \
> "$ROOT_DIR/.capture-client.log" 2>&1) &
save_pid "client" $!
fi
# ---------------------------------------------------------------------------
# 4. Wait for server + client
# ---------------------------------------------------------------------------
wait_for_url "http://localhost:8180/api/actuator/health" "Server" 180
wait_for_url "http://localhost:3100" "Client" 60
# ---------------------------------------------------------------------------
# 5. Playwright browsers
# ---------------------------------------------------------------------------
log "Ensuring Playwright chromium browser is installed..."
(cd "$CLIENT_DIR" && pnpm exec playwright install chromium) || {
err "Playwright browser install failed."
exit 1
}
# ---------------------------------------------------------------------------
# 6. Capture screenshots
# ---------------------------------------------------------------------------
mkdir -p "$OUTPUT_DIR"
echo ""
log "Capturing screenshots into $OUTPUT_DIR ..."
echo ""
cd "$CLIENT_DIR"
EXIT_CODE=0
pnpm exec playwright test --config "$CONFIG_REL" "${PLAYWRIGHT_ARGS[@]+"${PLAYWRIGHT_ARGS[@]}"}" || EXIT_CODE=$?
echo ""
if [[ $EXIT_CODE -eq 0 ]]; then
ok "All screenshots captured."
ok "Output: $OUTPUT_DIR"
else
err "Some scenes failed (exit code $EXIT_CODE)"
warn "View report: cd client && pnpm exec playwright show-report screenshots/screenshot-report"
fi
warn "Services are still running. Use './capture-screenshots.sh --stop' to stop them."
exit $EXIT_CODE