-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
117 lines (96 loc) · 3.79 KB
/
build.ps1
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
# build.ps1 - Cross-platform build script for Windows and macOS
# Parse command-line arguments
param (
[switch]$prod, # -prod switch for production build
[string]$version # -version parameter to set the version
)
# Default values
$appName = "RestoreSafe"
$prodBuild = $false
$outputDir = "test"
$buildName = "development"
$buildVersion = "0.0.1-dev"
# Check for production build flag
if ($prod) {
$prodBuild = $true
$outputDir = "dist"
$buildName = "production"
# Override the default version if -version parameter was provided
if ($version) {
$buildVersion = $version
} else {
Write-Host "Using default version $buildVersion for production build." -ForegroundColor Yellow
}
}
# Create the output directory if it doesn't exist
if (!(Test-Path -Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir | Out-Null
}
# Clean the output directory (only old versions of the app)
if (Test-Path -Path $outputDir) {
Write-Host "Cleaning old versions from output directory..."
# Create a regex pattern to match old versions
$searchPattern = ""
if ($prodBuild) {
# Matches "AppName-Version_"
$searchPattern = "^$appName-(.+)_"
} else {
# Matches "AppName.exe"
$searchPattern = "^$appName\.exe$"
}
Get-ChildItem -Path $outputDir | Where-Object { $_.Name -match $searchPattern } | Remove-Item -Force
}
# Define the build targets based on build configuration
$targets = @()
if ($prodBuild) {
# Production builds
$targets += @{ GOOS = "windows"; GOARCH = "amd64"; Output = "$outputDir/$appName-$buildVersion" + "_win-x64.exe" }
$targets += @{ GOOS = "darwin"; GOARCH = "arm64"; Output = "$outputDir/$appName-$buildVersion" + "_mac-arm64" }
} else {
# Dev build (only Windows for faster local development)
$targets += @{ GOOS = "windows"; GOARCH = "amd64"; Output = "$outputDir/$appName" + ".exe" }
}
# Build the application for each target
foreach ($target in $targets) {
# Set environment variables
$env:GOOS = $target.GOOS
$env:GOARCH = $target.GOARCH
# Build the application
Write-Host "Building $buildName build for $($target.GOOS)/$($target.GOARCH)..."
# Define the build command as an array
$cmd = @("go", "build", "-o", $target.Output)
# Add ldflags for version embedding (for production builds)
if ($prodBuild) {
$ldflags = "-ldflags ""-X main.version=$buildVersion -X main.buildTime=$(Get-Date -UFormat %s)"""
$cmd += $ldflags
}
# Add the package or .go file AFTER the ldflags
$cmd += "main.go"
# Invoke the build command and capture output (same as before)
$processInfo = New-Object System.Diagnostics.ProcessStartInfo
$processInfo.FileName = $cmd[0]
$processInfo.Arguments = ($cmd[1..($cmd.Length - 1)]) -join " " # Corrected join
$processInfo.RedirectStandardOutput = $true
$processInfo.RedirectStandardError = $true
$processInfo.UseShellExecute = $false
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $processInfo
$process.Start() | Out-Null
$stdout = $process.StandardOutput.ReadToEnd()
$stderr = $process.StandardError.ReadToEnd()
$process.WaitForExit()
if ($process.ExitCode -ne 0) {
Write-Host "Build failed for $($target.GOOS)/$($target.GOARCH)." -ForegroundColor Red
if ($stderr) {
Write-Host "Error details (stderr):" -ForegroundColor Red
Write-Host $stderr -ForegroundColor DarkGray
}
if ($stdout) {
Write-Host "Error details (stdout):" -ForegroundColor Red
Write-Host $stdout -ForegroundColor DarkGray
}
exit 1
}
Write-Host "Build successful: $($target.Output)" -ForegroundColor Green
}
Write-Host "All builds completed!" -ForegroundColor Cyan