-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME.md.ps1
More file actions
229 lines (181 loc) · 5.96 KB
/
README.md.ps1
File metadata and controls
229 lines (181 loc) · 5.96 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
<#
.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
.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'
),
# 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)"
if ($help.examples.example) {
# 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)"
}
}
# 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/>'
)|"
}
""
}
}
}
}
#endregion Exported Functions
#region Copyright Notice
if ($module.Copyright) {
"> $($module.Copyright)"
}
if ($module.PrivateData.PSData.LicenseUri) {
""
"> [LICENSE]($($module.PrivateData.PSData.LicenseUri))"
}
#endregion Copyright Notice
Pop-Location