-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.psake.ps1
122 lines (92 loc) · 3.56 KB
/
build.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
# PSake makes variables declared here available in other scriptblocks
Properties {
$ProjectRoot = $ENV:BHProjectPath
if (-not $ProjectRoot) {
$ProjectRoot = $PSScriptRoot
}
$Timestamp = Get-date -uformat "%Y%m%d-%H%M%S"
$PSVersion = $PSVersionTable.PSVersion.Major
$lines = '----------------------------------------------------------------------'
$Verbose = @{}
if ($ENV:BHCommitMessage -match "!verbose") {
$Verbose = @{Verbose = $True}
}
# Pester
$TestRootDir = "$ProjectRoot\Tests"
$TestScripts = Get-ChildItem "$ProjectRoot\Tests\*Tests.ps1"
$TestFile = "TestResults_PS$PSVersion`_$TimeStamp.xml"
# Script Analyzer
[ValidateSet('Error', 'Warning', 'Any', 'None')]
$ScriptAnalysisFailBuildOnSeverityLevel = 'None'
$ScriptAnalyzerSettingsPath = "$ProjectRoot\PSScriptAnalyzerSettings.psd1"
}
Task Default -Depends Test
Task Init {
$lines
Set-Location $ProjectRoot
"Build System Details:"
Get-Item ENV:BH*
"`n"
}
Task Analyze -Depends Init {
$Results = Invoke-ScriptAnalyzer -Path $ENV:BHModulePath -Recurse -Settings $ScriptAnalyzerSettingsPath -Verbose:$VerbosePreference
$Results | Select-Object RuleName, Severity, ScriptName, Line, Message | Format-List
switch ($ScriptAnalysisFailBuildOnSeverityLevel) {
'None' {
return
}
'Error' {
Assert -conditionToCheck (
($Results | Where-Object Severity -eq 'Error').Count -eq 0
) -failureMessage 'One or more ScriptAnalyzer errors were found. Build cannot continue!'
}
'Warning' {
Assert -conditionToCheck (
($Results | Where-Object {
$_.Severity -eq 'Warning' -or $_.Severity -eq 'Error'
}).Count -eq 0) -failureMessage 'One or more ScriptAnalyzer warnings were found. Build cannot continue!'
}
default {
Assert -conditionToCheck ($analysisResult.Count -eq 0) -failureMessage 'One or more ScriptAnalyzer issues were found. Build cannot continue!'
}
}
}
Task Test -Depends Analyze {
$lines
"`nSTATUS: Testing with PowerShell $PSVersion"
# Gather test results. Store them in a variable and file
$TestFilePath = Join-Path -Path $ProjectRoot -ChildPath $TestFile
$TestResults = Invoke-Pester -Script $TestScripts -PassThru -OutputFormat NUnitXml -OutputFile $TestFilePath -PesterOption @{IncludeVSCodeMarker = $true}
# Upload test results to Appveyor
if ($ENV:BHBuildSystem -eq 'AppVeyor') {
Add-TestResultToAppVeyor -TestFile $TestFilePath
}
Remove-Item $TestFilePath -Force -ErrorAction SilentlyContinue
# Fail build if any tests fail
if ($TestResults.FailedCount -gt 0) {
Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed"
}
"`n"
}
Task Build -Depends Test {
$lines
# Load the module, read the exported functions, update the psd1 FunctionsToExport
Set-ModuleFunctions -Name $env:BHPSModuleManifest
# Bump the module version
try {
$Version = Get-NextPSGalleryVersion -Name $env:BHProjectName -ErrorAction Stop
Update-Metadata -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -Value $Version -ErrorAction stop
}
catch {
"Failed to update version for '$env:BHProjectName': $_.`nContinuing with existing version"
}
}
Task Deploy -Depends Build {
$lines
$Params = @{
Path = "$ProjectRoot"
Force = $true
Recurse = $false
}
Invoke-PSDeploy @Verbose @Params
}