-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild-PSModuleDocumentation.ps1
More file actions
161 lines (142 loc) · 6.9 KB
/
Build-PSModuleDocumentation.ps1
File metadata and controls
161 lines (142 loc) · 6.9 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
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
)
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 -Force -PassThru
# Get all exported commands from the module
$commands = $moduleInfo.ExportedCommands.Values | Where-Object { $_.CommandType -ne 'Alias' }
Write-Host "Found $($commands.Count) commands to process"
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 ' - ✓' -ForegroundColor Green
} catch {
Write-Host ' - ✗' -ForegroundColor Red
$_
}
}
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$fileName = $_.Name
Write-Host "::group:: - [$fileName]"
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
$moduleDocsFolder = Join-Path $DocsOutputFolder.FullName $ModuleName
Get-ChildItem -Path $moduleDocsFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$file = $_
$relPath = [System.IO.Path]::GetRelativePath($moduleDocsFolder, $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, $moduleDocsFolder).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 $moduleDocsFolder -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 source files to docs'
$moduleDocsFolder = Join-Path $DocsOutputFolder.FullName $ModuleName
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, $moduleDocsFolder)
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 '────────────────────────────────────────────────────────────────────────────────'
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$fileName = $_.Name
Write-Host "::group:: - [$fileName]"
Show-FileContent -Path $_
}
}