-
Notifications
You must be signed in to change notification settings - Fork 5
/
psake.ps1
293 lines (249 loc) · 13.1 KB
/
psake.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# PSake makes variables declared here available in other scriptblocks
# Init some things
Properties {
# Find the build folder based on build system
$ProjectRoot = $ENV:BHProjectPath
if (-not $ProjectRoot) {
$ProjectRoot = $PSScriptRoot
}
$Timestamp = Get-Date -UFormat '%Y%m%d-%H%M%S'
$PSVersion = $PSVersionTable.PSVersion.Major
$TestFile = "TestResults_PS$PSVersion`_$TimeStamp.xml"
$lines = '----------------------------------------------------------------------'
$Verbose = @{}
if ($ENV:BHCommitMessage -match '!verbose') {
$Verbose = @{Verbose = $True }
}
}
Task Default -depends 'Deploy'
Task Init {
$lines
Set-Location $ProjectRoot
'Build System Details:'
Get-Item ENV:BH*
"`n"
}
Task Analyze -depends Init {
$lines
# ScriptAnalyzer
"`nSCRIPTANALYZER: CHECKING..."
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Add-AppveyorTest -name 'PsScriptAnalyzer' -Outcome 'Running'
}
$CodeResults = Invoke-ScriptAnalyzer -Path "$ProjectRoot\PSDokuWiki" -Recurse -Severity 'Error' -ErrorAction SilentlyContinue @Verbose
If ($CodeResults) {
$ResultString = $CodeResults | Out-String
Write-Warning $ResultString
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Add-AppveyorMessage -Message "PSScriptAnalyzer output contained one or more result(s) with 'Error' severity. Check the 'Tests' tab of this build for more details." -Category Error
Update-AppveyorTest -name 'PsScriptAnalyzer' -Outcome 'Failed' -ErrorMessage $ResultString
}
# Failing the build
Throw 'Build failed - PSScriptAnalyzer'
} else {
"`tNO ERRORS`n"
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Update-AppveyorTest -name 'PsScriptAnalyzer' -Outcome 'Passed'
}
}
"`n"
}
Task Test -depends Analyze {
$lines
"`n`tSTATUS: Testing with PowerShell $PSVersion"
Import-Module (Join-Path -Path $ProjectRoot -ChildPath 'PSDokuWiki') -Force @Verbose
# Gather test results. Store them in a variable and file
$Script:TestResults = Invoke-Pester -Path "$ProjectRoot\Tests" -OutputFormat 'NUnitXml' -OutputFile "$ProjectRoot\$TestFile" -PassThru -CodeCoverage "$ProjectRoot\PSDokuWiki\*\*.ps1" @Verbose -ExcludeTag PostBuild
$Script:TestResults | Export-Clixml -Path "$ProjectRoot\PesterResults$PSVersion.xml"
$AllFiles = Get-ChildItem -Path "$ProjectRoot\*Results*.xml" | Select-Object -ExpandProperty 'FullName'
"COLLATING FILES:`n$($AllFiles | Out-String)"
# In Appveyor? Upload our tests! #Abstract this into a function?
If ($ENV:BHBuildSystem -eq 'AppVeyor') {
Get-ChildItem -Path "$ProjectRoot\TestResults_PS*.xml" | ForEach-Object -Process {
$Address = "https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)"
$Source = $_.FullName
"UPLOADING FILES: $Address $Source"
(New-Object 'System.Net.WebClient').UploadFile( $Address, $Source )
}
}
$Results = @( Get-ChildItem -Path "$ProjectRoot\PesterResults*.xml" | Import-Clixml )
$FailedCount = $Results | Select-Object -ExpandProperty 'FailedCount' | Measure-Object -Sum | Select-Object -ExpandProperty 'Sum'
if ($FailedCount -gt 0) {
$FailedItems = $Results | Select-Object -ExpandProperty 'TestResult' | Where-Object -FilterScript { $_.Passed -notlike $True }
"FAILED TESTS SUMMARY:`n"
$FailedItems | ForEach-Object -Process {
$Test = $_
[pscustomobject]@{
Describe = $Test.Describe
Context = $Test.Context
Name = "It $($Test.Name)"
Result = $Test.Result
}
} | Sort-Object -Property 'Describe', 'Context', 'Name', 'Result' | Format-List
throw "$FailedCount tests failed."
}
Get-ChildItem -Path "$ProjectRoot\PesterResults*.xml" | Remove-Item -Force -ErrorAction 'SilentlyContinue'
Remove-Item "$ProjectRoot\$TestFile" -Force -ErrorAction 'SilentlyContinue'
}
Task Coverage -depends Test {
# CODE COVERAGE
"`nCODE COVERAGE:"
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Add-AppveyorTest -name 'PesterStatementCoverage' -Outcome 'Running'
Add-AppveyorTest -name 'PesterFunctionCoverage' -Outcome 'Running'
}
$CodeCoverage = @{
Functions = @{}
Statement = @{
Analyzed = $Script:TestResults.CodeCoverage.NumberOfCommandsAnalyzed
Executed = $Script:TestResults.CodeCoverage.NumberOfCommandsExecuted
Missed = $Script:TestResults.CodeCoverage.NumberOfCommandsMissed
Coverage = 0
}
Function = @{}
}
$CodeCoverage.Statement.Coverage = [math]::Round($CodeCoverage.Statement.Executed / $CodeCoverage.Statement.Analyzed * 100, 2)
$Script:TestResults.CodeCoverage.HitCommands | Group-Object -Property Function | ForEach-Object {
if (-Not $CodeCoverage.Functions.ContainsKey($_.Name)) {
$CodeCoverage.Functions.Add($_.Name, @{
Name = $_.Name
Analyzed = 0
Executed = 0
Missed = 0
Coverage = 0
})
}
$CodeCoverage.Functions[$_.Name].Analyzed += $_.Count
$CodeCoverage.Functions[$_.Name].Executed += $_.Count
}
$Script:TestResults.CodeCoverage.MissedCommands | Group-Object -Property Function | ForEach-Object {
if (-Not $CodeCoverage.Functions.ContainsKey($_.Name)) {
$CodeCoverage.Functions.Add($_.Name, @{
Name = $_.Name
Analyzed = 0
Executed = 0
Missed = 0
Coverage = 0
})
}
$CodeCoverage.Functions[$_.Name].Analyzed += $_.Count
$CodeCoverage.Functions[$_.Name].Missed += $_.Count
}
foreach ($function in $CodeCoverage.Functions.Values) {
$function.Coverage = [math]::Round($function.Executed / $function.Analyzed * 100)
}
$CodeCoverage.Function = @{
Analyzed = $CodeCoverage.Functions.Count
Executed = ($CodeCoverage.Functions.Values | Where-Object { $_.Executed -gt 0 }).Length
Missed = ($CodeCoverage.Functions.Values | Where-Object { $_.Executed -eq 0 }).Length
}
$CodeCoverage.Function.Coverage = [math]::Round($CodeCoverage.Function.Executed / $CodeCoverage.Function.Analyzed * 100, 2)
# Define thresholds for pass / fail dont actually fail the build though yet, just for more info
$StatementThreshold = 80
$FunctionThreshold = 100
"`n`tStatement coverage: $($CodeCoverage.Statement.Analyzed) analyzed, $($CodeCoverage.Statement.Executed) executed, $($CodeCoverage.Statement.Missed) missed, $($CodeCoverage.Statement.Coverage)%."
if ($CodeCoverage.Statement.Coverage -ge $StatementThreshold) {
# passed Statement coverage test
Write-Host "`tPassed statement coverage threshold of: $StatementThreshold%" -ForegroundColor 'Green'
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Update-AppveyorTest -name 'PesterStatementCoverage' -Outcome 'Passed'
}
} else {
# failed Statement coverage test
Write-Warning "`tFailed function coverage threshold of: $StatementThreshold%"
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Add-AppveyorMessage -Message "`tFailed function coverage threshold of: $StatementThreshold%`n" -Category 'Error'
Update-AppveyorTest -name 'PesterStatementCoverage' -Outcome 'Failed' -ErrorMessage "Pester statement coverage did not meet threshold of $StatementThreshold%"
}
# Failing the build
Throw 'Build failed'
}
"`n`tFunction coverage: $($CodeCoverage.Function.Analyzed) analyzed, $($CodeCoverage.Function.Executed) executed, $($CodeCoverage.Function.Missed) missed, $($CodeCoverage.Function.Coverage)%."
if ($CodeCoverage.Function.Coverage -ge $FunctionThreshold) {
# passed Function coverage test
Write-Host "`tPassed function coverage threshold of: $FunctionThreshold%" -ForegroundColor 'Green'
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Update-AppveyorTest -name 'PesterFunctionCoverage' -Outcome 'Passed'
}
} else {
# failed Function coverage test
Write-Warning "`tFailed function coverage threshold of: $FunctionThreshold%"
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Add-AppveyorMessage -Message "`tFailed function coverage threshold of: $FunctionThreshold%`n" -Category 'Error'
Update-AppveyorTest -name 'PesterFunctionCoverage' -Outcome 'Failed' -ErrorMessage "Pester statement coverage did not meet threshold of $FunctionThreshold%"
}
# Failing the build
Throw 'Build failed'
}
"`n"
}
Task Build -depends Coverage {
$lines
Write-Host "`nUpdating exported module members"
Set-ModuleFunctions @Verbose
Write-Host "`nIncrementing build number"
Update-Metadata -Path $env:BHPSModuleManifest @Verbose
# Generate help for the module
Get-Module -name 'PlatyPS', 'PSDokuWiki' | Remove-Module -Force
Import-Module (Join-Path -Path $ProjectRoot -ChildPath 'PSDokuWiki\PSDokuWiki.psm1') -Global -Force @Verbose
Write-Host 'Generating markdown help'
Update-MarkdownHelpModule -Path "$ProjectRoot\docs\" -AlphabeticParamsOrder -Force -RefreshModulePage @Verbose | Out-Null
New-Item -Path "$ProjectRoot\PSDokuWiki\en-US" -ItemType 'Directory' -ErrorAction 'SilentlyContinue' @Verbose | Out-Null
try {
Write-Host 'Generating MAML help'
New-ExternalHelp -Path '.\docs\' -OutputPath '.\PSDokuWiki\en-US' -Force -ErrorAction 'Stop' @Verbose | Out-Null
} catch {
throw 'Build failed - Failed to generate help files'
$_
}
"`n"
}
Task PostBuildTest -depends Build {
$lines
"`n`tSTATUS: Post-build test with PowerShell $PSVersion"
Get-Module -name 'PSDokuWiki' | Remove-Module -Force
Import-Module (Join-Path -Path $ProjectRoot -ChildPath 'PSDokuWiki\PSDokuWiki.psd1') -Force @Verbose
# Gather test results. Store them in a variable and file
$Script:TestResults = Invoke-Pester -Path "$ProjectRoot\Tests" -OutputFormat 'NUnitXml' -OutputFile "$ProjectRoot\$TestFile" -PassThru @Verbose -Tag PostBuild
$Script:TestResults | Export-Clixml -Path "$ProjectRoot\PesterResults$PSVersion.xml"
$AllFiles = Get-ChildItem -Path "$ProjectRoot\*Results*.xml" | Select-Object -ExpandProperty 'FullName'
"COLLATING FILES:`n$($AllFiles | Out-String)"
# In Appveyor? Upload our tests! #Abstract this into a function?
If ($ENV:BHBuildSystem -eq 'AppVeyor') {
Get-ChildItem -Path "$ProjectRoot\TestResults_PS*.xml" | ForEach-Object -Process {
$Address = "https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)"
$Source = $_.FullName
"UPLOADING FILES: $Address $Source"
(New-Object 'System.Net.WebClient').UploadFile( $Address, $Source )
}
}
$Results = @( Get-ChildItem -Path "$ProjectRoot\PesterResults*.xml" | Import-Clixml )
$FailedCount = $Results | Select-Object -ExpandProperty 'FailedCount' | Measure-Object -Sum | Select-Object -ExpandProperty 'Sum'
if ($FailedCount -gt 0) {
$FailedItems = $Results | Select-Object -ExpandProperty 'TestResult' | Where-Object -FilterScript { $_.Passed -notlike $True }
"FAILED TESTS SUMMARY:`n"
$FailedItems | ForEach-Object -Process {
$Test = $_
[pscustomobject]@{
Describe = $Test.Describe
Context = $Test.Context
Name = "It $($Test.Name)"
Result = $Test.Result
}
} | Sort-Object -Property 'Describe', 'Context', 'Name', 'Result' | Format-List
throw "$FailedCount tests failed."
}
Get-ChildItem -Path "$ProjectRoot\PesterResults*.xml" | Remove-Item -Force -ErrorAction 'SilentlyContinue'
Remove-Item "$ProjectRoot\$TestFile" -Force -ErrorAction 'SilentlyContinue'
"`n"
}
Task Deploy -depends PostBuildTest {
$lines
# Publish to gallery with a few restrictions
if (($env:BHProjectName) -and ($env:BHProjectName.Count -eq 1) -and ($env:BHBuildSystem -ne 'Unknown') -and ($env:BHBranchName -eq 'master') -and ($env:BHCommitMessage -match '!deploy') -and ($isWindows -eq $true)) {
Write-Host 'Deploying to PSGallery...'
Publish-Module -Path $ENV:BHPSModulePath -Repository 'PSGallery' -NuGetApiKey $ENV:NugetApiKey -ErrorAction Stop @Verbose
} else {
"Skipping deployment: To deploy, ensure that...`n" + "`t* You are in a known build system (Current: $ENV:BHBuildSystem)`n" + "`t* You are committing to the master branch (Current: $ENV:BHBranchName) `n" + "`t* Your commit message includes !deploy (Current: $ENV:BHCommitMessage)" + "`t* The build OS is Windows (Current: $isWindows)" | Write-Host
}
}