-
Notifications
You must be signed in to change notification settings - Fork 8
/
mkdocs.ps1
290 lines (225 loc) · 13.9 KB
/
mkdocs.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
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
# Copyright © 2017 Chocolatey Software, Inc
# Copyright © 2011 - 2017 RealDimensions Software, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Special thanks to Glenn Sarti (https://github.com/glennsarti) for his help on this.
$ErrorActionPreference = 'Stop'
$psModuleName = 'ChocoCCM'
$psModuleLocation = [System.IO.Path]::GetFullPath("$PSScriptRoot\Output\ChocoCCM\ChocoCCM.psm1")
$docsFolder = [System.IO.Path]::GetFullPath("$PSScriptRoot\Output\gen\generated")
$lineFeed = "`r`n"
function Get-Aliases($commandName) {
$aliasOutput = ''
Get-Alias -Definition $commandName -ErrorAction SilentlyContinue | ForEach-Object { $aliasOutput += "``$($_.Name)``$lineFeed" }
if ($aliasOutput -eq $null -or $aliasOutput -eq '') {
$aliasOutput = 'None'
}
Write-Output $aliasOutput
}
function Convert-Example($objItem) {
@"
**$($objItem.title.Replace('-','').Trim())**
~~~powershell
$($objItem.Code.Replace("`n",$lineFeed))
$($objItem.remarks | Where-Object { $_.Text -ne ''} | ForEach-Object { Write-Output $_.Text.Replace("`n", $lineFeed) })
~~~
"@
}
function Replace-CommonItems($text) {
if ($text -eq $null) { return $text }
$text = $text.Replace("`n", $lineFeed)
Write-Output $text
}
function Convert-Syntax($objItem, $hasCmdletBinding) {
$cmd = $objItem.Name
if ($objItem.parameter -ne $null) {
$objItem.parameter | ForEach-Object {
$cmd += ' `' + $lineFeed
$cmd += " "
if ($_.required -eq $false) { $cmd += '[' }
$cmd += "-$($_.name.substring(0,1).toupper() + $_.name.substring(1))"
if ($_.parameterValue -ne $null) { $cmd += " <$($_.parameterValue)>" }
if ($_.parameterValueGroup -ne $null) { $cmd += " {" + ($_.parameterValueGroup.parameterValue -join ' | ') + "}" }
if ($_.required -eq $false) { $cmd += ']' }
}
}
if ($hasCmdletBinding) { $cmd += " [<CommonParameters>]" }
Write-Output "$lineFeed~~~powershell$lineFeed$($cmd)$lineFeed~~~"
}
function Convert-Parameter($objItem, $commandName) {
$parmText = $lineFeed + "### -$($objItem.name.substring(0,1).ToUpper() + $objItem.name.substring(1))"
if ( ($objItem.parameterValue -ne $null) -and ($objItem.parameterValue -ne 'SwitchParameter') ) {
$parmText += ' '
if ([string]($objItem.required) -eq 'false') { $parmText += "[" }
$parmText += "<$($objItem.parameterValue)>"
if ([string]($objItem.required) -eq 'false') { $parmText += "]" }
}
$parmText += $lineFeed
if ($objItem.description -ne $null) {
$parmText += (($objItem.description | ForEach-Object { Replace-CommonItems $_.Text }) -join "$lineFeed") + $lineFeed + $lineFeed
}
if ($objItem.parameterValueGroup -ne $null) {
$parmText += "$($lineFeed)Valid options: " + ($objItem.parameterValueGroup.parameterValue -join ", ") + $lineFeed + $lineFeed
}
$aliases = [string]((Get-Command -Name $commandName).parameters."$($objItem.Name)".Aliases -join ', ')
$required = [string]($objItem.required)
$position = [string]($objItem.position)
$defValue = [string]($objItem.defaultValue)
$acceptPipeline = [string]($objItem.pipelineInput)
$padding = ($aliases.Length, $required.Length, $position.Length, $defValue.Length, $acceptPipeline.Length | Measure-Object -Maximum).Maximum
$parmText += @"
Property | Value
---------------------- | $([string]('-' * $padding))
Aliases | $($aliases)
Required? | $($required)
Position? | $($position)
Default Value | $($defValue)
Accept Pipeline Input? | $($acceptPipeline)
"@
Write-Output $parmText
}
function Convert-CommandText {
param(
[string]$commandText,
[string]$commandName = ''
)
if ( $commandText -match '^\s?NOTE: Options and switches apply to all items passed, so if you are\s?$' `
-or $commandText -match '^\s?installing multiple packages, and you use \`\-\-version\=1\.0\.0\`, it is\s?$' `
-or $commandText -match '^\s?going to look for and try to install version 1\.0\.0 of every package\s?$' `
-or $commandText -match '^\s?passed\. So please split out multiple package calls when wanting to\s?$' `
-or $commandText -match '^\s?pass specific options\.\s?$' `
) {
return
}
$commandText = $commandText -creplace '^(.+)(\s+Command\s*)$', "# `$1`$2 (choco $commandName)"
$commandText = $commandText -creplace '^(Usage|Troubleshooting|Examples|Exit Codes|Connecting to Chocolatey.org|See It In Action|Alternative Sources|Resources|Packages.config|Scripting \/ Integration - Best Practices \/ Style Guide)', '## $1'
$commandText = $commandText -replace '^(Commands|How To Pass Options)', '## $1'
$commandText = $commandText -replace '^(WebPI|Windows Features|Ruby|Cygwin|Python)\s*$', '### $1'
$commandText = $commandText -replace 'NOTE\:', '**NOTE:**'
$commandText = $commandText -replace 'the command reference', '[[how to pass arguments|CommandsReference#how-to-pass-options--switches]]'
$commandText = $commandText -replace '(community feed[s]?|community repository)', '[$1](https://chocolatey.org/packages)'
#$commandText = $commandText -replace '\`(apikey|install|upgrade|uninstall|list|search|info|outdated|pin)\`', '[[`$1`|Commands$1]]'
$commandText = $commandText -replace '\`([choco\s]*)(apikey|install|upgrade|uninstall|list|search|info|outdated|pin)\`', '[[`$1$2`|Commands$2]]'
$commandText = $commandText -replace '^(.+):\s(.+.gif)$', '![$1]($2)'
$commandText = $commandText -replace '^(\s+)\<\?xml', "~~~xml$lineFeed`$1<?xml"
$commandText = $commandText -replace '^(\s+)</packages>', "`$1</packages>$lineFeed~~~"
$commandText = $commandText -replace '(Chocolatey for Business|Chocolatey Professional|Chocolatey Pro)(?=[^\w])', '[$1](https://chocolatey.org/compare)'
$commandText = $commandText -replace '(Pro[fessional]\s?/\s?Business)', '[$1](https://chocolatey.org/compare)'
$commandText = $commandText -replace '([Ll]icensed editions)', '[$1](https://chocolatey.org/compare)'
$commandText = $commandText -replace '([Ll]icensed versions)', '[$1](https://chocolatey.org/compare)'
$optionsSwitches = @'
## $1
**NOTE:** Options and switches apply to all items passed, so if you are
running a command like install that allows installing multiple
packages, and you use `--version=1.0.0`, it is going to look for and
try to install version 1.0.0 of every package passed. So please split
out multiple package calls when wanting to pass specific options.
Includes [[default options/switches|CommandsReference#default-options-and-switches]] (included below for completeness).
~~~
'@
$commandText = $commandText -replace '^(Options and Switches)', $optionsSwitches
$optionsSwitches = @'
## $1
**NOTE:** Options and switches apply to all items passed, so if you are
running a command like install that allows installing multiple
packages, and you use `--version=1.0.0`, it is going to look for and
try to install version 1.0.0 of every package passed. So please split
out multiple package calls when wanting to pass specific options.
~~~
'@
$commandText = $commandText -replace '^(Default Options and Switches)', $optionsSwitches
Write-Output $commandText
}
function Convert-CommandReferenceSpecific($commandText) {
$commandText = [Regex]::Replace($commandText, '\s?\s?\*\s(\w+)\s\-',
{
param($m)
$commandName = $m.Groups[1].Value
$commandNameUpper = $($commandName.Substring(0, 1).ToUpper() + $commandName.Substring(1))
" * [[$commandName|Commands$($commandNameUpper)]] -"
}
)
#$commandText = $commandText -replace '\s?\s?\*\s(\w+)\s\-', ' * [[$1|Commands$1]] -'
$commandText = $commandText.Replace("## Default Options and Switches", "## See Help Menu In Action$lineFeed$lineFeed![choco help in action](https://raw.githubusercontent.com/wiki/chocolatey/choco/images/gifs/choco_help.gif)$lineFeed$lineFeed## Default Options and Switches")
Write-Output $commandText
}
function Generate-TopLevelCommandReference {
Write-Host "Generating Top Level Command Reference"
$fileName = "$docsFolder\CommandsReference.md"
$commandOutput = @("# Command Reference$lineFeed")
$commandOutput += @("<!-- This file is automatically generated based on output from the files at $sourceCommands using $($sourceLocation)GenerateDocs.ps1. Contributions are welcome at the original location(s). --> $lineFeed")
$commandOutput += $(& $chocoExe -? -r)
$commandOutput += @("$lineFeed~~~$lineFeed")
$commandOutput += @("$lineFeed$lineFeed*NOTE:* This documentation has been automatically generated from ``choco -h``. $lineFeed")
$commandOutput | ForEach-Object { Convert-CommandText($_) } | ForEach-Object { Convert-CommandReferenceSpecific($_) } | Out-File $fileName -Encoding UTF8 -Force
}
function Generate-CommandReference($commandName) {
$fileName = Join-Path $docsFolder "Commands$($commandName).md"
Write-Host "Generating $fileName ..."
$commandOutput += @("<!-- This file is automatically generated based on output from $($sourceCommands)/Chocolatey$($commandName)Command.cs using $($sourceLocation)GenerateDocs.ps1. Contributions are welcome at the original location(s). If the file is not found, it is not part of the open source edition of Chocolatey or the name of the file is different. --> $lineFeed")
$commandOutput += $(& $chocoExe $commandName.ToLower() -h -r)
$commandOutput += @("$lineFeed~~~$lineFeed$lineFeed[[Command Reference|CommandsReference]]")
$commandOutput += @("$lineFeed$lineFeed*NOTE:* This documentation has been automatically generated from ``choco $($commandName.ToLower()) -h``. $lineFeed")
$commandOutput | ForEach-Object { Convert-CommandText $_ $commandName.ToLower() } | Out-File $fileName -Encoding UTF8 -Force
}
try {
Write-Host "Importing the Module $psModuleName ..."
Import-Module "$psModuleLocation" -Force -Verbose
# Switch Get-PackageParameters back for documentation
if (Test-Path($docsFolder)) { Remove-Item $docsFolder -Force -Recurse -ErrorAction SilentlyContinue }
if (-not(Test-Path $docsFolder)) { New-Item $docsFolder -ItemType Directory -ErrorAction Continue | Out-Null }
Write-Host 'Creating per PowerShell function markdown files...'
Get-Command -Module $psModuleName -CommandType Function | ForEach-Object -Process { Get-Help $_ -Full } | ForEach-Object -Process {
$commandName = $_.Name
$fileName = Join-Path $docsFolder "$($_.Name.Replace('-','')).md"
$global:powerShellReferenceTOC += "$lineFeed * [[$commandName|$([System.IO.Path]::GetFileNameWithoutExtension($fileName))]]"
$hasCmdletBinding = (Get-Command -Name $commandName).CmdLetBinding
Write-Host "Generating $fileName ..."
@"
# $($_.Name)
<!-- This documentation is automatically generated from $sourceFunctions/$($_.Name)`.ps1 using $($sourceLocation)GenerateDocs.ps1. Contributions are welcome at the original location(s). -->
$(Replace-CommonItems $_.Synopsis)
## Syntax
$( ($_.syntax.syntaxItem | ForEach-Object { Convert-Syntax $_ $hasCmdletBinding }) -join "$lineFeed$lineFeed")
$( if ($_.description -ne $null) { $lineFeed + "## Description" + $lineFeed + $lineFeed + $(Replace-CommonItems $_.description.Text) })
$( if ($_.alertSet -ne $null) { $lineFeed + "## Notes" + $lineFeed + $lineFeed + $(Replace-CommonItems $_.alertSet.alert.Text) })
## Aliases
$(Get-Aliases $_.Name)
$( if ($_.Examples -ne $null) { Write-Output "$lineFeed## Examples$lineFeed$lineFeed"; ($_.Examples.Example | ForEach-Object { Convert-Example $_ }) -join "$lineFeed$lineFeed"; Write-Output "$lineFeed" })
## Inputs
$( if ($_.InputTypes -ne $null -and $_.InputTypes.Length -gt 0 -and -not $_.InputTypes.Contains('inputType')) { $lineFeed + " * $($_.InputTypes)" + $lineFeed} else { 'None'})
## Outputs
$( if ($_.ReturnValues -ne $null -and $_.ReturnValues.Length -gt 0 -and -not $_.ReturnValues.StartsWith('returnValue')) { "$lineFeed * $($_.ReturnValues)$lineFeed"} else { 'None'})
## Parameters
$( if ($_.parameters.parameter.count -gt 0) { $_.parameters.parameter | ForEach-Object { Convert-Parameter $_ $commandName }}) $( if ($hasCmdletBinding) { "$lineFeed### <CommonParameters>$lineFeed$($lineFeed)This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see ``about_CommonParameters`` http://go.microsoft.com/fwlink/p/?LinkID=113216 ." } )
$( if ($_.relatedLinks -ne $null) {Write-Output "$lineFeed## Links$lineFeed$lineFeed"; $_.relatedLinks.navigationLink | Where-Object { $_.linkText -ne $null} | ForEach-Object { Write-Output "* [[$($_.LinkText)|Helpers$($_.LinkText.Replace('-',''))]]$lineFeed" }})
[[Function Reference|HelpersReference]]
***NOTE:*** This documentation has been automatically generated from ``Import-Module `"`ChocoCCM`" -Force; Get-Help $($_.Name) -Full``.
View the source for [$($_.Name)]($sourceFunctions/$($_.Name)`.ps1)
"@ | Out-File $fileName -Encoding UTF8 -Force
}
Write-Host "Generating Top Level PowerShell Reference"
$fileName = Join-Path $docsFolder 'HelpersReference.md'
$global:powerShellReferenceTOC += @'
'@
$global:powerShellReferenceTOC | Out-File $fileName -Encoding UTF8 -Force
Write-Host "Generating command reference markdown files"
#Get-Command -Module ChocoCCM | Foreach-Object { Generate-CommandReference("$($_.Name)")}
#Generate-TopLevelCommandReference
Exit 0
}
catch {
Throw "Failed to generate documentation. $_"
Exit 255
}