forked from violetljj/blind-assist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_docs_index.ps1
More file actions
348 lines (322 loc) · 12.5 KB
/
Copy pathcheck_docs_index.ps1
File metadata and controls
348 lines (322 loc) · 12.5 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
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
param(
[string]$DocsRoot = (Join-Path (Split-Path -Parent $PSScriptRoot) 'docs')
)
$ErrorActionPreference = 'Stop'
$docsRootPath = (Resolve-Path -LiteralPath $DocsRoot).Path
$repoRootPath = [IO.Path]::GetFullPath((Split-Path -Parent $docsRootPath))
$repoRootPrefix = $repoRootPath.TrimEnd(
[IO.Path]::DirectorySeparatorChar,
[IO.Path]::AltDirectorySeparatorChar
) + [IO.Path]::DirectorySeparatorChar
$indexPath = Join-Path $docsRootPath 'README.md'
if (-not (Test-Path -LiteralPath $indexPath -PathType Leaf)) {
throw "Documentation index is missing: $indexPath"
}
$indexText = Get-Content -LiteralPath $indexPath -Raw -Encoding utf8
$failures = [Collections.Generic.List[string]]::new()
if (-not (Test-Path -LiteralPath (Join-Path $docsRootPath 'PROJECT_STATE.md') -PathType Leaf)) {
$failures.Add('Cold-start navigation file is missing: PROJECT_STATE.md')
}
elseif ($indexText -notmatch [regex]::Escape('PROJECT_STATE.md')) {
$failures.Add('Documentation index does not link cold-start navigation: PROJECT_STATE.md')
}
Get-ChildItem -LiteralPath $docsRootPath -File -Filter *.md |
Where-Object { $_.Name -ne 'README.md' } |
Sort-Object Name |
ForEach-Object {
if ($indexText -notmatch [regex]::Escape($_.Name)) {
$failures.Add("Top-level documentation file is not indexed: $($_.Name)")
}
}
$researchRoot = Join-Path $docsRootPath 'research'
$algorithmCurrentPath = Join-Path $researchRoot 'ALGORITHM_RESEARCH_CURRENT.md'
if (Test-Path -LiteralPath $researchRoot -PathType Container) {
$researchIndexPath = Join-Path $researchRoot 'README.md'
$requiredResearchEntries = @(
'README.md',
'ALGORITHM_RESEARCH_CURRENT.md',
'DATA_RESEARCH_CURRENT.md',
'SYSTEM_RESEARCH_CURRENT.md'
)
foreach ($entry in $requiredResearchEntries) {
$entryPath = Join-Path $researchRoot $entry
if (-not (Test-Path -LiteralPath $entryPath -PathType Leaf)) {
$failures.Add("Required research entry is missing: research/$entry")
}
}
if (Test-Path -LiteralPath $researchIndexPath -PathType Leaf) {
$researchIndexText = Get-Content -LiteralPath $researchIndexPath -Raw -Encoding utf8
foreach ($entry in @(
'ALGORITHM_RESEARCH_CURRENT.md',
'DATA_RESEARCH_CURRENT.md',
'SYSTEM_RESEARCH_CURRENT.md'
)) {
if ($researchIndexText -notmatch [regex]::Escape($entry)) {
$failures.Add("Research index does not link required category entry: research/$entry")
}
}
}
Get-ChildItem -LiteralPath $researchRoot -Directory |
Sort-Object Name |
ForEach-Object {
$domainReadme = Join-Path $_.FullName 'README.md'
if (-not (Test-Path -LiteralPath $domainReadme -PathType Leaf)) {
$failures.Add("Research documentation domain lacks a README index: research/$($_.Name)/README.md")
}
else {
$routeLineCount = [IO.File]::ReadAllLines(
$domainReadme,
[Text.Encoding]::UTF8
).Count
if ($routeLineCount -gt 180) {
$failures.Add(
"Research route README exceeds the 180-line operating-surface budget: research/$($_.Name)/README.md ($routeLineCount lines)"
)
}
}
$domainIndexTarget = "research/$($_.Name)/README.md"
if ($indexText -notmatch [regex]::Escape($domainIndexTarget)) {
$failures.Add("Research documentation domain is not linked from docs/README.md: $domainIndexTarget")
}
}
if (Test-Path -LiteralPath $algorithmCurrentPath -PathType Leaf) {
foreach ($line in [IO.File]::ReadAllLines($algorithmCurrentPath, [Text.Encoding]::UTF8)) {
if ($line -match '^\|' -and
$line -notmatch '^\|\s*---' -and
$line -notmatch '^\|\s*路线\s*\|' -and
$line.Length -gt 700) {
$failures.Add(
"Algorithm current route row exceeds the 700-character summary budget: $($line.Substring(0, [Math]::Min(80, $line.Length)))..."
)
}
}
}
}
$linkPattern = '\[[^\]]+\]\(([^)#]+)(?:#[^)]*)?\)'
$linkSourcePaths = [Collections.Generic.HashSet[string]]::new(
[StringComparer]::OrdinalIgnoreCase
)
foreach ($sourcePath in @(
$indexPath,
(Join-Path $researchRoot 'README.md'),
$algorithmCurrentPath,
(Join-Path $researchRoot 'DATA_RESEARCH_CURRENT.md'),
(Join-Path $researchRoot 'SYSTEM_RESEARCH_CURRENT.md')
)) {
if (Test-Path -LiteralPath $sourcePath -PathType Leaf) {
[void]$linkSourcePaths.Add([IO.Path]::GetFullPath($sourcePath))
}
}
# Ordinary history preserves source text. Aggregate README files still carry
# navigation responsibility, including when they live under history/archive.
foreach ($candidate in Get-ChildItem -LiteralPath $docsRootPath -Recurse -File -Filter *.md) {
$relativeCandidate = [IO.Path]::GetRelativePath(
$docsRootPath,
$candidate.FullName
).Replace('\', '/')
$head = (Get-Content -LiteralPath $candidate.FullName -TotalCount 16 -Encoding utf8) -join [Environment]::NewLine
$declaresCurrent = $head -match '(?im)^状态:\s*(?:\x60)?current'
$isHistoricalSnapshotName = $candidate.Name -match '(?i)(?:^|_)CURRENT_SNAPSHOT_\d{4}-\d{2}-\d{2}\.md$'
if ($isHistoricalSnapshotName -and $declaresCurrent) {
$failures.Add("Historical current-snapshot declares current authority: $relativeCandidate")
}
$isHistorical = $relativeCandidate.StartsWith(
'history/',
[StringComparison]::OrdinalIgnoreCase
) -or $relativeCandidate -match '(?i)(^|/)archive/'
$isAggregateIndex = $candidate.Name -match '(?i)^README(?:[_-].+)?\.md$'
if ($isHistorical -and -not $isAggregateIndex) {
continue
}
$isRouteIndex = $candidate.Name -eq 'README.md'
$isProtocol = $candidate.Name -match '(?i)PROTOCOL'
if ($declaresCurrent -or $isRouteIndex -or $isProtocol -or $isAggregateIndex) {
[void]$linkSourcePaths.Add($candidate.FullName)
}
}
$validatedLocalLinkCount = 0
foreach ($sourcePath in @($linkSourcePaths | Sort-Object)) {
if (-not (Test-Path -LiteralPath $sourcePath -PathType Leaf)) {
continue
}
$sourceText = Get-Content -LiteralPath $sourcePath -Raw -Encoding utf8
foreach ($match in [regex]::Matches($sourceText, $linkPattern)) {
$target = $match.Groups[1].Value.Trim().Trim('<', '>')
if ($target -match '^[a-zA-Z][a-zA-Z0-9+.-]*:' -or $target.StartsWith('/')) {
continue
}
$validatedLocalLinkCount++
$targetPath = Join-Path (Split-Path -Parent $sourcePath) $target
if (-not (Test-Path -LiteralPath $targetPath)) {
$relativeSource = [IO.Path]::GetRelativePath(
$docsRootPath,
$sourcePath
).Replace('\', '/')
$failures.Add("Documentation entry link target is missing in $($relativeSource): $target")
}
}
}
$governedPathPrefixes = @(
'.github/',
'app/',
'apps/',
'codex/',
'configs/',
'core/',
'docs/',
'feature/',
'gradle/',
'releases/',
'schemas/',
'scripts/'
)
$governedRootFiles = [Collections.Generic.HashSet[string]]::new(
[StringComparer]::OrdinalIgnoreCase
)
foreach ($rootFile in @(
'AGENTS.md',
'CHANGELOG.md',
'CONTRIBUTING.md',
'GOVERNANCE.md',
'LICENSE',
'README.md',
'SECURITY.md',
'THIRD_PARTY_NOTICES.md',
'build.gradle.kts',
'gradle.properties',
'settings.gradle.kts'
)) {
[void]$governedRootFiles.Add($rootFile)
}
$jsonPathFieldCount = 0
$validatedJsonPathCount = 0
function Test-GovernedJsonPath {
param(
[string]$Value,
[string]$JsonRelativePath,
[string]$PropertyName
)
$normalized = $Value.Trim().Replace('\', '/')
if ([string]::IsNullOrWhiteSpace($normalized) -or
$normalized -match '^[a-zA-Z][a-zA-Z0-9+.-]*://' -or
[IO.Path]::IsPathRooted($normalized) -or
$normalized.StartsWith('/') -or
$normalized.StartsWith('artifacts.local/', [StringComparison]::OrdinalIgnoreCase) -or
$normalized.StartsWith('test-artifacts.local/', [StringComparison]::OrdinalIgnoreCase) -or
$normalized.StartsWith('build/', [StringComparison]::OrdinalIgnoreCase) -or
$normalized -match '(?i)(^|/)build/') {
return
}
$isGoverned = $governedRootFiles.Contains($normalized)
if (-not $isGoverned) {
foreach ($prefix in $governedPathPrefixes) {
if ($normalized.StartsWith($prefix, [StringComparison]::OrdinalIgnoreCase)) {
$isGoverned = $true
break
}
}
}
if (-not $isGoverned) {
return
}
$script:validatedJsonPathCount++
$resolvedTarget = [IO.Path]::GetFullPath(
(Join-Path $repoRootPath $normalized.Replace('/', [IO.Path]::DirectorySeparatorChar))
)
if (-not (
$resolvedTarget.Equals($repoRootPath, [StringComparison]::OrdinalIgnoreCase) -or
$resolvedTarget.StartsWith($repoRootPrefix, [StringComparison]::OrdinalIgnoreCase)
)) {
$failures.Add(
"Governed JSON path escapes the repository in $($JsonRelativePath) [$PropertyName]: $Value"
)
return
}
if (-not (Test-Path -LiteralPath $resolvedTarget)) {
$failures.Add(
"Governed JSON path target is missing in $($JsonRelativePath) [$PropertyName]: $Value"
)
}
}
function Visit-JsonNode {
param(
[AllowNull()][object]$Node,
[string]$JsonRelativePath
)
if ($null -eq $Node) {
return
}
if ($Node -is [Management.Automation.PSCustomObject]) {
foreach ($property in $Node.PSObject.Properties) {
if ($property.Name -match '(?i)^(?:path|.+_path)$' -and
$property.Value -is [string]) {
$script:jsonPathFieldCount++
Test-GovernedJsonPath -Value ([string]$property.Value) -JsonRelativePath $JsonRelativePath -PropertyName $property.Name
}
Visit-JsonNode -Node $property.Value -JsonRelativePath $JsonRelativePath
}
return
}
if ($Node -is [Collections.IDictionary]) {
foreach ($key in $Node.Keys) {
$value = $Node[$key]
if ([string]$key -match '(?i)^(?:path|.+_path)$' -and $value -is [string]) {
$script:jsonPathFieldCount++
Test-GovernedJsonPath -Value ([string]$value) -JsonRelativePath $JsonRelativePath -PropertyName ([string]$key)
}
Visit-JsonNode -Node $value -JsonRelativePath $JsonRelativePath
}
return
}
if ($Node -is [Collections.IEnumerable] -and $Node -isnot [string]) {
foreach ($item in $Node) {
Visit-JsonNode -Node $item -JsonRelativePath $JsonRelativePath
}
}
}
$jsonDocumentCount = 0
foreach ($jsonFile in Get-ChildItem -LiteralPath $docsRootPath -Recurse -File -Filter *.json) {
$jsonRelativePath = [IO.Path]::GetRelativePath(
$repoRootPath,
$jsonFile.FullName
).Replace('\', '/')
$docsRelativePath = [IO.Path]::GetRelativePath(
$docsRootPath,
$jsonFile.FullName
).Replace('\', '/')
if ($docsRelativePath.StartsWith('history/', [StringComparison]::OrdinalIgnoreCase) -or
$docsRelativePath -match '(?i)(^|/)archive/') {
continue
}
$jsonDocumentCount++
try {
$jsonNode = Get-Content -LiteralPath $jsonFile.FullName -Raw -Encoding utf8 |
ConvertFrom-Json -Depth 100
Visit-JsonNode -Node $jsonNode -JsonRelativePath $jsonRelativePath
}
catch {
$failures.Add("Governed documentation JSON is malformed: $($jsonRelativePath): $($_.Exception.Message)")
}
}
if ($failures.Count -gt 0) {
Write-Host 'Documentation index check failed:'
foreach ($failure in $failures) {
Write-Host " - $failure"
}
exit 1
}
$researchDomainCount = if (Test-Path -LiteralPath $researchRoot -PathType Container) {
(Get-ChildItem -LiteralPath $researchRoot -Directory).Count
}
else {
0
}
Write-Host (
"Documentation index check passed for {0} top-level Markdown file(s), {1} research domain(s), {2} authority-surface local link(s), and {3}/{4} governed path field(s) across {5} JSON document(s)." -f
((Get-ChildItem -LiteralPath $docsRootPath -File -Filter *.md).Count - 1),
$researchDomainCount,
$validatedLocalLinkCount,
$validatedJsonPathCount,
$jsonPathFieldCount,
$jsonDocumentCount
)