Repository navigation
Expand file tree
/
Copy pathConfluencePS.build.ps1
More file actions
669 lines (575 loc) · 29.5 KB
/
Copy pathConfluencePS.build.ps1
File metadata and controls
669 lines (575 loc) · 29.5 KB
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
[CmdletBinding()]
param(
[ValidateSet('None', 'Normal' , 'Detailed', 'Diagnostic')]
[String] $PesterVerbosity = 'Normal',
[Parameter()]
[String] $VersionToPublish,
# Test filtering parameters
[Parameter()]
[String[]] $Tag,
[Parameter()]
[String[]] $ExcludeTag,
# Integration test parameters
[Parameter()]
[ValidateRange(1, 16)]
[Int] $ThrottleLimit = 1,
[Parameter()]
[String[]] $IntegrationTestPath
)
Import-Module "$PSScriptRoot/Tools/BuildTools.psm1" -Force -ErrorAction Stop
function Import-ConfluencePSStandard {
[CmdletBinding()]
param()
$buildRequirements = Import-PowerShellDataFile -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Tools/build.requirements.psd1')
$standardsRequirement = $buildRequirements |
Where-Object { $_.ModuleName -eq 'AtlassianPS.Standards' } |
Select-Object -First 1
if (-not $standardsRequirement) {
throw 'AtlassianPS.Standards is missing from Tools/build.requirements.psd1.'
}
Import-Module AtlassianPS.Standards -RequiredVersion $standardsRequirement.RequiredVersion -Force -ErrorAction Stop
}
Import-ConfluencePSStandard
Remove-Item -Path env:\BH* -ErrorAction SilentlyContinue
$ProjectName = 'ConfluencePS'
$env:BHProjectName = $ProjectName
$env:BHProjectPath = $PSScriptRoot
$env:BHModulePath = Join-Path $PSScriptRoot $ProjectName
$env:BHPSModulePath = $env:BHModulePath
$env:BHPSModuleManifest = Join-Path $env:BHModulePath "$ProjectName.psd1"
$env:BHBuildOutput = Join-Path $PSScriptRoot 'Release'
function Initialize-BuildEnvironmentInfo {
if ($env:GITHUB_ACTIONS) {
$env:BHBuildSystem = 'GitHub Actions'
# On PR builds GITHUB_REF_NAME is `<pr>/merge`; the source branch lives in GITHUB_HEAD_REF.
$env:BHBranchName = if ($env:GITHUB_HEAD_REF) { $env:GITHUB_HEAD_REF } else { $env:GITHUB_REF_NAME }
$env:BHCommitHash = $env:GITHUB_SHA
$env:BHBuildNumber = $env:GITHUB_RUN_NUMBER
}
else {
$env:BHBuildSystem = 'Unknown'
$env:BHBranchName = git -C $env:BHProjectPath rev-parse --abbrev-ref HEAD 2>$null
$env:BHCommitHash = git -C $env:BHProjectPath rev-parse HEAD 2>$null
$env:BHBuildNumber = '0'
}
$env:BHCommitMessage = (git -C $env:BHProjectPath log -1 --pretty=%B 2>$null) -join "`n"
}
function Ensure-PSScriptAnalyzerSettings {
$settingsPath = Join-Path $PSScriptRoot 'PSScriptAnalyzerSettings.psd1'
Assert-True (Test-Path $settingsPath) "Missing PSScriptAnalyzer settings at '$settingsPath'. Run ./Tools/setup.ps1 or restore the file."
}
Ensure-PSScriptAnalyzerSettings
#region HarmonizeVariables
switch ($true) {
{ $IsWindows } {
$OS = "Windows"
if (-not ($IsCoreCLR)) {
$OSVersion = $PSVersionTable.BuildVersion.ToString()
}
}
{ $IsLinux } {
$OS = "Linux"
}
{ $IsMacOs } {
$OS = "OSX"
}
{ $IsCoreCLR } {
$OSVersion = $PSVersionTable.OS
}
}
#endregion HarmonizeVariables
if ($VersionToPublish) {
$VersionToPublish = $VersionToPublish.TrimStart('v')
}
$builtManifestPath = "$env:BHBuildOutput/$env:BHProjectName/$env:BHProjectName.psd1"
Task ShowDebugInfo {
Initialize-BuildEnvironmentInfo
Write-Build Gray
Write-Build Gray ('BHBuildSystem: {0}' -f $env:BHBuildSystem)
Write-Build Gray '-------------------------------------------------------'
Write-Build Gray ('BHProjectName {0}' -f $env:BHProjectName)
Write-Build Gray ('BHProjectPath: {0}' -f $env:BHProjectPath)
Write-Build Gray ('BHModulePath: {0}' -f $env:BHModulePath)
Write-Build Gray ('BHPSModuleManifest: {0}' -f $env:BHPSModuleManifest)
Write-Build Gray ('BHBuildOutput: {0}' -f $env:BHBuildOutput)
Write-Build Gray ('builtManifestPath: {0}' -f $builtManifestPath)
Write-Build Gray '-------------------------------------------------------'
Write-Build Gray ('BHBranchName: {0}' -f $env:BHBranchName)
Write-Build Gray ('BHCommitHash: {0}' -f $env:BHCommitHash)
Write-Build Gray ('BHCommitMessage: {0}' -f $env:BHCommitMessage)
Write-Build Gray ('BHBuildNumber {0}' -f $env:BHBuildNumber)
Write-Build Gray ('VersionToPublish {0}' -f $VersionToPublish)
Write-Build Gray '-------------------------------------------------------'
Write-Build Gray ('PowerShell version: {0}' -f $PSVersionTable.PSVersion.ToString())
Write-Build Gray ('OS: {0}' -f $OS)
Write-Build Gray ('OS Version: {0}' -f $OSVersion)
Write-Build Gray
}
# Synopsis: Run style checks and PSScriptAnalyzer. Collects both result sets
# before throwing so a single run surfaces every issue. Emits GitHub Actions
# workflow commands when running under CI so violations appear as inline
# annotations on the PR diff.
Task Lint {
Remove-Item $env:BHBuildOutput -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item "Test*.xml" -Force -ErrorAction SilentlyContinue
Get-Module Pester | Remove-Module -Force -ErrorAction SilentlyContinue
Import-Module Pester -MinimumVersion '6.2.0' -MaximumVersion '6.999' -Force -ErrorAction Stop
$styleConfig = New-PesterConfiguration -Hashtable @{
Run = @{
PassThru = $true
Path = "$PSScriptRoot/Tests/Style.Tests.ps1"
}
Output = @{
Verbosity = $PesterVerbosity
}
}
$styleResults = Invoke-Pester -Configuration $styleConfig
Assert-True (($styleResults.FailedCount + $styleResults.FailedBlocksCount + $styleResults.FailedContainersCount) -eq 0) "$($styleResults.FailedCount) style test(s), $($styleResults.FailedBlocksCount) block(s), and $($styleResults.FailedContainersCount) container(s) failed."
$pssaConfig = New-PesterConfiguration -Hashtable @{
Run = @{
PassThru = $true
Path = "$PSScriptRoot/Tests/PSScriptAnalyzer.Tests.ps1"
}
Output = @{
Verbosity = $PesterVerbosity
}
}
$pssaResults = Invoke-Pester -Configuration $pssaConfig
Assert-True (($pssaResults.FailedCount + $pssaResults.FailedBlocksCount + $pssaResults.FailedContainersCount) -eq 0) "$($pssaResults.FailedCount) analyzer test(s), $($pssaResults.FailedBlocksCount) block(s), and $($pssaResults.FailedContainersCount) container(s) failed."
}
Task Clean {
Remove-Item $env:BHBuildOutput -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item "Test*.xml" -Force -ErrorAction SilentlyContinue
# `ConfluencePS/<locale>/` is preserved as the GenerateExternalHelp incremental cache.
}
Task Build Clean, {
if (-not (Test-Path "$env:BHBuildOutput/$env:BHProjectName")) {
$null = New-Item -Path "$env:BHBuildOutput", "$env:BHBuildOutput/$env:BHProjectName" -ItemType Directory
}
}, GenerateExternalHelp, RemoveOrphanedExternalHelp, CopyModuleFiles, CompileModule, UpdateManifest
# Synopsis: Remove generated help artifacts whose source markdown no longer exists.
# Deletions in `docs/` would otherwise survive in `ConfluencePS/<locale>/` (the
# GenerateExternalHelp cache) and ship via CopyModuleFiles.
Task RemoveOrphanedExternalHelp {
if (-not (Test-Path $env:BHModulePath)) { return }
$docsRoot = Join-Path $env:BHProjectPath 'docs'
# Safety net: only sweep dirs that already look like generated help output.
# Prevents a missing/renamed `docs/<locale>/` from wiping `Public/` or `Private/`.
$isHelpOutputDir = {
param($dir)
$files = @(Get-ChildItem $dir.FullName -File -ErrorAction SilentlyContinue)
if ($files.Count -eq 0) { return $false }
@($files | Where-Object { $_.Name -notlike '*.help.txt' -and $_.Name -notlike '*-help.xml' }).Count -eq 0
}
$helpDirs = Get-ChildItem $env:BHModulePath -Directory -ErrorAction SilentlyContinue |
Where-Object { & $isHelpOutputDir $_ }
foreach ($localeDir in $helpDirs) {
$localeDocs = Join-Path $docsRoot $localeDir.Name
if (-not (Test-Path $localeDocs)) {
Remove-Item $localeDir.FullName -Recurse -Force
continue
}
$expected = [System.Collections.Generic.HashSet[string]]::new(
[System.StringComparer]::OrdinalIgnoreCase)
$hasCommandHelp = Get-ChildItem (Join-Path $localeDocs 'commands/*.md') -File -ErrorAction SilentlyContinue |
Where-Object { $_.Name -ne 'index.md' } |
Select-Object -First 1
if ($hasCommandHelp) {
$null = $expected.Add("$env:BHProjectName-help.xml")
}
Get-ChildItem (Join-Path $localeDocs 'about_*.md') -File -ErrorAction SilentlyContinue |
ForEach-Object { $null = $expected.Add("$($_.BaseName).help.txt") }
Get-ChildItem (Join-Path $localeDocs 'commands/about_*.md') -File -ErrorAction SilentlyContinue |
ForEach-Object { $null = $expected.Add("$($_.BaseName).help.txt") }
Get-ChildItem $localeDir.FullName -File -ErrorAction SilentlyContinue |
Where-Object { -not $expected.Contains($_.Name) } |
Remove-Item -Force
}
}
# Synopsis: Generate ./Release structure
Task CopyModuleFiles {
Copy-Item -Path "$env:BHModulePath/*" -Destination "$env:BHBuildOutput/$env:BHProjectName" -Recurse -Force
Copy-Item -Path @(
"$env:BHProjectPath/CHANGELOG.md"
"$env:BHProjectPath/LICENSE"
"$env:BHProjectPath/README.md"
) -Destination "$env:BHBuildOutput/$env:BHProjectName" -Force
$null = New-Item -Path "$env:BHBuildOutput/Tests" -ItemType Directory -ErrorAction SilentlyContinue
Copy-Item -Path "$env:BHProjectPath/Tests" -Destination $env:BHBuildOutput -Recurse -Force
Copy-Item -Path "$env:BHProjectPath/PSScriptAnalyzerSettings.psd1" -Destination $env:BHBuildOutput -Force
}
# Synopsis: Compile all functions into the .psm1 file
Task CompileModule {
$regionsToKeep = @('Dependencies', 'Configuration')
$targetFile = "$env:BHBuildOutput/$env:BHProjectName/$env:BHProjectName.psm1"
$content = Get-Content -Encoding UTF8 -LiteralPath $targetFile
$capture = $false
$compiled = ""
foreach ($line in $content) {
if ($line -match "^#region ($($regionsToKeep -join "|"))$") {
$capture = $true
}
if (($capture -eq $true) -and ($line -match "^#endregion")) {
$capture = $false
}
if ($capture) {
$compiled += "$line`r`n"
}
}
$PublicFunctions = @( Get-ChildItem -Path "$env:BHBuildOutput/$env:BHProjectName/Public/*.ps1" -ErrorAction SilentlyContinue )
$PrivateFunctions = @( Get-ChildItem -Path "$env:BHBuildOutput/$env:BHProjectName/Private/*.ps1" -ErrorAction SilentlyContinue )
foreach ($function in @($PublicFunctions + $PrivateFunctions)) {
$compiled += (Get-Content -Path $function.FullName -Raw)
$compiled += "`r`n"
}
$utf8Bom = [System.Text.UTF8Encoding]::new($true)
[System.IO.File]::WriteAllText($targetFile, $compiled, $utf8Bom)
"Private", "Public" | ForEach-Object { Remove-Item -Path "$env:BHBuildOutput/$env:BHProjectName/$_" -Recurse -Force }
}
# Synopsis: Use PlatyPS to generate External-Help
Task GenerateExternalHelp -Inputs {
Get-ChildItem "$env:BHProjectPath/docs" -Recurse -File -Filter '*.md'
} -Outputs {
foreach ($locale in (Get-ChildItem "$env:BHProjectPath/docs" -Attribute Directory)) {
$localeOut = Join-Path $env:BHModulePath $locale.BaseName
$hasCommandHelp = Get-ChildItem "$($locale.FullName)/commands/*.md" -File -ErrorAction SilentlyContinue |
Where-Object { $_.Name -ne 'index.md' -and $_.Name -notlike 'about_*.md' } |
Select-Object -First 1
if ($hasCommandHelp) {
Join-Path $localeOut "$env:BHProjectName-help.xml"
}
@(
Get-ChildItem "$($locale.FullName)/about_*.md" -File -ErrorAction SilentlyContinue
Get-ChildItem "$($locale.FullName)/commands/about_*.md" -File -ErrorAction SilentlyContinue
) |
ForEach-Object { Join-Path $localeOut "$($_.BaseName).help.txt" }
}
} {
Import-Module Microsoft.PowerShell.PlatyPS -Force
try {
foreach ($locale in (Get-ChildItem "$env:BHProjectPath/docs" -Attribute Directory)) {
$outputPath = "$env:BHModulePath/$($locale.Basename)"
$null = New-Item -ItemType Directory -Path $outputPath -Force
# Cleanup for older help generation runs that left a nested module folder
# under locale output, which can block PlatyPS writes on subsequent runs.
$staleNestedPath = Join-Path $outputPath $env:BHProjectName
if (Test-Path $staleNestedPath) {
Remove-Item $staleNestedPath -Recurse -Force -ErrorAction SilentlyContinue
}
$commandHelpFiles = Get-ChildItem "$($locale.FullName)/commands/*.md" -File |
Where-Object { $_.Name -ne 'index.md' -and $_.Name -notlike 'about_*.md' }
if ($commandHelpFiles) {
$commandHelp = @($commandHelpFiles | Import-MarkdownCommandHelp)
$commandHelp | Export-MamlCommandHelp -OutputFolder $outputPath -Force
# PlatyPS 1.0 still drops the per-command MAML into a nested
# <ModuleName>/ subdirectory; flatten so the help loader finds it.
$nestedPath = Join-Path $outputPath $env:BHProjectName
if (Test-Path $nestedPath) {
Get-ChildItem $nestedPath -Filter '*.xml' | Move-Item -Destination $outputPath -Force
Remove-Item $nestedPath -Recurse -Force
}
$mamlFile = Join-Path $outputPath "$env:BHProjectName-help.xml"
Assert-True (Test-Path $mamlFile) "Expected MAML help file was not created: $mamlFile"
# Export-MamlCommandHelp drops `aliases` / `pipelineInput` /
# `<dev:defaultValue>` even though the markdown YAML and the
# parsed CommandHelp object both carry them. Splice them back
# in from the in-memory CommandHelp objects so Get-Help -Full
# surfaces the same data Import-MarkdownCommandHelp captured.
$xml = [xml](Get-Content $mamlFile -Raw)
$ns = [System.Xml.XmlNamespaceManager]::new($xml.NameTable)
$ns.AddNamespace('command', 'http://schemas.microsoft.com/maml/dev/command/2004/10')
$ns.AddNamespace('dev', 'http://schemas.microsoft.com/maml/dev/2004/10')
$ns.AddNamespace('maml', 'http://schemas.microsoft.com/maml/2004/10')
foreach ($help in $commandHelp) {
$cmdNode = $xml.SelectSingleNode("//command:command[command:details/command:name='$($help.Title)']", $ns)
if (-not $cmdNode) { continue }
# Export-MamlCommandHelp dumps every example's full markdown
# (fence + prose) into <maml:introduction> and leaves
# <dev:code> / <dev:remarks> empty. Get-Help only reads
# those two elements, so split the markdown on the first
# fenced code block and re-populate them.
$exNodes = @($cmdNode.SelectNodes('command:examples/command:example', $ns))
for ($i = 0; $i -lt $exNodes.Count -and $i -lt $help.Examples.Count; $i++) {
$ex = $exNodes[$i]
$remarksMd = $help.Examples[$i].Remarks
if (-not $remarksMd) { continue }
$codeText = ''
$proseText = $remarksMd
$fence = [regex]::Match($remarksMd, '(?s)```[a-zA-Z0-9_+\-]*\r?\n(.*?)\r?\n```')
if ($fence.Success) {
$codeText = $fence.Groups[1].Value.TrimEnd()
$proseText = ($remarksMd.Substring(0, $fence.Index) + $remarksMd.Substring($fence.Index + $fence.Length)).Trim()
}
$intro = $ex.SelectSingleNode('maml:introduction', $ns)
if ($intro) { [void]$ex.RemoveChild($intro) }
$codeNode = $ex.SelectSingleNode('dev:code', $ns)
if (-not $codeNode) {
$codeNode = $xml.CreateElement('dev', 'code', 'http://schemas.microsoft.com/maml/dev/2004/10')
[void]$ex.AppendChild($codeNode)
}
$codeNode.InnerText = $codeText
$remarksNode = $ex.SelectSingleNode('dev:remarks', $ns)
if (-not $remarksNode) {
$remarksNode = $xml.CreateElement('dev', 'remarks', 'http://schemas.microsoft.com/maml/dev/2004/10')
[void]$ex.AppendChild($remarksNode)
}
# One <maml:para> per paragraph; Get-Help inserts a
# blank line between sibling para elements.
while ($remarksNode.HasChildNodes) { [void]$remarksNode.RemoveChild($remarksNode.FirstChild) }
foreach ($para in ($proseText -split "\r?\n\r?\n")) {
if (-not $para.Trim()) { continue }
$pn = $xml.CreateElement('maml', 'para', 'http://schemas.microsoft.com/maml/2004/10')
$pn.InnerText = $para
[void]$remarksNode.AppendChild($pn)
}
}
$paramMap = @{}
foreach ($p in $help.Parameters) { $paramMap[$p.Name] = $p }
foreach ($pNode in $cmdNode.SelectNodes('.//command:parameter', $ns)) {
$pName = $pNode.SelectSingleNode('maml:name', $ns).InnerText
if (-not $paramMap.ContainsKey($pName)) { continue }
$p = $paramMap[$pName]
$aliasText = if ($p.Aliases) { $p.Aliases -join ', ' } else { 'none' }
$pNode.SetAttribute('aliases', $aliasText)
$byVal = $false; $byName = $false
foreach ($set in $p.ParameterSets) {
if ($set.ValueFromPipeline) { $byVal = $true }
if ($set.ValueFromPipelineByPropertyName) { $byName = $true }
}
$pipelineText = if ($byVal -and $byName) {
'True (ByValue, ByPropertyName)'
}
elseif ($byVal) { 'True (ByValue)' }
elseif ($byName) { 'True (ByPropertyName)' }
else { 'False' }
$pNode.SetAttribute('pipelineInput', $pipelineText)
# MAML schema places <dev:defaultValue> only on the flat
# <command:parameters> entries, not on syntax-item copies.
if ($pNode.ParentNode.LocalName -eq 'parameters' -and $p.DefaultValue) {
$existing = $pNode.SelectSingleNode('dev:defaultValue', $ns)
if ($existing) { $pNode.RemoveChild($existing) | Out-Null }
$dv = $xml.CreateElement('dev', 'defaultValue', 'http://schemas.microsoft.com/maml/dev/2004/10')
$dv.InnerText = $p.DefaultValue
$null = $pNode.AppendChild($dv)
}
}
}
$xml.Save($mamlFile)
}
# Copy about topics as help text files. UTF-8 with BOM for PowerShell 5
# compatibility (matches the CompileModule convention introduced in 3107e3a).
$utf8Bom = [System.Text.UTF8Encoding]::new($true)
@(
Get-ChildItem "$($locale.FullName)/about_*.md" -File -ErrorAction SilentlyContinue
Get-ChildItem "$($locale.FullName)/commands/about_*.md" -File -ErrorAction SilentlyContinue
) | ForEach-Object {
$helpTxtName = $_.BaseName + '.help.txt'
$content = [System.IO.File]::ReadAllText($_.FullName)
# Tolerate files where the closing `---` is the final line (no trailing newline).
$content = $content -replace '\A---\r?\n[\s\S]*?\r?\n---\r?\n?', ''
[System.IO.File]::WriteAllText((Join-Path $outputPath $helpTxtName), $content, $utf8Bom)
}
}
}
finally {
Remove-Module Microsoft.PowerShell.PlatyPS -ErrorAction SilentlyContinue
}
}
# Synopsis: Update the manifest of the module
Task UpdateManifest {
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module $env:BHPSModuleManifest -Force
$moduleFunctions = (Get-ChildItem "$env:BHModulePath/Public/*.ps1").BaseName
Metadata\Update-Metadata -Path $builtManifestPath -PropertyName "FunctionsToExport" -Value @($moduleFunctions)
Metadata\Update-Metadata -Path $builtManifestPath -PropertyName "AliasesToExport" -Value ''
$moduleAlias = Get-Alias | Where-Object { $_.ModuleName -eq "$env:BHProjectName" }
if ($moduleAlias) {
Metadata\Update-Metadata -Path $builtManifestPath -PropertyName "AliasesToExport" -Value @($moduleAlias.Name)
}
}
Task SetSourceVersion {
if (-not $VersionToPublish) {
throw 'VersionToPublish is required for SetSourceVersion. Use -VersionToPublish <semver>.'
}
$null = Set-AtlassianPSModuleManifestVersion `
-BuiltManifestPath $env:BHPSModuleManifest `
-ModuleName $env:BHProjectName `
-VersionToPublish $VersionToPublish
}
Task SetVersion {
if (-not $VersionToPublish) {
throw 'VersionToPublish is required for SetVersion. Use -VersionToPublish <semver>.'
}
$releaseNotes = Get-AtlassianPSReleaseNotesFromChangelog `
-ChangelogPath (Join-Path -Path $env:BHProjectPath -ChildPath 'CHANGELOG.md') `
-ReleaseVersion $VersionToPublish
$versionString = Set-AtlassianPSModuleManifestVersion `
-BuiltManifestPath $builtManifestPath `
-ModuleName $env:BHProjectName `
-VersionToPublish $VersionToPublish `
-ReleaseNotes $releaseNotes
Write-Build Gray "Resolved release version: $versionString"
}
Task Test {
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Get-Module Pester | Remove-Module -Force -ErrorAction SilentlyContinue
Import-Module Pester -MinimumVersion '6.2.0' -MaximumVersion '6.999' -Force -ErrorAction Stop
# Skip integration test files at discovery time so normal Test runs do
# not execute setup blocks or require integration credentials.
$integrationPaths = @(Get-ChildItem -Path (Join-Path $env:BHBuildOutput 'Tests/Integration') -Filter '*.Integration.Tests.ps1' -File -Recurse -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty FullName)
$pesterConfigHash = @{
Run = @{
PassThru = $true
Path = "$env:BHBuildOutput/Tests"
ExcludePath = $integrationPaths
}
TestResult = @{
Enabled = $true
OutputFormat = 'NUnitXml'
OutputPath = "Test-$OS-$($PSVersionTable.PSVersion.ToString()).xml"
}
Output = @{
Verbosity = $PesterVerbosity
}
Filter = @{
ExcludeTag = @('Integration')
}
}
if ($Tag) {
$pesterConfigHash.Filter.Tag = $Tag
$pesterConfigHash.Filter.ExcludeTag = @($pesterConfigHash.Filter.ExcludeTag | Where-Object { $_ -notin $Tag })
if ('Integration' -in $Tag) {
$pesterConfigHash.Run.ExcludePath = @()
}
}
if ($ExcludeTag) {
$merged = @($pesterConfigHash.Filter.ExcludeTag) + @($ExcludeTag) | Select-Object -Unique
if ($Tag) {
$merged = @($merged | Where-Object { $_ -notin $Tag })
}
$pesterConfigHash.Filter.ExcludeTag = $merged
}
$pesterConfig = New-PesterConfiguration -Hashtable $pesterConfigHash
$testResults = Invoke-Pester -Configuration $pesterConfig
Assert-True (($testResults.FailedCount + $testResults.FailedBlocksCount + $testResults.FailedContainersCount) -eq 0) "$($testResults.FailedCount) Pester test(s), $($testResults.FailedBlocksCount) block(s), and $($testResults.FailedContainersCount) container(s) failed."
}
# Synopsis: Run integration tests against live Confluence (Cloud or Data Center)
Task TestIntegration {
$helpersPath = Join-Path $env:BHProjectPath 'Tests/Helpers/IntegrationTestTools.ps1'
if (Test-Path $helpersPath) {
. $helpersPath
Read-DotEnvFile -Path (Join-Path $env:BHProjectPath '.env') -ExcludeName (Get-DotEnvExcludedName)
}
else {
throw "Integration helper not found: $helpersPath"
}
$deploymentType = Get-ConfluenceIntegrationDeploymentType
$requiredEnvVars = Get-ConfluenceIntegrationRequiredVariables -DeploymentType $deploymentType
$missing = $requiredEnvVars | Where-Object {
[string]::IsNullOrEmpty([Environment]::GetEnvironmentVariable($_))
}
if ($missing) {
throw @"
Required environment variables for the $deploymentType integration test track are not set: $($missing -join ', ')
For CI: configure these as repository secrets (Cloud) or workflow env vars (DataCenter) under Settings -> Secrets and variables -> Actions.
For local development: copy .env.example to .env and configure the required variables.
"@
}
$runnerPath = "$env:BHProjectPath/Tests/Invoke-ParallelPester.ps1"
if (-not (Test-Path $runnerPath)) {
throw "Integration test runner not found: $runnerPath"
}
$defaultIntegrationPath = Join-Path $env:BHBuildOutput 'Tests/Integration'
if (-not (Test-Path $defaultIntegrationPath)) {
$defaultIntegrationPath = Join-Path $env:BHProjectPath 'Tests/Integration'
}
$runnerParams = @{
Path = @($defaultIntegrationPath)
ThrottleLimit = $ThrottleLimit
Output = $PesterVerbosity
OutputPath = 'Test-Integration.xml'
}
if ($IntegrationTestPath) {
$runnerParams.Path = $IntegrationTestPath
Write-Build Gray "Restricting integration tests to: $($IntegrationTestPath -join ', ')"
}
if ($Tag) {
$runnerParams.Tag = $Tag
Write-Build Gray "Running integration tests with tag(s): $($Tag -join ', ')"
}
else {
$runnerParams.Tag = @('Integration')
Write-Build Gray 'Running integration tests (tag: Integration)'
}
if ($ExcludeTag) {
$runnerParams.ExcludeTag = $ExcludeTag
Write-Build Gray "Excluding tag(s): $($ExcludeTag -join ', ')"
}
Write-Build Gray "ThrottleLimit: $ThrottleLimit"
Write-Build Gray "Output: $($runnerParams.OutputPath)"
& $runnerPath @runnerParams
Assert-True ($LASTEXITCODE -eq 0) "Integration tests failed with exit code $LASTEXITCODE"
}
# Synopsis: Start the local Confluence Data Center Docker container for integration tests
Task StartConfluenceDocker {
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
throw "Docker is required for the Data Center integration track. See https://docs.docker.com/get-docker/."
}
$composeFile = Join-Path $env:BHProjectPath 'docker-compose.yml'
Assert-True (Test-Path $composeFile) "docker-compose.yml not found at $composeFile"
Write-Build Gray "Starting Confluence Data Center container via $composeFile..."
Invoke-BuildExec { docker compose -f $composeFile up -d }
if (-not $env:CI_CONFLUENCE_TYPE) {
$env:CI_CONFLUENCE_TYPE = 'DataCenter'
}
if (-not $env:CI_CONFLUENCE_URL) {
$env:CI_CONFLUENCE_URL = 'http://localhost:1990/confluence'
}
if (-not $env:CI_CONFLUENCE_USER) {
$env:CI_CONFLUENCE_USER = 'admin'
}
if (-not $env:CI_CONFLUENCE_PASSWORD) {
$env:CI_CONFLUENCE_PASSWORD = 'admin'
}
if (-not $env:CONFLUENCE_ALLOW_UNENCRYPTED_AUTH) {
$env:CONFLUENCE_ALLOW_UNENCRYPTED_AUTH = 'true'
}
& (Join-Path $env:BHProjectPath 'Tools/Wait-ConfluenceServer.ps1')
}
# Synopsis: Stop the local Confluence Data Center Docker container
Task StopConfluenceDocker {
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
throw "Docker is required for the Data Center integration track. See https://docs.docker.com/get-docker/."
}
$composeFile = Join-Path $env:BHProjectPath 'docker-compose.yml'
Assert-True (Test-Path $composeFile) "docker-compose.yml not found at $composeFile"
Write-Build Gray "Stopping Confluence Data Center container ($composeFile)..."
Invoke-BuildExec { docker compose -f $composeFile down -v }
}
Task Package {
$script:PackagePath = New-AtlassianPSModulePackage -BuildOutputPath $env:BHBuildOutput -ModuleName $env:BHProjectName
}
Task VerifyReleaseArtifact Package, {
if (-not $VersionToPublish) {
throw 'VersionToPublish is required for VerifyReleaseArtifact. Use -VersionToPublish <semver>.'
}
$null = Test-AtlassianPSModulePackage `
-BuildOutputPath $env:BHBuildOutput `
-ModuleName $env:BHProjectName `
-PackagePath $script:PackagePath `
-ExpectedVersion $VersionToPublish `
-RequireReleaseNotes
}
Task TestPublish Build, Package, {
$testPackageParameters = @{
BuildOutputPath = $env:BHBuildOutput
ModuleName = $env:BHProjectName
}
if ($script:PackagePath) {
$testPackageParameters.PackagePath = $script:PackagePath
}
$package = Test-AtlassianPSModulePackage @testPackageParameters
Write-Build Green "Publish dry-run passed: $($package.PackagePath)"
}
Task . Clean, Build, Test