-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInvoke-Task.ps1
673 lines (673 loc) · 25 KB
/
Invoke-Task.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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#Requires -Modules BuildHelpers,pester,PSScriptAnalyzer
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('AdvancedFunctionHelpContent', '')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('RequireDirective', '')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
[CmdletBinding()]
Param(
[Switch] $Lint,
[Switch] $Test,
[Switch] $Plus,
[Switch] $Mutate,
[ValidateSet('Geodetic', 'Graph', 'Matrix')]
[String] $Project,
[Switch] $Detailed,
[Switch] $WithCoverage,
[Switch] $GenerateCoverageReport,
[Switch] $Show,
[Switch] $CI,
[Switch] $Build,
[Switch] $BuildOnly,
[ValidateSet('2022', '2019')]
[String] $Version = '2022',
[String] $Offering = 'Community',
[ValidateSet('x64', 'x86')]
[String] $Architecture = 'x64',
[Switch] $Check,
[Switch] $Publish,
[Switch] $Major,
[Switch] $Minor,
[Switch] $Benchmark,
[ValidateSet('windows', 'linux')]
[String] $Platform = 'windows',
[ValidateSet('dotnet', 'powershell')]
[String[]] $Skip,
[String] $Filter,
[ValidateSet('Local', 'Remote', 'WindowsOnly', 'LinuxOnly')]
[String[]] $Tags,
[ValidateSet('Local', 'Remote', 'WindowsOnly', 'LinuxOnly')]
[String] $Exclude = '',
[Switch] $DryRun,
[Switch] $Help
)
$Prefix = if ($DryRun) { '[DRYRUN] ' } else { '' }
$SourceDirectory = Join-Path 'Prelude' 'src'
$VisualStudioData = @{
Version = $Version
Offering = $Offering
Architecture = $Architecture
}
if ([String]::IsNullOrEmpty($Exclude)) {
switch ($Platform) {
'linux' {
$Exclude = 'WindowsOnly'
}
Default {
$Exclude = 'LinuxOnly'
}
}
}
function Write-Message {
Param(
[Parameter(Position = 0, ValueFromPipeline = $True)]
[String] $Text,
[Switch] $Success,
[Switch] $Fail
)
if ($Success -and (-not $Fail)) {
'[+] ' | Write-Host -ForegroundColor Green -NoNewline
$Text | Write-Host
} elseif ($Fail) {
'[-] ' | Write-Host -ForegroundColor Red -NoNewline
$Text | Write-Host
} else {
$Text | Write-Host -ForegroundColor DarkGray
}
}
function Write-Result {
[CmdletBinding()]
Param(
[Parameter(Position = 0)]
[ValidateSet('done', 'pass', 'fail')]
[String] $Result = 'done'
)
$Color = switch ($Result) {
'done' { 'Gray' }
'pass' { 'Green' }
'fail' { 'Red' }
}
$Message = switch ($Result) {
pass {
@(
'██████╗ █████╗ ███████╗███████╗'
'██╔══██╗██╔══██╗██╔════╝██╔════╝'
'██████╔╝███████║███████╗███████╗'
'██╔═══╝ ██╔══██║╚════██║╚════██║'
'██║ ██║ ██║███████║███████║'
'╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝'
)
}
fail {
@(
' █████▒▄▄▄ ██▓ ██▓ '
'▓██ ▒▒████▄ ▓██▒▓██▒ '
'▒████ ░▒██ ▀█▄ ▒██▒▒██░ '
'░▓█▒ ░░██▄▄▄▄██ ░██░▒██░ '
'░▒█░ ▓█ ▓██▒░██░░██████▒'
'▒ ░ ▒▒ ▓▒█░░▓ ░ ▒░▓ ░ '
'░ ▒ ▒▒ ░ ▒ ░░ ░ ▒ ░ '
'░ ░ ░ ▒ ▒ ░ ░ ░ '
' ░ ░ ░ ░ ░ '
)
}
done {
@(
'██████ ██████ ███ ██ ███████ '
'██ ██ ██ ██ ████ ██ ██ '
'██ ██ ██ ██ ██ ██ ██ █████ '
'██ ██ ██ ██ ██ ██ ██ ██ '
'██████ ██████ ██ ████ ███████ '
)
}
}
'' | Write-Host
$Message | ForEach-Object {
$_ | Write-Host -ForegroundColor $Color
}
'' | Write-Host
}
function Get-TaskList {
[CmdletBinding()]
Param()
enum Task {
help
benchmark
check
lint
test
mutate
build
publish
}
$Tasks = @()
if ($Help) {
$Tasks += [Task]'help'
}
if ($Benchmark) {
$Tasks += [Task]'benchmark'
}
if ($Check) {
$Tasks += [Task]'check'
}
if ($Lint) {
$Tasks += [Task]'lint'
}
if ($Test) {
$Tasks += [Task]'test'
}
if ($Mutate) {
$Tasks += [Task]'mutate'
}
if ($Build) {
$Tasks += [Task]'build'
}
if ($Publish) {
$Tasks += [Task]'publish'
}
$Tasks
}
function Get-VisualStudioRoot {
[CmdletBinding()]
[OutputType([String])]
Param(
[ValidateSet('2022', '2019')]
[String] $Version = '2022',
[String] $Offering = 'Community',
[ValidateSet('x64', 'x86')]
[String] $Architecture = 'x64'
)
$ProgramFilesPostfix = if ($Architecture -eq 'x86') { ' (x86)' } else { '' }
"C:\Program Files${ProgramFilesPostfix}\Microsoft Visual Studio\${Version}\${Offering}"
}
function Invoke-Benchmark {
<#
.SYNOPSIS
Run C# benchmarks using BenchmarkDotNet
.EXAMPLE
.\Invoke-Task.ps1 -Benchmark
.NOTES
When -Benchmark parameter is used, no other tasks will be executed.
#>
[CmdletBinding()]
Param()
'==> [INFO] Running C# Benchmarks' | Write-Message
$ProjectPath = "${PSScriptRoot}/csharp/Performance/Performance.csproj"
dotnet run --project $ProjectPath --configuration 'Release'
}
function Invoke-Build {
<#
.SYNOPSIS
Format C# code, run C# tests, and build link libraries from C# code
.PARAMETER BuildOnly
Skip formatting code and running tests
.EXAMPLE
.\Invoke-Task.ps1 -Build
.NOTES
Build link libraries are saved to ./Prelude/bin directory
#>
[CmdletBinding()]
Param(
[ValidateSet('2022', '2019')]
[String] $Version = '2022',
[String] $Offering = 'Community',
[ValidateSet('x64', 'x86')]
[String] $Architecture = 'x64'
)
$VisualStudioData = @{
Version = $Version
Offering = $Offering
Architecture = $Architecture
}
$GAC_MSIL = 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL'
$VisualStudioRoot = Get-VisualStudioRoot @VisualStudioData
$ToolsDirectory = "${VisualStudioRoot}\Common7\Tools"
$CompilerPath = "${VisualStudioRoot}\MSBuild\Current\Bin\Roslyn\csc.exe"
if ((Test-Path $ToolsDirectory)) {
'==> [INFO] Setting environment variables' | Write-Message
& (Join-Path $ToolsDirectory 'VsDevCmd.bat') -no_logo
} else {
'==> [ERROR] Could not find VsDevCmd.bat which is needed to set environment variables' | Write-Error
}
if ((Test-Path $CompilerPath)) {
$CsharpDirectory = "${PSScriptRoot}\csharp"
$OutputDirectory = "${PSScriptRoot}\Prelude\bin"
$SystemCollections = "${GAC_MSIL}\System.Collections\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Collections.dll"
$SystemNumerics = "${GAC_MSIL}\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll"
[Xml]$ProjectData = Get-Content .\csharp\CommandLineInterface\CommandLineInterface.csproj
$NugetPackages = (dotnet nuget locals global-packages --list) -split ': ' | Select-Object -Last 1
if (-not (Test-Path "${OutputDirectory}\System.Runtime.dll")) {
$SystemRuntimeVersion = $ProjectData.Project.ItemGroup[0].PackageReference.Version
$SystemRuntime = "${NugetPackages}\system.runtime\${SystemRuntimeVersion}\lib\net462\System.Runtime.dll"
Copy-Item -Path $SystemRuntime -Destination $OutputDirectory
}
if (-not (Test-Path "${OutputDirectory}\Spectre.Console.dll")) {
$SpectreVersion = $ProjectData.Project.ItemGroup[1].PackageReference.Version
$TargetFramework = $ProjectData.Project.PropertyGroup.TargetFramework
$SpectreConsole = "${NugetPackages}\spectre.console\${SpectreVersion}\lib\${TargetFramework}\Spectre.Console.dll"
Copy-Item -Path $SpectreConsole -Destination $OutputDirectory
}
'CommandLineInterface' | ForEach-Object {
"==> [INFO] Building ${_} link library" | Write-Message
& $CompilerPath "$CsharpDirectory/${_}/${_}.cs" -out:"$OutputDirectory/${_}.dll" -optimize -nologo -target:library -reference:"${OutputDirectory}\Spectre.Console.dll" -reference:"${OutputDirectory}\System.Runtime.dll" -reference:$SystemCollections
}
'Datum' | ForEach-Object {
"==> [INFO] Building ${_} link library" | Write-Message
& $CompilerPath "$CsharpDirectory/Geodetic/${_}.cs" -out:"$OutputDirectory/${_}.dll" -optimize -nologo -target:library
}
'Coordinate' | ForEach-Object {
"==> [INFO] Building ${_} link library" | Write-Message
& $CompilerPath "$CsharpDirectory/Geodetic/${_}.cs" -out:"$OutputDirectory/${_}.dll" -optimize -nologo -target:library -lib:$OutputDirectory -reference:Datum.dll
}
'Matrix' | ForEach-Object {
"==> [INFO] Building ${_} link library" | Write-Message
& $CompilerPath "$CsharpDirectory/${_}/${_}.cs" -out:"$OutputDirectory/${_}.dll" -optimize -nologo -target:library -reference:$SystemNumerics
}
'Node' | ForEach-Object {
"==> [INFO] Building ${_} link library" | Write-Message
& $CompilerPath "$CsharpDirectory/Graph/${_}.cs" -out:"$OutputDirectory/${_}.dll" -optimize -nologo -target:library -lib:$OutputDirectory
}
'Item' | ForEach-Object {
"==> [INFO] Building ${_} link library" | Write-Message
& $CompilerPath "$CsharpDirectory/Graph/${_}.cs" -out:"$OutputDirectory/${_}.dll" -optimize -nologo -target:library -lib:$OutputDirectory -reference:Node.dll
}
'Edge', 'PriorityQueue' | ForEach-Object {
"==> [INFO] Building ${_} link library" | Write-Message
& $CompilerPath "$CsharpDirectory/Graph/${_}.cs" -out:"$OutputDirectory/${_}.dll" -optimize -nologo -target:library -lib:$OutputDirectory -reference:Matrix.dll -reference:Node.dll -reference:Item.dll
}
'DirectedEdge', 'Graph' | ForEach-Object {
"==> [INFO] Building ${_} link library" | Write-Message
& $CompilerPath "$CsharpDirectory/Graph/${_}.cs" -out:"$OutputDirectory/${_}.dll" -optimize -nologo -target:library -lib:$OutputDirectory -reference:$SystemNumerics -reference:Matrix.dll -reference:Node.dll -reference:Edge.dll -reference:PriorityQueue.dll
}
Write-Result done
} else {
'==> [ERROR] Could not find C# compiler (csc.exe)' | Write-Error
}
}
function Invoke-Check {
<#
.SYNOPSIS
Run series of checks to determine if environment supports Prelude development.
.DESCRIPTION
Checks are run against Visual Studio 2022 Community Edition by default. Checks can be performed against other versions and offering using the -Version and -Offering parameters, respectively.
.EXAMPLE
.\Invoke-Task.ps1 -Check
.NOTES
When -Benchmark parameter is used, no other tasks will be executed.
#>
[CmdletBinding()]
Param(
[ValidateSet('2022', '2019')]
[String] $Version = '2022',
[String] $Offering = 'Community',
[ValidateSet('x64', 'x86')]
[String] $Architecture = 'x64'
)
$VisualStudioData = @{
Version = $Version
Offering = $Offering
Architecture = $Architecture
}
$VisualStudioRoot = Get-VisualStudioRoot @VisualStudioData
$Fails = 0
"`n==> [INFO] Checking build requirements" | Write-Message
"==> [INFO] VS Studio Version: ${Version}" | Write-Message
"==> [INFO] Offering: ${Offering}" | Write-Message
"==> [INFO] Architecture: ${Architecture}`n" | Write-Message
if ((Get-Command -Name 'dotnet' -ErrorAction Ignore)) {
'dotnet command is available!' | Write-Message -Success
} else {
'Failed to find dotnet command...' | Write-Message -Fail
$Fails++
}
$ToolList = dotnet tool list
@('reportgenerator', 'dotnet-stryker') | ForEach-Object {
$ToolName = $_
$DotnetToolInstalled = (($ToolList -match $ToolName) -split '\s+') -contains $ToolName
$CommandName = $ToolName -replace 'dotnet-', ''
if ($DotnetToolInstalled) {
"`"dotnet ${CommandName}`" command is available!" | Write-Message -Success
} else {
"Failed to find ${CommandName} command..." | Write-Message -Fail
$Fails++
}
}
if ((Test-Path "$VisualStudioRoot\Common7\Tools\VsDevCmd.bat")) {
'Successfully found VsDevCmd.bat!' | Write-Message -Success
} else {
'Failed to find necessary BAT file...' | Write-Message -Fail
$Fails++
}
if ((Test-Path "$VisualStudioRoot\MSBuild\Current\Bin\Roslyn\csc.exe")) {
'Successfully found csc.exe!' | Write-Message -Success
} else {
'Failed to find C# compiler...' | Write-Message -Fail
$Fails++
}
$Result = if ($Fails -eq 0) { 'pass' } else { 'fail' }
Write-Result $Result
}
function Invoke-Help {
[CmdletBinding()]
Param()
switch (Get-TaskList) {
benchmark {
Get-Help Invoke-Benchmark -Full
}
check {
Get-Help Invoke-Check -Full
}
lint {
Get-Help Invoke-Lint -Full
}
test {
Get-Help Invoke-Test -Full
}
build {
Get-Help Invoke-Build -Full
}
publish {
Get-Help Invoke-Publish -Full
}
}
}
function Invoke-Lint {
<#
.SYNOPSIS
Format C# code using dotnet-format and run static analysis on PowerShell code (with auto-fix enabled) using PSScriptAnalyzer
.PARAMETER CI
Configure function for running on continuous integration (CI) server (example: AppVeyor)
.PARAMETER DryRun
Analyze code without saving any changes
.PARAMETER Skip
Skip static analysis powershell and/or dotnet
.EXAMPLE
.\Invoke-Task.ps1 -Lint
.EXAMPLE
.\Invoke-Task.ps1 -Lint -Skip dotnet
.EXAMPLE
.\Invoke-Task.ps1 -Lint -Skip powershell
.NOTES
- PSScriptAnalyzer is configured by ./PSScriptAnalyzerSettings.psd1
- Custom PSScriptAnalyzer rules are added from ./PSScriptAnalyzerCustomRules.psm1
#>
[CmdletBinding()]
Param(
[Switch] $CI,
[Alias('noop')]
[Switch] $DryRun,
[String[]] $Skip
)
if (-not ($Skip -contains 'dotnet')) {
"==> [INFO] Formatting C# code`n" | Write-Message
$Format = {
Param(
[String] $Name
)
$Path = Join-Path "$PSScriptRoot/csharp/$Name" "${Name}.csproj"
if ($DryRun) {
dotnet tool run dotnet-format --check $Path --verbosity diagnostic
} else {
dotnet tool run dotnet-format $Path --verbosity detailed
}
}
if ((Get-Command -Name 'dotnet' -ErrorAction Ignore)) {
'CommandLineInterface', 'Matrix', 'Geodetic', 'Graph', 'Tests' | ForEach-Object {
& $Format -Name $_
}
} else {
'==> [ERROR] Global dotnet-format tool is required. Please run "./Invoke-Setup.ps1"' | Write-Error
}
}
if (-not ($Skip -contains 'powershell')) {
$FolderName = if ($Plus) { 'Plus' } else { 'src' }
$PesterData = Import-Module -Name Pester -PassThru -MinimumVersion 5.0.4
$PSScriptAnalyzerData = Import-Module -Name PSScriptAnalyzer -PassThru -MinimumVersion 1.20.0
$Parameters = @{
Path = (Join-Path $PSScriptRoot "Prelude/${FolderName}")
Settings = 'PSScriptAnalyzerSettings.psd1'
Fix = (-not $DryRun)
EnableExit = $CI
ReportSummary = $True
Recurse = $True
}
# Run say command to clear an annoying error message
try {
'Linting Code' | Invoke-Speak
} catch {
'Linting Code' | Out-Null
}
"`n==> [INFO] Linting PowerShell code (Path = $($Parameters.Path))" | Write-Message
"==> [INFO] Using Pester v$($PesterData.Version.ToString())" | Write-Message
"==> [INFO] Using PSScriptAnalyzer v$($PSScriptAnalyzerData.Version.ToString())`n" | Write-Message
Invoke-ScriptAnalyzer @Parameters
}
"`n" | Write-Host
}
function Invoke-Mutate {
<#
.SYNOPSIS
Execute Stryker mutation tests
#>
[CmdletBinding()]
Param(
[ValidateSet('Geodetic', 'Graph', 'Matrix')]
[String] $Project,
[String] $Configuration = 'stryker-config.json'
)
$Path = Join-Path $PSScriptRoot $Configuration
"`n==> [INFO] Running Stryker mutation tests on ${Project}" | Write-Message
"==> [INFO] Using Stryker configuration file at ${Path}`n" | Write-Message
$FileName = switch ($Project) {
'Geodetic' { 'Geodetic.csproj' }
'Graph' { 'Graph.csproj' }
'Matrix' { 'Matrix.csproj' }
}
dotnet stryker --config-file $Path --project $FileName --open-report
}
function Invoke-Publish {
<#
.SYNOPSIS
Update version and publish module to PowerShell Gallery
.PARAMETER DryRun
Go through steps without actually making changes or publishing module
.PARAMETER Major
Designate major version bump (i.e. 1.1.1 -> 2.0.0)
.PARAMETER Minor
Designate minor version bump (i.e. 1.1.1 -> 1.2.0)
.EXAMPLE
.\Invoke-Task.ps1 -Publish
.EXAMPLE
.\Invoke-Task.ps1 -Publish -Major
.EXAMPLE
.\Invoke-Task.ps1 -Publish -Minor
.NOTES
When no version switch is used (-Major or -Minor), "build" version will be used (i.e. 1.1.1 -> 1.1.2)
#>
[CmdletBinding()]
Param(
[Alias('noop')]
[Switch] $DryRun,
[Switch] $Major,
[Switch] $Minor
)
$ModulePath = Join-Path $PSScriptRoot 'Prelude'
$ProjectManifestPath = Join-Path $ModulePath 'Prelude.psd1'
$ValidateManifest = if (Test-ModuleManifest -Path $ProjectManifestPath) { 'Manifest is Valid' } else { 'Manifest is NOT Valid' }
$ValidateApiKey = if ((Write-Output $Env:NUGET_API_KEY).Length -eq 46) { 'API Key is Valid' } else { 'API Key is NOT Valid' }
"${Prefix}==> $ValidateManifest" | Write-Message
"${Prefix}==> $ValidateApiKey" | Write-Message
$Increment = if ($Major) {
'Major'
} elseif ($Minor) {
'Minor'
} else {
'Build'
}
if (-not $DryRun) {
"==> [INFO] Updating Module $(${Increment}.ToUpper()) Version..." | Write-Message
Update-Metadata $ProjectManifestPath -Increment $Increment
'==> [INFO] Publishing module...' | Write-Message
Publish-Module -Path $ModulePath -NuGetApiKey $Env:NUGET_API_KEY -SkipAutomaticTags -Verbose
"`n==> DONE`n" | Write-Message
} else {
"${Prefix}Updating Module $(${Increment}.ToUpper()) Version..." | Write-Message
"${Prefix}Publishing module..." | Write-Message
"${Prefix}==> DONE" | Write-Message
}
}
function Invoke-Test {
<#
.SYNOPSIS
Run unit tests for C# using dotnet and PowerShell using Pester
.PARAMETER Exclude
Skip PowerShell tests with certain tag (example: LinuxOnly)
.PARAMETER Filter
Only run tests (Describe or It) that match filter string (-like wildcards allowed)
.PARAMETER Skip
Skip running tests for powershell and/or dotnet
.PARAMETER Tags
Only run tests (Describe or It) with certain tags
.PARAMETER WithCoverage
Generate code coverage data from unit tests
.EXAMPLE
.\Invoke-Task.ps1 -Test -Skip powershell
.EXAMPLE
.\Invoke-Task.ps1 -Test -WithCoverage
.EXAMPLE
.\Invoke-Task.ps1 -Test -Tags Remote -Platform windows
.EXAMPLE
.\Invoke-Task.ps1 -Test -Filter '*Readability*'
.NOTES
Coverage report can be opened with "Invoke-Item .\coverage\index.htm"
#>
[CmdletBinding()]
Param(
[String] $Exclude = '',
[String] $Filter,
[String[]] $Skip,
[String[]] $Tags,
[Switch] $WithCoverage,
[String] $MinimumVersion = '5.6.1'
)
Set-BuildEnvironment -VariableNamePrefix 'Prelude' -Force
$BuildSystem = if ($Env:PreludeBuildSystem -eq 'Unknown') { 'Local Computer' } else { $Env:PreludeBuildSystem }
if (-not ($Skip -contains 'dotnet')) {
$ProjectPath = "$PSScriptRoot/csharp/Tests/Tests.csproj"
"==> [INFO] Executing C# tests on $BuildSystem" | Write-Message
if ($WithCoverage) {
dotnet test $ProjectPath /p:CollectCoverage=true /p:CoverletOutput=coverage.xml /p:CoverletOutputFormat=opencover
} else {
dotnet test $ProjectPath --logger:'console;verbosity=detailed'
}
"`n`n" | Write-Output
}
if (-not ($Skip -contains 'powershell')) {
$PesterData = Import-Module -Name Pester -PassThru -RequiredVersion $MinimumVersion
$Parameters = @{
Run = @{ PassThru = $True }
Filter = @{ ExcludeTag = $Exclude }
Debug = @{
ShowNavigationMarkers = $True
WriteVSCodeMarker = $True
}
}
$Configuration = if ($PesterData.Version.Minor -ge 2) {
New-PesterConfiguration -Hashtable $Parameters
} else {
[PesterConfiguration]$Parameters
}
if ($Filter) {
$Configuration.Filter.FullName = $Filter
} elseif ($Tags) {
$Configuration.Filter.Tag = $Tags
}
if ($WithCoverage) {
$Configuration.CodeCoverage = @{
Enabled = $True
Path = (Get-ChildItem (Join-Path $PSScriptRoot $SourceDirectory) -Recurse -Include '*.ps1').FullName
}
$Configuration.TestResult = @{ Enabled = $False }
}
if ($Detailed) {
$Configuration.Output.Verbosity = 'Detailed'
}
"`n==> [INFO] Executing PowerShell tests on $BuildSystem" | Write-Message
"==> [INFO] Using Pester v$($PesterData.Version.ToString())`n" | Write-Message
$Result = Invoke-Pester -Configuration $Configuration
if ($Result.FailedCount -gt 0) {
$FailedMessage = "==> FAILED - $($Result.FailedCount) PowerShell test(s) failed"
throw $FailedMessage
} else {
Write-Result pass
}
}
}
switch (Get-TaskList) {
help {
Invoke-Help
Break
}
benchmark {
Invoke-Benchmark
Break
}
check {
Invoke-Check @VisualStudioData
Break
}
lint {
$Parameters = @{
CI = $CI
DryRun = $DryRun
Skip = $Skip
}
Invoke-Lint @Parameters
}
test {
$Parameters = @{
Exclude = $Exclude
Filter = $Filter
Skip = $Skip
Tags = $Tags
WithCoverage = $WithCoverage
}
Invoke-Test @Parameters
Set-Location $PSScriptRoot
if ($GenerateCoverageReport) {
$SourceDirs = $SourceDirectory
$ReportTypes = 'Html;HtmlSummary;HtmlChart'
dotnet reportgenerator -reports:'**/coverage.xml' -targetdir:coverage -sourcedirs:$SourceDirs -historydir:.history -reporttypes:$ReportTypes
if ($Show) {
Invoke-Item ./coverage/index.htm
}
}
}
mutate {
$Parameters = @{
Project = $Project
}
Invoke-Mutate @Parameters
Break
}
build {
if (-not $BuildOnly) {
$Parameters = @{ Skip = 'powershell' }
Invoke-Lint @Parameters
Invoke-Test @Parameters
}
if ($LASTEXITCODE -eq 0) {
Invoke-Build @VisualStudioData
} else {
Write-Result fail
break
}
}
publish {
$Parameters = @{
DryRun = $DryRun
Major = $Major
Minor = $Minor
}
Invoke-Publish @Parameters
Break
}
}