-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbuild.bat
More file actions
386 lines (334 loc) · 12.4 KB
/
build.bat
File metadata and controls
386 lines (334 loc) · 12.4 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
@echo off
setlocal enableextensions enabledelayedexpansion
:: Parse command-line arguments
set BUILD_TARGET=%1
if "%BUILD_TARGET%"=="" set BUILD_TARGET=win
echo ===================================================
echo Building Grok Desktop - Target: %BUILD_TARGET%
echo ===================================================
:: Validate build target
if /i not "%BUILD_TARGET%"=="win" if /i not "%BUILD_TARGET%"=="linux" if /i not "%BUILD_TARGET%"=="all" (
echo ERROR: Invalid build target "%BUILD_TARGET%"
echo Usage: build.bat [win^|linux^|all]
echo.
echo win - Build Windows packages only ^(default^)
echo linux - Build Linux packages ^(RPM + DEB^)
echo all - Build all platforms
goto :error
)
:: Extract version from package.json
for /f "tokens=2 delims=:, " %%a in ('findstr /C:"\"version\"" package.json') do (
set VERSION=%%a
)
set VERSION=%VERSION:"=%
echo Version: %VERSION%
echo.
:: Clean previous build files
echo Cleaning previous build files...
rem Try to stop any running instances that may lock files
echo Stopping running Grok Desktop instances ^(if any^)...
taskkill /IM "Grok Desktop.exe" /F >nul 2>&1
taskkill /IM "Grok Desktop.exe" /T /F >nul 2>&1
taskkill /IM "electron.exe" /F >nul 2>&1
taskkill /IM "electron.exe" /T /F >nul 2>&1
rem Retry removing build directory if locked
set tries=0
:retry_rmdir_build
if exist build (
rmdir build /s /q
if exist build (
set /a tries=!tries!+1
if !tries! LSS 5 (
echo Build directory is in use, retrying removal ^(!tries!/5^)...
timeout /t 2 /nobreak >nul
goto :retry_rmdir_build
) else (
echo WARNING: Could not fully remove build directory; continuing.
)
)
)
if exist node_modules rmdir node_modules /s /q
:: Create build directory if it doesn't exist
if not exist build mkdir build
:: Check if npm is installed
where npm >nul 2>nul
if %ERRORLEVEL% NEQ 0 (
echo ERROR: npm is not installed or not in PATH.
echo Please install Node.js and npm before running this script.
goto :error
)
:: Set npm configuration for better reliability
echo Setting npm configuration for better reliability...
call npm config set fetch-retry-mintimeout 20000
call npm config set fetch-retry-maxtimeout 120000
call npm config set fetch-retries 5
call npm config set registry https://registry.npmjs.org/
:: Clean npm cache to prevent integrity issues
echo Cleaning npm cache to prevent integrity checksum errors...
call npm cache clean --force
:: Install project dependencies (prefer reproducible installs)
echo Installing project dependencies...
if exist package-lock.json (
echo Using npm ci with package-lock.json...
call npm ci --no-fund --no-audit --loglevel=http
) else (
echo package-lock.json not found, falling back to npm install...
call npm install --prefer-offline --no-fund --no-audit --loglevel=http
)
if %ERRORLEVEL% NEQ 0 (
echo WARNING: Failed to install project dependencies, but continuing with build...
)
:: Build based on target selection
if /i "%BUILD_TARGET%"=="win" goto :build_windows
if /i "%BUILD_TARGET%"=="linux" goto :build_linux
if /i "%BUILD_TARGET%"=="all" goto :build_all
:build_windows
echo ===================================================
echo Building Windows targets...
echo ===================================================
echo Building: NSIS, MSI, Portable
echo Archives (ZIP, 7z) will be created after build
echo.
call npx --yes electron-builder@latest --win --x64
if %ERRORLEVEL% EQU 0 (
echo ===================================================
echo Windows build completed successfully!
echo.
echo Creating archive files from unpacked directory...
echo.
if exist "build\win-unpacked" (
echo Creating ZIP archive...
powershell -Command "Compress-Archive -Path 'build\win-unpacked\*' -DestinationPath 'build\Grok-Desktop_Windows-v%VERSION%.zip' -Force"
if %ERRORLEVEL% EQU 0 (
echo Created: Grok-Desktop_Windows-v%VERSION%.zip
) else (
echo WARNING: Failed to create ZIP archive
)
echo Creating 7z archive...
where 7z >nul 2>nul
if %ERRORLEVEL% EQU 0 (
7z a -t7z "build\Grok-Desktop_Windows-v%VERSION%.7z" "build\win-unpacked\*" -mx=9 >nul
if %ERRORLEVEL% EQU 0 (
echo Created: Grok-Desktop_Windows-v%VERSION%.7z
) else (
echo WARNING: Failed to create 7z archive
)
) else (
echo NOTE: 7-Zip not found. Skipping 7z archive.
echo Install from https://www.7-zip.org/ to enable 7z archives.
)
) else (
echo WARNING: win-unpacked directory not found
)
echo.
echo All files can be found in the build directory:
dir /b build\Grok-Desktop_Installer*.exe build\Grok-Desktop_Installer*.msi build\Grok-Desktop_Portable*.exe build\Grok-Desktop_Windows*.zip build\Grok-Desktop_Windows*.7z 2>nul
echo ===================================================
) else (
echo ===================================================
echo Windows build failed with error code %ERRORLEVEL%
echo Please check the error messages above.
echo ===================================================
goto :error
)
goto :end
:build_linux
echo ===================================================
echo Building Linux targets with Docker...
echo ===================================================
echo Building: RPM, DEB
echo Archives (tar.gz, 7z) will be created after build
echo Using Docker to ensure RPM compatibility on Windows
echo.
:: Check if Docker is installed and running
where docker >nul 2>nul
if %ERRORLEVEL% NEQ 0 (
echo ERROR: Docker is not installed or not in PATH.
echo Please install Docker Desktop for Windows from:
echo https://www.docker.com/products/docker-desktop
goto :error
)
:: Check if Docker daemon is running
docker info >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
echo ERROR: Docker daemon is not running.
echo Please start Docker Desktop and try again.
goto :error
)
echo Pulling electron-builder Docker image...
docker pull electronuserland/builder:wine
if %ERRORLEVEL% NEQ 0 (
echo ERROR: Failed to pull Docker image.
goto :error
)
echo.
echo Building Linux packages in Docker container...
echo This may take several minutes...
echo.
:: Run build in Docker with volume mounts
docker run --rm ^
-v "%CD%:/project" ^
-w /project ^
electronuserland/builder:wine ^
/bin/bash -c "npm install && npx electron-builder --linux"
if %ERRORLEVEL% EQU 0 (
echo.
echo ===================================================
echo Linux build completed successfully!
echo.
echo Creating archive files from unpacked directory...
echo.
if exist "build\linux-unpacked" (
echo Creating tar.gz archive using Docker...
docker run --rm -v "%CD%\build:/build" -w /build alpine tar -czf "Grok-Desktop_Linux-v%VERSION%.tar.gz" -C linux-unpacked . 2>nul
if %ERRORLEVEL% EQU 0 (
echo Created: Grok-Desktop_Linux-v%VERSION%.tar.gz
) else (
echo WARNING: Failed to create tar.gz archive
)
echo Creating 7z archive using Docker...
docker run --rm -v "%CD%\build:/build" -w /build alpine sh -c "apk add --no-cache p7zip >nul 2>&1 && 7z a -t7z Grok-Desktop_Linux-v%VERSION%.7z ./linux-unpacked/* -mx=9" >nul 2>&1
if %ERRORLEVEL% EQU 0 (
echo Created: Grok-Desktop_Linux-v%VERSION%.7z
) else (
echo WARNING: Failed to create 7z archive
)
) else (
echo WARNING: linux-unpacked directory not found
)
echo.
echo All files can be found in the build directory:
dir /b build\Grok-Desktop-v*.rpm build\Grok-Desktop-v*.deb build\Grok-Desktop_Linux*.tar.gz build\Grok-Desktop_Linux*.7z 2>nul
echo ===================================================
echo.
echo Cleaning up Docker image...
docker rmi electronuserland/builder:wine
if %ERRORLEVEL% EQU 0 (
echo Docker image removed successfully.
) else (
echo WARNING: Failed to remove Docker image. You can remove it manually with:
echo docker rmi electronuserland/builder:wine
)
) else (
echo.
echo ===================================================
echo Linux build failed with error code %ERRORLEVEL%
echo ===================================================
echo Cleaning up Docker image...
docker rmi electronuserland/builder:wine >nul 2>&1
goto :error
)
goto :end
:build_all
echo ===================================================
echo Building ALL platforms ^(Windows + Linux^)...
echo ===================================================
echo Building: Windows ^(NSIS, MSI, Portable^) + Linux ^(RPM, DEB^)
echo Archives ^(ZIP, 7z, tar.gz^) will be created after builds complete
echo Windows will build natively, Linux will build in Docker
echo.
:: Check if Docker is installed and running
where docker >nul 2>nul
if %ERRORLEVEL% NEQ 0 (
echo WARNING: Docker is not installed. Skipping Linux builds.
echo Building Windows only...
goto :build_windows_only_all
)
docker info >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
echo WARNING: Docker daemon is not running. Skipping Linux builds.
echo Building Windows only...
goto :build_windows_only_all
)
echo [1/3] Building Windows packages natively...
echo ===================================================
call npx --yes electron-builder@latest --win --x64
if %ERRORLEVEL% NEQ 0 (
echo WARNING: Windows build failed, but continuing with Linux...
)
echo.
echo [2/3] Pulling Docker image for Linux build...
echo ===================================================
docker pull electronuserland/builder:wine
if %ERRORLEVEL% NEQ 0 (
echo ERROR: Failed to pull Docker image.
goto :show_all_results
)
echo.
echo [3/3] Building Linux packages in Docker...
echo ===================================================
docker run --rm ^
-v "%CD%:/project" ^
-w /project ^
electronuserland/builder:wine ^
/bin/bash -c "npm install && npx electron-builder --linux"
if %ERRORLEVEL% NEQ 0 (
echo WARNING: Linux build failed.
)
:show_all_results
echo.
echo Creating archive files from unpacked directories...
echo.
if exist "build\win-unpacked" (
echo Creating Windows archives...
powershell -Command "Compress-Archive -Path 'build\win-unpacked\*' -DestinationPath 'build\Grok-Desktop_Windows-v%VERSION%.zip' -Force" 2>nul
where 7z >nul 2>nul
if %ERRORLEVEL% EQU 0 (
7z a -t7z "build\Grok-Desktop_Windows-v%VERSION%.7z" "build\win-unpacked\*" -mx=9 >nul
)
)
if exist "build\linux-unpacked" (
echo Creating Linux archives...
docker run --rm -v "%CD%\build:/build" -w /build alpine tar -czf "Grok-Desktop_Linux-v%VERSION%.tar.gz" -C linux-unpacked . 2>nul
docker run --rm -v "%CD%\build:/build" -w /build alpine sh -c "apk add --no-cache p7zip >nul 2>&1 && 7z a -t7z Grok-Desktop_Linux-v%VERSION%.7z ./linux-unpacked/* -mx=9" >nul 2>&1
)
echo.
echo ===================================================
echo Multi-platform build completed!
echo All files can be found in the build directory:
echo.
echo Windows files:
dir /b build\Grok-Desktop_Installer*.exe build\Grok-Desktop_Installer*.msi build\Grok-Desktop_Portable*.exe build\Grok-Desktop_Windows*.zip build\Grok-Desktop_Windows*.7z 2>nul
echo.
echo Linux files:
dir /b build\Grok-Desktop-v*.rpm build\Grok-Desktop-v*.deb build\Grok-Desktop_Linux*.tar.gz build\Grok-Desktop_Linux*.7z 2>nul
echo ===================================================
echo.
echo Cleaning up Docker image...
docker rmi electronuserland/builder:wine
if %ERRORLEVEL% EQU 0 (
echo Docker image removed successfully.
) else (
echo WARNING: Failed to remove Docker image.
)
goto :end
:build_windows_only_all
call npx --yes electron-builder@latest --win --x64
if %ERRORLEVEL% EQU 0 (
echo ===================================================
echo Windows build completed successfully!
echo.
echo Creating archive files...
if exist "build\win-unpacked" (
powershell -Command "Compress-Archive -Path 'build\win-unpacked\*' -DestinationPath 'build\Grok-Desktop_Windows-v%VERSION%.zip' -Force" 2>nul
where 7z >nul 2>nul
if %ERRORLEVEL% EQU 0 (
7z a -t7z "build\Grok-Desktop_Windows-v%VERSION%.7z" "build\win-unpacked\*" -mx=9 >nul
)
)
echo.
echo All files can be found in the build directory:
dir /b build\Grok-Desktop_Installer*.exe build\Grok-Desktop_Installer*.msi build\Grok-Desktop_Portable*.exe build\Grok-Desktop_Windows*.zip build\Grok-Desktop_Windows*.7z 2>nul
echo ===================================================
) else (
echo ===================================================
echo Windows build failed with error code %ERRORLEVEL%
echo ===================================================
goto :error
)
goto :end
:error
echo Build process terminated with errors.
exit /b 1
:end
pause