forked from UAVLab-SLU/DRV_public
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdev.ps1
More file actions
325 lines (294 loc) · 12.2 KB
/
Copy pathdev.ps1
File metadata and controls
325 lines (294 loc) · 12.2 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
param(
[Parameter(Position = 0)]
[string]$Command
)
Write-Host "If you get an execution policy error, run this once:"
Write-Host ""
Write-Host "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"
Write-Host ""
# DroneWorld Development Helper Script (PowerShell)
# Usage: .\dev.ps1 [command]
function Test-Token {
# Check if token is in environment
if (-not [string]::IsNullOrWhiteSpace($env:GITHUB_TOKEN)) {
return $true
}
# Check if token is in .env file and auto-export it
if (Test-Path ".env") {
$envContent = Get-Content ".env"
$tokenLine = $envContent | Where-Object { $_ -match "^GITHUB_TOKEN=" }
if ($tokenLine) {
$token = $tokenLine -replace "^GITHUB_TOKEN=", ""
if (-not [string]::IsNullOrWhiteSpace($token)) {
$env:GITHUB_TOKEN = $token
Write-Host " Loaded GITHUB_TOKEN from .env" -ForegroundColor Green
return $true
}
}
}
Write-Host " GITHUB_TOKEN not found." -ForegroundColor Yellow
Write-Host "Run '.\dev.ps1 token' to set it up." -ForegroundColor Yellow
return $false
}
function Set-Token {
Write-Host " Setting up GITHUB_TOKEN..." -ForegroundColor Green
Write-Host ""
# Check if token already exists in .env
if (Test-Path ".env") {
$envContent = Get-Content ".env"
$tokenLine = $envContent | Where-Object { $_ -match "^GITHUB_TOKEN=" }
if ($tokenLine) {
$currentToken = $tokenLine -replace "^GITHUB_TOKEN=", ""
if (-not [string]::IsNullOrWhiteSpace($currentToken)) {
$preview = $currentToken.Substring(0, [Math]::Min(10, $currentToken.Length))
Write-Host " Found existing token in .env: $preview..." -ForegroundColor Green
$response = Read-Host "Use existing token? (Y/n)"
if ([string]::IsNullOrWhiteSpace($response) -or $response -match "^[Yy]$") {
$env:GITHUB_TOKEN = $currentToken
Write-Host " Token exported for current session" -ForegroundColor Green
return
}
}
}
}
# Prompt for new token
$secureToken = Read-Host "Enter your GitHub Personal Access Token" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureToken)
$token = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR)
if ([string]::IsNullOrWhiteSpace($token)) {
Write-Host " No token provided" -ForegroundColor Red
return
}
# Set environment variable for current session
$env:GITHUB_TOKEN = $token
# Save to .env file in root
$envFile = ".env"
$tokenLine = "GITHUB_TOKEN=$token"
if (Test-Path $envFile) {
$content = Get-Content $envFile
$found = $false
$newContent = $content | ForEach-Object {
if ($_ -match "^GITHUB_TOKEN=") {
$found = $true
$tokenLine
}
else {
$_
}
}
if ($found) {
$newContent | Set-Content $envFile
Write-Host " Updated GITHUB_TOKEN in .env" -ForegroundColor Green
}
else {
Add-Content $envFile "`n$tokenLine"
Write-Host " Added GITHUB_TOKEN to .env" -ForegroundColor Green
}
}
else {
$tokenLine | Set-Content $envFile
Write-Host " Created .env with GITHUB_TOKEN" -ForegroundColor Green
}
Write-Host " Token exported for current session" -ForegroundColor Green
}
function Get-FrontendEnvValue {
param(
[Parameter(Mandatory = $true)]
[string]$Name
)
if (-not (Test-Path "frontend/.env")) {
return $null
}
$line = Get-Content "frontend/.env" | Where-Object { $_ -match "^\s*$Name\s*=" } | Select-Object -Last 1
if (-not $line) {
return $null
}
$value = ($line -replace "^\s*$Name\s*=\s*", "").Trim()
if ($value.Length -ge 2) {
if (($value.StartsWith('"') -and $value.EndsWith('"')) -or ($value.StartsWith("'") -and $value.EndsWith("'"))) {
$value = $value.Substring(1, $value.Length - 2)
}
}
return $value
}
function Test-FrontendLockAutoSyncEnabled {
$value = $null
if (-not [string]::IsNullOrWhiteSpace($env:AUTO_SYNC_FRONTEND_LOCKFILE)) {
$value = $env:AUTO_SYNC_FRONTEND_LOCKFILE
}
else {
$value = Get-FrontendEnvValue -Name "AUTO_SYNC_FRONTEND_LOCKFILE"
}
if ([string]::IsNullOrWhiteSpace($value)) {
return $true
}
switch ($value.Trim().ToLowerInvariant()) {
"0" { return $false }
"false" { return $false }
"no" { return $false }
"off" { return $false }
default { return $true }
}
}
function Sync-FrontendLockfileIfNeeded {
if (-not (Test-FrontendLockAutoSyncEnabled)) {
Write-Host " Frontend lockfile auto-sync disabled (AUTO_SYNC_FRONTEND_LOCKFILE=false)." -ForegroundColor Yellow
return
}
if (-not (Test-Path "frontend/package.json")) {
return
}
$frontendPath = (Resolve-Path ".\frontend").Path
Write-Host " Checking frontend lockfile consistency (node:20/npm)..." -ForegroundColor Cyan
& docker run --rm -v "${frontendPath}:/app" -w /app node:20 npm ci --dry-run --ignore-scripts --no-audit --no-fund *> $null
if ($LASTEXITCODE -eq 0) {
Write-Host " Frontend lockfile is in sync." -ForegroundColor Green
return
}
Write-Host " Frontend lockfile out of sync. Regenerating package-lock.json..." -ForegroundColor Yellow
& docker run --rm -v "${frontendPath}:/app" -w /app node:20 npm install --package-lock-only --no-audit --no-fund
if ($LASTEXITCODE -ne 0) {
Write-Host " Failed to regenerate frontend/package-lock.json." -ForegroundColor Red
exit 1
}
Write-Host " Updated frontend/package-lock.json using node:20/npm." -ForegroundColor Green
}
function Show-Usage {
Write-Host ""
Write-Host "Usage: .\dev.ps1 [command]" -ForegroundColor Cyan
Write-Host ""
Write-Host "Commands:" -ForegroundColor Yellow
Write-Host " token - Set GITHUB_TOKEN for building simulator (required for 'full' and 'simulator')"
Write-Host " full - Start all services (frontend, backend, simulator)"
Write-Host " full-rebuild - Rebuild and start full stack (frontend, backend, simulator)"
Write-Host " dev - Start development services only (frontend, backend)"
Write-Host " dev-rebuild - Rebuild and start development services (frontend, backend)"
Write-Host " dev-rebuild-frontend - Rebuild frontend image, then start frontend in dev compose"
Write-Host " dev-rebuild-backend - Rebuild backend image, then start backend in dev compose"
Write-Host " frontend - Start frontend only"
Write-Host " backend - Start backend only"
Write-Host " simulator - Start simulator only"
Write-Host " logs - Follow logs for dev services"
Write-Host " logs-all - Follow logs for all services"
Write-Host " stop - Stop all services"
Write-Host " stop-dev - Stop development services only"
Write-Host " clean - Stop and remove all containers and volumes"
Write-Host " help - Show this help message"
Write-Host ""
Write-Host "Examples:" -ForegroundColor Cyan
Write-Host " .\dev.ps1 token # Set GitHub token (needed before 'full' or 'simulator')"
Write-Host " .\dev.ps1 dev # Quick start for development"
Write-Host " .\dev.ps1 dev-rebuild # Rebuild frontend/backend images, then start dev services"
Write-Host " .\dev.ps1 dev-rebuild-frontend # Rebuild only frontend image for faster UI iteration"
Write-Host " .\dev.ps1 dev-rebuild-backend # Rebuild only backend image for faster API iteration"
Write-Host " .\dev.ps1 full # Start everything including simulator"
Write-Host " .\dev.ps1 full-rebuild # Rebuild all images, then start full stack"
Write-Host " frontend/.env: AUTO_SYNC_FRONTEND_LOCKFILE=false # Opt out of auto lockfile sync"
Write-Host ""
Write-Host "If you get an execution policy error, run this once:" -ForegroundColor Red
Write-Host ""
Write-Host "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"
Write-Host ""
}
switch ($Command) {
"token" {
Set-Token
}
"full" {
if (-not (Test-Token)) {
Write-Host ""
$response = Read-Host "Continue without token? The simulator will fail to build. (y/N)"
if ($response -notmatch "^[Yy]$") {
exit 1
}
}
Write-Host " Starting full stack (frontend + backend + simulator)..." -ForegroundColor Green
docker-compose up
}
"full-rebuild" {
if (-not (Test-Token)) {
Write-Host ""
$response = Read-Host "Continue without token? The simulator will fail to build. (y/N)"
if ($response -notmatch "^[Yy]$") {
exit 1
}
}
Write-Host " Rebuilding and starting full stack (frontend + backend + simulator)..." -ForegroundColor Green
Sync-FrontendLockfileIfNeeded
docker-compose down
docker-compose build
docker-compose up
}
"dev" {
Write-Host " Starting development services (frontend + backend only)..." -ForegroundColor Green
docker-compose -f docker-compose.dev.yaml up
}
"dev-rebuild" {
Write-Host " Rebuilding and starting development services (frontend + backend only)..." -ForegroundColor Green
Sync-FrontendLockfileIfNeeded
docker-compose -f docker-compose.dev.yaml down
docker-compose -f docker-compose.dev.yaml build frontend backend
docker-compose -f docker-compose.dev.yaml up --renew-anon-volumes
}
"dev-rebuild-frontend" {
Write-Host " Rebuilding frontend image and starting frontend in development compose..." -ForegroundColor Green
Sync-FrontendLockfileIfNeeded
docker-compose -f docker-compose.dev.yaml build frontend
docker-compose -f docker-compose.dev.yaml up --force-recreate --renew-anon-volumes frontend
}
"dev-rebuild-backend" {
Write-Host " Rebuilding backend image and starting backend in development compose..." -ForegroundColor Green
docker-compose -f docker-compose.dev.yaml build backend
docker-compose -f docker-compose.dev.yaml up backend
}
"frontend" {
Write-Host " Starting frontend only..." -ForegroundColor Green
docker-compose up frontend
}
"backend" {
Write-Host " Starting backend only..." -ForegroundColor Green
docker-compose up backend
}
"simulator" {
if (-not (Test-Token)) {
Write-Host ""
$response = Read-Host "Continue without token? The simulator will fail to build. (y/N)"
if ($response -notmatch "^[Yy]$") {
exit 1
}
}
Write-Host " Starting simulator only..." -ForegroundColor Green
docker-compose up drv-unreal
}
"logs" {
Write-Host " Following development service logs..." -ForegroundColor Green
docker-compose -f docker-compose.dev.yaml logs -f frontend backend
}
"logs-all" {
Write-Host " Following all service logs..." -ForegroundColor Green
docker-compose logs -f
}
"stop" {
Write-Host " Stopping all services..." -ForegroundColor Yellow
docker-compose down
}
"stop-dev" {
Write-Host " Stopping development services..." -ForegroundColor Yellow
docker-compose -f docker-compose.dev.yaml down
}
"clean" {
Write-Host " Cleaning up all containers and volumes..." -ForegroundColor Yellow
docker-compose down -v
docker-compose -f docker-compose.dev.yaml down -v
Write-Host " Cleanup complete" -ForegroundColor Green
}
{ $_ -eq "help" -or $_ -eq "" -or $null -eq $_ } {
Show-Usage
}
default {
Write-Host " Unknown command: $Command" -ForegroundColor Red
Show-Usage
exit 1
}
}