generated from PSModule/Template-Action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild-PSModuleDocumentation.ps1
More file actions
247 lines (204 loc) · 9.69 KB
/
Build-PSModuleDocumentation.ps1
File metadata and controls
247 lines (204 loc) · 9.69 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
function Build-PSModuleDocumentation {
<#
.SYNOPSIS
Builds a module.
.DESCRIPTION
Builds a module.
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingWriteHost', '', Scope = 'Function',
Justification = 'Want to just write to the console, not the pipeline.'
)]
param(
# Name of the module.
[Parameter(Mandatory)]
[string] $ModuleName,
# Path to the folder where the modules are located.
[Parameter(Mandatory)]
[string] $ModuleSourceFolderPath,
# Path to the folder where the built modules are outputted.
[Parameter(Mandatory)]
[string] $ModulesOutputFolderPath,
# Path to the folder where the documentation is outputted.
[Parameter(Mandatory)]
[string] $DocsOutputFolderPath,
# Show GitHub Step Summary even when all commands succeed.
[Parameter()]
[bool] $ShowSummaryOnSuccess = $false
)
Write-Host "::group::Documenting module [$ModuleName]"
[pscustomobject]@{
ModuleName = $ModuleName
ModuleSourceFolderPath = $ModuleSourceFolderPath
ModulesOutputFolderPath = $ModulesOutputFolderPath
DocsOutputFolderPath = $DocsOutputFolderPath
} | Format-List | Out-String
if (-not (Test-Path -Path $ModuleSourceFolderPath)) {
Write-Error "Source folder not found at [$ModuleSourceFolderPath]"
exit 1
}
$moduleSourceFolder = Get-Item -Path $ModuleSourceFolderPath
$moduleOutputFolder = New-Item -Path $ModulesOutputFolderPath -Name $ModuleName -ItemType Directory -Force
$docsOutputFolder = New-Item -Path $DocsOutputFolderPath -ItemType Directory -Force
Write-Host '::group::Build docs - Generate markdown help - Raw'
Install-PSModule -Path $ModuleOutputFolder
$moduleInfo = Import-Module -Name $ModuleName -PassThru -Verbose:$false -Force
# Get all exported commands from the module
$commands = $moduleInfo.ExportedCommands.Values | Where-Object { $_.CommandType -ne 'Alias' }
Write-Host "::group::Build docs - Generating markdown help files for $($commands.Count) commands in [$docsOutputFolder]"
$commandResults = [System.Collections.Generic.List[PSObject]]::new()
foreach ($command in $commands) {
try {
Write-Host "$($command.Name)" -NoNewline
$params = @{
CommandInfo = $command
OutputFolder = $docsOutputFolder
Encoding = 'utf8'
ProgressAction = 'SilentlyContinue'
ErrorAction = 'Stop'
Force = $true
}
$null = New-MarkdownCommandHelp @params
Write-Host " - $($PSStyle.Foreground.Green)✓$($PSStyle.Reset)"
$commandResults.Add([PSCustomObject]@{
CommandName = $command.Name
Status = 'Success'
Error = $null
ErrorString = $null
})
} catch {
Write-Host " - $($PSStyle.Foreground.Red)✗$($PSStyle.Reset)"
$commandResults.Add([PSCustomObject]@{
CommandName = $command.Name
Status = 'Failed'
Error = $_
ErrorString = $_.ToString()
})
Write-Warning "Failed to generate markdown help for $($command.Name): $_"
}
}
Write-Host '::endgroup::'
$failedCommands = $commandResults | Where-Object { $_.Status -eq 'Failed' }
$successfulCommands = $commandResults | Where-Object { $_.Status -eq 'Success' }
$hasFailures = $failedCommands.Count -gt 0
$shouldShowSummary = $hasFailures -or $ShowSummaryOnSuccess
# Generate summary if ShowSummaryOnSuccess is enabled
if ($shouldShowSummary) {
$statusIcon = $hasFailures ? '❌' : '✅'
$statusText = $hasFailures ? 'Failed' : 'Succeeded'
Write-Host "::group::Build docs - Documentation generation summary $statusIcon"
$successCount = $successfulCommands.Count
$failureCount = $failedCommands.Count
$summaryContent = @"
# $statusIcon Documentation Build $($statusText.ToLower())
| Success | Failure |
|---------|---------|
| $successCount | $failureCount |
"@
if ($failedCommands) {
$summaryContent += @"
<details><summary>Failed</summary>
<p>
$(($failedCommands | ForEach-Object { "- ``$($_.CommandName)`` `n" }) -join '')
</p>
</details>
"@
}
if ($successfulCommands) {
$summaryContent += @"
<details><summary>Succeeded</summary>
<p>
$(($successfulCommands | ForEach-Object { "- ``$($_.CommandName)`` `n" }) -join '')
</p>
</details>
"@
}
$summaryContent | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
Write-Host "$summaryContent"
Write-Host '::endgroup::'
}
# Fail the task if there were any failures (independent of summary display)
if ($hasFailures) {
Write-Error "Documentation generation failed for $($failedCommands.Count) command(s). See above for details."
exit 1
}
Write-Host '::group::Build docs - Generated files'
Get-ChildItem -Path $docsOutputFolder -Recurse | Select-Object -ExpandProperty FullName
Get-ChildItem -Path $docsOutputFolder -Recurse -Force -Include '*.md' | Sort-Object FullName | ForEach-Object {
$relPath = [System.IO.Path]::GetRelativePath($docsOutputFolder, $_.FullName)
Write-Host "::group:: - [$relPath]"
Show-FileContent -Path $_
}
Write-Host '::group::Build docs - Fix markdown code blocks'
Get-ChildItem -Path $docsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$content = Get-Content -Path $_.FullName
$fixedOpening = $false
$newContent = @()
foreach ($line in $content) {
if ($line -match '^```$' -and -not $fixedOpening) {
$line = $line -replace '^```$', '```powershell'
$fixedOpening = $true
} elseif ($line -match '^```.+$') {
$fixedOpening = $true
} elseif ($line -match '^```$') {
$fixedOpening = $false
}
$newContent += $line
}
$newContent | Set-Content -Path $_.FullName
}
Write-Host '::group::Build docs - Fix markdown escape characters'
Get-ChildItem -Path $docsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$content = Get-Content -Path $_.FullName -Raw
$content = $content -replace '\\`', '`'
$content = $content -replace '\\\[', '['
$content = $content -replace '\\\]', ']'
$content = $content -replace '\\\<', '<'
$content = $content -replace '\\\>', '>'
$content = $content -replace '\\\\', '\'
$content | Set-Content -Path $_.FullName
}
Write-Host '::group::Build docs - Structure markdown files to match source files'
$PublicFunctionsFolder = Join-Path $ModuleSourceFolder.FullName 'functions\public' | Get-Item
Get-ChildItem -Path $docsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$file = $_
$relPath = [System.IO.Path]::GetRelativePath($docsOutputFolder, $file.FullName)
Write-Host " - $relPath"
Write-Host " Path: $file"
# find the source code file that matches the markdown file
$scriptPath = Get-ChildItem -Path $PublicFunctionsFolder -Recurse -Force | Where-Object { $_.Name -eq ($file.BaseName + '.ps1') }
Write-Host " PS1 path: $scriptPath"
$docsFilePath = ($scriptPath.FullName).Replace($PublicFunctionsFolder.FullName, $docsOutputFolder).Replace('.ps1', '.md')
Write-Host " MD path: $docsFilePath"
$docsFolderPath = Split-Path -Path $docsFilePath -Parent
$null = New-Item -Path $docsFolderPath -ItemType Directory -Force
Move-Item -Path $file.FullName -Destination $docsFilePath -Force
}
Write-Host '::group::Build docs - Fix frontmatter title'
Get-ChildItem -Path $docsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$content = Get-Content -Path $_.FullName -Raw
# Replace 'title:' with 'ms.title:' in frontmatter only (between --- markers)
$content = $content -replace '(?s)^(---.*?)title:(.*?---)', '$1ms.title:$2'
$content | Set-Content -Path $_.FullName
}
Write-Host '::group::Build docs - Move markdown files from public functions folder to docs output folder'
Get-ChildItem -Path $PublicFunctionsFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$file = $_
$relPath = [System.IO.Path]::GetRelativePath($PublicFunctionsFolder.FullName, $file.FullName)
Write-Host " - $relPath"
Write-Host " Path: $file"
$docsFilePath = ($file.FullName).Replace($PublicFunctionsFolder.FullName, $docsOutputFolder)
Write-Host " MD path: $docsFilePath"
$docsFolderPath = Split-Path -Path $docsFilePath -Parent
$null = New-Item -Path $docsFolderPath -ItemType Directory -Force
Move-Item -Path $file.FullName -Destination $docsFilePath -Force
}
Write-Host '::endgroup::'
Write-Host '────────────────────────────────────────────────────────────────────────────────'
Get-ChildItem -Path $docsOutputFolder -Recurse -Force -Include '*.md' | Sort-Object FullName | ForEach-Object {
$relPath = [System.IO.Path]::GetRelativePath($docsOutputFolder, $_.FullName)
Write-Host "::group:: - [$relPath]"
Show-FileContent -Path $_
}
}