-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathgenerate-pesterconfiguration-docs.ps1
More file actions
190 lines (159 loc) · 6.48 KB
/
Copy pathgenerate-pesterconfiguration-docs.ps1
File metadata and controls
190 lines (159 loc) · 6.48 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
<#
.SYNOPSIS
Updates the Configuration page with latest PesterConfiguration Sections and Options
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $False)]
[string] $PesterVersion,
[ValidateSet('Current','v5')]
[string] $DocsVersion = 'Current',
[ValidateSet('List','Table')]
[string] $Style = 'List'
)
#region helpers
# Run.RepoRoot is resolved by New-PesterConfiguration to the absolute path of the
# repository the generator runs in (machine-specific). We replace it with an
# illustrative placeholder repo root so no build path leaks into the published docs.
$repoRootPlaceholder = 'C:\MyProject'
function Format-MdxDescription ($text) {
# Escape characters that are structurally significant to MDX 3 / Markdown tables so raw
# PesterConfiguration help text (e.g. "{ . './setup.ps1' }") doesn't break the docs build.
# MDX 3 treats `{` as a JS expression and `<` as a JSX tag; `|` would corrupt a table row.
if ($null -eq $text) { return '' }
return ([string]$text) -replace '([{}<|])', '\$1'
}
function Format-NicelyMini ($value) {
if ($null -eq $value) {
return '$null'
}
if ($value -is [bool]) {
if ($value) {
'$true'
} else {
'$false'
}
}
if ($value -is [int] -or $value -is [decimal]) {
return $value
}
if ($value -is [string]) {
if ([String]::IsNullOrEmpty($value)) {
return '$null'
} else {
return "'$value'"
}
}
# does not work with [object[]] when building for some reason
if ($value -is [System.Collections.IEnumerable]) {
if (0 -eq $value.Count) {
return '@()'
}
$v = foreach ($i in $value) {
Format-NicelyMini $i
}
return "@($($v -join ', '))"
}
}
function generateSectionsMarkdownAsTable {
$configObject = New-PesterConfiguration
foreach ($configSection in $configObject.PSObject.Properties) {
$sectionName = $configSection.Name
$sectionDescription = $configSection.Value -as [string]
$section = $configSection.Value
$options = foreach ($configOption in $section.PSObject.Properties) {
$optionName = $configOption.Name
$option = $configOption.Value
$rawDefault = $option.Default
# Run.RepoRoot defaults to an auto-detected, machine-specific path. Render an illustrative placeholder (C:\MyProject) so the generated docs stay machine-independent.
if ($rawDefault -is [string] -and $rawDefault -eq $configObject.Run.RepoRoot.Value) { $rawDefault = $repoRootPlaceholder }
$default = Format-NicelyMini $rawDefault
# Use the declared property type so options with a $null default (e.g. CodeCoverage.ReportRoot) still report their type.
$type = $option.GetType().GetProperty('Default').PropertyType -as [string] -replace '^Pester\.'
"| $sectionName.$optionName | $(Format-MdxDescription $option.Description) | ``$type`` | ``$default`` |"
}
@"
### ${sectionName}
$sectionDescription
<div className="table-wrapper">
| Option | Description | Type | Default |
|--------|-------------|-----:|--------:|
$($options -join $eol)
</div>
"@
}
}
function generateSectionsMarkdownAsList {
$configObject = New-PesterConfiguration
foreach ($configSection in $configObject.PSObject.Properties) {
$sectionName = $configSection.Name
$sectionDescription = $configSection.Value -as [string]
$section = $configSection.Value
$options = foreach ($configOption in $section.PSObject.Properties) {
$optionName = $configOption.Name
$option = $configOption.Value
$rawDefault = $option.Default
# Run.RepoRoot defaults to an auto-detected, machine-specific path. Render an illustrative placeholder (C:\MyProject) so the generated docs stay machine-independent.
if ($rawDefault -is [string] -and $rawDefault -eq $configObject.Run.RepoRoot.Value) { $rawDefault = $repoRootPlaceholder }
$default = Format-NicelyMini $rawDefault
# Use the declared property type so options with a $null default (e.g. CodeCoverage.ReportRoot) still report their type.
$type = $option.GetType().GetProperty('Default').PropertyType -as [string]
@"
#### $sectionName.$optionName
**Type:** ``$type``<br/>
**Default:** ``$default``
$($option.Description | ForEach-Object { Format-MdxDescription $_ })
"@
}
# Output markdown string per section
@"
### ${sectionName}
$sectionDescription
$($options -join $eol)
"@
}
}
#endregion
$loadedModule = if ($PSBoundParameters.ContainsKey('PesterVersion')) {
Import-Module Pester -RequiredVersion ($PesterVersion -replace '-\w+$') -PassThru
} else {
Import-Module Pester -PassThru
}
$loadedVersion = if ($loadedModule.PrivateData -and $loadedModule.PrivateData.PSData -and $loadedModule.PrivateData.PSData.PreRelease) {
"$($loadedModule.Version)-$($loadedModule.PrivateData.PSData.PreRelease)"
} else {
$loadedModule.Version
}
if ($PSBoundParameters.ContainsKey('PesterVersion') -and $loadedVersion -ne $PesterVersion) {
throw "Pester $PesterVersion was requested, but version '$loadedVersion' was loaded. Aborting."
}
# generate help for config object and insert it
$startComment = 'GENERATED_PESTER_CONFIGURATION_DOCS_START'
$endComment = 'GENERATED_PESTER_CONFIGURATION_DOCS_END'
$eol = "`n"
$encoding = 'UTF8'
$ConfigurationPagePath = switch ($DocsVersion) {
'Current' { "$PSScriptRoot/docs/usage/configuration.mdx" }
'v5' { "$PSScriptRoot/versioned_docs/version-v5/usage/configuration.mdx" }
}
$generatedConfigDocs = switch ($Style) {
'List' { generateSectionsMarkdownAsList }
'Table' { generateSectionsMarkdownAsTable }
}
$pageContent = Get-Content $ConfigurationPagePath -Encoding $encoding
$sbf = [System.Text.StringBuilder]''
$sectionFound = $false
foreach ($line in $pageContent) {
if (-not $sectionFound -and $line -match $startComment) {
$null = $sbf.AppendLine("$line$eol")
$null = $sbf.AppendLine("*This section was generated using Pester $loadedVersion.*$eol")
$sectionFound = $true
$null = $sbf.AppendJoin($eol, $generatedConfigDocs)
} elseif ($sectionFound -and ($line -match $endComment)) {
$sectionFound = $false
}
if (-not $sectionFound) {
$null = $sbf.AppendLine($line)
}
}
Set-Content -Encoding $encoding -Value $sbf.ToString().TrimEnd() -Path $ConfigurationPagePath