-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbuild.ps1
More file actions
534 lines (455 loc) · 16.3 KB
/
build.ps1
File metadata and controls
534 lines (455 loc) · 16.3 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# MediaProxy PowerShell Build Script
# Designed for Windows development environment with multi-platform compilation and size optimization
param(
[string]$Platform = "",
[switch]$All = $false,
[switch]$Clean = $false,
[switch]$Help = $false,
[switch]$NoUpx = $false,
[switch]$Dev = $false,
[switch]$Verbose = $false,
[switch]$Special = $false
)
# Set error handling
$ErrorActionPreference = "Stop"
# Project information
$AppName = "mediaProxy"
$BuildDir = "build"
$DistDir = "dist"
# Supported platforms
$Platforms = @(
"linux/amd64",
"linux/arm64",
"linux/386",
"windows/amd64",
"windows/386",
"darwin/amd64",
"darwin/arm64",
"freebsd/amd64"
)
# Color output function
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
switch ($Color) {
"Red" { Write-Host $Message -ForegroundColor Red }
"Green" { Write-Host $Message -ForegroundColor Green }
"Yellow" { Write-Host $Message -ForegroundColor Yellow }
"Blue" { Write-Host $Message -ForegroundColor Blue }
"Cyan" { Write-Host $Message -ForegroundColor Cyan }
default { Write-Host $Message -ForegroundColor White }
}
}
# Get version information
function Get-VersionInfo {
try {
$version = git describe --tags --always --dirty 2>$null
if (-not $version) { $version = "dev" }
} catch {
$version = "dev"
}
try {
$gitCommit = git rev-parse --short HEAD 2>$null
if (-not $gitCommit) { $gitCommit = "unknown" }
} catch {
$gitCommit = "unknown"
}
$buildTime = (Get-Date).ToUniversalTime().ToString("yyyy-MM-dd_HH:mm:ss_UTC")
return @{
Version = $version
GitCommit = $gitCommit
BuildTime = $buildTime
}
}
# Clean function
function Clear-BuildDirs {
Write-ColorOutput "Cleaning build directories..." "Yellow"
if (Test-Path $BuildDir) {
Remove-Item $BuildDir -Recurse -Force
}
if (Test-Path $DistDir) {
Remove-Item $DistDir -Recurse -Force
}
Write-ColorOutput "Clean completed" "Green"
}
# Create directories
function New-BuildDirs {
Write-ColorOutput "Creating build directories..." "Blue"
New-Item -ItemType Directory -Path $BuildDir -Force | Out-Null
New-Item -ItemType Directory -Path $DistDir -Force | Out-Null
}
# Check Go environment
function Test-GoEnvironment {
try {
$goVersion = go version
Write-ColorOutput "Go environment check passed" "Green"
Write-ColorOutput $goVersion "Cyan"
return $true
} catch {
Write-ColorOutput "Error: Go environment not found" "Red"
return $false
}
}
# Check UPX availability
function Test-UpxAvailable {
try {
upx --version | Out-Null
return $true
} catch {
return $false
}
}
# Build single platform
function Build-Platform {
param(
[string]$PlatformString,
[hashtable]$VersionInfo,
[bool]$UseUpx = $true
)
$parts = $PlatformString.Split('/')
$goos = $parts[0]
$goarch = $parts[1]
$outputName = $AppName
if ($goos -eq "windows") {
$outputName = "$AppName.exe"
}
$outputPath = Join-Path $BuildDir "${AppName}_${goos}_${goarch}"
if ($goos -eq "windows") {
$outputPath = "$outputPath.exe"
}
Write-ColorOutput "Building $goos/$goarch..." "Blue"
# Set environment variables
$env:GOOS = $goos
$env:GOARCH = $goarch
$env:CGO_ENABLED = "0"
# Build flags
$ldflags = "-s -w"
$ldflags += " -X main.Version=$($VersionInfo.Version)"
$ldflags += " -X main.BuildTime=$($VersionInfo.BuildTime)"
$ldflags += " -X main.GitCommit=$($VersionInfo.GitCommit)"
try {
# Execute build
if ($Verbose) {
Write-ColorOutput "Executing: go build -ldflags=`"$ldflags`" -trimpath -o `"$outputPath`" ." "Cyan"
}
go build -ldflags="$ldflags" -trimpath -o "$outputPath" .
if ($LASTEXITCODE -ne 0) {
throw "Build failed"
}
# Get file size
$fileInfo = Get-Item $outputPath
$sizeKB = [math]::Round($fileInfo.Length / 1024, 2)
Write-ColorOutput "✓ $goos/$goarch build successful (size: $sizeKB KB)" "Green"
# UPX compression
if ($UseUpx -and (Test-UpxAvailable)) {
Write-ColorOutput "Compressing with UPX..." "Yellow"
try {
upx --best --lzma "$outputPath" 2>$null
$compressedInfo = Get-Item $outputPath
$compressedSizeKB = [math]::Round($compressedInfo.Length / 1024, 2)
Write-ColorOutput "✓ Compression completed (compressed: $compressedSizeKB KB)" "Green"
} catch {
Write-ColorOutput "UPX compression failed, continuing..." "Yellow"
}
}
# Create release package
New-ReleasePackage -Goos $goos -Goarch $goarch -BinaryPath $outputPath -VersionInfo $VersionInfo
return $true
} catch {
Write-ColorOutput "✗ $goos/$goarch build failed: $($_.Exception.Message)" "Red"
return $false
} finally {
# Clean environment variables
Remove-Item env:GOOS -ErrorAction SilentlyContinue
Remove-Item env:GOARCH -ErrorAction SilentlyContinue
Remove-Item env:CGO_ENABLED -ErrorAction SilentlyContinue
}
}
# Build special platforms with custom names
function Build-SpecialPlatforms {
param(
[hashtable]$VersionInfo,
[bool]$UseUpx = $true
)
# Define special platforms with custom output names
$specialPlatforms = @(
@{ Platform = "android/arm64"; OutputName = "mediaProxy-android" },
@{ Platform = "windows/amd64"; OutputName = "mediaProxy-win.exe" },
@{ Platform = "linux/amd64"; OutputName = "mediaProxy-linux" }
)
Write-ColorOutput "Building special platforms with custom names..." "Cyan"
Write-Host ""
$successCount = 0
$totalCount = $specialPlatforms.Count
foreach ($spec in $specialPlatforms) {
$platformString = $spec.Platform
$customName = $spec.OutputName
$parts = $platformString.Split('/')
$goos = $parts[0]
$goarch = $parts[1]
# For Android, use linux as GOOS
if ($goos -eq "android") {
$goos = "linux"
}
$outputPath = Join-Path $BuildDir $customName
Write-ColorOutput "Building $($spec.Platform) -> $customName..." "Blue"
# Set environment variables
$env:GOOS = $goos
$env:GOARCH = $goarch
$env:CGO_ENABLED = "0"
# Build flags
$ldflags = "-s -w"
$ldflags += " -X main.Version=$($VersionInfo.Version)"
$ldflags += " -X main.BuildTime=$($VersionInfo.BuildTime)"
$ldflags += " -X main.GitCommit=$($VersionInfo.GitCommit)"
try {
# Execute build
if ($Verbose) {
Write-ColorOutput "Executing: go build -ldflags=`"$ldflags`" -trimpath -o `"$outputPath`" ." "Cyan"
}
go build -ldflags="$ldflags" -trimpath -o "$outputPath" .
if ($LASTEXITCODE -ne 0) {
throw "Build failed"
}
# Get file size
$fileInfo = Get-Item $outputPath
$sizeKB = [math]::Round($fileInfo.Length / 1024, 2)
Write-ColorOutput "✓ $($spec.Platform) -> $customName build successful (size: $sizeKB KB)" "Green"
# UPX compression
if ($UseUpx -and (Test-UpxAvailable)) {
Write-ColorOutput "Compressing with UPX..." "Yellow"
try {
upx --best --lzma "$outputPath" 2>$null
$compressedInfo = Get-Item $outputPath
$compressedSizeKB = [math]::Round($compressedInfo.Length / 1024, 2)
Write-ColorOutput "✓ Compression completed (compressed: $compressedSizeKB KB)" "Green"
} catch {
Write-ColorOutput "UPX compression failed, continuing..." "Yellow"
}
}
$successCount++
} catch {
Write-ColorOutput "✗ $($spec.Platform) -> $customName build failed: $($_.Exception.Message)" "Red"
} finally {
# Clean environment variables
Remove-Item env:GOOS -ErrorAction SilentlyContinue
Remove-Item env:GOARCH -ErrorAction SilentlyContinue
Remove-Item env:CGO_ENABLED -ErrorAction SilentlyContinue
}
Write-Host ""
}
return @{ Success = $successCount; Total = $totalCount }
}
# Create release package
function New-ReleasePackage {
param(
[string]$Goos,
[string]$Goarch,
[string]$BinaryPath,
[hashtable]$VersionInfo
)
$packageName = "${AppName}_$($VersionInfo.Version)_${Goos}_${Goarch}"
$packageDir = Join-Path $DistDir $packageName
New-Item -ItemType Directory -Path $packageDir -Force | Out-Null
# Copy binary file
Copy-Item $BinaryPath $packageDir
# Copy documentation
if (Test-Path "README.md") {
Copy-Item "README.md" $packageDir
}
# Create start script
if ($Goos -eq "windows") {
$startScript = @"
@echo off
echo Starting MediaProxy...
$AppName.exe -port 57574
pause
"@
$startScript | Out-File -FilePath (Join-Path $packageDir "start.bat") -Encoding ASCII
} else {
$startScript = @"
#!/bin/bash
echo "Starting MediaProxy..."
./$AppName -port 57574
"@
$startScript | Out-File -FilePath (Join-Path $packageDir "start.sh") -Encoding UTF8
}
# Create configuration example
$configExample = @"
# MediaProxy Configuration Example
# Usage: ./$AppName -port 57574 -dns 8.8.8.8 -debug
# Default port
PORT=57574
# DNS server
DNS=8.8.8.8
# Debug mode
DEBUG=false
"@
$configExample | Out-File -FilePath (Join-Path $packageDir "config.example") -Encoding UTF8
# Package
try {
Push-Location $DistDir
if ($Goos -eq "windows") {
Compress-Archive -Path $packageName -DestinationPath "$packageName.zip" -Force
Write-ColorOutput "✓ Created release package: $packageName.zip" "Green"
} else {
# For non-Windows platforms, create tar.gz if tar command is available
if (Get-Command tar -ErrorAction SilentlyContinue) {
tar -czf "$packageName.tar.gz" $packageName
Write-ColorOutput "✓ Created release package: $packageName.tar.gz" "Green"
} else {
Write-ColorOutput "✓ Created release directory: $packageName" "Green"
}
}
} catch {
Write-ColorOutput "Packaging failed: $($_.Exception.Message)" "Yellow"
} finally {
Pop-Location
}
# Clean temporary directory
Remove-Item $packageDir -Recurse -Force -ErrorAction SilentlyContinue
}
# Show help
function Show-Help {
Write-ColorOutput "MediaProxy PowerShell Build Script" "Cyan"
Write-Host ""
Write-ColorOutput "Usage:" "White"
Write-Host " .\build.ps1 [options]"
Write-Host ""
Write-ColorOutput "Options:" "White"
Write-Host " -All Build all platforms (default)"
Write-Host " -Platform <name> Specify platform (e.g., windows/amd64)"
Write-Host " -Special Build special platforms (Android ARM64, Windows AMD64, Linux AMD64) with custom names"
Write-Host " -Clean Clean build directories"
Write-Host " -Dev Development mode build (fast build, no optimization)"
Write-Host " -NoUpx Disable UPX compression"
Write-Host " -Verbose Verbose output"
Write-Host " -Help Show help information"
Write-Host ""
Write-ColorOutput "Supported platforms:" "White"
foreach ($platform in $Platforms) {
Write-Host " $platform"
}
Write-Host ""
Write-ColorOutput "Examples:" "White"
Write-Host " .\build.ps1 # Build all platforms"
Write-Host " .\build.ps1 -Platform windows/amd64 # Build Windows 64-bit only"
Write-Host " .\build.ps1 -Special # Build special platforms (Android, Windows, Linux) with custom names"
Write-Host " .\build.ps1 -Dev # Development mode build"
Write-Host " .\build.ps1 -Clean # Clean build directories"
}
# Main function
function Main {
Write-Host ""
Write-ColorOutput "========================================" "Cyan"
Write-ColorOutput "MediaProxy PowerShell Build Script" "Cyan"
Write-ColorOutput "========================================" "Cyan"
# Show help
if ($Help) {
Show-Help
return
}
# Clean mode
if ($Clean) {
Clear-BuildDirs
return
}
# Check Go environment
if (-not (Test-GoEnvironment)) {
return
}
# Get version information
$versionInfo = Get-VersionInfo
Write-ColorOutput "Version: $($versionInfo.Version)" "Blue"
Write-ColorOutput "Commit: $($versionInfo.GitCommit)" "Blue"
Write-ColorOutput "Build Time: $($versionInfo.BuildTime)" "Blue"
Write-Host ""
# Check UPX
$upxAvailable = Test-UpxAvailable
if (-not $upxAvailable -and -not $NoUpx) {
Write-ColorOutput "Warning: UPX not found, compression will be skipped" "Yellow"
Write-ColorOutput "Install UPX to further reduce binary size" "Yellow"
Write-Host ""
}
# Clean and create directories
Clear-BuildDirs
New-BuildDirs
# Download dependencies
Write-ColorOutput "Downloading dependencies..." "Blue"
try {
go mod tidy
go mod download
} catch {
Write-ColorOutput "Dependency download failed: $($_.Exception.Message)" "Red"
return
}
Write-Host ""
# Build
$successCount = 0
$totalCount = 0
$useUpx = $upxAvailable -and -not $NoUpx -and -not $Dev
if ($Special) {
# Build special platforms with custom names
$result = Build-SpecialPlatforms -VersionInfo $versionInfo -UseUpx $useUpx
$successCount = $result.Success
$totalCount = $result.Total
} elseif ($Platform) {
# Build specified platform
if ($Platform -notin $Platforms) {
Write-ColorOutput "Error: Unsupported platform '$Platform'" "Red"
Write-ColorOutput "Supported platforms: $($Platforms -join ', ')" "Yellow"
return
}
$totalCount = 1
if (Build-Platform -PlatformString $Platform -VersionInfo $versionInfo -UseUpx $useUpx) {
$successCount = 1
}
} else {
# Build all platforms
foreach ($platformString in $Platforms) {
$totalCount++
if (Build-Platform -PlatformString $platformString -VersionInfo $versionInfo -UseUpx $useUpx) {
$successCount++
}
Write-Host ""
}
}
# Build summary
Write-Host ""
Write-ColorOutput "========================================" "Cyan"
Write-ColorOutput "Build completed!" "Green"
Write-ColorOutput "Success: $successCount/$totalCount" "Blue"
Write-ColorOutput "========================================" "Cyan"
if (Test-Path $DistDir) {
$items = Get-ChildItem $DistDir
if ($items.Count -gt 0) {
Write-Host ""
Write-ColorOutput "Release packages location: $DistDir" "Blue"
$items | ForEach-Object {
$size = if ($_.PSIsContainer) { "Directory" } else { "$([math]::Round($_.Length / 1MB, 2)) MB" }
Write-Host " $($_.Name) ($size)"
}
}
}
if ($successCount -lt $totalCount) {
Write-Host ""
Write-ColorOutput "Some builds failed, please check error messages" "Yellow"
exit 1
} else {
Write-Host ""
Write-ColorOutput "All builds completed successfully!" "Green"
}
}
# Execute main function
try {
Main
} catch {
Write-ColorOutput "Build script execution failed: $($_.Exception.Message)" "Red"
if ($Verbose) {
Write-ColorOutput "Detailed error information:" "Red"
Write-Host $_.Exception.StackTrace
}
exit 1
}