-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathExport-Readme.ps1
371 lines (338 loc) · 15.3 KB
/
Export-Readme.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<#
.SYNOPSIS
Generate README.md file for the scripts repo.
.LINK
https://www.microsoft.com/download/details.aspx?id=40760
#>
#Requires -Version 7
#Requires -Modules SqlServer
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','',
Justification='This script deals with lists, and this is a pretty questionable rule.')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter','',
Justification='ScriptAnalyzer does not recognize parameters used as global values.')]
[CmdletBinding()][OutputType([void])] Param(
# The oldest change to mark as updated.
[string] $StatusAge = '2 weeks ago',
# Commit the update.
[switch] $Commit
)
function Get-StatusSymbol([string]$status,[switch]$entity)
{
if($entity)
{
switch($status)
{
A {'🆕 '}
B {'❗ '}
C {'➕ '}
D {'➖ '}
M {'🆙 '}
R {'📛 '}
T {'〰️ '}
U {'🔴 '}
X {'🔸 '}
default {'🔷 '}
}
}
else
{
switch($status)
{
A {':new: '}
B {':heavy_exclamation_mark: '}
C {':heavy_plus_sign: '}
D {':heavy_minus_sign: '}
M {':up: '}
R {':name_badge: '}
T {':wavy_dash: '}
U {':red_circle: '}
X {':large_orange_diamond: '}
default {':large_blue_diamond: '}
}
}
}
filter Test-HasTest([ValidateScript({Test-Path $_.FullName -Type Leaf})][IO.FileInfo] $Script)
{
if($Script.Name -notmatch '\A\w+-\w+\.ps1\z') {return $false}
if(Test-Path "$PSScriptRoot\test\$($Script.BaseName).Tests.ps1" -Type Leaf) {return $true}
else {return $false}
}
function Measure-PesterCoveragePerMil([Parameter(ValueFromPipeline)][IO.FileInfo] $Script)
{
End
{
return [int](1000 * ($input |Where-Object {Test-HasTest $_} |Measure-Object).Count / $input.Count)
}
}
function Measure-CommitsTimeSpan
{
[CmdletBinding()][OutputType([TimeSpan])] Param(
[Parameter(Mandatory=$true)][string] $Path
)
return git log --follow --format=%ad --date=iso ($Path -replace '\\','/') |
ForEach-Object {[DateTimeOffset]$_} |
Measure-Object -Minimum -Maximum |
ForEach-Object {$_.Maximum - $_.Minimum}
}
filter Format-RoundTimeSpan([Parameter(ValueFromPipeline)][TimeSpan] $TimeSpan)
{
switch($TimeSpan)
{
{$_.Days -gt 0} {return "$($_.Days) days"}
{$_.Hours -gt 0} {return "$($_.Hours) hours"}
{$_.Minutes -gt 0} {return "$($_.Minutes) minutes"}
{$_.Seconds -gt 0} {return "$($_.Seconds) seconds"}
{$_.Milliseconds -gt 0} {return "$($_.Milliseconds) milliseconds"}
{$_.Microseconds -gt 0} {return "$($_.Microseconds) microseconds"}
{$_.Ticks -eq 0} {return "at once"}
default {return "$($_.Ticks) ticks"}
}
}
function Get-PesterCoverageBadge([switch]$UseLines)
{
if($UseLines)
{
$unit = [uri]::EscapeUriString((Get-Unicode.ps1 0x2031)) # PER TEN THOUSAND SIGN (permyriad)
[int] $coverage = 10000 * (Get-Item $PSScriptRoot\*.ps1 |
Where-Object {Test-HasTest $_} |
Get-Content |
Measure-Object -Line).Lines /
(Get-Item $PSScriptRoot\*.ps1 |Get-Content |Measure-Object -Line).Lines
$color = switch($coverage)
{
{$_ -gt 9500} {'brightgreen'}
{$_ -gt 8000} {'green'}
{$_ -gt 5000} {'yellowgreen'}
{$_ -gt 3000} {'orange'}
{$_ -gt 500} {'red'}
default {'lightgray'}
}
}
else
{
$unit = [uri]::EscapeUriString((Get-Unicode.ps1 0x2030)) # PER MILLE SIGN (permil)
[int] $coverage = 1000 * (Get-Item $PSScriptRoot\test\*.ps1 |Measure-Object).Count /
(Get-Item $PSScriptRoot\*.ps1 |Measure-Object).Count
$color = switch($coverage)
{
{$_ -gt 950} {'brightgreen'}
{$_ -gt 800} {'green'}
{$_ -gt 500} {'yellowgreen'}
{$_ -gt 300} {'orange'}
{$_ -gt 50} {'red'}
default {'lightgray'}
}
}
return "https://img.shields.io/badge/Pester_coverage-${coverage}_$unit-$color"
}
function Format-PSScripts([string] $Extension = '', [switch] $entities)
{
Write-Progress 'Enumerating PowerShell scripts' 'Getting list of recent changes'
$status = @{}
git diff --name-status $(git rev-list -1 --before="$StatusAge" main) |
ForEach-Object {
if($_ -match '^(?<Status>\w)\t(?<Script>\S.*)')
{
$status[$Matches.Script] = Get-StatusSymbol $Matches.Status -entity:$entities
}
}
[Microsoft.PowerShell.Commands.GroupInfo[]] $functionalities = Get-Item $PSScriptRoot\*.ps1 |
ForEach-Object {Get-Help $_.FullName} |
Group-Object Functionality |
Sort-Object {if($_.Name){$_.Name}else{'ZZZ'}}
foreach($functionality in $functionalities)
{
''
$category = if($functionality.Name) {$functionality.Name} else {'Other'}
"### $category"
''
$functionality.Group |
ForEach-Object {
$name = Split-Path $_.Name -Leaf
$symbol = if($status.ContainsKey($name)){$status[$name]}
"- $symbol**[$name]($name$Extension)**: $($_.Synopsis)"
}
}
}
function Export-FSharpFormatting
{
Write-Progress 'Exporting F# script documentation' 'Checking for FSharp.Formatting'
Use-Command.ps1 dotnet "$env:ProgramFiles\dotnet\dotnet.exe" -cinst dotnet-sdk
if("$(dotnet fsdocs version 2>&1)" -notmatch 'fsdocs *')
{
if(!(Test-Path .config/dotnet-tools.json -Type Leaf)) {dotnet new tool-manifest}
dotnet tool install FSharp.Formatting.CommandTool
}
Write-Progress 'Exporting F# script documentation' 'Exporting literate documentation as HTML'
$input,$output = "$PSScriptRoot\.fsxtmp","$PSScriptRoot\docs"
if(Test-Path $input -PathType Container) {Remove-Item -Force -Recurse $input}
mkdir $input |Out-Null
Copy-Item $PSScriptRoot\*.fsx $input
if(!(Test-Path $output -Type Container)) {mkdir $output}
dotnet fsdocs build --input $input --output $output --noapidocs --parameters root / fsdocs-list-of-namespaces . 2>&1 |Write-Verbose
Remove-Item -Force -Recurse $input
Write-Progress 'Exporting F# script documentation' -Completed
}
function Format-FSScripts
{
Export-FSharpFormatting
Write-Progress 'Enumerating F# scripts'
$base = 'https://webcoder.info/scripts/'
$FSFHeadPattern = @'
(?mx) \A \s*
^\(\*\* \s* $ \s*
^(?<Title>\S.*\S) \s* $ \s*
^ =+ \r? $ \s*
^(?<Synopsis>\S.*\S) \s* $
'@
[IO.FileInfo[]] $scripts = Get-Item $PSScriptRoot\*.fsx
$i,$max = 0,($scripts.Count/100)
$scripts |
ForEach-Object {(Resolve-Path $_.FullName -Relative) -replace '\\','/' -replace '\A\./',''} |
Where-Object {(Get-Content $_ -TotalCount 1) -match '\A\s*\(\*\*\s*\z'} |
ForEach-Object {
Write-Progress 'Enumerating F# scripts' 'Writing list' -curr $_ -percent ($i++/$max)
if((Get-Content $_ -Raw) -match $FSFHeadPattern)
{
"- **[$($Matches.Title)]($base$([IO.Path]::ChangeExtension($_,'html')))**: $($Matches.Synopsis)"
}
}
Write-Progress 'Enumerating F# scripts' -Completed
}
function Format-VBAScripts
{
Write-Progress 'Enumerating VisualBasic for Applications scripts'
$VBAHeadPattern = '(?mx) \A (?<Description>(?:^'' .* $ \s*)+) ^(?!'')'
[IO.FileInfo[]] $scripts = Get-Item .\*.vba
$i,$max = 0,($scripts.Count/100)
$scripts |
ForEach-Object {(Resolve-Path $_.FullName -Relative) -replace '\\','/' -replace '\A\./',''} |
ForEach-Object {
Write-Progress 'Enumerating VisualBasic for Applications scripts' 'Writing list' -curr $_ -percent ($i++/$max)
if((Get-Content $_ -Raw) -match $VBAHeadPattern)
{
"- **[$_]($_)**: $($Matches.Description -replace '(?m)^''\s*','' -replace '\s+',' ')"
}
}
Write-Progress 'Enumerating VisualBasic for Applications scripts' -Completed
}
filter Format-TestsVerb
{
Param(
[Parameter(ValueFromPipelineByPropertyName=$true)][string] $Name,
[Parameter(ValueFromPipelineByPropertyName=$true)][IO.FileInfo[]] $Group
)
$permil = $Group |Measure-PesterCoveragePerMil
$time = $permil -eq 0 ? '' : "📅 $(Measure-CommitsTimeSpan "$PSScriptRoot\test\$Name*.Tests.ps1" |Format-RoundTimeSpan)"
$progress = $permil -eq 0 ? 'not started' : "<meter low='300' max='1000' optimum='1000' value='$permil'>$permil ‰</meter>"
$Local:OFS = [Environment]::NewLine
return @"
<li><details><summary>$progress $Name ($($Group.Count)) $time</summary>
$($Group |ForEach-Object {"- $((Test-HasTest $_) ? '✔️' : '✖️') $($_.Name)"})
</details></li>
"@
}
filter Format-TestsLetter
{
Param(
[Parameter(ValueFromPipelineByPropertyName=$true)][string] $Name,
[Parameter(ValueFromPipelineByPropertyName=$true)][IO.FileInfo[]] $Group
)
$permil = $Group |Measure-PesterCoveragePerMil
$time = $permil -eq 0 ? '' : "📅 $(Measure-CommitsTimeSpan "$PSScriptRoot\test\$Name*.Tests.ps1" |Format-RoundTimeSpan)"
$progress = $permil -eq 0 ? 'not started' : "<meter low='300' max='1000' optimum='1000' value='$permil'>$permil ‰</meter>"
$Local:OFS = [Environment]::NewLine
return @"
<li><details><summary>$progress $Name ($($Group.Count)) $time</summary>
<ul>$($Group |Group-Object {($_.BaseName -split '-',2)[0]} |Format-TestsVerb)</ul></details></li>
"@
}
function Format-TestsReadme
{
$Scripts = Get-Item $PSScriptRoot\*.ps1
$permil = $Scripts |Measure-PesterCoveragePerMil
$time = $permil -eq 0 ? '' : "📅 $(Measure-CommitsTimeSpan "$PSScriptRoot\test\*.Tests.ps1" |Format-RoundTimeSpan)"
$progress = $permil -eq 0 ? 'not started' : "<meter low='300' max='1000' optimum='1000' value='$permil'>$permil ‰</meter>"
$Local:OFS = [Environment]::NewLine
return @"
Script Tests
============
<details><summary>$progress Scripts repo ($($Scripts.Count)) $time</summary>
<ul>$(Get-Item $PSScriptRoot\*.ps1 |Group-Object {$_.Name[0]} |Format-TestsLetter)</ul></details>
"@
}
function Format-Readme
{
Write-Progress 'Building readme' 'Exporting dependencies'
Write-Progress 'Building readme' 'Writing readme.md'
$Local:OFS = [Environment]::NewLine
return @"
Useful General-Purpose Scripts
==============================
[][pester.yml]
[][pester.yml]
[)](https://github.com/brianary/scripts/tree/main/test)
[](https://mit-license.org/ "MIT License")
[](https://github.com/brianary/scripts/stargazers "Stars")
[](https://github.com/brianary/scripts/watchers "Watchers")
[](https://github.com/brianary/scripts/network/members "Forks")
[](https://github.com/brianary/scripts/issues "Issues")
[](https://github.com/brianary/scripts/commits/main "Commits")
[](https://github.com/brianary/scripts/commits/main "Last commit")
[![Mastodon: @[email protected]](https://badgen.net/badge/@dataelemental/@tilde.zone/blue?icon=mastodon)](https://tilde.zone/@dataelemental "DataElemental Mastodon profile")
[![Mastodon: @[email protected]](https://badgen.net/mastodon/follow/[email protected]?icon=mastodon)](https://mastodon.spotek.io/@brianary "Mastodon profile")
[pester.yml]: https://github.com/brianary/scripts/actions/workflows/pester.yml "Pester test run history"
This repo contains a collection of generally useful scripts (mostly Windows PowerShell).
See [PS5](PS5) for legacy scripts, [syscfg](syscfg) for single-use system config scripts.
PowerShell Scripts
------------------
$(Format-PSScripts)
F# Scripts
----------
$(Format-FSScripts)
Office VBA Scripts
------------------
$(Format-VBAScripts)
<!-- generated $(Get-Date) -->
"@
Write-Progress 'Building readme' -Completed
}
function Export-PSScriptPages
{
Import-Module platyPS
Write-Progress 'Export PowerShell script help pages' 'Removing docs for old scripts'
Get-Item $PSScriptRoot\docs\*.ps1.md |
Where-Object {!(Test-Path "$PSScriptRoot\$(Split-Path $_.Name -LeafBase)" -Type Leaf)} |
Remove-Item
Write-Progress 'Export PowerShell script help pages' 'Updating script docs'
Update-MarkdownHelp $PSScriptRoot\docs\*.ps1.md -ErrorAction Ignore |Write-Verbose
Write-Progress 'Export PowerShell script help pages' 'Adding docs for new scripts'
Get-Item $PSScriptRoot\*.ps1 |
Where-Object {!(Test-Path "$PSScriptRoot\$($_.Name).md" -Type Leaf)} |
ForEach-Object {New-MarkdownHelp -Command $_.Name -OutputFolder docs -ErrorAction Ignore} |
Write-Verbose
$Local:OFS = [Environment]::NewLine
return @"
<script data-goatcounter="https://webcoderscripts.goatcounter.com/count" async src="//gc.zgo.at/count.js"></script>
[][pester.yml]
[][pester.yml]
[)](https://github.com/brianary/scripts/tree/main/test)
[](https://mit-license.org/ "MIT License")
[](https://github.com/brianary/scripts/stargazers "Stars")
[](https://github.com/brianary/scripts/watchers "Watchers")
[](https://github.com/brianary/scripts/network/members "Forks")
[](https://github.com/brianary/scripts/issues "Issues")
[](https://github.com/brianary/scripts/commits/main "Commits")
[](https://github.com/brianary/scripts/commits/main "Last commit")
[![Mastodon: @[email protected]](https://badgen.net/badge/@dataelemental/@tilde.zone/blue?icon=mastodon)](https://tilde.zone/@dataelemental "DataElemental Mastodon profile")
[![Mastodon: @[email protected]](https://badgen.net/mastodon/follow/[email protected]?icon=mastodon)](https://mastodon.spotek.io/@brianary "brianary Mastodon profile")
[pester.yml]: https://github.com/brianary/scripts/actions/workflows/pester.yml "Pester test run history"
Scripts from the [Scripts](https://github.com/brianary/Scripts/) repo.
$(Format-PSScripts -Extension '.md' -entities)
"@ |Out-File docs\index.md
Write-Progress 'Export PowerShell script help pages' -Completed
}
Format-Readme |Out-File $PSScriptRoot\README.md -Encoding utf8 -Width ([int]::MaxValue)
Format-TestsReadme |Out-File $PSScriptRoot\test\README.md -Encoding utf8 -Width ([int]::MaxValue)
Export-PSScriptPages
if($Commit) {git add -A ; git commit -m "$(Get-Unicode.ps1 0x1F4DD) Update readme"}