-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate-Readme.ps1
More file actions
183 lines (157 loc) · 3.84 KB
/
Update-Readme.ps1
File metadata and controls
183 lines (157 loc) · 3.84 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
<#
.SYNOPSIS
Updates README.md
.DESCRIPTION
This script extracts information from the script files to generate
a simple markdown table of all the scripts.
.PARAMETER ConsoleOnly
If specified, the output is rendered on the console rather than into
the readme file.
#>
Param
(
[Parameter()]
[switch]$ConsoleOnly
)
$CONST = Data {
@{
TOPTEXT = @"
PowerShellScrapbook
====================
This is simply a repository of PowerShell functions and scripts that might sometime
in the future become handy once more.
###Overview
"@
FILENAME = "README.md"
}
}
Function Get-PowerShellHelpCommentBlock
{
Param
(
[Parameter()]
[string]$Path,
[Parameter()]
[string]$Keyword,
[Parameter()]
[switch]$KeepKeywordLine
)
$eating = $false
$capturedLines = @()
foreach ($line in (Get-Content -Path $Path))
{
if ($eating)
{
if ($line -match "^\s*\." -or $line -match "#>")
{
break
}
$capturedLines += $line
}
if ($line -match $Keyword)
{
$eating = $true
if ($KeepKeywordLine)
{
$capturedLines += $line
}
}
}
$capturedLines
}
Function Format-MarkdownTable
{
Param
(
[Parameter(ValueFromPipeline = $true)]
[PSObject]$InputObject,
[Parameter()]
[string[]]$Properties
)
Begin
{
$maxWidths = @()
$allRows = @()
if ($Properties)
{
$header = $Properties
$maxWidths = @(0) * $header.Length
$allRows += ,$header
}
}
Process
{
if (-not $header)
{
$header = @()
foreach ($p in Get-Member -InputObject $InputObject -MemberType Property,NoteProperty)
{
$header += $p.Name
$maxWidths += 0
}
$allRows += ,$header
}
$row = @()
for ($i = 0; $i -lt $header.Length; $i++)
{
[string]$value = $InputObject.$($header[$i])
$maxWidths[$i] = [Math]::Max($maxWidths[$i], $value.Length)
$row += $value
}
$allRows += ,$row
}
End
{
$firstRow = $true
foreach ($r in $allRows)
{
$cells = @()
for ($i = 0; $i -lt $header.Length; $i++)
{
if ($r[$i])
{
$cells += $r[$i].PadRight($maxWidths[$i], " ")
}
else
{
$cells += New-Object String(" ", $maxWidths[$i])
}
}
$row = $cells -join " | "
Write-Output -InputObject $row
if ($firstRow)
{
$cells = @()
for ($i = 0; $i -lt $header.Length; $i++)
{
$cells += New-Object String("-", $maxWidths[$i])
}
$row = $cells -join " | "
Write-Output -InputObject $row
$firstRow = $false
}
}
}
}
$rows = @()
'Scripts', 'Functions' | foreach {
$type = $_ -replace "s$", ""
Get-ChildItem -Path "$PSScriptRoot\$_" | foreach {
$synopsis = (Get-PowerShellHelpCommentBlock -Path $_.FullName -Keyword ".SYNOPSIS" | %{ $_.Trim() }) -join " "
$rows += New-Object -TypeName PSObject -Property @{
Name = $_.Name
Type = $type
Synopsis = $synopsis
}
}
}
$text = $CONST.TOPTEXT
$text += ($rows | Sort-Object -Property Name | Format-MarkdownTable -Properties Name,Type,Synopsis | Out-String)
if ($ConsoleOnly)
{
Write-Host $text
}
else
{
Set-Content -Value $text -Path $CONST.FILENAME
}