-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
GenerateDocs.ps1
274 lines (210 loc) · 11.9 KB
/
GenerateDocs.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
# Based on the script from Chocolatey Software (https://github.com/chocolatey/choco/)
# Special thanks to Glenn Sarti (https://github.com/glennsarti) for his help on this.
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.Web
$thisDirectory = (Split-Path -parent $MyInvocation.MyCommand.Definition)
$psModuleDirectory = [System.IO.Path]::GetFullPath("$thisDirectory\extensions\")
$lineFeed = "`r`n"
$sourceLocation = 'https://github.com/mkevenaar/chocolatey-packages/tree/master/'
$navigationFile = Join-Path $thisDirectory "docs\_data\navigation.yml"
$docsFolder = [System.IO.Path]::GetFullPath("$thisDirectory\docs")
$navigation = "toc:$lineFeed"
# $packagesFolder = "automatic,deprecated,manual"
$packagesFolder = @("automatic", "extensions", "deprecated" ,"manual")
function Get-Aliases($commandName){
$aliasOutput = ''
Get-Alias -Definition $commandName -ErrorAction SilentlyContinue | %{ $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)
$text = $text -replace '(community feed[s]?|community repository)', '[$1](https://community.chocolatey.org/packages)'
$text = $text -replace '(Chocolatey for Business|Chocolatey Professional|Chocolatey Pro)(?=[^\w])', '[$1](https://chocolatey.org/compare)'
$text = $text -replace '(Pro[fessional]\s?/\s?Business)', '[$1](https://chocolatey.org/compare)'
$text = $text -replace '([Ll]icensed editions)', '[$1](https://chocolatey.org/compare)'
$text = $text -replace '([Ll]icensed versions)', '[$1](https://chocolatey.org/compare)'
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
}
try
{
$dir = Get-ChildItem $psModuleDirectory | Where-Object {$_.PSISContainer}
foreach ($psModuleName in $dir) {
$navigation += " - title: $psModuleName$($lineFeed)"
$navigation += " subfolderitems:$($lineFeed)"
$psModuleLocation = [System.IO.Path]::GetFullPath("$thisDirectory\extensions\$($psModuleName)\extensions\$($psModuleName -replace ".extension").psm1")
$moduleDocsFolder = [System.IO.Path]::GetFullPath("$thisDirectory\docs\$($psModuleName)")
$sourceFunctions = "$($sourceLocation)/extensions/$($psModuleName)/extensions"
Write-Host "Importing the Module $psModuleName ..."
Import-Module "$psModuleLocation" -Force -Verbose
$relativeName = $psModuleLocation -replace ".extension" -replace [regex]::Escape($thisDirectory), "`$env:ChocolateyInstall"
Write-Host "Working on $docsFolder"
if (Test-Path($moduleDocsFolder)) { Remove-Item $moduleDocsFolder -Force -Recurse -EA SilentlyContinue }
if(-not(Test-Path $moduleDocsFolder)){ mkdir $moduleDocsFolder -EA Continue | Out-Null }
Write-Host 'Copying Index file...'
Copy-Item -Path "$thisDirectory\extensions\$($psModuleName)\README.md" -Destination $moduleDocsFolder\Index.md
$navigation += " - page: Documentation$($lineFeed)"
$navigation += " url: /$psModuleName/Index.html$($lineFeed)"
Write-Host 'Copying Changelog file...'
Copy-Item -Path "$thisDirectory\extensions\$($psModuleName)\CHANGELOG.md" -Destination $moduleDocsFolder\Changelog.md
$navigation += " - page: Changelog$($lineFeed)"
$navigation += " url: /$psModuleName/Changelog.html$($lineFeed)"
Write-Host 'Creating per PowerShell function markdown files...'
Get-Command -Module ($psModuleName -replace ".extension") -CommandType Function | ForEach-Object -Process { Get-Help $_ -Full } | ForEach-Object -Process { `
$commandName = $_.Name
$fileName = Join-Path $moduleDocsFolder "Helpers$($_.Name.Replace('-','')).md"
$url = "Helpers$($_.Name.Replace('-',''))"
$hasCmdletBinding = (Get-Command -Name $commandName).CmdLetBinding
$navigation += " - page: $commandName$($lineFeed)"
$navigation += " url: /$psModuleName/$url.html$($lineFeed)"
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 `"$($relativeName)`" -Force; Get-Help $($_.Name) -Full``.
View the source for [$($_.Name)]($sourceFunctions/$($_.Name)`.ps1)
"@ | Out-File $fileName -Encoding UTF8 -Force
}
}
# Do the packages
$navigation += " - title: Packages$($lineFeed)"
$navigation += " subfolderitems:$($lineFeed)"
$packages = Get-ChildItem -Path $packagesFolder -Directory | Where-Object {$_.PSISContainer} | Sort-Object -Property Name
$packagesDocsFolder = Join-Path $docsFolder "packages"
if (Test-Path($packagesDocsFolder)) { Remove-Item $packagesDocsFolder -Force -Recurse -EA SilentlyContinue }
if(-not(Test-Path $packagesDocsFolder)){ mkdir $packagesDocsFolder -EA Continue | Out-Null }
foreach ($package in $packages) {
#Write-Host "Working on package $package ..."
$NuspecPath = "$($package.FullName)\$($package.Name).nuspec"
if (-not (Test-Path $NuspecPath)) {
#skip, nuspec file does not exists.
continue
}
#Write-Host "Nuspec File $NuspecPath ..."
$filename = Join-Path $packagesDocsFolder "$($package.Name.Replace('-','')).md"
$url = "$($package.Name.Replace('-',''))"
$parameters = ''
if(Test-Path "$($package.FullName)\PARAMETERS.md") {
$parameters = (Get-Content -Path "$($package.FullName)\PARAMETERS.md" | Select-Object -Skip 6 | Out-String)
}
[xml]$nuspec = Get-Content "$NuspecPath" -Encoding UTF8
$meta = $nuspec.package.metadata
$readme += @"
# $( if ( $meta.iconUrl ) { Write-Output "<img src=`"$($meta.iconUrl)`" width=`"32`" height=`"32`"/>" }) [![$($meta.title)](https://img.shields.io/chocolatey/v/$($meta.id).svg?label=$([System.Net.WebUtility]::UrlEncode($meta.title)))](https://community.chocolatey.org/packages/$($meta.id)) [![$($meta.title)](https://img.shields.io/chocolatey/dt/$($meta.id).svg)](https://community.chocolatey.org/packages/$($meta.id))
## Usage
To install $($meta.title), run the following command from the command line or from PowerShell:
```````powershell
choco install $($meta.id)
```````
To upgrade $($meta.title), run the following command from the command line or from PowerShell:
```````powershell
choco upgrade $($meta.id)
```````
To uninstall $($meta.title), run the following command from the command line or from PowerShell:
```````powershell
choco uninstall $($meta.id)
```````
## Description
$( if($meta.description.InnerText) {$meta.description.InnerText} else {$meta.description} )
## Links
[Chocolatey Package Page](https://community.chocolatey.org/packages/$($meta.id))
[Software Site]($($meta.projectUrl))
[Package Source]($($meta.packageSourceUrl))
"@ -replace "<!-- PARAMETERS.md -->", $parameters | Out-File -Encoding UTF8 $filename
$navigation += " - page: `"$($nuspec.package.metadata.title)`"$($lineFeed)"
$navigation += " url: /packages/$url.html$($lineFeed)"
}
Write-Host "Writing navigation file $navigationFile"
$navigation| Out-File $navigationFile -Encoding UTF8 -Force
# if ((git status "$path" -s)) {
# git add $path | Out-Null;
# $message = "($PackageName) Updated icon"
# if ((git log --oneline -1) -match "$([regex]::Escape($message))$") {
# git commit --amend -m "$message" "$path" | Out-Null
# } else {
# git commit -m "$message" "$path" | Out-Null;
# }
# }
Exit 0
}
catch
{
Throw "Failed to generate documentation. $_"
Exit 255
}