-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocgenerator.ps1
More file actions
220 lines (182 loc) · 8.19 KB
/
Copy pathdocgenerator.ps1
File metadata and controls
220 lines (182 loc) · 8.19 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
<#
.SYNOPSIS
Creates Markdown documentations from PowerShell modules and scripts.
.DESCRIPTION
Generates a Markdown (.md) documentation for each PowerShell script (.ps1) in a folder or each Cmdlet in a Module, which has the necessary headers required by Get-Help.
Also generates an index document which lists (and links to) all generated files. Each file name can be preceded by a prefix so that they are listed together when viewing the Wiki documents.
.PARAMETER SourceScriptFolder
Source folder where the scripts are located
.PARAMETER SourceModul
Source module from which the documentation are generated
.PARAMETER DocumentationOutputFolder
Output folder where the documentation will be created
.PARAMETER DocumentationIndexPath
The name of the main file that refer all generated files
.EXAMPLE
C:\PS> .\docgenerator.ps1 -SourceScriptFolder 'C:\temp\project\scripts' -DocumentationOutputFolder 'C:\temp\project\docs' -DocumentationIndexPath 'README.md'
.EXAMPLE
C:\PS> .\docgenerator.ps1 -SourceModul AzureAD -DocumentationOutputFolder 'C:\temp\project\docs' -DocumentationIndexPath 'README.md'
rserser
.LINK
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comment_based_help?view=powershell-7
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Script", HelpMessage = "Source folder where the scripts are located")]
[ValidateScript({ Test-Path $_ -PathType 'Container' })]
[string]$SourceScriptFolder,
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Module", HelpMessage = "Source folder where the scripts are located")]
[ValidateScript({ Get-Module -Name $_ })]
[string]$SourceModul,
[Parameter(Mandatory = $true, Position = 1, HelpMessage = "Output folder where the documentation will be created")]
[ValidateScript({ Test-Path $_ -PathType 'Container' })]
[string]$DocumentationOutputFolder,
[Parameter(Mandatory = $false, Position = 2, HelpMessage = "Name of the index (markdown) file")]
[ValidateScript({ Test-Path $_ -IsValid })]
[string]$DocumentationIndex="index.md"
)
$arrHelpProperties = [ordered]@{
Synopsis = $true;
Syntax = $true;
Description = $true;
Examples = $true;
Parameters = $false;
relatedLinks= $false;
}
$arrParameterProperties = @(
"Required",
"DefaultValue",
"ParameterValue",
"Position",
"PipelineInput"
)
# Prefix and Suffix of files
$docNamePrefix = ""
$docNameSuffix = ".md"
function Write-Markdown {
param (
[string]$Titel,
[string]$Description,
[int]$Indent,
[string]$Path
)
"#" * $Indent + " " + $Titel | Out-File $Path -Append
if ($Description) {
$Description + "`n"| Out-File $Path -Append
}
}
function Get-MarkdownScriptBlock {
param (
[string]$Language,
[string]$Code
)
$block = "``````" + $Language
$block += "`n{0}`n" -f $Code
$block += "``````"
return $block
}
function Get-DocFromHelp {
param (
$help,
$name,
$outputFile,
$DocumentationIndexPath,
$SourceScriptFolder="_"
)
if ($help.getType().Name -eq "String") {
# If there's no inline help in the script then Get-Help returns a string
Write-Warning -Message "Inline help not found for $($Name)"
return
}
foreach ($i in $arrHelpProperties.Keys) {
if (!($help.$i)) {
if ($arrHelpProperties[$i]) {
Write-Warning -Message ("{0} not defined in {1}" -f $i, $Name)
return
} else {
Write-Warning -Message ("{0} not defined in {1}" -f $i, $Name)
}
}
}
# Delete old file
Remove-Item -Path $outputFile -Force -ErrorAction Ignore
# Script and Synposis to the index
$Titel = "[{0}]({1})" -f $name, ($docNamePrefix + $Name + $docNameSuffix)
Write-Markdown -Titel $Titel -Description $help.synopsis -Indent 2 -Path $DocumentationIndexPath
# Titel and Synopsis
Write-Markdown -Titel $name -Description $help.Synopsis -Indent 1 -Path $outputFile
# Syntax
$syntax = ($help.Syntax | Out-String).trim().Replace($SourceScriptFolder + "\", "").Split([Environment]::NewLine, [StringSplitOptions]::RemoveEmptyEntries)
$Description = foreach ($i in $syntax) {
Get-MarkdownScriptBlock -Language "PowerShell" -Code $i
"`n"
}
Write-Markdown -Titel "Syntax" -Description $Description -Indent 2 -Path $outputFile
# Description
Write-Markdown -Titel "Description" -Description $help.Description.Text -Indent 2 -Path $outputFile
# Examples
Write-Markdown -Titel "Examples" -Indent 2 -Path $outputFile
forEach ($i in $help.Examples.Example) {
$Titel = $i.title.Replace("--------------------------", "").Replace("BEISPIEL", "Example")
$Description = Get-MarkdownScriptBlock -Language "PowerShell" -Code $i.Code
$Description += if (![string]::IsNullOrWhiteSpace($i.Remarks.Text)) {
"`n"
($i.Remarks | Out-String).Trim()
}
Write-Markdown -Titel $Titel -Description $Description -Indent 3 -Path $outputFile
}
# Paramaters
Write-Markdown -Titel "Paramaters" -Indent 2 -Path $outputFile
forEach ($i in $help.Parameters.Parameter) {
$Titel = "``-{0}``" -f $i.name
$Description = "{0}`n`n" -f ($i.Description | Out-String).Trim()
$Description += "| | |`n|---|---|`n"
$Description += "| Type: | {0} |`n" -f $i.Type.Name
$Description += forEach ($j in $arrParameterProperties) {
if ($i.$j) {
"| {0}: | {1} |`n" -f $j, $i.$j
}
}
Write-Markdown -Titel $Titel -Description $Description -Indent 3 -Path $outputFile
}
# Related Links
$relatedLinks = $help.relatedLinks.navigationLink.linkText
$relatedLinks += $help.relatedLinks.navigationLink.uri
$Description = ForEach ($i in $relatedLinks) {
if ($i) {
"* $i`n"
}
}
Write-Markdown -Titel "Related Links" -Description $Description -Indent 2 -Path $outputFile
}
$DocumentationIndexPath = Join-Path -Path $DocumentationOutputFolder -ChildPath $DocumentationIndex
Remove-Item -Path $DocumentationIndexPath -Force -ErrorAction Ignore
if ($SourceScriptFolder) {
# Index header
Write-Markdown -Titel "PowerShell Scripts" -Indent 1 -Path $DocumentationIndexPath
# Get the scripts from the folder
$SourceScriptFolder = Resolve-Path -Path $SourceScriptFolder
$scripts = Get-Childitem $SourceScriptFolder -Filter "*.ps1"
$index = 0
foreach ($script in $scripts) {
$index ++
Write-Progress -Activity "Documenting scripts" -Status ("Script $index of $($scripts.count)") -CurrentOperation ("Documenting: $($Script.BaseName)") -PercentComplete ($index / $scripts.count * 100)
$help = Get-Help $script.FullName -ErrorAction "SilentlyContinue"
$outputFile = Join-Path -Path $DocumentationOutputFolder -ChildPath ($docNamePrefix + $script.BaseName + $docNameSuffix)
Get-DocFromHelp -help $help -name $script.name -outputFile $outputFile -DocumentationIndexPath $DocumentationIndexPath
}
} else {
# Index header
Write-Markdown -Titel "$SourceModul Module" -Indent 1 -Path $DocumentationIndexPath
# Get Cmdlets from Modul
$moduls = Get-Command -Module $SourceModul
$index = 0
foreach ($modul in $moduls.Name) {
$index ++
Write-Progress -Activity "Documenting Modul" -Status ("Script $index of $($moduls.count)") -CurrentOperation ("Documenting: $($Script.BaseName)") -PercentComplete ($index / $moduls.count * 100)
$help = Get-Help $modul -ErrorAction "SilentlyContinue"
$outputFile = Join-Path -Path $DocumentationOutputFolder -ChildPath ($docNamePrefix + $modul + $docNameSuffix)
Get-DocFromHelp -help $help -name $modul -outputFile $outputFile -DocumentationIndexPath $DocumentationIndexPath
}
}
Write-Progress -Activity "Documenting scripts" -Completed