-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProcessEx.build.ps1
267 lines (230 loc) · 8.43 KB
/
ProcessEx.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
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
[CmdletBinding()]
param(
[ValidateSet('Debug', 'Release')]
[string]
$Configuration = 'Debug'
)
$modulePath = [IO.Path]::Combine($PSScriptRoot, 'module')
$manifestItem = Get-Item ([IO.Path]::Combine($modulePath, '*.psd1'))
$ModuleName = $manifestItem.BaseName
$Manifest = Test-ModuleManifest -Path $manifestItem.FullName -ErrorAction Ignore -WarningAction Ignore
$Version = $Manifest.Version
$BuildPath = [IO.Path]::Combine($PSScriptRoot, 'output')
$PowerShellPath = [IO.Path]::Combine($PSScriptRoot, 'module')
$CSharpPath = [IO.Path]::Combine($PSScriptRoot, 'src')
$ReleasePath = [IO.Path]::Combine($BuildPath, $ModuleName, $Version)
$IsUnix = $PSEdition -eq 'Core' -and -not $IsWindows
[xml]$csharpProjectInfo = Get-Content ([IO.Path]::Combine($CSharpPath, '*.csproj'))
$TargetFrameworks = @($csharpProjectInfo.Project.PropertyGroup.TargetFrameworks[0].Split(
';', [StringSplitOptions]::RemoveEmptyEntries))
$PSFramework = if ($PSVersionTable.PSVersion.Major -eq 5) {
$TargetFrameworks[0]
}
else {
$TargetFrameworks[1]
}
task Clean {
if (Test-Path $ReleasePath) {
Remove-Item $ReleasePath -Recurse -Force
}
New-Item -ItemType Directory $ReleasePath | Out-Null
}
task BuildDocs {
$helpParams = @{
Path = [IO.Path]::Combine($PSScriptRoot, 'docs', 'en-US')
OutputPath = [IO.Path]::Combine($ReleasePath, 'en-US')
}
New-ExternalHelp @helpParams | Out-Null
}
task BuildManaged {
Push-Location -Path $CSharpPath
$arguments = @(
'publish'
'--configuration', $Configuration
'--verbosity', 'q'
'-nologo'
"-p:Version=$Version"
# On a compiler failure a background dotnet process continues to live on keeping the user profile open.
# The ElevatePrivileges.ps1 runner requires everything to have finished for it to complete so disabling this
# will ensure dotnet doesn't have any lingering child processes.
"-p:UseSharedCompilation=false"
)
try {
foreach ($framework in $TargetFrameworks) {
Write-Host "Compiling for $framework"
dotnet @arguments --framework $framework
if ($LASTEXITCODE) {
throw "Failed to compiled code for $framework"
}
}
}
finally {
Pop-Location
}
}
task CopyToRelease {
$copyParams = @{
Path = [IO.Path]::Combine($PowerShellPath, '*')
Destination = $ReleasePath
Recurse = $true
Force = $true
}
Copy-Item @copyParams
foreach ($framework in $TargetFrameworks) {
$buildFolder = [IO.Path]::Combine($CSharpPath, 'bin', $Configuration, $framework, 'publish')
$binFolder = [IO.Path]::Combine($ReleasePath, 'bin', $framework)
if (-not (Test-Path -LiteralPath $binFolder)) {
New-Item -Path $binFolder -ItemType Directory | Out-Null
}
Copy-Item ([IO.Path]::Combine($buildFolder, "*")) -Destination $binFolder
}
}
task Sign {
$vaultName = $env:AZURE_KEYVAULT_NAME
$vaultCert = $env:AZURE_KEYVAULT_CERT
if (-not $vaultName -or -not $vaultCert) {
return
}
$key = Get-OpenAuthenticodeAzKey -Vault $vaultName -Certificate $vaultCert
$signParams = @{
Key = $key
TimeStampServer = 'http://timestamp.digicert.com'
}
Get-ChildItem -LiteralPath $ReleasePath -Recurse -ErrorAction SilentlyContinue |
Where-Object {
$_.Extension -in ".ps1", ".psm1", ".psd1", ".ps1xml" -or (
$_.Extension -eq ".dll" -and $_.BaseName -like "$ModuleName*"
)
} |
ForEach-Object -Process {
Set-OpenAuthenticodeSignature -LiteralPath $_.FullName @signParams
}
}
task Package {
$nupkgPath = [IO.Path]::Combine($BuildPath, "$ModuleName.$Version*.nupkg")
if (Test-Path $nupkgPath) {
Remove-Item $nupkgPath -Force
}
$repoParams = @{
Name = 'LocalRepo'
SourceLocation = $BuildPath
PublishLocation = $BuildPath
InstallationPolicy = 'Trusted'
}
if (Get-PSRepository -Name $repoParams.Name -ErrorAction SilentlyContinue) {
Unregister-PSRepository -Name $repoParams.Name
}
Register-PSRepository @repoParams
try {
Publish-Module -Path $ReleasePath -Repository $repoParams.Name
}
finally {
Unregister-PSRepository -Name $repoParams.Name
}
}
task Analyze {
$pssaSplat = @{
Path = $ReleasePath
Settings = [IO.Path]::Combine($PSScriptRoot, 'ScriptAnalyzerSettings.psd1')
Recurse = $true
ErrorAction = 'SilentlyContinue'
}
$results = Invoke-ScriptAnalyzer @pssaSplat
if ($null -ne $results) {
$results | Out-String
throw "Failed PsScriptAnalyzer tests, build failed"
}
}
task DoUnitTest {
$testsPath = [IO.Path]::Combine($PSScriptRoot, 'tests', 'units')
if (-not (Test-Path -LiteralPath $testsPath)) {
Write-Host "No unit tests found, skipping"
return
}
$resultsPath = [IO.Path]::Combine($BuildPath, 'TestResults')
if (-not (Test-Path -LiteralPath $resultsPath)) {
New-Item $resultsPath -ItemType Directory -ErrorAction Stop | Out-Null
}
# dotnet test places the results in a subfolder of the results-directory. This subfolder is based on a random guid
# so a temp folder is used to ensure we only get the current runs results
$tempResultsPath = [IO.Path]::Combine($resultsPath, "TempUnit")
if (Test-Path -LiteralPath $tempResultsPath) {
Remove-Item -LiteralPath $tempResultsPath -Force -Recurse
}
New-Item -Path $tempResultsPath -ItemType Directory | Out-Null
try {
$runSettingsPrefix = 'DataCollectionRunSettings.DataCollectors.DataCollector.Configuration'
$arguments = @(
'test'
'"{0}"' -f $testsPath
'--results-directory', $tempResultsPath
'--collect:"XPlat Code Coverage"'
'--'
"$runSettingsPrefix.Format=json"
"$runSettingsPrefix.IncludeDirectory=`"$CSharpPath`""
)
Write-Host "Running unit tests"
dotnet @arguments
if ($LASTEXITCODE) {
throw "Unit tests failed"
}
Move-Item -Path $tempResultsPath/*/*.json -Destination $resultsPath/UnitCoverage.json -Force
}
finally {
Remove-Item -LiteralPath $tempResultsPath -Force -Recurse
}
}
task DoTest {
$resultsPath = [IO.Path]::Combine($BuildPath, 'TestResults')
if (-not (Test-Path $resultsPath)) {
New-Item $resultsPath -ItemType Directory -ErrorAction Stop | Out-Null
}
$resultsFile = [IO.Path]::Combine($resultsPath, 'Pester.xml')
if (Test-Path $resultsFile) {
Remove-Item $resultsFile -ErrorAction Stop -Force
}
$pesterScript = [IO.Path]::Combine($PSScriptRoot, 'tools', 'PesterTest.ps1')
$pwsh = [Environment]::GetCommandLineArgs()[0] -replace '\.dll$', ''
$arguments = @(
'-NoProfile'
'-NonInteractive'
if (-not $IsUnix) {
'-ExecutionPolicy', 'Bypass'
}
'-File', ('"{0}"' -f $pesterScript)
'-TestPath', ('"{0}"' -f [IO.Path]::Combine($PSScriptRoot, 'tests'))
'-OutputFile', ('"{0}"' -f $resultsFile)
)
# We use coverlet to collect code coverage of our binary
$unitCoveragePath = [IO.Path]::Combine($resultsPath, 'UnitCoverage.json')
$arguments = @(
'"{0}"' -f ([IO.Path]::Combine($ReleasePath, 'bin', $PSFramework))
'--target', $pwsh
'--targetargs', (($arguments -join " ") -replace '"', '\"')
'--output', ([IO.Path]::Combine($resultsPath, 'Coverage.xml'))
'--format', 'cobertura'
if (Test-Path -LiteralPath $unitCoveragePath) {
'--merge-with', $unitCoveragePath
}
)
& coverlet $arguments
if ($LASTEXITCODE) {
throw "Pester failed tests"
}
}
task DoInstall {
$installBase = $Home
if ($profile) {
$installBase = $profile | Split-Path
}
$installPath = [IO.Path]::Combine($installBase, 'Modules', $ModuleName, $Version)
if (-not (Test-Path $installPath)) {
New-Item $installPath -ItemType Directory | Out-Null
}
Copy-Item -Path ([IO.Path]::Combine($ReleasePath, '*')) -Destination $installPath -Force -Recurse
}
task Build -Jobs Clean, BuildManaged, CopyToRelease, BuildDocs, Sign, Package
# FIXME: Work out why we need the obj and bin folder for coverage to work
task Test -Jobs BuildManaged, Analyze, DoUnitTest, DoTest
task Install -Jobs DoInstall
task . Build