-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME.md.ps1
More file actions
307 lines (252 loc) · 8.49 KB
/
README.md.ps1
File metadata and controls
307 lines (252 loc) · 8.49 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<#
.SYNOPSIS
README.md.ps1
.DESCRIPTION
README.md.ps1 makes README.md
This is a simple and helpful scripting convention for writing READMEs.
`./README.md.ps1 > ./README.md`
Feel free to copy and paste this code.
Please document your parameters, and add NOTES.
.NOTES
This README.md.ps1 is used to generate help for a module.
It:
* Outputs the name and description
* Provides installation instructions
* Lists commands
* Lists parameters
* Lists examples
* Lists Extended Types
.EXAMPLE
./README.md.ps1 > ./README.md
.EXAMPLE
Get-Help ./README.md.ps1
#>
param(
# The name of the module
[string]$ModuleName = $($PSScriptRoot | Split-Path -Leaf),
# The domains that serve git repositories.
# If the project uri links to this domain,
# installation instructions will show how to import the module locally.
[string[]]
$GitDomains = @(
'github.com', 'tangled.org', 'tangled.sh', 'codeberg.org'
),
# A list of types the module exposes
[Alias('ModuleTypeNames','ModuleTypes')]
[string[]]
$ModuleTypeName = @(
Get-ChildItem -Path $PSScriptRoot -Filter *.types.ps1xml |
Select-Xml /Types/Type/Name |
Foreach-Object {$_.Node.InnerText } |
Sort-Object
),
# The name of the root directory containing types.
[string]
$TypeRoot = 'Types',
# If set, we don't need no badges.
[switch]
$NoBadge,
# If set, will not display gallery instructions or badges
[switch]
$NotOnGallery
)
Push-Location $PSScriptRoot
# Import the module
$module = Import-Module "./$ModuleName.psd1" -PassThru
# And output a header
"# $module"
if (-not $NoBadge) {
# If it is on the gallery, show the downloads badge.
if (-not $NotOnGallery) {
@(
"[!"
"[$ModuleName](https://img.shields.io/powershellgallery/dt/$ModuleName)"
"](https://www.powershellgallery.com/packages/$ModuleName/)"
) -join ''
}
}
# Show the module description
"## $($module.Description)"
# Show any intro section defined in the manifest
$module.PrivateData.PSData.PSIntro
#region Boilerplate installation instructions
if (-not $NotOnGallery) {
@"
## Installing and Importing
You can install $ModuleName from the [PowerShell gallery](https://powershellgallery.com/)
~~~PowerShell
Install-Module $($ModuleName) -Scope CurrentUser -Force
~~~
Once installed, you can import the module with:
~~~PowerShell
Import-Module $ModuleName -PassThru
~~~
"@
}
#endregion Gallery installation instructions
#region Git installation instructions
$projectUri = $module.PrivateData.PSData.ProjectURI -as [uri]
if ($projectUri.DnsSafeHost -in $GitDomains) {
@"
You can also clone the repo and import the module locally:
~~~PowerShell
git clone $projectUri
cd ./$ModuleName
Import-Module ./ -PassThru
~~~
"@
}
#endregion Git installation instructions
#region Exported Functions
$exportedFunctions = $module.ExportedFunctions
if ($exportedFunctions) {
"## Functions"
"$($ModuleName) has $($exportedFunctions.Count) function$(
if ($exportedFunctions.Count -gt 1) { "s"}
)"
foreach ($export in $exportedFunctions.Keys | Sort-Object) {
# Get help if it there is help to get
$help = Get-Help $export
# If the help is a string,
if ($help -is [string]) {
# make it preformatted text
"~~~"
"$export"
"~~~"
} else {
# Otherwise, add list the export
"### $($export)"
# And make it's synopsis a header
"#### $($help.SYNOPSIS)"
# put the description below that
"$($help.Description.text -join [Environment]::NewLine)"
# Make a table of parameters
if ($help.parameters.parameter) {
"##### Parameters"
""
"|Name|Type|Description|"
"|-|-|-|"
foreach ($parameter in $help.Parameters.Parameter) {
"|$($parameter.Name)|$($parameter.type.name)|$(
$parameter.description.text -replace '(?>\r\n|\n)', '<br/>'
)|"
}
""
}
# Show our examples
"##### Examples"
$exampleNumber = 0
foreach ($example in $help.examples.example) {
$markdownLines = @()
$exampleNumber++
$nonCommentLine = $false
"###### Example $exampleNumber"
# Combine the code and remarks
$exampleLines =
@(
$example.Code
foreach ($remark in $example.Remarks.text) {
if (-not $remark) { continue }
$remark
}
) -join ([Environment]::NewLine) -split '(?>\r\n|\n)' # and split into lines
# Go thru each line in the example as part of a loop
$codeBlock = @(foreach ($exampleLine in $exampleLines) {
# Any comments until the first uncommentedLine are markdown
if ($exampleLine -match '^\#' -and -not $nonCommentLine) {
$markdownLines += $exampleLine -replace '^\#\s{0,1}'
} else {
$nonCommentLine = $true
$exampleLine
}
}) -join [Environment]::NewLine
$markdownLines
"~~~PowerShell"
$CodeBlock
"~~~"
}
$relatedUris = foreach ($link in $help.relatedLinks.navigationLink) {
if ($link.uri) {
$link.uri
}
}
if ($relatedUris) {
"#### Links"
foreach ($related in $relatedUris) {
"* [$related]($related)"
}
}
}
}
}
#endregion Exported Functions
#region Exported Types
if ($ModuleTypeName) {
$typeData = Get-TypeData -TypeName $ModuleTypeName
if ($typeData) {
"## Types"
}
foreach ($typeInfo in $typeData) {
$memberDirectory = "./Types/$($typeInfo.TypeName)"
if (-not (Test-Path $memberDirectory)) { continue }
$psTypeNames = @(Get-ChildItem -Path $memberDirectory |
Where-Object Name -match 'PSTypeNames?\.txt$' |
Get-Content)
if (-not $psTypeNames) {
$psTypeNames = @($typeInfo.TypeName)
}
"### $($typeInfo.TypeName)"
if ($psTypeNames.Count -gt 1) {
"> Also Known As:"
$psTypeNames |
Where-Object { $_ -ne $typeInfo.TypeName} |
Foreach-Object { "* $_"}
}
"#### Members"
"|Name|MemberType|"
"|-|-|"
foreach ($memberName in $typeInfo.Members.Keys) {
"|$(
$memberPath = "./Types/$($typeInfo.TypeName)/$memberName.ps1"
if (Test-Path $memberPath) {
"[$memberName]($($memberPath -replace '^\./'))"
}
else {
$getPath = "./Types/$($typeInfo.TypeName)/get_$memberName.ps1"
$setPath = "./Types/$($typeInfo.TypeName)/set_$memberName.ps1"
if (
(Test-Path $getPath) -and
(Test-Path $setPath)) {
"[get]($(
$getPath -replace '^\./'
))/[set]($(
$setPath -replace '^\./'
))_[$memberName]($(
$getPath -replace '^\./'
))"
}
elseif (Test-Path $getPath) {
"[get_$memberName]($(
$getPath -replace '^\./'
))"
} else {
$memberName
}
}
)|$(
$typeInfo.Members[$memberName].GetType().Name -replace 'Data$'
)|"
}
}
}
#endregion Exported Types
#region Copyright Notice
if ($module.Copyright) {
"> $($module.Copyright)"
}
if ($module.PrivateData.PSData.LicenseUri) {
""
"> [LICENSE]($($module.PrivateData.PSData.LicenseUri))"
}
#endregion Copyright Notice
Pop-Location