-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.ps1.old
523 lines (444 loc) · 20.2 KB
/
build.ps1.old
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
[CmdletBinding()]
param(
)
Set-StrictMode -Version Latest
task Clean CleanRelease, CleanOutput, CleanDocs
# Default configuration
# Override this using $BuildOptions hashtable in your .<modulename>.build.ps1
$BuildDefault = @{
ModuleName = '' # You NEED to override this!
RequiredModules = "Pester", "PSScriptAnalyzer", "PSCodeHealth", "Configuration", "platyPS" # If you override this, make sure to include these!
SourcePath = Join-Path -Path $BuildRoot -ChildPath 'source'
ReleasePath = Join-Path -Path $BuildRoot -ChildPath 'release'
BuildPath = Join-Path -Path $BuildRoot -ChildPath 'build'
TestPath = Join-Path -Path $BuildRoot -ChildPath 'tests'
DocDir = 'docs'
DocPath = Join-Path -Path $BuildRoot -ChildPath 'docs'
OutputPath = Join-Path -Path $BuildRoot -ChildPath 'output'
ModuleFiles = '' # other files and folders to copy the build folder
MDConvert = '' # markdown files to convert to HTML
PSSAOutputPath = Join-Path -Path $BuildRoot -ChildPath 'output\psscriptanalyzer.csv'
PSSASeverity = 'Error', 'Warning' # Can be 'Error', 'Warning' and / or 'Information'
PesterOutputPath = Join-Path -Path $BuildRoot -ChildPath 'output\pester-output.xml'
CodeCoverageOutputPath = Join-Path -Path $BuildRoot -ChildPath 'output\codecoverage.csv'
CodeCoverageThreshold = 0.8 # 80% - 0 to disable
ModuleHeader = ''
ModuleHFooter = ''
FunctionHeader = ''
FunctionFooter = "`n"
}
Enter-Build {
# build the config
$BuildConfig = $BuildDefault.Clone()
$BuildOptions.Keys | ForEach-Object {
$BuildConfig.$_ = $BuildOptions.$_
}
# check we have some paths we need
if (! (Test-Path -Path $BuildConfig.SourcePath)) {
throw "Cannot find source path '$($BuildConfig.SourcePath)'."
}
# create paths needed
$BuildConfig.ReleasePath, $BuildConfig.TestPath, $BuildConfig.DocPath, $BuildConfig.OutputPath | ForEach-Object {
if (! (Test-Path -Path $_)) {
$null = New-Item -Path $_ -ItemType Directory
}
}
}
task InstallDependencies {
$BuildConfig.RequiredModules | ForEach-Object {
if (!(Get-Module -Name $_ -ListAvailable)) {
Install-Module $_ -Force -Scope CurrentUser
}
Import-Module -Name $_ -Force
}
# Check if Chocolatey is installed
if (! [bool](Get-Command -Name 'choco' -ErrorAction SilentlyContinue)) {
try {
Write-Verbose 'Chocolatey not installed. Installing.'
# taken from https://chocolatey.org/install
Set-ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}
catch {
throw 'Could not install Chocolatey.'
}
}
else {
Write-Verbose "Chocolatey already installed."
}
# Chocolatey is installed
$packageInstalled = $false
@(
@{
package = 'pandoc'
assert = [scriptblock] { [bool](Get-Command -Name 'pandoc') }
},
@{
package = '7Zip'
assert = [scriptblock] { [bool](Get-Command -Name '7zfm') }
}
) | ForEach-Object {
# check the package is NOT installed already
if (!$_.assert) {
Write-Verbose "Installing '$($_.package)' package."
choco install $_.package -y
if ($LASTEXITCODE -ne 0) {
throw "Failed to install Chocolatey package '$_'."
}
$packageInstalled = $true
}
else {
Write-Verbose "Chocolatey package '$($_.package)' already installed. Skipping."
}
}
if ($packageInstalled) {
Write-Verbose 'Refreshing the PATH'
refreshenv
}
}
# Synopsis: Remove contents of the release folder
task CleanRelease {
$null = Remove-Item -Path $BuildConfig.ReleasePath -Force -Recurse -ErrorAction SilentlyContinue
$null = New-Item -Path $BuildCOnfig.ReleasePath -ItemType Directory
}
task CleanOutput {
$null = Remove-Item -Path $BuildConfig.OutputPath -Force -Recurse -ErrorAction SilentlyContinue
$null = New-Item -Path $BuildConfig.OutputPath -ItemType Directory
}
task CleanDocs {
$null = Remove-Item -Path $BuildConfig.DocPath -Force -Recurse -ErrorAction SilentlyContinue
$null = New-Item -Path $BuildConfig.DocPath -ItemType Directory
}
# Synopsis: Cleans the module from all PowerShell module paths
task CleanModule {
Get-Module $BuildConfig.ModuleName -ListAvailable | ForEach-Object {
Remove-Module $_.Path -ErrorAction SilentlyContinue
Remove-Item -Path (Split-Path -Path $_.Path -Parent) -Force -Recurse
}
}
# Synopsis: Warn about not empty git status if .git exists.
task GitStatus -If (Test-Path .git) {
$status = exec { git status -s }
if ($status) {
Write-Warning "Git status: $($status -join ', ')"
}
}
# Synopsis: Build the PowerShell help file.
# <https://github.com/nightroman/Helps>
task Help {
. Helps.ps1
Convert-Helps Invoke-Build-Help.ps1 Invoke-Build-Help.xml
}
# Synopsis: Set $script:Version.
task Version {
# get the version from Release-Notes
$script:Version = . { switch -Regex -File Changelog.md {'##\s+v(\d+\.\d+\.\d+)' {return $Matches[1]}} }
assert ($Version)
}
# Synopsis: Convert markdown files to HTML.
# <http://johnmacfarlane.net/pandoc/>
task MakeHTMLDocs -If { [bool](Get-Command -Name 'pandoc') } {
ForEach ($name in $BuildConfig.MDConvert) {
$sourcePath = Join-Path -Path $BuildRoot -ChildPath $name
if (Test-Path $sourcePath) {
$baseName = (Get-Item -Path $sourcePath).BaseName
$destPath = Join-Path -Path $BuildConfig.ReleasePath -ChildPath "$baseName.html"
exec { pandoc.exe --standalone --from=markdown_strict --metadata=title:$name --output=$destPath $sourcePath }
Write-Verbose "Converted markdown file '$name' to '$destPath'"
} # end if
} # end foreach
}
task UpdateModuleHelp -If (Get-Module platyPS -ListAvailable) CleanDocs, {
try {
$modulePath = Join-Path -Path $BuildConfig.ReleasePath -ChildPath "$($BuildConfig.ModuleName).psm1"
$moduleInfo = Import-Module -FullyQualifiedName $modulePath -ErrorAction Stop -PassThru -Force
if ($moduleInfo.ExportedFunctions.Count -gt 0) {
$moduleInfo.ExportedFunctions.Keys | ForEach-Object {
if ($ManifestOptions.ContainsKey('ProjectUri')) {
$onlineUrl = $ManifestOptions.ProjectUri
if (-not $onlineUrl.EndsWith('/')) {
$onlineUrl += '/'
}
$onlineUrl += "blob/master/$($BuildConfig.DocDir)/$_.md"
}
else {
$onlineUrl = ''
}
$params = @{
Command = $_
OutputFolder = $BuildConfig.DocPath
OnlineVersionUrl = $onlineUrl
AlphabeticParamsOrder = $true
Force = $true
}
New-MarkdownHelp @params | Out-Null
}
New-ExternalHelp -Path $BuildConfig.DocPath `
-OutputPath (Join-Path -Path $BuildConfig.ReleasePath -ChildPath 'en-US') -Force | Out-Null
}
Remove-Module -Name $BuildConfig.ModuleName -Force
}
catch {
throw
}
}
# Synopsis: Make the build folder.
task Build InstallDependencies, CleanRelease, TestFunctionSyntax, TestFunctionAttributeSyntax, BuildManifest, BuildScriptModule, {
# copy files
$BuildConfig.ModuleFiles | ForEach-Object {
Copy-Item -Path (Join-Path -Path $BuildRoot -ChildPath $_) `
-Destination $BuildConfig.ReleasePath -Recurse
Write-Verbose "Copied $_ to build directory '$($BuildConfig.ReleasePath)'"
}
}, MakeHTMLDocs
# Synopsis: Builds the module manifest
task BuildManifest Version, BuildScriptModule, {
$releaseManifestPath = Join-Path -Path $BuildConfig.ReleasePath -ChildPath "$($BuildConfig.ModuleName).psd1"
$sourceManifestPath = Join-Path -Path $BuildConfig.SourcePath -ChildPath "$($BuildConfig.ModuleName).psd1"
# copy existing manifest
Copy-Item -Path $sourceManifestPath -Destination $releaseManifestPath -ErrorAction Stop
# Update the copied manifest
if (Test-Path -Path variable:ManifestOptions) {
ForEach ($key in $ManifestOptions.Keys) {
Update-Metadata -Path $releaseManifestPath -PropertyName $key -Value $ManifestOptions.$key -ErrorAction Stop
}
}
}, TestModule
task BuildScriptModule {
# build the psm1 module file with all of the scripts
$modulePath = Join-Path -Path $BuildConfig.ReleasePath -ChildPath "$($BuildConfig.ModuleName).psm1"
Remove-Item $modulePath -Force -ErrorAction SilentlyContinue
if ($BuildConfig.ContainsKey('ModuleHeader') -and !([string]::IsNullOrEmpty($BuildConfig.ModuleHeader))) {
Add-Content -Path $modulePath -Value $BuildConfig.ModuleHeader
Write-Verbose "Added ModuleHeader contents to script module '$modulePath'."
}
# get a list of all scipts in the public and private directories and subdirectories
$functions = Get-ChildItem (Join-Path -Path $BuildConfig.SourcePath -ChildPath "public\*.ps1") -Recurse #).FullName) #| ForEach-Object { "public\$_" }
$functions += Get-ChildItem (Join-Path -Path $BuildConfig.SourcePath -ChildPath "private\*.ps1") -Recurse -ErrorAction SilentlyContinue #.FullName) #| ForEach-Object { "private\$_" }
Foreach ($function in $functions) {
if ($BuildConfig.ContainsKey('FunctionHeader') -and !([string]::IsNullOrEmpty($BuildConfig.FunctionHeader))) {
Add-Content -Path $modulePath -Value $BuildConfig.FunctionHeader
}
Get-Content -Path $function | Add-Content -Path $modulePath
if ($BuildConfig.ContainsKey('FunctionFooter') -and !([string]::IsNullOrEmpty($BuildConfig.FunctionFooter))) {
Add-Content -Path $modulePath -Value $BuildConfig.FunctionFooter
}
Write-Verbose "Added $($function.name) to script module."
}
if ($BuildConfig.ContainsKey('ModuleFooter') -and !([string]::IsNullOrEmpty($BuildConfig.ModuleFooter))) {
Add-Content -Path $modulePath -Value $BuildConfig.ModuleFooter
Write-Verbose "Added ModuleFooter contents to script module '$modulePath'."
}
}, TestScriptModule, UpdateModuleHelp
# Synopsis: Push with a version tag.
task PushRelease Version, {
$changes = exec { git status --short }
assert (!$changes) "Please, commit changes."
exec { git push }
exec { git tag -a "v$Version" -m "v$Version" }
exec { git push origin "v$Version" }
}
task PushPSGallery Test, CodeAnalysis, CleanModule, Build, {
if (-not $BuildConfig.PSGalleryApiKey) {
Write-Error "You need to set the environment variable PSGALLERY_API_KEY to the PowerShell Gallery API Key"
}
# exec {$null = robocopy.exe $($BuildConfig.ReleasePath) "$($BuildConfig.ModuleLoadPath)\$($BuildConfig.ModuleName)" /mir} (0..2)
# Write-Verbose "Copied $($BuildConfig.ReleasePath) to $($BuildConfig.ModuleLoadPath)\$($BuildConfig.ModuleName)"
#Import-Module "$($BuildConfig.ReleasePath)\$($BuildConfig.ModuleName).psd1"
Publish-Module -Name $BuildConfig.ModuleName -NuGetApiKey $BuildConfig.PSGalleryApiKey
}, CleanModule, CleanRelease
# Synopsis: Test and check expected output.
# Requires PowerShelf/Assert-SameFile.ps1
task Test3 {
# invoke tests, get output and result
$output = Invoke-Build . Tests\.build.ps1 -Result result -Summary | Out-String -Width:200
if ($NoTestDiff) {return}
# process and save the output
$resultPath = "$BuildRoot\Invoke-Build-Test.log"
$samplePath = "$HOME\data\Invoke-Build-Test.$($PSVersionTable.PSVersion.Major).log"
$output = $output -replace '\d\d:\d\d:\d\d(?:\.\d+)?( )? *', '00:00:00.0000000$1'
[System.IO.File]::WriteAllText($resultPath, $output, [System.Text.Encoding]::UTF8)
# compare outputs
Assert-SameFile $samplePath $resultPath $env:MERGE
Remove-Item $resultPath
}
# Synopsis: Test with PowerShell v2.
task Test2 {
$diff = if ($NoTestDiff) {'-NoTestDiff'}
exec {powershell.exe -Version 2 -NoProfile -Command Invoke-Build Test3 $diff}
}
# Synopsis: Test with PowerShell v6.
task Test6 -If $env:powershell6 {
$diff = if ($NoTestDiff) {'-NoTestDiff'}
exec {& $env:powershell6 -NoProfile -Command Invoke-Build Test3 $diff}
}
task TestModule {
$pesterParams = @{
EnableExit = $false;
PassThru = $true;
Strict = $true;
Show = "Failed"
}
# will throw an error and stop the build if errors
Test-ModuleManifest -Path (Join-Path -Path $BuildConfig.ReleasePath -ChildPath "$($BuildConfig.ModuleName).psd1") -ErrorAction Stop | Out-Null
# remove the module before we test it
#Remove-Module $BuildConfig.ModuleName -Force -ErrorAction SilentlyContinue
#$results = Invoke-Pester @pesterParams
#$fails = @($results).FailedCount
#assert($fails -eq 0) ('Failed "{0}" unit tests.' -f $fails)
}
task TestScriptModule {
$path = Join-Path $BuildConfig.ReleasePath -ChildPath "$($BuildConfig.ModuleName).psm1"
Import-Module -Name $path -ErrorAction Stop -PassThru | Remove-Module
}
# https://github.com/indented-automation/Indented.Build
task TestFunctionSyntax {
$hasSyntaxErrors = $false
Get-ChildItem -Path $BuildConfig.SourcePath -Include '*.ps1' -Recurse | ForEach-Object {
Write-Verbose "Checking source code syntax on '$($_.name)'"
$tokens = $null
[System.Management.Automation.Language.ParseError[]]$parseErrors = @()
$null = [System.Management.Automation.Language.Parser]::ParseInput(
(Get-Content $_.FullName -Raw),
$_.FullName,
[Ref]$tokens,
[Ref]$parseErrors
)
if ($parseErrors.Count -gt 0) {
$parseErrors | Write-Error
$hasSyntaxErrors = $true
}
}
if ($hasSyntaxErrors) {
throw 'TestFunctionSyntax failed'
}
else {
Write-Verbose "No syntax errors"
}
}
# https://github.com/indented-automation/Indented.Build
task TestFunctionAttributeSyntax {
$hasSyntaxErrors = $false
Get-ChildItem -Path $BuildConfig.SourcePath -Include '*.ps1' -Recurse | ForEach-Object {
Write-Verbose "Checking source code attribute syntax on '$($_.name)'"
$tokens = $null
[System.Management.Automation.Language.ParseError[]]$parseErrors = @()
$ast = [System.Management.Automation.Language.Parser]::ParseInput(
(Get-Content $_.FullName -Raw),
$_.FullName,
[Ref]$tokens,
[Ref]$parseErrors
)
# Test attribute syntax
$attributes = $ast.FindAll( {
param( $ast )
$ast -is [System.Management.Automation.Language.AttributeAst]
},
$true
)
foreach ($attribute in $attributes) {
if (($type = $attribute.TypeName.FullName -as [Type]) -or ($type = ('{0}Attribute' -f $attribute.TypeName.FullName) -as [Type])) {
$propertyNames = $type.GetProperties().Name
if ($attribute.NamedArguments.Count -gt 0) {
foreach ($argument in $attribute.NamedArguments) {
if ($argument.ArgumentName -notin $propertyNames) {
'Invalid property name in attribute declaration: {0}: {1} at line {2}, character {3}' -f
$_.Name,
$argument.ArgumentName,
$argument.Extent.StartLineNumber,
$argument.Extent.StartColumnNumber
$hasSyntaxErrors = $true
}
}
}
}
else {
'Invalid attribute declaration: {0}: {1} at line {2}, character {3}' -f
$_.Name,
$attribute.TypeName.FullName,
$attribute.Extent.StartLineNumber,
$attribute.Extent.StartColumnNumber
$hasSyntaxErrors = $true
}
}
}
if ($hasSyntaxErrors) {
throw 'TestFunctionAttributeSyntax failed'
}
else {
Write-Verbose "No attribute syntax errors"
}
}
task PSScriptAnalyzer -If (Get-Module PSScriptAnalyzer -ListAvailable) {
$splat = @{
Path = $BuildConfig.ReleasePath
Severity = $BuildConfig.PSSASeverity
Recurse = $true
Verbose = $VerbosePreference
}
Write-Verbose "Running PSScriptAnalyzer default rules on '$($splat.Path)'."
Invoke-ScriptAnalyzer @splat | ForEach-Object {
$_
$_ | Export-Csv $BuildConfig.PSSAOutputPath -NoTypeInformation -Append
}
}
task Pester -If { (Get-Module PSScriptAnalyzer -ListAvailable) -and (Get-ChildItem -Path $BuildConfig.TestPath -Filter '*.tests.ps1' -Recurse -File) } {
Import-Module -Name (Join-Path -Path $BuildConfig.ReleasePath -ChildPath "$($BuildConfig.ModuleName).psd1")`
-Global -ErrorAction Stop -Force
$params = @{
Script = $BuildConfig.TestPath
CodeCoverage = Join-Path -Path $BuildConfig.ReleasePath -ChildPath "$($BuildConfig.ModuleName).psm1"
OutputFile = Join-Path -Path $BuildConfig.OutputPath -ChildPath "$($BuildConfig.ModuleName)-nunit.xml"
PassThru = $true
Show = if ($VerbosePreference -eq 'SilentlyContinue') { 'None' } else { 'all' }
Strict = $true
}
$pester = Invoke-Pester @params -Verbose
$pester | Export-CliXml $BuildConfig.PesterOutputPath
}
task ValidateTestResults PSScriptAnalyzer, Pester, {
$testsFailed = $false
# PSScriptAnalyzer
if ((Test-Path -Path $BuildConfig.PSSAOutputPath) -and ($testResults = Import-Csv -Path $BuildConfig.PSSAOutputPath)) {
'{0} warnings were raised by PSScriptAnalyzer' -f @($testResults).Count
$testsFailed = $true
}
else {
Write-Verbose '0 warnings were raised by PSScriptAnalyzer'
}
# Pester tests
if (Test-Path -Path $BuildConfig.PesterOutputPath) {
$pester = Import-CliXml -Path $BuildConfig.PesterOutputPath
if ($pester.FailedCount -gt 0) {
'{0} of {1} Pester tests are failing' -f $pester.FailedCount, $pester.TotalCount
$testsFailed = $true
}
else {
Write-Verbose 'All Pester tests passed.'
}
# Pester code coverage
[Double]$codeCoverage = $pester.CodeCoverage.NumberOfCommandsExecuted / $pester.CodeCoverage.NumberOfCommandsAnalyzed
$pester.CodeCoverage.MissedCommands | `
Export-Csv -Path $BuildConfig.CodeCoverageOutputPath -NoTypeInformation
if ($codecoverage -lt $BuildConfig.CodeCoverageThreshold) {
'Pester code coverage ({0:P}) is below threshold {1:P}.' -f $codeCoverage, $BuildConfig.CodeCoverageThreshold
$testsFailed = $true
}
}
else {
Write-Warning 'Pester tests not run.'
}
if ($testsFailed) {
throw 'Test result validation failed'
}
}
task CreateCodeHealthReport -If (Get-Module PSCodeHealth -ListAvailable) {
Import-Module -FullyQualifiedName $BuildInfo.BuildManifestPath -Global -ErrorAction Stop
$params = @{
Path = $BuildInfo.BuildModulePath
Recurse = $true
TestsPath = $BuildInfo.TestPath
HtmlReportPath = Join-Path -Path $BuildInfo.OutputPath -ChildPath "$($Buildinfo.ModuleName)-code-health.html"
}
Invoke-PSCodeHealth @params
}