forked from hectorgimenez/koolo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetter_build.bat
287 lines (252 loc) · 8.23 KB
/
better_build.bat
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
@echo off
setlocal enabledelayedexpansion
:: Change to the script's directory
cd /d "%~dp0"
call :print_header "Starting Koolo Build Process"
:: Check for Go installation
call :check_go_installation
:: Main script execution
call :main
if !errorlevel! neq 0 (
call :print_error "Build process failed with error code !errorlevel!"
exit /b !errorlevel!
)
echo.
powershell -Command "Write-Host 'Press any key to exit...' -ForegroundColor Yellow"
pause > nul
exit /b 0
:check_go_installation
call :print_info "Checking if Go is installed"
where go >nul 2>&1
if %errorlevel% neq 0 (
call :print_error "Go is not installed or not in the system PATH."
call :print_info "You can download Go from https://golang.org/dl/"
call :get_user_input "Do you want to attempt automatic installation using Chocolatey? (Y/N) " install_go
if /i "!install_go!"=="Y" (
call :install_go_with_chocolatey
) else (
call :print_info "Please install Go manually and run this script again."
exit /b 1
)
) else (
for /f "tokens=3" %%v in ('go version') do set go_version=%%v
call :print_success "Go version !go_version! is installed."
)
goto :eof
:install_go_with_chocolatey
call :print_step "Attempting to install Go using Chocolatey..."
where choco >nul 2>&1
if %errorlevel% neq 0 (
call :print_error "Chocolatey is not installed. Please install Go manually."
call :print_info "You can install Chocolatey from https://chocolatey.org/install"
exit /b 1
)
powershell -Command "Start-Process powershell -Verb runAs -ArgumentList 'choco install golang -y' -Wait"
where go >nul 2>&1
if %errorlevel% neq 0 (
call :print_error "Failed to install Go. Please install it manually."
exit /b 1
) else (
call :print_success "Go has been successfully installed."
)
goto :eof
:main
:: Initial validation checks
call :validate_environment
if !errorlevel! neq 0 exit /b !errorlevel!
:: Build Koolo binary
call :print_header "Building Koolo Binary"
call :print_step "Compiling Koolo"
if "%1"=="" (set VERSION=dev) else (set VERSION=%1)
go build -trimpath -tags static --ldflags -extldflags="-static" -ldflags="-s -w -H windowsgui -X 'github.com/hectorgimenez/koolo/internal/config.Version=%VERSION%'" -o build/koolo.exe ./cmd/koolo
if !errorlevel! neq 0 (
call :print_error "Failed to build Koolo binary"
exit /b 1
)
call :print_success "Successfully built koolo.exe"
:: Handle tools folder first
call :print_header "Handling Tools"
if exist build\tools (
call :print_step "Removing existing tools folder"
rmdir /s /q build\tools
if exist build\tools (
call :print_error "Failed to delete tools folder"
call :check_folder_permissions "build\tools"
exit /b 1
)
)
call :print_step "Copying tools folder"
xcopy /q /E /I /y tools build\tools > nul
if !errorlevel! neq 0 (
call :print_error "Failed to copy tools folder"
call :check_folder_permissions "tools"
call :check_folder_permissions "build"
exit /b 1
)
call :print_success "Tools folder successfully copied"
:: Handle Settings.json
call :print_header "Handling Configuration Files"
if not exist build\config mkdir build\config
if exist build\config\Settings.json (
call :print_step "Checking Settings.json"
call :print_info "Settings.json found in %cd%\build\config"
call :get_user_input " Do you want to replace it? (Y/N) " replace_settings
if /i "!replace_settings!"=="Y" (
call :print_step "Replacing Settings.json"
copy /y config\Settings.json build\config\Settings.json > nul
if !errorlevel! equ 0 (
call :print_success "Settings.json successfully replaced"
) else (
call :print_error "Failed to copy Settings.json"
exit /b 1
)
) else (
call :print_info "Keeping existing Settings.json"
)
) else (
call :print_info "No existing Settings.json found in %cd%\build\config"
call :print_step "Copying Settings.json"
copy /y config\Settings.json build\config\Settings.json > nul
if !errorlevel! neq 0 (
call :print_error "Failed to copy Settings.json"
exit /b 1
)
call :print_success "Settings.json successfully copied"
)
:: Handle koolo.yaml
if not exist build\config\koolo.yaml (
call :print_step "Copying koolo.yaml.dist"
copy config\koolo.yaml.dist build\config\koolo.yaml > nul
if !errorlevel! neq 0 (
call :print_error "Failed to copy koolo.yaml.dist"
exit /b 1
)
call :print_success "koolo.yaml.dist successfully copied"
) else (
call :print_info "koolo.yaml already exists in build\config, skipping copy"
)
:: Copy template folder
call :print_step "Copying template folder"
if exist build\config\template rmdir /s /q build\config\template
xcopy /q /E /I /y config\template build\config\template > nul
if !errorlevel! neq 0 (
call :print_error "Failed to copy template folder"
exit /b 1
)
call :print_success "Template folder successfully copied"
:: Copy README
call :print_step "Copying README.md"
copy README.md build > nul
if !errorlevel! neq 0 (
call :print_error "Failed to copy README.md"
exit /b 1
)
call :print_success "README.md successfully copied"
call :print_header "Build Process Completed"
call :print_success "Artifacts are in the build directory"
goto :eof
:error
call :print_header "Build Process Failed"
call :print_error "Error occurred during the build process"
goto :eof
:: Function to get user input
:get_user_input
setlocal enabledelayedexpansion
call :print_prompt "%~1"
set /p "user_input="
endlocal & set "%~2=%user_input%"
goto :eof
:: Function to print a colored prompt
:print_prompt
powershell -Command "Write-Host '%~1' -ForegroundColor Yellow -NoNewline"
goto :eof
:: Function to print a header
:print_header
echo.
powershell -Command "Write-Host '=== %~1 ===' -ForegroundColor Magenta"
echo.
goto :eof
:: Function to print a step
:print_step
powershell -Command "Write-Host ' - %~1' -ForegroundColor Cyan"
goto :eof
:: Function to print a success message
:print_success
powershell -Command "Write-Host ' SUCCESS: %~1' -ForegroundColor Green"
goto :eof
:: Function to print an error message
:print_error
powershell -Command "Write-Host ' ERROR: %~1' -ForegroundColor Red"
goto :eof
:: Function to print an info message
:print_info
powershell -Command "Write-Host ' INFO: %~1' -ForegroundColor Yellow"
goto :eof
:: Function to check file permissions
:check_file_permissions
dir "%~1" >nul 2>&1
if !errorlevel! neq 0 (
call :print_error "Cannot access file: %~1"
) else (
call :print_info "File %~1 is accessible"
)
goto :eof
:: Function to check folder permissions
:check_folder_permissions
dir "%~1\*" >nul 2>&1
if !errorlevel! neq 0 (
call :print_error "Cannot access directory: %~1"
) else (
call :print_info "Directory %~1 is accessible"
)
goto :eof
:: Function to validate environment
:validate_environment
call :print_header "Validating Environment"
:: Check for required source files and folders
if not exist config (
call :print_error "Config directory is missing"
exit /b 1
)
if not exist config\koolo.yaml.dist (
call :print_error "koolo.yaml.dist is missing from config directory"
exit /b 1
)
if not exist config\Settings.json (
call :print_error "Settings.json is missing from config directory"
exit /b 1
)
if not exist tools (
call :print_error "Tools directory is missing"
exit /b 1
)
:: Check for required tools
if not exist tools\handle64.exe (
call :print_error "handle64.exe is missing from tools directory"
exit /b 1
)
if not exist tools\koolo-map.exe (
call :print_error "koolo-map.exe is missing from tools directory"
exit /b 1
)
:: Check for required build dependencies
call :print_step "Checking build dependencies"
go version >nul 2>&1
if !errorlevel! neq 0 (
call :print_error "Go is not installed or not in PATH"
exit /b 1
)
:: Verify write permissions in current directory
call :print_step "Checking write permissions"
echo. > test_write.tmp 2>nul
if !errorlevel! neq 0 (
call :print_error "No write permissions in current directory"
exit /b 1
)
del test_write.tmp >nul 2>&1
call :print_success "Environment validation completed"
goto :eof
:: Function to print a warning message
:print_warning
powershell -Command "Write-Host ' WARNING: %~1' -ForegroundColor Yellow"
goto :eof