-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
88 lines (77 loc) · 2.92 KB
/
build.ps1
File metadata and controls
88 lines (77 loc) · 2.92 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
# PowerShell script for cross-platform Go builds
# Define available platforms
$platforms = @(
@{OS = "linux"; Arch = "amd64"; Display = "Linux (64-bit x86)"},
@{OS = "linux"; Arch = "arm64"; Display = "Linux (64-bit ARM)"},
@{OS = "windows"; Arch = "amd64"; Display = "Windows (64-bit x86)"},
@{OS = "darwin"; Arch = "amd64"; Display = "macOS (64-bit x86)"},
@{OS = "darwin"; Arch = "arm64"; Display = "macOS (64-bit ARM)"}
)
# Get the current directory name as the app name
$appName = Split-Path -Leaf (Get-Location)
# Create builds directory if it doesn't exist
$buildDir = "builds"
if (-not (Test-Path $buildDir)) {
New-Item -ItemType Directory -Path $buildDir | Out-Null
}
function Build-GoBinary {
param (
[string]$OS,
[string]$Arch,
[string]$OutputName
)
Write-Host "`nBuilding for $OS/$Arch..." -ForegroundColor Cyan
# Set environment variables for cross-compilation
$env:GOOS = $OS
$env:GOARCH = $Arch
# Build the binary
try {
$output = go build -o $OutputName 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host "Successfully built binary: $OutputName" -ForegroundColor Green
} else {
Write-Host "Build failed: $output" -ForegroundColor Red
}
}
catch {
Write-Host "Error during build: $_" -ForegroundColor Red
}
}
# Display menu
Write-Host "Available platforms:" -ForegroundColor Yellow
for ($i = 0; $i -lt $platforms.Count; $i++) {
Write-Host "$($i + 1). $($platforms[$i].Display)"
}
Write-Host "A. Build for all platforms"
Write-Host "Q. Quit"
# Get user choice
$choice = Read-Host "`nEnter your choice (1-$($platforms.Count), A for all, Q to quit)"
switch ($choice.ToUpper()) {
"Q" {
Write-Host "Exiting script..." -ForegroundColor Yellow
exit
}
"A" {
Write-Host "`nBuilding for all platforms..." -ForegroundColor Cyan
foreach ($platform in $platforms) {
$extension = if ($platform.OS -eq "windows") { ".exe" } else { "" }
$outputPath = Join-Path $buildDir "$appName`_$($platform.OS)_$($platform.Arch)$extension"
Build-GoBinary -OS $platform.OS -Arch $platform.Arch -OutputName $outputPath
}
}
default {
$index = [int]$choice - 1
if ($index -ge 0 -and $index -lt $platforms.Count) {
$platform = $platforms[$index]
$extension = if ($platform.OS -eq "windows") { ".exe" } else { "" }
$outputPath = Join-Path $buildDir "$appName`_$($platform.OS)_$($platform.Arch)$extension"
Build-GoBinary -OS $platform.OS -Arch $platform.Arch -OutputName $outputPath
}
else {
Write-Host "Invalid choice. Please run the script again." -ForegroundColor Red
}
}
}
# Display build directory contents after completion
Write-Host "`nContents of builds directory:" -ForegroundColor Yellow
Get-ChildItem $buildDir | Format-Table Name, Length