forked from MethodsAndPractices/vsteam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build-Module.ps1
252 lines (201 loc) · 8.47 KB
/
Build-Module.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
[CmdletBinding(DefaultParameterSetName = "All")]
param(
#output path of the build module
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[string]$outputDir = './dist',
# Building help is skipped by default to speed your inner loop.
# Use this flag to include building the help
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$buildHelp,
# By default the build will not install dependencies
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$installDep,
# built module will be imported into session
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$ipmo,
# run the scripts with the PS script analyzer
[Parameter(ParameterSetName = "All")]
[Parameter(ParameterSetName = "UnitTest")]
[switch]$analyzeScript,
# runs the unit tests
[Parameter(ParameterSetName = "UnitTest", Mandatory = $true)]
[Parameter(ParameterSetName = "All")]
[switch]$runTests,
# can be used to filter the unit test parts that should be run
# see also: https://github.com/pester/Pester/wiki/Invoke%E2%80%90Pester#testname-alias-name
[Parameter(ParameterSetName = "UnitTest")]
[string]$testName,
# outputs the code coverage
[Parameter(ParameterSetName = "UnitTest")]
[switch]$codeCoverage,
# runs the integration tests
[Parameter(ParameterSetName = "UnitTest")]
[Parameter(ParameterSetName = "All")]
[switch]$runIntegrationTests,
[Parameter(ParameterSetName = "UnitTest")]
[Parameter(ParameterSetName = "All")]
[switch]$skipLibBuild,
[ValidateSet('LibOnly', 'Debug', 'Release')]
[string]$configuration = "LibOnly",
[ValidateSet('Diagnostic', 'Detailed', 'Normal', 'Minimal', 'None', 'ErrorsOnly')]
[string]$testOutputLevel = "ErrorsOnly",
[switch]$ci
)
function Import-Pester {
if ($null -eq $(Get-Module -ListAvailable Pester | Where-Object Version -like '5.*')) {
Write-Output "Installing Pester 5"
Install-Module -Name Pester -Repository PSGallery -Force -AllowPrerelease -MinimumVersion '5.0.2' -Scope CurrentUser -AllowClobber -SkipPublisherCheck
}
# This loads [PesterConfiguration] into scope
Import-Module Pester -MinimumVersion 5.0.0
}
function Start-IntegrationTests {
[CmdletBinding(DefaultParameterSetName = "All", SupportsShouldProcess, ConfirmImpact = "High")]
param()
process {
Write-Output ' Testing: Functions (integration)'
if (-not $(Test-Path -Path './Tests/TestResults')) {
New-Item -Path './Tests/TestResults' -ItemType Directory | Out-Null
}
Import-Pester
$pesterArgs = [PesterConfiguration]::Default
$pesterArgs.Run.Path = './Tests/integration'
$pesterArgs.Run.Exit = $true
$pesterArgs.TestResult.Enabled = $true
$pesterArgs.TestResult.OutputPath = './Tests/TestResults/integrationTest-results.xml'
$pesterArgs.Run.PassThru = $false
if ('ErrorsOnly' -eq $testOutputLevel) {
$pesterArgs.Output.Verbosity = 'none'
$pesterArgs.Run.PassThru = $true
$intTestResults = Invoke-Pester -Configuration $pesterArgs
$intTestResults.Failed | Select-Object -ExpandProperty ErrorRecord
}
else {
$pesterArgs.Output.Verbosity = $testOutputLevel
Invoke-Pester -Configuration $pesterArgs
}
}
}
. ./Merge-File.ps1
if ($installDep.IsPresent -or $analyzeScript.IsPresent) {
# Load the psd1 file so you can read the required modules and install them
$manifest = Import-PowerShellDataFile .\Source\VSTeam.psd1
# Install each module
if ($manifest.RequiredModules) {
$manifest.RequiredModules | ForEach-Object { if (-not (get-module $_ -ListAvailable)) { Write-Host "Installing $_"; Install-Module -SkipPublisherCheck -Name $_ -Repository PSGallery -F -Scope CurrentUser } }
}
}
if ([System.IO.Path]::IsPathRooted($outputDir)) {
$output = $outputDir
}
else {
$output = Join-Path (Get-Location) $outputDir
}
$output = [System.IO.Path]::GetFullPath($output)
Merge-File -inputFile ./config.json -outputDir $output
# Build the help
if ($buildHelp.IsPresent) {
Write-Output 'Processing: External help file'
Push-Location
Set-Location ./.docs
Try {
./gen-help.ps1
}
Finally {
Pop-Location
}
}
Write-Output 'Publishing: About help files'
Copy-Item -Path ./Source/en-US -Destination "$output/" -Recurse -Force
Write-Output 'Publishing: Manifest file'
Copy-Item -Path ./Source/VSTeam.psm1 -Destination "$output/VSTeam.psm1" -Force
Write-Output ' Updating: Functions To Export'
$newValue = ((Get-ChildItem -Path "./Source/Public" -Filter '*.ps1').BaseName |
ForEach-Object -Process { Write-Output "'$_'" }) -join ','
(Get-Content "./Source/VSTeam.psd1") -Replace ("FunctionsToExport.+", "FunctionsToExport = ($newValue)") | Set-Content "$output/VSTeam.psd1"
if (-not $skipLibBuild.IsPresent) {
Write-Output " Building: C# project ($configuration config)"
if (-not $(Test-Path -Path $output\bin)) {
New-Item -Path $output\bin -ItemType Directory | Out-Null
}
# Once build machines were updated to .NET 5.0 builds started failing.
$buildOutput = dotnet restore --no-cache --ignore-failed-sources --force --force-evaluate | Out-String
$buildOutput += dotnet build --nologo --configuration $configuration | Out-String
if (-not ($buildOutput | Select-String -Pattern 'succeeded') -or $ci.IsPresent) {
Write-Output $buildOutput
}
Copy-Item -Destination "$output\bin\vsteam-lib.dll" -Path ".\Source\Classes\bin\$configuration\netstandard2.0\vsteam-lib.dll" -Force
Copy-Item -Destination "$output\bin\Trackyon.System.Management.Automation.Abstractions.dll" -Path ".\Source\Classes\bin\$configuration\netstandard2.0\Trackyon.System.Management.Automation.Abstractions.dll" -Force
}
Write-Output "Publishing: Complete to $output"
# run the unit tests with Pester
if ($runTests.IsPresent) {
if (-not $skipLibBuild.IsPresent -and $configuration -ne 'LibOnly') {
Write-Output ' Testing: C# project (unit)'
$testOutput = dotnet test --nologo --configuration $configuration | Out-String
if (-not ($testOutput | Select-String -Pattern 'Test Run Successful')) {
Write-Output $testOutput
}
}
Write-Output ' Testing: Functions (unit)'
if (-not $(Test-Path -Path './Tests/TestResults')) {
New-Item -Path './Tests/TestResults' -ItemType Directory | Out-Null
}
Import-Pester
$pesterArgs = [PesterConfiguration]::Default
$pesterArgs.Run.Path = './Tests/function'
$pesterArgs.TestResult.Enabled = $true
$pesterArgs.TestResult.OutputPath = './Tests/TestResults/test-results.xml'
if ($codeCoverage.IsPresent) {
$pesterArgs.CodeCoverage.Enabled = $true
$pesterArgs.CodeCoverage.OutputFormat = 'JaCoCo'
$pesterArgs.CodeCoverage.OutputPath = "coverage.xml"
$pesterArgs.CodeCoverage.Path = "./Source/**/*.ps1"
}
else {
$pesterArgs.Run.PassThru = $false
}
if ($testName) {
$pesterArgs.Filter.FullName = $testName
}
if ('ErrorsOnly' -eq $testOutputLevel) {
$pesterArgs.Output.Verbosity = 'none'
$pesterArgs.Run.PassThru = $true
$unitTestResults = Invoke-Pester -Configuration $pesterArgs
$unitTestResults.Failed | Select-Object -ExpandProperty ErrorRecord
}
else {
$pesterArgs.Output.Verbosity = $testOutputLevel
Invoke-Pester -Configuration $pesterArgs
}
}
# reload the just built module
if ($ipmo.IsPresent -or $analyzeScript.IsPresent -or $runIntegrationTests.IsPresent) {
# module needs to be unloaded if present
if ((Get-Module VSTeam)) {
Remove-Module VSTeam
}
Write-Host " Importing: Module"
Import-Module "$output/VSTeam.psd1" -Force
Set-VSTeamAlias
}
# Run this last so the results can be seen even if tests were also run
# if not the results scroll off and my not be in the buffer.
# run PSScriptAnalyzer
if ($analyzeScript.IsPresent) {
Write-Output "Starting static code analysis..."
if ($null -eq $(Get-Module -Name PSScriptAnalyzer)) {
Install-Module -Name PSScriptAnalyzer -Repository PSGallery -Force -Scope CurrentUser
}
$r = Invoke-ScriptAnalyzer -Path $output -Recurse
$r | ForEach-Object { Write-Host "##vso[task.logissue type=$($_.Severity);sourcepath=$($_.ScriptPath);linenumber=$($_.Line);columnnumber=$($_.Column);]$($_.Message)" }
Write-Output "Static code analysis complete."
}
# run integration tests with Pester
if ($runIntegrationTests.IsPresent) {
Start-IntegrationTests
}