-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
Copy pathregenerate.ps1
executable file
·342 lines (294 loc) · 9.94 KB
/
regenerate.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
#! /usr/bin/env pwsh
[CmdletBinding()]
Param(
[String]$VcpkgRoot = ''
)
if ([String]::IsNullOrEmpty($VcpkgRoot)) {
$VcpkgRoot = "${PSScriptRoot}/.."
}
$VcpkgRoot = Resolve-Path $VcpkgRoot
if (-not (Test-Path "$VcpkgRoot/.vcpkg-root")) {
throw "Invalid vcpkg instance, did you forget -VcpkgRoot?"
}
class CMakeDocumentation {
[String]$Filename
[String[]]$ActualDocumentation
[Bool]$IsDeprecated
[String]$DeprecationMessage
[Bool]$HasError
}
[String[]]$cmakeScriptsPorts = @(
'vcpkg-cmake'
'vcpkg-cmake-config'
'vcpkg-pkgconfig-get-modules'
)
[CMakeDocumentation[]]$tableOfContents = @()
[CMakeDocumentation[]]$internalTableOfContents = @()
$portTableOfContents = [ordered]@{}
function RelativeUnixPathTo
{
Param(
[Parameter(Mandatory)]
[String]$Path,
[Parameter(Mandatory)]
[String]$Base
)
$Path = Resolve-Path -LiteralPath $Path
$Base = Resolve-Path -LiteralPath $Base
if ($IsWindows)
{
if ((Split-Path -Qualifier $Path) -ne (Split-Path -Qualifier $Base))
{
throw "It is not possible to get the relative unix path from $Base to $Path"
}
}
$Path = $Path -replace '\\','/'
$Base = $Base -replace '\\','/'
[String[]]$PathArray = $Path -split '/'
[String[]]$BaseArray = $Base -split '/'
[String[]]$Result = @()
$Idx = 0
while ($Idx -lt $PathArray.Length -and $Idx -lt $BaseArray.Length)
{
if ($PathArray[$Idx] -ne $BaseArray[$Idx])
{
break
}
++$Idx
}
for ($BaseIdx = $Idx; $BaseIdx -lt $BaseArray.Length; ++$BaseIdx)
{
$Result += '..'
}
for ($PathIdx = $Idx; $PathIdx -lt $PathArray.Length; ++$PathIdx)
{
$Result += $PathArray[$PathIdx]
}
$Result -join '/'
}
function WriteFile
{
Param(
[String[]]$Value,
[String]$Path
)
# note that we use this method of getting the utf-8 bytes in order to:
# - have no final `r`n, which happens when Set-Content does the thing automatically on Windows
# - have no BOM, which happens when one uses [System.Text.Encoding]::UTF8
[byte[]]$ValueAsBytes = (New-Object -TypeName 'System.Text.UTF8Encoding').GetBytes($Value -join "`n")
Set-Content -Path $Path -Value $ValueAsBytes -AsByteStream
}
function FinalDocFile
{
Param(
[CMakeDocumentation]$Docs,
[String]$PathToFile # something like docs/maintainers/blah.md
)
[String[]]$documentation = @()
if ($Docs.ActualDocumentation.Length -eq 0)
{
throw "Invalid documentation: empty docs"
}
$documentation += $Docs.ActualDocumentation[0] # name line
if ($Docs.IsDeprecated)
{
if ($null -eq $Docs.DeprecationMessage)
{
$documentation += @("", "**This function has been deprecated**")
}
else
{
$documentation += @("", "**This function has been deprecated $($Docs.DeprecationMessage)**")
}
}
$documentation += @("", "The latest version of this document lives in the [vcpkg repo](https://github.com/Microsoft/vcpkg/blob/master/docs/$PathToFile).")
$documentation += $Docs.ActualDocumentation[1..$Docs.ActualDocumentation.Length]
$relativePath = RelativeUnixPathTo $Docs.Filename $VcpkgRoot
$documentation += @(
"",
"## Source",
"[$($relativePath -replace '_','\_')](https://github.com/Microsoft/vcpkg/blob/master/$relativePath)",
""
)
$documentation
}
function ParseCmakeDocComment
{
Param(
[Parameter(Mandatory)]
[System.IO.FileSystemInfo]$Filename
)
$Docs = New-Object 'CMakeDocumentation'
$Docs.HasError = $False
$Docs.IsDeprecated = $False
$Docs.Filename = $Filename.FullName
[String[]]$contents = Get-Content $Filename
if ($contents[0] -eq '# DEPRECATED')
{
$Docs.IsDeprecated = $True
}
elseif($contents[0] -match '^# DEPRECATED: (.*)$')
{
$Docs.IsDeprecated = $True
$Docs.DeprecationMessage = $matches[1]
}
[String]$startCommentRegex = '#\[(=*)\['
[String]$endCommentRegex = ''
[Bool]$inComment = $False
$contents = $contents | ForEach-Object {
if (-not $inComment) {
if ($_ -match "^\s*${startCommentRegex}(\.[a-z]*)?:?\s*$") {
if (-not [String]::IsNullOrEmpty($matches[2]) -and $matches[2] -ne '.md') {
Write-Warning "The documentation in $($Filename.FullName) doesn't seem to be markdown (extension: $($matches[2])). Only markdown is supported; please rewrite the documentation in markdown."
}
$inComment = $True
$endCommentRegex = "\]$($matches[1])\]"
} elseif ($_ -match $startCommentRegex) {
$Docs.HasError = $True
Write-Warning "Invalid start of comment -- the comment start must be at the beginning of the line.
(on line: `"$_`")"
} else {
# do nothing -- we're outside a comment, so cmake code
}
} else {
if ($_ -match "^\s*#?${endCommentRegex}\s*$") {
$inComment = $False
$endCommentRegex = ''
} elseif ($_ -match $endCommentRegex) {
$Docs.HasError = $True
Write-Warning "Invalid end of comment -- the comment end must be on it's own on a line.
(on line: `"$_`")"
} else {
# regular documentation line
$_
}
}
}
if ($inComment) {
Write-Warning "File $($Filename.FullName) has an unclosed comment."
$Docs.HasError = $True
}
if ($contents.Length -ne 0)
{
$Docs.ActualDocumentation = $contents
}
$Docs
}
Get-ChildItem "$VcpkgRoot/scripts/cmake" -Filter '*.cmake' | ForEach-Object {
$docs = ParseCmakeDocComment $_
[Bool]$isInternalFunction = $_.Name.StartsWith("vcpkg_internal") -or $_.Name.StartsWith("z_vcpkg")
if ($docs.IsDeprecated -and $null -eq $docs.ActualDocumentation)
{
return
}
if ($docs.HasError)
{
return
}
if ($null -ne $docs.ActualDocumentation)
{
if ($isInternalFunction)
{
$pathToFile = "maintainers/internal/$($_.BaseName).md"
WriteFile `
-Path "$PSScriptRoot/$pathToFile" `
-Value (FinalDocFile $docs)
$internalTableOfContents += $docs
}
else
{
$pathToFile = "maintainers/$($_.BaseName).md"
WriteFile `
-Path "$PSScriptRoot/$pathToFile" `
-Value (FinalDocFile $docs $pathToFile)
$tableOfContents += $docs
}
}
elseif (-not $isInternalFunction)
{
# don't worry about undocumented internal functions
Write-Warning "The cmake function in file $($_.FullName) doesn't seem to have any documentation. Make sure the documentation comments are correctly written."
}
}
$cmakeScriptsPorts | ForEach-Object {
$portName = $_
Copy-Item "$VcpkgRoot/ports/$portName/README.md" "$PSScriptRoot/maintainers/ports/$portName.md"
New-Item -Path "$PSScriptRoot/maintainers/ports/$portName" -Force -ItemType 'Directory' | Out-Null
$portTableOfContents[$portName] = @()
Get-ChildItem "$VcpkgRoot/ports/$portName" -Filter '*.cmake' | ForEach-Object {
if ($_.Name -eq 'vcpkg-port-config.cmake' -or $_.Name -eq 'portfile.cmake')
{
return
}
$docs = ParseCmakeDocComment $_
if ($docs.IsDeprecated -and $null -eq $docs.ActualDocumentation)
{
return
}
if ($docs.HasError)
{
return
}
if ($null -ne $docs.ActualDocumentation)
{
$pathToFile = "maintainers/ports/$portName/$($_.BaseName).md"
WriteFile `
-Path "$PSScriptRoot/$pathToFile" `
-Value (FinalDocFile $docs $pathToFile)
$portTableOfContents[$portName] += $docs
}
else
{
Write-Warning "The cmake function in file $($_.FullName) doesn't seem to have any documentation. Make sure the documentation comments are correctly written."
}
}
}
$portfileFunctionsContent = @(
'<!-- Run regenerate.ps1 to extract scripts documentation -->',
'',
'# Portfile helper functions')
$DocsName = @{ expression = { Split-Path -LeafBase $_.Filename } }
$tableOfContents | Sort-Object -Property $DocsName -Culture '' | ForEach-Object {
$name = Split-Path -LeafBase $_.Filename
if ($_.IsDeprecated)
{
$portfileFunctionsContent += "- [$($name -replace '_','\_')]($name.md) (deprecated)"
}
else
{
$portfileFunctionsContent += "- [$($name -replace '_','\_')]($name.md)"
}
}
$portfileFunctionsContent += @("", "## Internal Functions", "")
$internalTableOfContents | Sort-Object -Property $DocsName -Culture '' | ForEach-Object {
$name = Split-Path -LeafBase $_.Filename
if ($_.IsDeprecated)
{
$portfileFunctionsContent += "- [$($name -replace '_','\_')](internal/$name.md) (deprecated)"
}
else
{
$portfileFunctionsContent += "- [$($name -replace '_','\_')](internal/$name.md)"
}
}
$portfileFunctionsContent += @("", "## Scripts from Ports")
$portTableOfContents.GetEnumerator() | ForEach-Object {
$portName = $_.Name
$cmakeDocs = $_.Value
$portfileFunctionsContent += @("", "### [$portName](ports/$portName.md)", "")
$cmakeDocs | ForEach-Object {
$name = Split-Path -LeafBase $_.Filename
if ($_.IsDeprecated)
{
$portfileFunctionsContent += "- [$($name -replace '_','\_')](ports/$portName/$name.md) (deprecated)"
}
else
{
$portfileFunctionsContent += "- [$($name -replace '_','\_')](ports/$portName/$name.md)"
}
}
}
$portfileFunctionsContent += "" # final newline
WriteFile `
-Path "$PSScriptRoot/maintainers/portfile-functions.md" `
-Value $portfileFunctionsContent