-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbuild_win.bat
More file actions
319 lines (289 loc) · 15.6 KB
/
build_win.bat
File metadata and controls
319 lines (289 loc) · 15.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
@echo off
setlocal EnableDelayedExpansion
REM ==============================================================================
REM == Batch Script to Build and Package MaClaw (GUI + TUI/CLI + DataSrv) for Windows ==
REM ==============================================================================
echo [INFO] Starting the build process...
REM -- Set Environment Variables --
set "APP_NAME=MaClaw"
set "OUTPUT_DIR=%~dp0dist"
set "NSIS_PATH=C:\Program Files (x86)\NSIS\makensis.exe"
set "GOVERSIONINFO_PATH=%USERPROFILE%\go\bin\goversioninfo.exe"
REM -- Ensure Go tools are in PATH --
set "GOPATH=%USERPROFILE%\go"
set "PATH=%GOPATH%\bin;%PATH%"
set "GOMAXPROCS=1"
REM -- Clean previous build artifacts --
echo [Step 1/13] Cleaning previous build...
if exist "%OUTPUT_DIR%" (
rmdir /s /q "%OUTPUT_DIR%" 2>nul
if exist "%OUTPUT_DIR%" (
echo [WARN] Could not fully clean %OUTPUT_DIR% - some files may be locked.
echo [WARN] Attempting to continue...
del /q "%OUTPUT_DIR%\*.exe" 2>nul
del /q "%OUTPUT_DIR%\*.zip" 2>nul
)
)
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"
REM -- Increment build number and set version (single PowerShell call) --
echo [Step 2/13] Updating version number...
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command ^
"$root = '%~dp0'; if (Test-Path ($root + 'build_number')) { $n = [int](Get-Content ($root + 'build_number')) + 1 } else { $n = 1 }; Set-Content -Path ($root + 'build_number') -Value $n -NoNewline; $cfg = Get-Content ($root + 'wails.json') -Raw | ConvertFrom-Json; $parts = $cfg.info.productVersion.Split('.'); $parts[3] = [string]$n; $ver = $parts -join '.'; Set-Content -Path ($root + 'temp_VERSION.txt') -Value $ver -NoNewline; Set-Content -Path ($root + 'temp_BUILD_NUM.txt') -Value ([string]$n) -NoNewline; Set-Content -Path ($root + 'temp_PRODUCT_NAME.txt') -Value '%APP_NAME%' -NoNewline; Set-Content -Path ($root + 'temp_COMPANY_NAME.txt') -Value $cfg.info.companyName -NoNewline; Set-Content -Path ($root + 'temp_COPYRIGHT.txt') -Value $cfg.info.copyright -NoNewline"
if !errorlevel! neq 0 (
echo [ERROR] Failed to update version info.
goto :error
)
set /p BUILD_NUM=<"%~dp0temp_BUILD_NUM.txt"
set /p VERSION=<"%~dp0temp_VERSION.txt"
set /p PRODUCT_NAME=<"%~dp0temp_PRODUCT_NAME.txt"
set /p COMPANY_NAME=<"%~dp0temp_COMPANY_NAME.txt"
set /p COPYRIGHT_TEXT=<"%~dp0temp_COPYRIGHT.txt"
del /q "%~dp0temp_BUILD_NUM.txt" "%~dp0temp_VERSION.txt" "%~dp0temp_PRODUCT_NAME.txt" "%~dp0temp_COMPANY_NAME.txt" "%~dp0temp_COPYRIGHT.txt" 2>nul
setlocal DisableDelayedExpansion
powershell -NoProfile -Command "$utf8NoBom = [System.Text.UTF8Encoding]::new($false); $content = @('!define INFO_PROJECTNAME ''%APP_NAME%''','!define PRODUCT_EXECUTABLE ''%APP_NAME%.exe''','!define INFO_PRODUCTNAME ''%PRODUCT_NAME%''','!define INFO_COMPANYNAME ''%COMPANY_NAME%''','!define INFO_COPYRIGHT ''%COPYRIGHT_TEXT%''','!define INFO_PRODUCTVERSION ''%VERSION%''','!define ARG_WAILS_AMD64_BINARY ''%OUTPUT_DIR%\%APP_NAME%_amd64.exe''','!define ARG_WAILS_ARM64_BINARY ''%OUTPUT_DIR%\%APP_NAME%_arm64.exe''') -join [Environment]::NewLine; [System.IO.File]::WriteAllText('%~dp0build\windows\installer\build_params.nsh.tmp', $content, $utf8NoBom)"
endlocal
echo [INFO] Building Version: %VERSION%
REM -- Sync version with frontend --
echo [Step 3/13] Syncing version with frontend...
powershell -NoProfile -Command "@('export const buildNumber = ''%BUILD_NUM%'';','export const appVersion = ''%VERSION%'';') | Set-Content -Path '%~dp0gui\frontend\src\version.ts' -Encoding Utf8"
REM -- Build Frontend --
echo [Step 4/13] Building frontend...
cd "%~dp0gui\frontend"
if not exist "node_modules" (
call npm.cmd install --cache ./.npm_cache
if !errorlevel! neq 0 (
echo [ERROR] npm install failed.
goto :error
)
)
if exist "dist" ( rmdir /s /q "dist" )
call npm.cmd run build
if !errorlevel! neq 0 (
echo [ERROR] Frontend build failed.
goto :error
)
cd "%~dp0"
REM -- Generate Windows Resources (icon + version info) --
echo [Step 5/13] Generating Windows resources...
del /q "%~dp0gui\resource_windows_*.syso" 2>nul
del /q "%~dp0resource_windows_*.syso" 2>nul
del /q "%~dp0tmp*.syso" 2>nul
del /q "%~dp0tmp*.json" 2>nul
del /q "%~dp0build\windows\wails.exe.manifest.tmp" 2>nul
del /q "%~dp0build\windows\versioninfo.json.tmp" 2>nul
if not exist "%GOVERSIONINFO_PATH%" (
echo [INFO] goversioninfo not found. Installing...
go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@latest
if !errorlevel! neq 0 (
echo [ERROR] Failed to install goversioninfo.
goto :error
)
)
powershell -NoProfile -Command "$cfg = Get-Content '%~dp0wails.json' -Raw | ConvertFrom-Json; $parts = '%VERSION%'.Split('.'); if ($parts.Length -ne 4) { throw 'Version must contain 4 numeric parts for Windows resources.' }; $safeName = ($cfg.name -replace '[^a-zA-Z0-9._-]',''); if (-not $safeName) { $safeName = 'MaClaw' }; $clampedBuild = [Math]::Min([int]$parts[3], 65534); $manifestVer = $parts[0]+'.'+$parts[1]+'.'+$parts[2]+'.'+$clampedBuild; $manifest = Get-Content '%~dp0build\windows\wails.exe.manifest' -Raw; $manifest = $manifest.Replace('{{.Name}}', $safeName).Replace('{{.Info.ProductVersion}}', $manifestVer); [System.IO.File]::WriteAllText('%~dp0build\windows\wails.exe.manifest.tmp', $manifest, [System.Text.UTF8Encoding]::new($false)); $versionInfo = @{ FixedFileInfo = @{ FileVersion = @{ Major = [int]$parts[0]; Minor = [int]$parts[1]; Patch = [int]$parts[2]; Build = $clampedBuild }; ProductVersion = @{ Major = [int]$parts[0]; Minor = [int]$parts[1]; Patch = [int]$parts[2]; Build = $clampedBuild } }; StringFileInfo = @{ Comments = $cfg.info.comments; CompanyName = $cfg.info.companyName; FileDescription = $cfg.info.productName; FileVersion = '%VERSION%'; InternalName = $cfg.info.productName; LegalCopyright = $cfg.info.copyright; OriginalFilename = '%APP_NAME%.exe'; ProductName = $cfg.info.productName; ProductVersion = '%VERSION%' }; VarFileInfo = @{ Translation = @{ LangID = '0409'; CharsetID = '04B0' } } } | ConvertTo-Json -Depth 6; [System.IO.File]::WriteAllText('%~dp0build\windows\versioninfo.json.tmp', $versionInfo, [System.Text.UTF8Encoding]::new($false))"
if !errorlevel! neq 0 (
echo [ERROR] Failed to prepare Windows version resource inputs.
goto :error
)
REM -- Build Go Binaries --
echo [Step 6/13] Compiling GUI binaries...
set "GOOS=windows"
set "GOARCH=amd64"
set "CGO_ENABLED=1"
set "CC=gcc"
"%GOVERSIONINFO_PATH%" -64 -icon "%~dp0build\windows\icon.ico" -manifest "%~dp0build\windows\wails.exe.manifest.tmp" -o "%~dp0gui\resource_windows_amd64.syso" "%~dp0build\windows\versioninfo.json.tmp"
if !errorlevel! neq 0 (
echo [ERROR] Failed to generate amd64 resources.
goto :error
)
call :go_build -p 1 -tags desktop,production -ldflags "-s -w -H windowsgui -X main.version=%VERSION%" -o "%OUTPUT_DIR%\%APP_NAME%_amd64.exe" ./gui/
if !errorlevel! neq 0 (
echo [ERROR] Go build for GUI amd64 failed.
goto :error
)
del "%~dp0gui\resource_windows_amd64.syso"
set "GOARCH=arm64"
set "CGO_ENABLED=0"
set "CC="
"%GOVERSIONINFO_PATH%" -64 -arm -icon "%~dp0build\windows\icon.ico" -manifest "%~dp0build\windows\wails.exe.manifest.tmp" -o "%~dp0gui\resource_windows_arm64.syso" "%~dp0build\windows\versioninfo.json.tmp"
if !errorlevel! neq 0 (
echo [ERROR] Failed to generate arm64 resources.
goto :error
)
call :go_build -p 1 -tags desktop,production -ldflags "-s -w -H windowsgui -X main.version=%VERSION%" -o "%OUTPUT_DIR%\%APP_NAME%_arm64.exe" ./gui/
if !errorlevel! neq 0 (
echo [ERROR] Go build for GUI arm64 failed.
goto :error
)
del "%~dp0gui\resource_windows_arm64.syso"
del "%~dp0build\windows\wails.exe.manifest.tmp"
del "%~dp0build\windows\versioninfo.json.tmp"
REM -- Build TUI/CLI Binaries --
echo [Step 7/13] Compiling TUI/CLI binaries...
set "CGO_ENABLED=0"
set "GOARCH=amd64"
call :go_build -p 1 -ldflags "-s -w -X main.version=%VERSION%" -o "%OUTPUT_DIR%\maclaw-tui_amd64.exe" ./tui/
if !errorlevel! neq 0 (
echo [ERROR] Go build for TUI amd64 failed.
goto :error
)
set "GOARCH=arm64"
call :go_build -p 1 -ldflags "-s -w -X main.version=%VERSION%" -o "%OUTPUT_DIR%\maclaw-tui_arm64.exe" ./tui/
if !errorlevel! neq 0 (
echo [ERROR] Go build for TUI arm64 failed.
goto :error
)
REM -- Build maclaw-tool Binary --
echo [Step 8/13] Compiling maclaw-tool binaries...
set "GOARCH=amd64"
call :go_build -p 1 -ldflags "-s -w -X main.version=%VERSION%" -o "%OUTPUT_DIR%\maclaw-tool_amd64.exe" ./cmd/maclaw-tool/
if !errorlevel! neq 0 (
echo [ERROR] Go build for maclaw-tool amd64 failed.
goto :error
)
set "GOARCH=arm64"
call :go_build -p 1 -ldflags "-s -w -X main.version=%VERSION%" -o "%OUTPUT_DIR%\maclaw-tool_arm64.exe" ./cmd/maclaw-tool/
if !errorlevel! neq 0 (
echo [ERROR] Go build for maclaw-tool arm64 failed.
goto :error
)
REM -- Build MaClaw Service Binary --
echo [Step 9/13] Compiling maclawsrv binaries...
set "GOARCH=amd64"
call :go_build -p 1 -ldflags "-s -w -X main.serviceVersion=%VERSION%" -o "%OUTPUT_DIR%\maclawsrv_amd64.exe" ./MaClawSrv/
if !errorlevel! neq 0 (
echo [ERROR] Go build for maclawsrv amd64 failed.
goto :error
)
set "GOARCH=arm64"
call :go_build -p 1 -ldflags "-s -w -X main.serviceVersion=%VERSION%" -o "%OUTPUT_DIR%\maclawsrv_arm64.exe" ./MaClawSrv/
if !errorlevel! neq 0 (
echo [ERROR] Go build for maclawsrv arm64 failed.
goto :error
)
REM -- Build MaClaw Data Service Binary --
echo [Step 10/13] Compiling maclaw-data-srv binaries...
set "GOARCH=amd64"
call :go_build_datasrv -p 1 -ldflags "-s -w -X main.serviceVersion=%VERSION%" -o "%OUTPUT_DIR%\maclaw-data-srv_amd64.exe" ./cmd/maclaw-data-srv/
if !errorlevel! neq 0 (
echo [ERROR] Go build for maclaw-data-srv amd64 failed.
goto :error
)
set "GOARCH=arm64"
call :go_build_datasrv -p 1 -ldflags "-s -w -X main.serviceVersion=%VERSION%" -o "%OUTPUT_DIR%\maclaw-data-srv_arm64.exe" ./cmd/maclaw-data-srv/
if !errorlevel! neq 0 (
echo [ERROR] Go build for maclaw-data-srv arm64 failed.
goto :error
)
REM Reset Env for NSIS
set "GOOS="
set "GOARCH="
set "CGO_ENABLED="
set "CC="
set "CXX="
REM -- Create NSIS Installer --
echo [Step 11/13] Creating NSIS installer...
if not exist "%NSIS_PATH%" goto nsis_missing
"%NSIS_PATH%" "%~dp0build\windows\installer\multiarch.nsi"
if !errorlevel! neq 0 (
echo [ERROR] NSIS installer creation failed.
goto :error
)
del /q "%~dp0build\windows\installer\build_params.nsh.tmp" 2>nul
if exist "%OUTPUT_DIR%\%APP_NAME%-Setup.exe" (
echo [SUCCESS] Windows installer created at: %OUTPUT_DIR%\%APP_NAME%-Setup.exe
)
REM -- Create standalone DataSrv NSIS installer --
echo [Step 12/13] Creating standalone maclawsrv NSIS installer...
setlocal DisableDelayedExpansion
powershell -NoProfile -Command "$utf8NoBom = [System.Text.UTF8Encoding]::new($false); $content = @('!define INFO_PRODUCTNAME ''MaClaw Service''','!define INFO_COMPANYNAME ''%COMPANY_NAME%''','!define INFO_COPYRIGHT ''%COPYRIGHT_TEXT%''','!define INFO_PRODUCTVERSION ''%VERSION%''','!define PRODUCT_EXECUTABLE ''maclawsrv.exe''','!define ARG_MACLAWSRV_AMD64_BINARY ''%OUTPUT_DIR%\maclawsrv_amd64.exe''','!define ARG_MACLAWSRV_ARM64_BINARY ''%OUTPUT_DIR%\maclawsrv_arm64.exe''') -join [Environment]::NewLine; [System.IO.File]::WriteAllText('%~dp0build\windows\installer\maclawsrv_build_params.nsh.tmp', $content, $utf8NoBom)"
endlocal
if !errorlevel! neq 0 (
echo [ERROR] Failed to prepare maclawsrv installer parameters.
goto :error
)
"%NSIS_PATH%" "%~dp0build\windows\installer\maclawsrv.nsi"
if !errorlevel! neq 0 (
echo [ERROR] maclawsrv NSIS installer creation failed.
goto :error
)
del /q "%~dp0build\windows\installer\maclawsrv_build_params.nsh.tmp" 2>nul
if exist "%OUTPUT_DIR%\maclawsrv-Setup.exe" (
echo [SUCCESS] maclawsrv Windows installer created at: %OUTPUT_DIR%\maclawsrv-Setup.exe
)
REM -- Create standalone DataSrv NSIS installer --
echo [Step 13/13] Creating standalone maclaw-data-srv NSIS installer...
setlocal DisableDelayedExpansion
powershell -NoProfile -Command "$utf8NoBom = [System.Text.UTF8Encoding]::new($false); $content = @('!define INFO_PRODUCTNAME ''MaClaw Data Service''','!define INFO_COMPANYNAME ''%COMPANY_NAME%''','!define INFO_COPYRIGHT ''%COPYRIGHT_TEXT%''','!define INFO_PRODUCTVERSION ''%VERSION%''','!define PRODUCT_EXECUTABLE ''maclaw-data-srv.exe''','!define ARG_DATASRV_AMD64_BINARY ''%OUTPUT_DIR%\maclaw-data-srv_amd64.exe''','!define ARG_DATASRV_ARM64_BINARY ''%OUTPUT_DIR%\maclaw-data-srv_arm64.exe''') -join [Environment]::NewLine; [System.IO.File]::WriteAllText('%~dp0build\windows\installer\datasrv_build_params.nsh.tmp', $content, $utf8NoBom)"
endlocal
if !errorlevel! neq 0 (
echo [ERROR] Failed to prepare DataSrv installer parameters.
goto :error
)
"%NSIS_PATH%" "%~dp0build\windows\installer\datasrv.nsi"
if !errorlevel! neq 0 (
echo [ERROR] DataSrv NSIS installer creation failed.
goto :error
)
del /q "%~dp0build\windows\installer\datasrv_build_params.nsh.tmp" 2>nul
if exist "%OUTPUT_DIR%\maclaw-data-srv-Setup.exe" (
echo [SUCCESS] DataSrv Windows installer created at: %OUTPUT_DIR%\maclaw-data-srv-Setup.exe
)
REM -- Copy/Rename Main Binaries for convenience --
echo - Creating main executable copies (amd64)...
copy /Y "%OUTPUT_DIR%\%APP_NAME%_amd64.exe" "%OUTPUT_DIR%\%APP_NAME%.exe" >nul
copy /Y "%OUTPUT_DIR%\maclaw-tui_amd64.exe" "%OUTPUT_DIR%\maclaw-tui.exe" >nul
copy /Y "%OUTPUT_DIR%\maclaw-tool_amd64.exe" "%OUTPUT_DIR%\maclaw-tool.exe" >nul
copy /Y "%OUTPUT_DIR%\maclawsrv_amd64.exe" "%OUTPUT_DIR%\maclawsrv.exe" >nul
copy /Y "%OUTPUT_DIR%\maclaw-data-srv_amd64.exe" "%OUTPUT_DIR%\maclaw-data-srv.exe" >nul
if exist "%OUTPUT_DIR%\%APP_NAME%.exe" (
echo [SUCCESS] GUI binary: %OUTPUT_DIR%\%APP_NAME%.exe
)
if exist "%OUTPUT_DIR%\maclaw-tui.exe" (
echo [SUCCESS] TUI/CLI binary: %OUTPUT_DIR%\maclaw-tui.exe
)
if exist "%OUTPUT_DIR%\maclaw-tool.exe" (
echo [SUCCESS] maclaw-tool binary: %OUTPUT_DIR%\maclaw-tool.exe
)
if exist "%OUTPUT_DIR%\maclawsrv.exe" (
echo [SUCCESS] maclawsrv binary: %OUTPUT_DIR%\maclawsrv.exe
)
if exist "%OUTPUT_DIR%\maclaw-data-srv.exe" (
echo [SUCCESS] maclaw-data-srv binary: %OUTPUT_DIR%\maclaw-data-srv.exe
)
echo - Creating Windows portable zip...
powershell -Command "Compress-Archive -Path '%OUTPUT_DIR%\%APP_NAME%.exe','%OUTPUT_DIR%\maclaw-tui.exe','%OUTPUT_DIR%\maclaw-tool.exe','%OUTPUT_DIR%\maclawsrv.exe','%OUTPUT_DIR%\maclaw-data-srv.exe' -DestinationPath '%OUTPUT_DIR%\%APP_NAME%-Windows-Portable.zip' -Force"
goto :success
:go_build
set "GO_BUILD_ATTEMPT=1"
:go_build_retry
go build %*
if !errorlevel! equ 0 exit /b 0
set "GO_BUILD_ERROR=!errorlevel!"
if !GO_BUILD_ATTEMPT! geq 8 exit /b !GO_BUILD_ERROR!
echo [WARN] go build failed with exit code !GO_BUILD_ERROR!, retrying...
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Get-Process go,compile,link,gcc -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue"
set /a GO_BUILD_ATTEMPT+=1
goto :go_build_retry
:go_build_datasrv
set "GO_BUILD_ATTEMPT=1"
:go_build_datasrv_retry
go -C "%~dp0datasrv" build %*
if !errorlevel! equ 0 exit /b 0
set "GO_BUILD_ERROR=!errorlevel!"
if !GO_BUILD_ATTEMPT! geq 8 exit /b !GO_BUILD_ERROR!
echo [WARN] datasrv go build failed with exit code !GO_BUILD_ERROR!, retrying...
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Get-Process go,compile,link,gcc -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue"
set /a GO_BUILD_ATTEMPT+=1
goto :go_build_datasrv_retry
:nsis_missing
echo [ERROR] NSIS not found at "%NSIS_PATH%". Please install NSIS.
goto :error
:success
echo.
echo [SUCCESS] Build and packaging complete!
echo Artifacts are in: %OUTPUT_DIR%
endlocal & exit /b 0
:error
echo.
echo [FAILED] The build process failed. Please check the output above for errors.
endlocal & exit /b 1