-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.ps1
128 lines (98 loc) · 3.75 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
118
119
120
121
122
123
124
125
126
127
128
param (
[string]
$BuildPath = ".\build",
[string[]]
$Architectures = @("amd64","386"),
[switch]
$Release = $false,
[string]
$ReleasePath = "winssh-pageant",
[string]
$ver = ""
)
# Cleanup
Remove-Item -LiteralPath $BuildPath -Force -Recurse -ErrorAction SilentlyContinue
# Build output directory
$outDir = New-Item -ItemType Directory -Force -Path $BuildPath
$releaseDir = New-Item -ItemType Directory -Force -Path ".\release"
$oldGOOS = $env:GOOS
$oldGOARCH = $env:GOARCH
$env:GOOS="windows"
$env:GOARCH=$null
$returnValue = 0
if($ver.Length -lt 1)
{
$ver = git describe --tags --abbrev=0
}
$__ = $ver -match '[a-zA-Z]*(\d+)\.(\d+)'
$verMajor = $Matches.1
$verMinor = $Matches.2
$env:path -split ';'
# Build release package
if ($Release)
{
Remove-Item -LiteralPath $ReleasePath -ErrorAction SilentlyContinue
Remove-Item -Path checksums.md -ErrorAction SilentlyContinue
Write-Output "
## Checksums
| Architecture | File Name | Checksum |
|---|---|---|" | Out-File -FilePath checksums.md -Encoding utf8
Push-Location resources
goversioninfo -ver-major ${verMajor} -ver-minor ${verMinor} -product-version ${ver} -product-ver-major ${verMajor} -product-ver-minor ${verMinor} -platform-specific
Pop-Location
}
function PrepareBuildDir ([string] $path, [string] $arch) {
$buildDir = New-Item -ItemType Directory -Force -Path "$path/$arch"
Copy-Item -Force .\README.md $buildDir
Copy-Item -Force .\LICENSE $buildDir
Copy-Item -Force "resources/resource*.syso" ./
Copy-Item -Recurse -Force -Path "resources/templates" -Destination $buildDir
Copy-Item -Force -Path "resources/wix.json" -Destination $buildDir
Copy-Item -Force -Path "resources/icon/icon.ico" -Destination $buildDir
return $buildDir
}
function CreateStandaloneZip ([string] $path, [string] $outDir, [string] $arch) {
Compress-Archive -Force -Path $path\README.md,$path\LICENSE,$path\*.exe -DestinationPath $outDir\$ReleasePath-${ver}_$arch.zip
}
# Build Standalone for each architecture
Foreach ($arch in $Architectures)
{
$env:GOARCH=$arch
if ($Release)
{
$buildDir = PrepareBuildDir $outDir $arch
$buildFlags = "-ldflags=""-w -s -H=windowsgui"" -trimpath"
$binary = "winssh-pageant.exe"
} else {
$buildDir = $outDir
$buildFlags = ""
$binary = "winssh-pageant-$arch.exe"
}
Invoke-Expression ("go build ${buildFlags} -o $buildDir\$binary" )
#Invoke-Expression ("upx --brute $buildDir\$binary" )
if ($LastExitCode -ne 0) { $returnValue = $LastExitCode }
if ($Release)
{
CreateStandaloneZip $buildDir $releaseDir $arch
Push-Location $buildDir
$msiName = "winssh-pageant-${ver}_${arch}.msi"
go-msi make --path $buildDir\wix.json --src $buildDir\templates --out $buildDir\tmp --version $ver --arch $arch --msi "${releaseDir}\${msiName}"
Pop-Location
$checksum = (Get-FileHash -Algorithm SHA256 -Path "${buildDir}\${binary}").Hash
Write-Output "| $arch | $binary | ``${checksum}`` |" | Out-File -FilePath checksums.md -Encoding utf8 -Append
$checksum = (Get-FileHash -Algorithm SHA256 -Path "${releaseDir}\${msiName}").Hash
Write-Output "| $arch | $msiName | ``$checksum`` |" | Out-File -FilePath checksums.md -Encoding utf8 -Append
}
}
# Restore env vars
$env:GOOS = $oldGOOS
$env:GOARCH = $oldGOARCH
# Cleanup
if ($Release)
{
Write-Output "" | Out-File -FilePath checksums.md -Encoding utf8 -Append
Remove-Item -LiteralPath $BuildPath -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item -Path .\resource*.syso -Force -ErrorAction SilentlyContinue
Remove-Item -Path .\resources\resource*.syso -Force -ErrorAction SilentlyContinue
}
exit $returnValue