-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
236 lines (177 loc) · 7.93 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
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
[CmdletBinding(DefaultParameterSetName = "NoSignerCertificate")]
param (
[System.IO.DirectoryInfo]
$BuildDir = "build",
[switch]
$Logging = $false,
[Parameter(ParameterSetName = "SignerCertificate", Mandatory = $true)]
[System.Security.Cryptography.X509Certificates.X509Certificate2]
$SignerCertificate,
[Parameter(ParameterSetName = "SignerCertificate", Mandatory = $false)]
[string]
$TimestampServer = "http://timestamp.digicert.com"
)
# IMPORTANT NOTE OF USEFULNESS:
# if you run this script with `-InformationAction Continue`,
# you'll see the build log in the console directly.
function Write-ThisDown {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
$InputObject,
[ValidateSet("Host", "Info", "Log")]
[string[]]
$Destination = @("Info", "Log"),
[ValidateSet("List", "Table", "Wide", "String", "JSON", "Raw")]
$Format = "Raw",
[switch]
$ShowAllProperties = $false,
[switch]
$PassThru
)
process {
$WriteHost = $Destination -contains "Host"
$WriteInfo = $Destination -contains "Info" -and (-not $WriteHost) -and $InformationPreference -ne "SilentlyContinue"
$WriteLog = $Destination -contains "Log" -and $Logging -and $LogFilePath
function Write-ThisDownHere {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
$InputObject,
[ValidateSet("Host", "Info", "Log")]
$Medium = "Host",
[ValidateSet("List", "Table", "Wide", "String", "JSON", "Raw")]
$Format = "Raw",
[switch]
$ShowAllProperties = $false
)
process {
$EffectiveMedium = $Medium
if (-not @("Host", "Info", "Log") -contains $EffectiveMedium) {return} # Unknown medium
if ($EffectiveMedium -eq "Host" -and (-not $WriteHost)) {return} # Host not requested
if ($EffectiveMedium -eq "Info" -and (-not $WriteInfo)) {return} # Info not requested or `InformationAction == SilentlyContinue`
if ($EffectiveMedium -eq "Log" -and (-not $WriteLog )) {return} # Log not requested or is disabled
if ($EffectiveMedium -eq "Host" -and (-not $Host.UI.SupportsVirtualTerminal)) {$EffectiveMedium = "NonVT-Host"}
$formatListProps = @{}
if ($ShowAllProperties) {$formatListProps = @{"Property"="*"}}
$convertJsonProps = @{}
switch ("$EffectiveMedium-$Format") {
"Host-Raw" {$InputObject | Out-Host}
"Host-String" {$InputObject | Out-String | Write-Host}
"Host-List" {$InputObject | Format-List @formatListProps | Out-Host}
"Host-Table" {$InputObject | Format-Table @formatListProps | Out-Host}
"Host-Wide" {$InputObject | Format-Wide @formatListProps | Out-Host}
"Host-JSON" {$InputObject | ConvertTo-Json @convertJsonProps | Out-Host}
"NonVT-Host-Raw" {$InputObject | Out-String | Write-Host}
"NonVT-Host-String" {$InputObject | Out-String | Write-Host}
"NonVT-Host-List" {$InputObject | Format-List @formatListProps | Out-String | Write-Host}
"NonVT-Host-Table" {$InputObject | Format-Table @formatListProps | Out-String | Write-Host}
"NonVT-Host-Wide" {$InputObject | Format-Wide @formatListProps | Out-String | Write-Host}
"NonVT-Host-JSON" {$InputObject | ConvertTo-Json @convertJsonProps | Write-Host}
"Info-Raw" {$InputObject | Out-String | Write-Information}
"Info-String" {$InputObject | Out-String | Write-Information}
"Info-List" {$InputObject | Format-List @formatListProps | Out-String | Write-Information}
"Info-Table" {$InputObject | Format-Table @formatListProps | Out-String | Write-Information}
"Info-Wide" {$InputObject | Format-Wide @formatListProps | Out-String | Write-Information}
"Info-JSON" {$InputObject | ConvertTo-Json @convertJsonProps | Write-Information}
"Log-Raw" {$InputObject | Out-File -FilePath $LogFilePath -Append}
"Log-String" {$InputObject | Out-String | Out-File -FilePath $LogFilePath -Append}
"Log-List" {$InputObject | Format-List @formatListProps | Out-File -FilePath $LogFilePath -Append}
"Log-Table" {$InputObject | Format-Table @formatListProps | Out-File -FilePath $LogFilePath -Append}
"Log-Wide" {$InputObject | Format-Wide @formatListProps | Out-File -FilePath $LogFilePath -Append}
"Log-JSON" {$InputObject | ConvertTo-Json @convertJsonProps | Out-File -FilePath $LogFilePath -Append}
Default {}
}
}
}
if ($WriteHost) {
Write-ThisDownHere -InputObject $InputObject -Medium "Host" -Format $Format -ShowAllProperties:$ShowAllProperties
}
if ($WriteInfo) {
Write-ThisDownHere -InputObject $InputObject -Medium "Info" -Format $Format -ShowAllProperties:$ShowAllProperties
}
if ($WriteLog) {
Write-ThisDownHere -InputObject $InputObject -Medium "Log" -Format $Format -ShowAllProperties:$ShowAllProperties
}
}
end {
if ($PassThru) {
$inputObject
}
}
}
# Mark the start time of the build.
$Started = Get-Date
# Define our log file path
$LogFilePath = $null
if ($Logging) {
$LogFilePath = "$BuildDir\build.log"
}
# Define our output executable path.
$OutputExe = "$BuildDir\cadscan.exe"
# Make sure the build directory exists or create it.
if (-not (Test-Path $BuildDir)) {
if (Test-Path $BuildDir -PathType Container) {
throw "Build directory ($BuildDir) is not a directory; bailing out."
}
New-Item -ItemType Directory -Path $BuildDir | Out-Null
if (-not (Test-Path $BuildDir -PathType Container)) {
throw "Build directory ($BuildDir) does not exist and was not created; bailing out."
}
}
# Mark the start of the build in the build log.
"[Started] $Started" | Write-ThisDown
# Grab the commit hash, if we're in a git repo.
$GitInfo = $null
if (Test-Path .git -PathType Container) {
$GitInfo = [PSCustomObject]@{
Commit = git rev-parse HEAD
Branch = git rev-parse --abbrev-ref HEAD
Remote = git remote -v
}
}
# Time to build!
go build -ldflags -H=windowsgui -o $OutputExe | Write-ThisDown
# Sign the executable, if we have a signer certificate in hand.
$Authenticode = $null
if ($SignerCertificate) {
Set-AuthenticodeSignature -FilePath $OutputExe -Certificate $SignerCertificate -TimestampServer $TimestampServer -IncludeChain all | Write-ThisDown
$fileSignature = Get-AuthenticodeSignature -FilePath $OutputExe -Verbose | Write-ThisDown -Format List -PassThru
$Authenticode = [PSCustomObject]@{
Valid = $fileSignature.Status -eq "Valid"
Status = $fileSignature.Status
StatusMessage = $fileSignature.StatusMessage
SignatureType = $fileSignature.SignatureType
Thumbprint = $fileSignature.SignerCertificate.Thumbprint
Subject = $fileSignature.SignerCertificate.Subject
Issuer = $fileSignature.SignerCertificate.Issuer
TimestampCertificate = $fileSignature.TimeStamperCertificate
SignerCertificate = $fileSignature.SignerCertificate
}
}
# Grab the output file's properties.
$OutputFile = Get-ChildItem -Path $OutputExe | Write-ThisDown -PassThru
# Mark the end time of the build.
$Finished = Get-Date
# Use a PSCustomObject to make the output usable by other scripts
$Result = [PSCustomObject]@{
DateTime = [PSCustomObject]@{
Started = $Started
Finished = $Finished
}
Versions = [PSCustomObject]@{
PowerShell = $PSVersionTable.PSVersion
Go = go version
Git = git version
}
Git = $GitInfo
Authenticode = $Authenticode
OutputFile = $OutputFile
}
# Log that output to the build log...
$Result | Write-ThisDown -ShowAllProperties -Format List -Destination Log
# Mark the end of the build in the build log.
"[Finished] $Finished" | Write-ThisDown
# Hand this back to the caller for their consumption.
$Result
# ❤️