-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.bat
More file actions
391 lines (337 loc) · 11.6 KB
/
Copy pathbuild.bat
File metadata and controls
391 lines (337 loc) · 11.6 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
@echo off
setlocal enabledelayedexpansion
:: ZimaOS-Blue Development Script
:: Usage: build.bat [command]
:: Commands: start (default), server, web, build, clean, prd
:: Optional signing env vars:
:: WINDOWS_CERTIFICATE_THUMBPRINT=certificate SHA1 thumbprint (required to sign)
:: WINDOWS_TIMESTAMP_URL=RFC3161 timestamp URL (default: http://timestamp.digicert.com)
:: SIGNTOOL_PATH=full path to signtool.exe (optional)
:: WINDOWS_SIGN_CSP=SafeNet CSP/KSP provider name (optional)
:: WINDOWS_SIGN_KC=SafeNet key container name (optional)
set "PROJECT_ROOT=%~dp0"
set "COMMAND=%~1"
:: Enable CGO for Windows native TTS/ASR (SAPI) and use Zig's GNU toolchain.
set "CGO_ENABLED=1"
set "CC=zig cc -target x86_64-windows-gnu"
set "CXX=zig c++ -target x86_64-windows-gnu"
set "CGO_LDFLAGS=-static-libgcc -static-libstdc++"
set "CGO_CFLAGS=-O2"
set "CGO_CXXFLAGS=-O2"
set "GO_BUILD_FLAGS=-trimpath -buildvcs=false"
if "%COMMAND%"=="" set "COMMAND=prd"
:: Subroutines must be defined before goto (Windows batch quirk)
goto :run
:check_prereqs
echo [INFO] Checking prerequisites...
where go >nul 2>&1
if errorlevel 1 (
echo [ERROR] Go is not installed. Download from https://golang.org/dl/
exit /b 1
)
where node >nul 2>&1
if errorlevel 1 (
echo [ERROR] Node.js is not installed. Download from https://nodejs.org/
exit /b 1
)
where npm >nul 2>&1
if errorlevel 1 (
echo [ERROR] npm is not installed.
exit /b 1
)
echo [OK] All prerequisites found
exit /b 0
:check_zig
where zig >nul 2>&1
if errorlevel 1 (
echo [ERROR] Zig is not installed or not on PATH. Install Zig to build the Windows Go binaries.
exit /b 1
)
exit /b 0
:install_deps
echo [INFO] Installing dependencies...
cd /d "%PROJECT_ROOT%server"
go mod download
if errorlevel 1 (
echo [ERROR] Failed to install Go dependencies
exit /b 1
)
cd /d "%PROJECT_ROOT%web"
if not exist "node_modules" (
echo [INFO] Installing Node dependencies...
call npm install
if errorlevel 1 (
echo [ERROR] Failed to install Node dependencies
exit /b 1
)
) else (
echo [INFO] Node modules already installed, skipping...
)
echo [OK] Dependencies installed
exit /b 0
:build_third_party
echo [INFO] Skipping third_party native libraries (Windows uses native TTS/ASR only)...
:: Windows uses native SAPI for TTS/ASR, no need to build:
:: - espeak-ng (replaced by Windows native TTS)
:: - whisper.cpp (replaced by Windows native ASR)
:: - opus (not needed for Windows native)
echo [OK] Third_party libraries not needed on Windows
exit /b 0
:run_web_audit
if exist "%PROJECT_ROOT%web\scripts\audit-ci.mjs" (
if exist "%PROJECT_ROOT%web\audit-allowlist.json" (
call node scripts\audit-ci.mjs --omit=dev
) else (
call node scripts\audit-ci.mjs --no-allowlist --omit=dev
)
) else (
call npm audit --omit=dev
)
exit /b %errorlevel%
:handle_web_audit_failure
set "AUDIT_STATUS=%~1"
if "%AUDIT_STATUS%"=="1" (
echo [ERROR] Production dependencies have vulnerabilities. Please fix them before building.
) else (
if "%AUDIT_STATUS%"=="2" (
echo [ERROR] Unable to complete npm audit because the npm registry request failed. Check network access and retry.
) else (
echo [ERROR] npm audit failed unexpectedly ^(exit code: %AUDIT_STATUS%^).
)
)
exit /b 0
:pack_dist_into_binary
set "BIN_PATH=%~1"
set "DIST_DIR=%~2"
set "ARCHIVE_PATH=%TEMP%\zimaos-dist.tar.gz"
if "%BIN_PATH%"=="" (
echo [ERROR] Missing binary path for pack step.
exit /b 1
)
if "%DIST_DIR%"=="" (
echo [ERROR] Missing dist directory for pack step.
exit /b 1
)
if exist "%ARCHIVE_PATH%" del /f /q "%ARCHIVE_PATH%" >nul 2>&1
echo [INFO] Packing dist into binary...
tar czf "%ARCHIVE_PATH%" -C "%DIST_DIR%" .
if errorlevel 1 (
echo [ERROR] Failed to create dist archive.
exit /b 1
)
for %%F in ("%BIN_PATH%") do set "OFFSET=%%~zF"
if not defined OFFSET (
echo [ERROR] Failed to determine binary size before packing.
if exist "%ARCHIVE_PATH%" del /f /q "%ARCHIVE_PATH%" >nul 2>&1
exit /b 1
)
copy /b "%BIN_PATH%" + "%ARCHIVE_PATH%" "%BIN_PATH%" >nul
if errorlevel 1 (
echo [ERROR] Failed to append dist archive to binary.
if exist "%ARCHIVE_PATH%" del /f /q "%ARCHIVE_PATH%" >nul 2>&1
exit /b 1
)
powershell -NoProfile -ExecutionPolicy Bypass -Command "$offset=[Int64]$env:OFFSET; $bytes=[System.BitConverter]::GetBytes($offset); $fs=[System.IO.File]::Open($env:BIN_PATH,[System.IO.FileMode]::Append,[System.IO.FileAccess]::Write,[System.IO.FileShare]::Read); try { $fs.Write($bytes,0,$bytes.Length) } finally { $fs.Dispose() }"
if errorlevel 1 (
echo [ERROR] Failed to append binary trailer.
if exist "%ARCHIVE_PATH%" del /f /q "%ARCHIVE_PATH%" >nul 2>&1
exit /b 1
)
del /f /q "%ARCHIVE_PATH%" >nul 2>&1
if exist "%ARCHIVE_PATH%" (
echo [ERROR] Failed to remove temporary dist archive: %ARCHIVE_PATH%
exit /b 1
)
set "OFFSET="
set "ARCHIVE_PATH="
set "DIST_DIR="
set "BIN_PATH="
exit /b 0
:: Windows signing is handled by scripts\sign-windows.cmd
:run
goto :%COMMAND% 2>nul || (
echo Unknown command: %COMMAND%
echo Usage: build.bat [start^|server^|web^|build^|clean^|prd]
exit /b 1
)
:start
echo.
echo ========================================
echo ZimaOS-Blue Development Environment
echo ========================================
echo.
echo Backend: http://localhost
echo Frontend: http://localhost:3000 (background)
echo.
echo Press Ctrl+C to stop backend server
echo.
:: Check prerequisites
call :check_prereqs
if errorlevel 1 exit /b 1
call :check_zig
if errorlevel 1 exit /b 1
:: Install dependencies
call :install_deps
if errorlevel 1 exit /b 1
:: Start Vite dev server in background FIRST (Go server proxies to it)
echo [INFO] Starting Vite dev server in background...
start "ZimaOS-Blue Web" /min cmd /c "cd /d "%PROJECT_ROOT%web" && npm run dev"
:: Wait for Vite to start
timeout /t 3 /nobreak >nul
:: Start Go server in foreground (dev mode - proxies to Vite)
echo [INFO] Starting Go server (dev mode)...
cd /d "%PROJECT_ROOT%server"
go run -tags dev ./cmd/blue
goto :eof
:server
call :check_prereqs
if errorlevel 1 exit /b 1
call :check_zig
if errorlevel 1 exit /b 1
echo [INFO] Starting Go server...
cd /d "%PROJECT_ROOT%server"
go run -tags dev ./cmd/blue
goto :eof
:web
call :check_prereqs
if errorlevel 1 exit /b 1
echo [INFO] Starting web dev server...
cd /d "%PROJECT_ROOT%web"
echo [INFO] Installing Node dependencies...
call npm install
call npm run dev
goto :eof
:build
call :check_prereqs
if errorlevel 1 exit /b 1
call :check_zig
if errorlevel 1 exit /b 1
echo [INFO] Building for production...
:: Check production dependencies for vulnerabilities
echo [INFO] Checking production dependencies for vulnerabilities...
cd /d "%PROJECT_ROOT%web"
call :run_web_audit
set "AUDIT_STATUS=%errorlevel%"
if not "%AUDIT_STATUS%"=="0" (
call :handle_web_audit_failure %AUDIT_STATUS%
exit /b 1
)
echo.
:: Build third_party native libraries
call :build_third_party
if errorlevel 1 exit /b 1
:: Build web FIRST (Go embeds frontend at compile time)
echo [INFO] Building web frontend...
cd /d "%PROJECT_ROOT%web"
if not exist "node_modules" call npm install
call npm run build
if errorlevel 1 (
echo [ERROR] Failed to build web
exit /b 1
)
echo [OK] Web built: web\dist\
:: Copy web\dist to server\internal\web\dist (required for go:embed)
echo [INFO] Copying web build to server\internal\web\dist...
if exist "%PROJECT_ROOT%server\internal\web\dist" rmdir /s /q "%PROJECT_ROOT%server\internal\web\dist"
xcopy /E /I /Y "%PROJECT_ROOT%web\dist" "%PROJECT_ROOT%server\internal\web\dist" >nul
if errorlevel 1 (
echo [ERROR] Failed to copy web dist
exit /b 1
)
echo [OK] Web assets copied to server\internal\web\dist
:: Copy canonical skills from assets\skills to server\internal\skill\embedded\skills (required for go:embed)
echo [INFO] Copying skills to server\internal\skill\embedded\skills...
if exist "%PROJECT_ROOT%server\internal\skill\embedded\skills" rmdir /s /q "%PROJECT_ROOT%server\internal\skill\embedded\skills"
xcopy /E /I /Y "%PROJECT_ROOT%assets\skills" "%PROJECT_ROOT%server\internal\skill\embedded\skills" >nul
if errorlevel 1 (
echo [ERROR] Failed to copy skills
exit /b 1
)
echo [OK] Skills copied to server\internal\skill\embedded\skills
:: Build server with pack-dist (appends web assets to binary)
echo [INFO] Building Go server...
cd /d "%PROJECT_ROOT%server"
go build %GO_BUILD_FLAGS% -ldflags="-s -w" -o bin\blue.exe ./cmd/blue
if errorlevel 1 (
echo [ERROR] Failed to build server
exit /b 1
)
:: Pack dist into binary (tar.gz + 8-byte LE offset trailer)
call :pack_dist_into_binary "bin\blue.exe" "internal\web\dist"
if errorlevel 1 exit /b 1
call "%PROJECT_ROOT%scripts\sign-windows.cmd" "%PROJECT_ROOT%server\bin\blue.exe"
if errorlevel 1 exit /b 1
echo [OK] Server built: server\bin\blue.exe
echo [OK] Production build complete!
goto :eof
:prd
call :check_prereqs
if errorlevel 1 exit /b 1
call :check_zig
if errorlevel 1 exit /b 1
echo [INFO] Production run: build web, copy to server/internal/web, start server...
:: Check production dependencies for vulnerabilities
echo [INFO] Checking production dependencies for vulnerabilities...
cd /d "%PROJECT_ROOT%web"
call npm install
call :run_web_audit
set "AUDIT_STATUS=%errorlevel%"
if not "%AUDIT_STATUS%"=="0" (
call :handle_web_audit_failure %AUDIT_STATUS%
exit /b 1
)
echo.
:: Build web
echo [INFO] Building web frontend...
cd /d "%PROJECT_ROOT%web"
call npm run build
if errorlevel 1 (
echo [ERROR] Failed to build web
exit /b 1
)
echo [OK] Web built: web\dist\
:: Copy web\dist to server\internal\web\dist
echo [INFO] Copying web build to server\internal\web\dist...
if exist "%PROJECT_ROOT%server\internal\web\dist" rmdir /s /q "%PROJECT_ROOT%server\internal\web\dist"
xcopy /E /I /Y "%PROJECT_ROOT%web\dist" "%PROJECT_ROOT%server\internal\web\dist" >nul
if errorlevel 1 (
echo [ERROR] Failed to copy web dist
exit /b 1
)
echo [OK] Web assets copied to server\internal\web\dist
:: Copy canonical skills from assets\skills to server\internal\skill\embedded\skills (required for go:embed)
echo [INFO] Copying skills to server\internal\skill\embedded\skills...
if exist "%PROJECT_ROOT%server\internal\skill\embedded\skills" rmdir /s /q "%PROJECT_ROOT%server\internal\skill\embedded\skills"
xcopy /E /I /Y "%PROJECT_ROOT%assets\skills" "%PROJECT_ROOT%server\internal\skill\embedded\skills" >nul
if errorlevel 1 (
echo [ERROR] Failed to copy skills
exit /b 1
)
echo [OK] Skills copied to server\internal\skill\embedded\skills
:: Build server with pack-dist (appends web assets to binary)
echo [INFO] Building Go server (production mode)...
cd /d "%PROJECT_ROOT%server"
go build %GO_BUILD_FLAGS% -ldflags="-s -w" -o bin\blue.exe ./cmd/blue
if errorlevel 1 (
echo [ERROR] Failed to build server
exit /b 1
)
:: Pack dist into binary (tar.gz + 8-byte LE offset trailer)
call :pack_dist_into_binary "bin\blue.exe" "internal\web\dist"
if errorlevel 1 exit /b 1
call "%PROJECT_ROOT%scripts\sign-windows.cmd" "%PROJECT_ROOT%server\bin\blue.exe"
if errorlevel 1 exit /b 1
echo [OK] Server built: server\bin\blue.exe
:: Run the built binary
echo [INFO] Starting server (production mode, http://localhost)...
bin\blue.exe
goto :eof
:clean
echo [INFO] Cleaning build artifacts...
if exist "%PROJECT_ROOT%server\blue.exe" del /f "%PROJECT_ROOT%server\blue.exe"
if exist "%PROJECT_ROOT%server\data" rmdir /s /q "%PROJECT_ROOT%server\data"
if exist "%PROJECT_ROOT%web\dist" rmdir /s /q "%PROJECT_ROOT%web\dist"
if exist "%PROJECT_ROOT%server\internal\web\dist" rmdir /s /q "%PROJECT_ROOT%server\internal\web\dist"
if exist "%PROJECT_ROOT%server\internal\skill\embedded\skills" rmdir /s /q "%PROJECT_ROOT%server\internal\skill\embedded\skills"
echo [OK] Clean complete!
goto :eof