-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptappd.sh
More file actions
executable file
·239 lines (204 loc) · 5.76 KB
/
Copy pathscriptappd.sh
File metadata and controls
executable file
·239 lines (204 loc) · 5.76 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
#! /usr/bin/env bash
in_che="0"
if [ -f /home/dev/gr2522-container ]; then
value="$(cat /home/dev/gr2522-container | tr -d '\r\n')" # strip newlines
if [ "$value" = "Running in che" ]; then
in_che="1"
fi
fi
print-help() {
echo "Usage: $0 <command>"
echo
echo "Available commands:"
echo
echo " run:app Start both frontend (JavaFX) and backend"
echo " run:web-app Start both web frontend and backend (Terminal colors don't work like this, we recommend using two seperate shell sessions with run:backend and run:web)"
echo " run:backend Start only the backend API"
echo " run:frontend Start only the frontend (JavaFX)"
echo " run:web Start only the web frontend"
echo
echo " test Run all tests. Optionally reset DB with fixtures first"
echo " test:backend Run only backend tests. Optionally reset with fixtures DB first"
echo " test:frontend Run only frontend tests"
echo
echo " migrate Apply all SQL migration files to the database"
echo " migrate-with-fixtures Apply migrations and load the latest fixture data"
echo " reset-db-with-fixtures Drop DB, recreate, and seed with fixtures"
echo " nuke-db Stop all containers and delete database volumes"
echo
echo " check-format Check code formatting using Maven Spotless"
echo " fmt Automatically fix formatting issues"
echo
echo " -h, --help Show this help message"
echo
echo "Examples:"
echo " $0 run:app"
echo " $0 migrate-with-fixtures"
echo " $0 fmt"
echo
}
if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
print-help
exit 0
fi
kill-backend() {
local pid=""
if command -v lsof &>/dev/null; then
pid=$(lsof -ti tcp:8080)
elif command -v ss &>/dev/null; then
pid=$(ss -ltnp 2>/dev/null | awk '/:8080 / {gsub("pid=", "", $NF); gsub(",.*", "", $NF); print $NF; exit}')
else
pid=$(netstat -anv -p tcp 2>/dev/null | awk '/\*.8080/ && /LISTEN/ {print $10; exit}')
fi
if [[ -n "$pid" ]]; then
echo "Killing backend process (PID $pid)..."
kill "$pid" || echo "Failed to kill process $pid"
else
echo "No backend process found on port 8080."
fi
}
nuke-db() {
if [ $in_che = 1 ]; then
psql postgres://user:user@localhost:5432/untappd -f /projects/gr2522/backend/src/main/resources/db/example-testing-queries/delete-all-tables.sql
else
docker compose down -v
fi
}
reset-db-with-fixtures() {
nuke-db
if [ $in_che = 0 ]; then
docker compose up -v
fi
migrate-with-fixtures
}
migrate() {
local extra_cmd="${1:-}"
local base_command='for f in $(ls /home/untappd/db-sql-files/migrations/*.sql); do psql postgres://user:user@localhost:5432/untappd -f "$f"; done'
local command="$base_command"
if [[ -n "$extra_cmd" ]]; then
command="$base_command && $extra_cmd"
fi
if [ $in_che = 1 ]; then
for f in $(ls /projects/gr2522/backend/src/main/resources/db/migrations/*.sql); do
psql postgres://user:user@localhost:5432/untappd -f "$f";
done
else
docker compose up -d && sleep 3
local container_name
container_name="$(basename "$(pwd)")-db-1"
echo "Running migrations inside container: $container_name"
docker exec "$container_name" /bin/sh -c "$command"
fi
}
migrate-with-fixtures() {
if [ $in_che = 0 ]; then
migrate 'psql postgres://user:user@localhost:5432/untappd -f $(ls /home/untappd/db-sql-files/fixtures/*.sql | sort -r | head -n1)'
else
migrate
psql postgres://user:user@localhost:5432/untappd -f $(ls /projects/gr2522/backend/src/main/resources/db/fixtures/*.sql | sort -r | head -n1)
fi
}
confirm_reset() {
read -rp "Wipe database and reset with fixtures before running tests? (y/N): " confirm
if [[ "$confirm" =~ ^[Yy]([Ee][Ss])?$ ]]; then
reset-db-with-fixtures
else
if [ $in_che = 0 ]; then
docker compose up -d
fi
fi
}
cmd="${1:-}"
if [[ -z "$cmd" || "$cmd" == "-h" || "$cmd" == "--help" ]]; then
print-help
exit 0
fi
case "$cmd" in
run:app)
echo "Starting frontend and backend..."
if [ $in_che = 0 ]; then
docker compose up -d && sleep 3
fi
mvn -f frontend javafx:run &
mvn -f backend spring-boot:run
;;
run:web-app)
echo "Starting backend and web frontend..."
trap 'echo "Stopping"; kill $BACK_PID $FRONT_PID 2>/dev/null; exit 0' INT
mvn -f backend spring-boot:run &
BACK_PID=$!
(
cd ./web
pnpm dev
FRONT_PID=$!
) &
wait $BACK_PID $FRONT_PID
;;
run:backend)
echo "Starting backend..."
if [ $in_che = 0 ]; then
docker compose up -d && sleep 3
fi
mvn -f backend spring-boot:run
;;
run:frontend)
echo "Starting frontend..."
mvn -f frontend javafx:run
;;
run:web)
echo "Starting web frontend..."
cd web
pnpm dev
;;
test)
confirm_reset
mvn -f backend test
echo "Starting backend for tests..."
mvn -f backend spring-boot:run >/dev/null 2>&1 &
sleep 3
mvn -f frontend test
kill-backend
;;
test:frontend)
if [ $in_che = 0 ]; then
docker compose up -d && sleep 3
fi
echo "Starting backend for tests..."
mvn -f backend spring-boot:run >/dev/null 2>&1 &
sleep 3
mvn -f frontend test
kill-backend
;;
test:backend)
confirm_reset
mvn -f backend test
;;
migrate)
migrate
;;
migrate-with-fixtures)
migrate-with-fixtures
;;
reset-db-with-fixtures)
reset-db-with-fixtures
;;
nuke-db)
nuke-db
;;
check-format)
pre-commit run --all-files
;;
fmt)
mvn spotless:apply
(
cd web
pnpm lint
)
;;
*)
echo "Unknown command: $cmd"
echo
print-help
exit 1
;;
esac