-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun_example.sh
More file actions
executable file
·139 lines (126 loc) · 3.59 KB
/
run_example.sh
File metadata and controls
executable file
·139 lines (126 loc) · 3.59 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
#!/usr/bin/env bash
# run_example.sh — set up and run the django-multiseek example projects.
#
# Subcommands:
# setup [name] migrate + fetch_assets + initial_data for one or all
# run <name> setup + start the dev server (foreground)
# test [name] setup + run the example's Playwright test suite
#
# Without arguments, prints help.
#
# Example projects live under examples/. Each ships a `fetch_assets`
# management command that downloads its JS/CSS dependencies into
# static/multiseek/vendor/ — that's why `setup` runs before `run`/`test`.
set -euo pipefail
EXAMPLES=(minimal bootstrap alpine htmx)
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
usage() {
cat <<EOF
Usage:
$(basename "$0") setup [example] prep one example (or all if omitted)
$(basename "$0") run <example> prep then start runserver on :8000
$(basename "$0") test [example] prep then run pytest (one example, or all)
Available examples: ${EXAMPLES[*]}
Each example uses its own SQLite DB and its own static/multiseek/vendor/
download cache. Re-running setup is idempotent — fetch_assets skips
files already present, migrate is no-op when up to date, initial_data
uses get_or_create.
EOF
}
# Resolve the example argument to a directory. Fails if the name isn't
# in the canonical EXAMPLES list.
resolve_example() {
local name="$1"
for ex in "${EXAMPLES[@]}"; do
if [[ "$ex" == "$name" ]]; then
printf '%s/examples/%s\n' "$SCRIPT_DIR" "$name"
return 0
fi
done
echo "ERROR: '$name' is not a known example." >&2
echo "Choose one of: ${EXAMPLES[*]}" >&2
return 1
}
setup_one() {
local name="$1"
local dir
dir="$(resolve_example "$name")"
echo "=== $name :: setup ==="
(
cd "$dir"
# alpine has its own pyproject.toml + .venv; the others use the
# repo-root venv via uv's upward-pyproject lookup.
if [[ -f pyproject.toml ]]; then
uv sync --all-extras
fi
uv run python manage.py migrate --noinput
uv run python manage.py fetch_assets
uv run python manage.py initial_data
)
echo ""
}
setup_all() {
for ex in "${EXAMPLES[@]}"; do
setup_one "$ex"
done
}
run_one() {
local name="$1"
local dir
dir="$(resolve_example "$name")"
setup_one "$name"
echo "=== $name :: serving at http://127.0.0.1:8000/multiseek/ ==="
cd "$dir"
exec uv run python manage.py runserver
}
test_one() {
local name="$1"
local dir
dir="$(resolve_example "$name")"
setup_one "$name"
echo "=== $name :: pytest (chromium) ==="
cd "$dir"
uv run pytest --browser chromium
}
test_all() {
local failed=()
for ex in "${EXAMPLES[@]}"; do
if ! test_one "$ex"; then
failed+=("$ex")
fi
done
if (( ${#failed[@]} > 0 )); then
echo ""
echo "FAILED: ${failed[*]}" >&2
return 1
fi
echo ""
echo "All ${#EXAMPLES[@]} examples passed."
}
main() {
case "${1:-}" in
""|-h|--help|help)
usage
;;
setup)
if [[ -n "${2:-}" ]]; then setup_one "$2"; else setup_all; fi
;;
run)
if [[ -z "${2:-}" ]]; then
echo "ERROR: 'run' requires an example name." >&2
usage
exit 1
fi
run_one "$2"
;;
test)
if [[ -n "${2:-}" ]]; then test_one "$2"; else test_all; fi
;;
*)
echo "ERROR: unknown subcommand '$1'." >&2
usage
exit 1
;;
esac
}
main "$@"