forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-cmdlet-documentation.ps1
163 lines (134 loc) · 6.28 KB
/
update-cmdlet-documentation.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
<#
.SYNOPSIS
Generates Markdown documentation for the Chocolatey.PowerShell portion of Chocolatey's installer module commands.
.DESCRIPTION
Use this script when modifying or adding commands into the Chocolatey.PowerShell project.
You will need the chocolatey/docs repository cloned locally in order to use this script.
When all documentation files this script is monitoring have been filled out (contain no remaining {{ template tokens }}), this script will generate/update the external help xml files for PowerShell.
#>
[CmdletBinding()]
param(
# Specify the path to the chocolatey/docs repository root locally. Defaults to ../docs
[Parameter()]
[string]
$DocsRepositoryPath = "$PSScriptRoot/../docs",
# Specify the new commands' names to generate new documentation pages for them.
[Parameter()]
[Alias('NewCommands')]
[string[]]
$NewCommand,
# Opens any new or incomplete files in the default editor for Markdown (.md) files for editing.
[Parameter()]
[switch]
$OpenUnfinished
)
if (-not (Get-Module -ListAvailable PlatyPS)) {
Write-Warning "PlatyPS module not found, attempting to install from PSGallery"
Install-Module PlatyPS -Scope CurrentUser
}
$documentationPath = Join-Path $DocsRepositoryPath -ChildPath "src\content\docs\en-us\create\cmdlets"
if (-not (Test-Path $DocsRepositoryPath)) {
throw "PowerShell commands docs folder was not found at '$documentationPath'. Please clone the chocolatey/docs repository locally first, and/or provide the path to the repo root as -DocsRepositoryPath to this script."
}
$dllPath = "$PSScriptRoot/code_drop/temp/_PublishedLibs/Chocolatey.PowerShell/Chocolatey.PowerShell.dll"
if (-not (Test-Path $dllPath)) {
throw "Please run this repository's build.ps1 file before trying to build markdown help for this module."
}
# Rename .mdx to .md and transform anything platyps doesn't like and can't handle
$renamedFiles = Get-ChildItem -Path $documentationPath -Filter '*.md*' |
Where-Object Name -notlike "index.*" |
Rename-Item -NewName { $_.BaseName + ".md" } -PassThru |
ForEach-Object {
$content = Get-Content -Path $_.FullName
$content = $content | ForEach-Object {
# replace xref with markdown-ish link so platyPS can process things
if ($_ -match '<Xref[^>]+?>') {
$xml = [xml]$_
$label = $xml.Xref.title
$xref = $xml.Xref.value
$anchor = $xml.Xref.anchor
$classes = $xml.Xref.classes
if ($anchor -and $classes) {
"[${label}](xref:${xref}#${anchor},${classes})"
}
elseif ($classes) {
"[${label}](xref:${xref},${classes})"
}
elseif ($anchor) {
"[${label}](xref:${xref}#${anchor})"
}
else {
"[${label}](xref:${xref})"
}
}
else {
$_
}
}
$content | Set-Content -Path $_.FullName
}
# Import the module .dll to generate / update help from.
Import-Module $dllPath
if (-not (Get-Module Chocolatey.PowerShell)) {
throw "The Chocolatey.PowerShell module was not able to be loaded, exiting documentation generation."
}
$newOrUpdatedFiles = [System.Collections.Generic.HashSet[System.IO.FileSystemInfo]] @(
if ($NewCommand) {
New-MarkdownHelp -Command $NewCommand -OutputFolder "$PSScriptRoot\docs" -ExcludeDontShow
}
Update-MarkdownHelp -Path $documentationPath -ExcludeDontShow
)
$incompleteFiles = $newOrUpdatedFiles | Select-String '\{\{[^}]+}}' | Select-Object -ExpandProperty Path
if ($incompleteFiles) {
Write-Warning "The following files contain {{ template tokens }} from PlatyPS that must be replaced with help content before they are committed to the repository:"
$incompleteFiles | Write-Warning
if ($OpenUnfinished) {
$incompleteFiles | Invoke-Item
}
Write-Warning "Run this script again once these files have been updated in order to generate the XML help documentation for the module."
}
else {
New-ExternalHelp -Path $documentationPath -OutputPath "$PSScriptRoot/src/Chocolatey.PowerShell" -Force
}
$newOrUpdatedFiles = $newOrUpdatedFiles |
Rename-Item -NewName { $_.BaseName + ".mdx" } -PassThru |
ForEach-Object {
$content = Get-Content -Path $_.FullName
$frontMatterBounds = 0
$content = $content | ForEach-Object {
if ($_ -match '\[(?<name>[^\]]+)\]\(xref:(?<xref>[^#,]+)(#(?<anchor>[^,]+))?,(?<classes>[^)]+)\)') {
# replace any lines that are an xref link with the html/xml format that astro uses <Xref ... />
$xml = [xml]::new()
$node = $xml.CreateElement('Xref')
$title = $xml.CreateAttribute('title')
$title.Value = $matches['name']
$null = $node.Attributes.Append($title)
$target = $xml.CreateAttribute('value')
$target.Value = $matches['xref']
$null = $node.Attributes.Append($target)
if ($matches['anchor']) {
$anchor = $xml.CreateAttribute('anchor')
$anchor.Value = $matches['anchor']
$null = $node.Attributes.Append($anchor)
}
$classes = $xml.CreateAttribute('classes')
$classes.Value = $matches['classes']
$null = $node.Attributes.Append($classes)
$node.OuterXml
}
else {
# after the second --- where we exit the frontmatter, add the xref import to the document
$_
if ($_ -eq '---') {
$frontMatterBounds++
if ($frontMatterBounds -eq 2) {
"import Xref from '@components/Xref.astro';"
}
}
}
}
$content | Set-Content -Path $_.FullName
$_
}
# Output the new/updated files so calling user knows what files the script has touched.
$newOrUpdatedFiles