-
Notifications
You must be signed in to change notification settings - Fork 24
/
pipeline.build.ps1
252 lines (200 loc) · 7.92 KB
/
pipeline.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
[CmdletBinding()]
param (
[Parameter(Mandatory = $False)]
[String]$Build = '0.0.1',
[Parameter(Mandatory = $False)]
[String]$Configuration = 'Debug',
[Parameter(Mandatory = $False)]
[String]$ApiKey,
[Parameter(Mandatory = $False)]
[Switch]$CodeCoverage = $False,
[Parameter(Mandatory = $False)]
[Switch]$Benchmark = $False,
[Parameter(Mandatory = $False)]
[String]$ArtifactPath = (Join-Path -Path $PWD -ChildPath out/modules),
[Parameter(Mandatory = $False)]
[String]$TestGroup = $Null
)
Write-Host -Object "[Pipeline] -- PowerShell v$($PSVersionTable.PSVersion.ToString())" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- PWD: $PWD" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- ArtifactPath: $ArtifactPath" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- BuildNumber: $($Env:BUILD_BUILDNUMBER)" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- SourceBranch: $($Env:BUILD_SOURCEBRANCH)" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- SourceBranchName: $($Env:BUILD_SOURCEBRANCHNAME)" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- Culture: $((Get-Culture).Name), $((Get-Culture).Parent)" -ForegroundColor Green;
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}
if ($Env:BUILD_SOURCEBRANCH -like '*/tags/*' -and $Env:BUILD_SOURCEBRANCHNAME -like 'v0.*') {
$Build = $Env:BUILD_SOURCEBRANCHNAME.Substring(1);
}
$version = $Build;
$versionSuffix = [String]::Empty;
if ($version -like '*-*') {
[String[]]$versionParts = $version.Split('-', [System.StringSplitOptions]::RemoveEmptyEntries);
$version = $versionParts[0];
if ($versionParts.Length -eq 2) {
$versionSuffix = $versionParts[1];
}
}
Write-Host -Object "[Pipeline] -- Using version: $version" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- Using versionSuffix: $versionSuffix" -ForegroundColor Green;
if ($Env:COVERAGE -eq 'true') {
$CodeCoverage = $True;
}
# Copy the PowerShell modules files to the destination path
function CopyModuleFiles {
param (
[Parameter(Mandatory = $True)]
[String]$Path,
[Parameter(Mandatory = $True)]
[String]$DestinationPath
)
process {
$sourcePath = Resolve-Path -Path $Path;
Get-ChildItem -Path $sourcePath -Recurse -File -Include *.ps1,*.psm1,*.psd1,*.ps1xml | Where-Object -FilterScript {
($_.FullName -notmatch '(\.(cs|csproj)|(\\|\/)(obj|bin))')
} | ForEach-Object -Process {
$filePath = $_.FullName.Replace($sourcePath, $destinationPath);
$parentPath = Split-Path -Path $filePath -Parent;
if (!(Test-Path -Path $parentPath)) {
$Null = New-Item -Path $parentPath -ItemType Directory -Force;
}
Copy-Item -Path $_.FullName -Destination $filePath -Force;
};
}
}
task BuildDotNet {
exec {
dotnet restore
}
exec {
# Build library
dotnet publish src/PSDocs -c $Configuration -f netstandard2.0 -o $(Join-Path -Path $PWD -ChildPath out/modules/PSDocs) -p:version=$Build
}
}
task TestDotNet {
if ($CodeCoverage) {
exec {
# Test library
dotnet test -r (Join-Path $PWD -ChildPath reports/) tests/PSDocs.Tests
}
}
else {
exec {
# Test library
dotnet test tests/PSDocs.Tests
}
}
}
task CopyModule {
CopyModuleFiles -Path src/PSDocs -DestinationPath out/modules/PSDocs;
# Copy third party notices
Copy-Item -Path ThirdPartyNotices.txt -Destination out/modules/PSDocs;
}
# Synopsis: Build modules only
task BuildModule BuildDotNet, CopyModule, VersionModule
# Synopsis: Build help
task BuildHelp BuildModule, Dependencies, {
# Avoid YamlDotNet issue in same app domain
exec {
$pwshPath = (Get-Process -Id $PID).Path;
&$pwshPath -Command {
# Generate MAML and about topics
Import-Module -Name PlatyPS -Verbose:$False;
$Null = New-ExternalHelp -OutputPath 'out/docs/PSDocs' -Path '.\docs\commands\PSDocs\en-US','.\docs\keywords\PSDocs\en-US', '.\docs\concepts\PSDocs\en-US' -Force;
}
}
if (!(Test-Path -Path 'out/docs/PSDocs/PSDocs-help.xml')) {
throw 'Failed find generated cmdlet help.';
}
# Copy generated help into module out path
$Null = Copy-Item -Path out/docs/PSDocs/* -Destination out/modules/PSDocs/en-US;
$Null = Copy-Item -Path out/docs/PSDocs/* -Destination out/modules/PSDocs/en-AU;
$Null = Copy-Item -Path out/docs/PSDocs/* -Destination out/modules/PSDocs/en-GB;
}
task ScaffoldHelp BuildModule, {
Import-Module (Join-Path -Path $PWD -ChildPath out/modules/PSDocs) -Force;
Update-MarkdownHelp -Path '.\docs\commands\PSDocs\en-US';
}
# Synopsis: Remove temp files.
task Clean {
Remove-Item -Path out,reports -Recurse -Force -ErrorAction SilentlyContinue;
}
task VersionModule {
$modulePath = Join-Path -Path $ArtifactPath -ChildPath 'PSDocs';
$manifestPath = Join-Path -Path $modulePath -ChildPath 'PSDocs.psd1';
Write-Verbose -Message "[VersionModule] -- Checking module path: $modulePath";
if (![String]::IsNullOrEmpty($Build)) {
# Update module version
if (![String]::IsNullOrEmpty($version)) {
Write-Verbose -Message "[VersionModule] -- Updating module manifest ModuleVersion";
Update-ModuleManifest -Path $manifestPath -ModuleVersion $version;
}
# Update pre-release version
if (![String]::IsNullOrEmpty($versionSuffix)) {
Write-Verbose -Message "[VersionModule] -- Updating module manifest Prerelease";
Update-ModuleManifest -Path $manifestPath -Prerelease $versionSuffix;
}
}
}
# Synopsis: Install NuGet provider
task NuGet {
if ($Null -eq (Get-PackageProvider -Name NuGet -ErrorAction Ignore)) {
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser;
}
}
task Dependencies NuGet, {
Import-Module $PWD/scripts/dependencies.psm1;
Install-Dependencies -Path $PWD/modules.json -Dev;
}
# Synopsis: Test the module
task TestModule Dependencies, {
Import-Module Pester -RequiredVersion 5.6.1 -Force;
# Define Pester configuration
$pesterConfig = [PesterConfiguration]::Default
$pesterConfig.Run.PassThru = $True
# Enable NUnitXml output
$pesterConfig.TestResult.OutputFormat = "NUnitXml"
$pesterConfig.TestResult.OutputPath = 'reports/pester-unit.xml'
$pesterConfig.TestResult.Enabled = $True
if ($CodeCoverage) {
$pesterConfig.CodeCoverage.OutputFormat = 'JaCoCo'
$pesterConfig.CodeCoverage.OutputPath = 'reports/pester-coverage.xml'
$pesterConfig.CodeCoverage.Path = (Join-Path -Path $PWD -ChildPath 'out/modules/**/*.psm1')
}
if (!(Test-Path -Path reports)) {
$Null = New-Item -Path reports -ItemType Directory -Force;
}
if ($Null -ne $TestGroup) {
$pesterConfig.Filter.Tag = $TestGroup
}
# Run Pester tests
$results = Invoke-Pester -Configuration $pesterConfig
# Throw an error if Pester tests failed
if ($Null -eq $results) {
throw 'Failed to get Pester test results.'
}
elseif ($results.Result.FailedCount -gt 0) {
throw "$($results.Result.FailedCount) tests failed."
}
}
task Benchmark {
if ($Benchmark -or $BuildTask -eq 'Benchmark') {
dotnet run --project src/PSDocs.Benchmark -f net7.0 -c Release -- benchmark --output $PWD;
}
}
# Synopsis: Run script analyzer
task Analyze Build, Dependencies, {
Invoke-ScriptAnalyzer -Path out/modules/PSDocs;
}
# Synopsis: Remove temp files.
task Clean {
Remove-Item -Path out,reports -Recurse -Force -ErrorAction SilentlyContinue;
}
task Build Clean, BuildModule, VersionModule, BuildHelp
task Test Build, TestDotNet, TestModule
# Synopsis: Build and test. Entry point for CI Build stage
task . Build, TestDotNet