-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathsnapshot.ps1
More file actions
841 lines (793 loc) · 31.3 KB
/
Copy pathsnapshot.ps1
File metadata and controls
841 lines (793 loc) · 31.3 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
param(
[string]$Version,
[switch]$All,
[switch]$Check,
[switch]$PromoteLatest,
[string[]]$CorrectPage,
[string]$ManifestPath,
[string]$WebsiteRoot
)
$ErrorActionPreference = "Stop"
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot "../..")).Path
. (Join-Path $PSScriptRoot "snapshot-state.ps1")
if (-not $WebsiteRoot) {
$WebsiteRoot = Join-Path $repoRoot "website"
}
$WebsiteRoot = [System.IO.Path]::GetFullPath($WebsiteRoot)
$canonicalManifestPath = Join-Path $WebsiteRoot "humanizer-versions.json"
if (-not $ManifestPath) {
$ManifestPath = $canonicalManifestPath
}
$ManifestPath = [System.IO.Path]::GetFullPath($ManifestPath)
if ($All) {
if ($Version -or $PromoteLatest -or $CorrectPage) {
throw "-All cannot be combined with version, promotion, or correction."
}
if (-not $Check) {
throw "-All is validation-only and requires -Check."
}
} elseif (-not $Version) {
throw "Choose exactly one of -Version or -All."
}
if (-not $Check -and $ManifestPath -ne
[System.IO.Path]::GetFullPath($canonicalManifestPath)) {
throw "Snapshot mutation requires the canonical version manifest."
}
if ($CorrectPage -and ($Check -or $PromoteLatest -or $Version -eq "current")) {
throw "Historical correction cannot check, promote, or target current."
}
if ($Version -eq "current" -and $PromoteLatest) {
throw "The current preview cannot be promoted."
}
function Assert-CorrectionPath {
param([Parameter(Mandatory = $true)][string]$Path)
if ([string]::IsNullOrWhiteSpace($Path) -or
[System.IO.Path]::IsPathRooted($Path) -or
$Path.Contains("\") -or
$Path -match "(^|/)\.\.(/|$)" -or
$Path -match "[*?\[\]]" -or
$Path -match "^api(/|$)" -or
($Path -notmatch "\.mdx?$" -and
$Path -notmatch "^_examples/.+\.(?:cs|csproj)$")) {
throw "Historical corrections require exact authored page or runnable example paths: $Path"
}
}
function Get-ExcludedDocIds {
param(
[Parameter(Mandatory = $true)][string]$DocsRoot,
[Parameter(Mandatory = $true)][object[]]$Exclusions
)
$ids = [System.Collections.Generic.HashSet[string]]::new(
[System.StringComparer]::Ordinal
)
foreach ($path in $Exclusions | Where-Object { $_ -match "\.mdx?$" }) {
$canonicalPath = Join-Path $DocsRoot $path
$content = Get-Content -Raw $canonicalPath
if ($content -notmatch "(?m)^id:\s*(?<id>[^\s]+)\s*$") {
throw "Excluded documentation page has no explicit ID: $path"
}
$directory = [System.IO.Path]::GetDirectoryName($path).Replace("\", "/")
$id = if ([string]::IsNullOrWhiteSpace($directory)) {
$Matches["id"]
} else {
"$directory/$($Matches["id"])"
}
$null = $ids.Add($id)
}
return $ids
}
function Convert-SidebarItem {
param(
[Parameter(Mandatory = $true)]$Item,
[Parameter(Mandatory = $true)]
[System.Collections.Generic.HashSet[string]]$ExcludedIds
)
if ($Item -is [string]) {
if ($ExcludedIds.Contains($Item)) {
return $null
}
return $Item
}
if ([string]$Item.type -ne "category") {
return $Item
}
$result = [ordered]@{
type = "category"
label = [string]$Item.label
}
if ($null -ne $Item.PSObject.Properties["collapsed"]) {
$result.collapsed = [bool]$Item.collapsed
}
if ($null -ne $Item.PSObject.Properties["link"]) {
if ([string]$Item.link.type -ne "doc" -or
-not $ExcludedIds.Contains([string]$Item.link.id)) {
$result.link = $Item.link
}
}
$items = [System.Collections.Generic.List[object]]::new()
foreach ($child in @($Item.items)) {
$converted = Convert-SidebarItem `
-Item $child `
-ExcludedIds $ExcludedIds
if ($null -ne $converted) {
$items.Add($converted)
}
}
if ($items.Count -gt 0) {
$result.items = @($items)
}
if ($items.Count -eq 0 -and -not $result.Contains("link")) {
return $null
}
return [PSCustomObject]$result
}
function New-PrunedSidebar {
param(
[Parameter(Mandatory = $true)][string]$SourcePath,
[Parameter(Mandatory = $true)][string]$OutputPath,
[Parameter(Mandatory = $true)]
[System.Collections.Generic.HashSet[string]]$ExcludedIds
)
$source = Get-Content -Raw $SourcePath | ConvertFrom-Json -Depth 100
$items = [System.Collections.Generic.List[object]]::new()
foreach ($item in @($source.docs)) {
$converted = Convert-SidebarItem `
-Item $item `
-ExcludedIds $ExcludedIds
if ($null -ne $converted) {
$items.Add($converted)
}
}
$labels = @(
$items |
Where-Object { $_ -isnot [string] } |
ForEach-Object label
)
foreach ($required in @(
"Start here",
"Scenarios",
"Languages",
"Upgrading",
"API reference"
)) {
if ($required -notin $labels) {
throw "Derived sidebar is missing required root '$required'."
}
}
Write-SnapshotJson `
-Value ([ordered]@{ docs = @($items) }) `
-Path $OutputPath
}
function Assert-SnapshotLinks {
param(
[Parameter(Mandatory = $true)][string]$DocsRoot,
[Parameter(Mandatory = $true)][string]$VersionLabel
)
foreach ($page in Get-ChildItem $DocsRoot -File -Recurse |
Where-Object Extension -in @(".md", ".mdx")) {
$resolvedDocsRoot = [System.IO.Path]::GetFullPath($DocsRoot).TrimEnd(
[System.IO.Path]::DirectorySeparatorChar
) + [System.IO.Path]::DirectorySeparatorChar
$relativePage = [System.IO.Path]::GetRelativePath(
$DocsRoot,
$page.FullName
).Replace("\", "/")
$content = Get-Content -Raw $page.FullName
if ($relativePage -notmatch "^api/" -and
($content -match "\]\(/docs/" -or
$content -match "\b(?:href|to)=[{`"']*/docs/")) {
throw "$VersionLabel contains a cross-version absolute docs link: $relativePage"
}
foreach ($match in [regex]::Matches(
$content,
"\]\((?<target>[^)#\s]+)(?:#[^)]*)?\)"
)) {
$target = $match.Groups["target"].Value
if ($target -match "^(?:[a-z]+:|//)" -or
$target -notmatch "\.mdx?$") {
continue
}
$resolved = [System.IO.Path]::GetFullPath(
(Join-Path $page.DirectoryName $target)
)
if (-not $resolved.StartsWith(
$resolvedDocsRoot,
[System.StringComparison]::Ordinal
) -or
-not (Test-Path $resolved -PathType Leaf)) {
throw "$VersionLabel has a broken relative documentation link in $relativePage`: $target"
}
}
foreach ($match in [regex]::Matches(
$content,
"!!raw-loader!(?<target>[^'`;]+)"
)) {
$target = $match.Groups["target"].Value
$resolved = [System.IO.Path]::GetFullPath(
(Join-Path $page.DirectoryName $target)
)
if (-not $resolved.StartsWith(
$resolvedDocsRoot,
[System.StringComparison]::Ordinal
) -or
-not (Test-Path $resolved -PathType Leaf)) {
throw "$VersionLabel has a broken example import in $relativePage`: $target"
}
}
}
}
function Assert-VersionNarrativeAccuracy {
param(
[Parameter(Mandatory = $true)][string]$DocsRoot,
[Parameter(Mandatory = $true)][string]$Version
)
$isTwoX = $Version.StartsWith("2.", [System.StringComparison]::Ordinal)
$rejectWordForm = $Version -in @("2.10.1", "2.11.10", "2.13.14")
foreach ($page in Get-ChildItem $DocsRoot -File -Recurse |
Where-Object Extension -in @(".md", ".mdx")) {
$relativePage = [System.IO.Path]::GetRelativePath(
$DocsRoot,
$page.FullName
).Replace("\", "/")
if ($relativePage -match "^(?:api|upgrading|analyzer)/") {
continue
}
$content = Get-Content -Raw $page.FullName
foreach ($match in [regex]::Matches(
$content,
"dotnet add package Humanizer --version\s+(?<version>\S+)"
)) {
if ($match.Groups["version"].Value -ne $Version) {
throw "$Version contains a mismatched Humanizer install command in ${relativePage}: $($match.Groups["version"].Value)"
}
}
if ($isTwoX) {
foreach ($forbidden in @(
"Configurator.UseEnumDescriptionPropertyLocator",
"DynamicLengthAndPreserveWords",
"DynamicNumberOfCharactersAndPreserveWords",
"TryToNumber"
)) {
if ($content.Contains(
$forbidden,
[System.StringComparison]::Ordinal
)) {
throw "$Version contains unavailable '$forbidden' guidance in $relativePage."
}
}
}
if ($rejectWordForm -and
$relativePage -in @(
"languages/grammar-and-platform-formatting.mdx",
"scenarios/numbers-in-words-and-ordinals.mdx"
) -and
$content.Contains(
"WordForm",
[System.StringComparison]::Ordinal
)) {
throw "$Version contains unavailable 'WordForm' guidance in $relativePage."
}
if ($relativePage -eq
"languages/grammar-and-platform-formatting.mdx") {
foreach ($previewOnlyClaim in @(
'`ByteSize.TryParse` uses only the decimal-separator override',
'`main/preview` locale data can provide narrowly scoped',
'YAML-backed `calendar` and `number.formatting` override surfaces'
)) {
if ($content.Contains(
$previewOnlyClaim,
[System.StringComparison]::Ordinal
)) {
throw "$Version grammar prose contains a preview-only claim: $previewOnlyClaim"
}
}
}
if ($relativePage -eq "languages/index.mdx" -and
$content -match "(?i)(?:generated\s+)?main/preview\s+inventory") {
throw "$Version labels selected language coverage as main/preview inventory."
}
}
if ($Version -ne "2.10.1") {
$fluentPage = Join-Path $DocsRoot (
"scenarios/fluent-dates-and-time-spans.mdx"
)
if ((Test-Path $fluentPage -PathType Leaf) -and
-not (Get-Content -Raw $fluentPage).Contains(
'DateOnly` fluent helpers begin in `2.11.10` on compatible',
[System.StringComparison]::Ordinal
)) {
throw "$Version does not state the exact DateOnly fluent-helper boundary."
}
}
$migrationPage = Join-Path $DocsRoot "upgrading/version-3-migration.mdx"
if ((Test-Path (Join-Path $DocsRoot "api/Humanizer.Resources.md")) -or
(Test-Path (Join-Path $DocsRoot "api/Humanizer.ResourceKeys.md"))) {
$migrationContent = Get-Content -Raw $migrationPage
if (-not $migrationContent.Contains(
'remain in stable Humanizer 3 releases through `3.0.10`',
[System.StringComparison]::Ordinal
)) {
throw "$Version migration prose incorrectly removes Resources or ResourceKeys."
}
}
}
function Assert-DocumentIdentity {
param(
[Parameter(Mandatory = $true)][string]$Source,
[Parameter(Mandatory = $true)][string]$Target,
[Parameter(Mandatory = $true)][string]$RelativePath
)
if ($RelativePath -notmatch "\.mdx?$") {
return
}
$sourceContent = Get-Content -Raw $Source
$targetContent = Get-Content -Raw $Target
if ($sourceContent -notmatch "(?m)^id:\s*(?<id>[^\s]+)\s*$") {
throw "Correction source has no document identity: $RelativePath"
}
$sourceId = $Matches["id"]
if ($targetContent -notmatch "(?m)^id:\s*(?<id>[^\s]+)\s*$" -or
$Matches["id"] -ne $sourceId) {
throw "Correction changes document identity: $RelativePath"
}
}
function Add-ApiLanding {
param(
[Parameter(Mandatory = $true)][string]$ApiRoot,
[Parameter(Mandatory = $true)]$Entry
)
$provenance = if ($Entry.version -eq "current") {
"Generated from the Humanizer 4 ``$($Entry.referenceTfm)`` reference assembly."
} else {
"Generated from ``$($Entry.apiPackage)`` ``$($Entry.source.packageVersion)`` using the ``$($Entry.referenceTfm)`` reference assembly."
}
$landing = @"
---
id: api
title: API reference
sidebar_position: 2
---
# Humanizer API reference
$provenance
"@
[System.IO.File]::WriteAllText(
(Join-Path $ApiRoot "index.md"),
"$landing`n",
[System.Text.UTF8Encoding]::new($false)
)
}
function Set-VersionedExampleDefaults {
param(
[Parameter(Mandatory = $true)][string]$ExamplesRoot,
[Parameter(Mandatory = $true)]$Entry
)
if ($Entry.source.kind -ne "nuget" -or
$Entry.source.packageVersion -ne $Entry.version) {
throw "Versioned example defaults require an exact NuGet-backed version."
}
$propsPath = Join-Path $ExamplesRoot "Directory.Build.props"
$props = Get-Content -Raw $propsPath
if ($props -match 'Label="HumanizerDocumentationSnapshot"' -or
([regex]::Matches($props, "</Project>")).Count -ne 1) {
throw "Versioned example props cannot be stamped safely: $propsPath"
}
$excludedAssetsProperty = $Entry.PSObject.Properties[
"exampleExcludedAssets"
]
$excludedAssets = if ($null -eq $excludedAssetsProperty) {
@()
} else {
@($excludedAssetsProperty.Value)
}
$excludedAssetsDefault = if ($excludedAssets.Count -eq 0) {
""
} else {
@"
<HumanizerExampleExcludeAssets
Condition="'`$(HumanizerProject)' == '' and '`$(HumanizerPackageVersion)' == '$($Entry.source.packageVersion)' and '`$(HumanizerExampleExcludeAssets)' == ''">$($excludedAssets -join ";")</HumanizerExampleExcludeAssets>
"@
}
if ($excludedAssetsDefault) {
$excludedAssetsDefault += "`n"
}
$defaults = @"
<PropertyGroup Label="HumanizerDocumentationSnapshot">
<HumanizerPackageVersion
Condition="'`$(HumanizerProject)' == '' and '`$(HumanizerPackageVersion)' == ''">$($Entry.source.packageVersion)</HumanizerPackageVersion>
$excludedAssetsDefault </PropertyGroup>
"@
[System.IO.File]::WriteAllText(
$propsPath,
$props.Replace("</Project>", "$defaults</Project>"),
[System.Text.UTF8Encoding]::new($false)
)
}
function Assert-FrozenSnapshot {
param([Parameter(Mandatory = $true)]$Entry)
if (-not $Entry.published) {
throw "Stable snapshot is not published: $($Entry.version)"
}
$docsRoot = Join-Path $WebsiteRoot (
"versioned_docs/version-$($Entry.version)"
)
& (Join-Path $PSScriptRoot "verify-api.ps1") `
-Version $Entry.version `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
& (Join-Path $PSScriptRoot "verify-examples.ps1") `
-Version $Entry.version `
-ManifestPath $ManifestPath `
-ExamplesRoot (Join-Path $docsRoot "_examples")
& (Join-Path $PSScriptRoot "generate-language-coverage.ps1") `
-Check `
-Version $Entry.version `
-ManifestPath $ManifestPath `
-OutputPath (Join-Path $docsRoot "languages/language-coverage.json")
Assert-SnapshotLinks `
-DocsRoot $docsRoot `
-VersionLabel $Entry.version
Assert-VersionNarrativeAccuracy `
-DocsRoot $docsRoot `
-Version $Entry.version
& (Join-Path $PSScriptRoot "verify-scenario-api.ps1") `
-Version $Entry.version `
-DocsRoot $docsRoot `
-ApiRoot (Join-Path $docsRoot "api") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
}
$mutationLock = $null
try {
$mutationLock = Enter-SnapshotMutation -WebsiteRoot $WebsiteRoot
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
$manifest = Get-Content -Raw $ManifestPath | ConvertFrom-Json -Depth 20
$entries = @($manifest.versions)
if ($All) {
foreach ($published in @(
$entries | Where-Object { $_.version -ne "current" -and $_.published }
)) {
Assert-FrozenSnapshot -Entry $published
}
Write-Host "All frozen snapshots passed direct validation."
return
}
$matchingEntries = @($entries | Where-Object version -eq $Version)
if ($matchingEntries.Count -ne 1) {
throw "Version '$Version' is not declared exactly once."
}
$entry = $matchingEntries[0]
if ($Version -eq "current") {
$stagingRoot = Join-Path $WebsiteRoot (
".snapshot-current-$([guid]::NewGuid().ToString('N'))"
)
$stagingApi = Join-Path $stagingRoot "api"
try {
New-Item -ItemType Directory -Path $stagingRoot | Out-Null
& (Join-Path $PSScriptRoot "verify-api.ps1") `
-Version current `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot `
-OutputDirectory $stagingApi
Add-ApiLanding -ApiRoot $stagingApi -Entry $entry
& (Join-Path $PSScriptRoot "verify-scenario-api.ps1") `
-Version current `
-DocsRoot (Join-Path $WebsiteRoot "docs") `
-ApiRoot $stagingApi `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
$liveApi = Join-Path $WebsiteRoot "docs/api"
if ($Check) {
if (-not (Test-Path $liveApi -PathType Container) -or
(Get-SnapshotDirectoryDigest $liveApi) -ne
(Get-SnapshotDirectoryDigest $stagingApi)) {
throw "The current API tree is stale or modified."
}
Write-Host "Current API passed deterministic validation."
return
}
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
Invoke-SnapshotTransaction `
-WebsiteRoot $WebsiteRoot `
-Changes @(
[PSCustomObject]@{
Target = "docs/api"
Source = $stagingApi
}
) `
-Validate {
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
& (Join-Path $PSScriptRoot "verify-scenario-api.ps1") `
-Version current `
-DocsRoot (Join-Path $WebsiteRoot "docs") `
-ApiRoot (Join-Path $WebsiteRoot "docs/api") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
}
Write-Host "Refreshed the current API atomically."
return
} finally {
Remove-Item $stagingRoot -Recurse -Force -ErrorAction SilentlyContinue
}
}
$snapshotPath = Join-Path $WebsiteRoot "versioned_docs/version-$Version"
$sidebarsPath = Join-Path $WebsiteRoot (
"versioned_sidebars/version-$Version-sidebars.json"
)
$overlayRoot = Join-Path $WebsiteRoot $entry.compatibilityOverlay
$overlay = Get-Content -Raw (Join-Path $overlayRoot "overlay.json") |
ConvertFrom-Json -Depth 20
if ($Check) {
Assert-FrozenSnapshot -Entry $entry
Write-Host "Frozen snapshot passed direct validation: $Version"
return
}
if ($CorrectPage.Count -gt 0) {
if (-not $entry.published -or
-not (Test-Path $snapshotPath -PathType Container)) {
throw "Historical correction requires a published snapshot: $Version"
}
$corrections = @($CorrectPage | Sort-Object -Unique)
if ($corrections.Count -ne $CorrectPage.Count) {
throw "Historical correction paths must be unique."
}
$stagingRoot = Join-Path $WebsiteRoot (
".snapshot-correction-$Version-$([guid]::NewGuid().ToString('N'))"
)
$stagingDocs = Join-Path $stagingRoot "docs"
$stagingManifest = Join-Path $stagingRoot "humanizer-versions.json"
try {
New-Item -ItemType Directory -Path $stagingRoot | Out-Null
Copy-Item $snapshotPath $stagingDocs -Recurse
$changed = [System.Collections.Generic.List[string]]::new()
foreach ($path in $corrections) {
Assert-CorrectionPath -Path $path
if ($path -in @($overlay.exclusions)) {
throw "Excluded page cannot be corrected in ${Version}: $path"
}
$sourceRoot = if ($path -in @($overlay.replacements)) {
$overlayRoot
} else {
Join-Path $WebsiteRoot "docs"
}
$sourcePath = Join-Path $sourceRoot $path
$targetPath = Join-Path $snapshotPath $path
if (-not (Test-Path $sourcePath -PathType Leaf) -or
-not (Test-Path $targetPath -PathType Leaf)) {
throw "Historical correction page is missing: $path"
}
Assert-DocumentIdentity `
-Source $sourcePath `
-Target $targetPath `
-RelativePath $path
$stagedPath = Join-Path $stagingDocs $path
Copy-Item $sourcePath $stagedPath -Force
if ((Get-FileHash $sourcePath -Algorithm SHA256).Hash -ne
(Get-FileHash $targetPath -Algorithm SHA256).Hash) {
$changed.Add($path)
}
}
Assert-SnapshotLinks `
-DocsRoot $stagingDocs `
-VersionLabel $Version
Assert-VersionNarrativeAccuracy `
-DocsRoot $stagingDocs `
-Version $Version
& (Join-Path $PSScriptRoot "verify-scenario-api.ps1") `
-Version $Version `
-DocsRoot $stagingDocs `
-ApiRoot (Join-Path $stagingDocs "api") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
if (@($corrections | Where-Object { $_ -match "^_examples/" }).Count -gt 0) {
& (Join-Path $PSScriptRoot "verify-examples.ps1") `
-Version $Version `
-ManifestPath $ManifestPath `
-ExamplesRoot (Join-Path $stagingDocs "_examples")
}
if ($changed.Count -eq 0) {
Write-Host "Historical correction is already applied: $Version"
return
}
$entry.immutability.snapshotSha256 = (
Get-SnapshotDirectoryDigest -Path $stagingDocs
)
Write-SnapshotJson -Value $manifest -Path $stagingManifest
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $stagingManifest `
-WebsiteRoot $WebsiteRoot `
-SkipNativeState
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
$changes = [System.Collections.Generic.List[object]]::new()
foreach ($path in $changed) {
$changes.Add([PSCustomObject]@{
Target = "versioned_docs/version-$Version/$path"
Source = (Join-Path $stagingDocs $path)
})
}
$changes.Add([PSCustomObject]@{
Target = "humanizer-versions.json"
Source = $stagingManifest
})
Invoke-SnapshotTransaction `
-WebsiteRoot $WebsiteRoot `
-Changes @($changes) `
-Validate {
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
Assert-SnapshotLinks `
-DocsRoot $snapshotPath `
-VersionLabel $Version
Assert-VersionNarrativeAccuracy `
-DocsRoot $snapshotPath `
-Version $Version
& (Join-Path $PSScriptRoot "verify-scenario-api.ps1") `
-Version $Version `
-DocsRoot $snapshotPath `
-ApiRoot (Join-Path $snapshotPath "api") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
}
Write-Host "Applied historical correction: $Version ($($changed -join ', '))"
return
} finally {
Remove-Item $stagingRoot -Recurse -Force -ErrorAction SilentlyContinue
}
}
if ($entry.published -or
(Test-Path $snapshotPath) -or
(Test-Path $sidebarsPath)) {
throw "New snapshot creation requires an unpublished version with absent outputs."
}
$stagingRoot = Join-Path $WebsiteRoot (
".snapshot-release-$Version-$([guid]::NewGuid().ToString('N'))"
)
$stagingDocs = Join-Path $stagingRoot "docs"
$stagingSidebar = Join-Path $stagingRoot "sidebars.json"
$stagingVersions = Join-Path $stagingRoot "versions.json"
$stagingManifest = Join-Path $stagingRoot "humanizer-versions.json"
try {
New-Item -ItemType Directory -Path $stagingDocs | Out-Null
Get-ChildItem (Join-Path $WebsiteRoot "docs") -Force |
Where-Object Name -ne "api" |
Copy-Item -Destination $stagingDocs -Recurse
foreach ($path in @($overlay.exclusions)) {
Remove-Item (Join-Path $stagingDocs $path) -Force
}
foreach ($path in @($overlay.replacements)) {
$destination = Join-Path $stagingDocs $path
New-Item -ItemType Directory `
-Path (Split-Path $destination -Parent) `
-Force | Out-Null
Copy-Item (Join-Path $overlayRoot $path) $destination -Force
}
Set-VersionedExampleDefaults `
-ExamplesRoot (Join-Path $stagingDocs "_examples") `
-Entry $entry
$stagingApi = Join-Path $stagingDocs "api"
& (Join-Path $PSScriptRoot "verify-api.ps1") `
-Version $Version `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot `
-OutputDirectory $stagingApi
Add-ApiLanding -ApiRoot $stagingApi -Entry $entry
& (Join-Path $PSScriptRoot "verify-examples.ps1") `
-Version $Version `
-ManifestPath $ManifestPath `
-ExamplesRoot (Join-Path $stagingDocs "_examples")
& (Join-Path $PSScriptRoot "generate-language-coverage.ps1") `
-Version $Version `
-ManifestPath $ManifestPath `
-OutputPath (
Join-Path $stagingDocs "languages/language-coverage.json"
)
$excludedIds = Get-ExcludedDocIds `
-DocsRoot (Join-Path $WebsiteRoot "docs") `
-Exclusions @($overlay.exclusions)
New-PrunedSidebar `
-SourcePath (Join-Path $WebsiteRoot "sidebars.json") `
-OutputPath $stagingSidebar `
-ExcludedIds $excludedIds
Assert-SnapshotLinks `
-DocsRoot $stagingDocs `
-VersionLabel $Version
Assert-VersionNarrativeAccuracy `
-DocsRoot $stagingDocs `
-Version $Version
& (Join-Path $PSScriptRoot "verify-scenario-api.ps1") `
-Version $Version `
-DocsRoot $stagingDocs `
-ApiRoot $stagingApi `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
$entry.published = $true
if ($PromoteLatest) {
$previousLatest = @($entries | Where-Object latestStable)
if ($previousLatest.Count -ne 1 -or
[System.Management.Automation.SemanticVersion]::Parse(
$entry.version
) -le
[System.Management.Automation.SemanticVersion]::Parse(
$previousLatest[0].version
)) {
throw "Latest promotion requires a newer stable version."
}
$previousLatest[0].latestStable = $false
$previousLatest[0].route = $previousLatest[0].version
$previousLatest[0].label = $previousLatest[0].version
$entry.latestStable = $true
$entry.route = ""
$entry.label = "$Version (latest)"
}
$entry | Add-Member -NotePropertyName immutability -NotePropertyValue (
[PSCustomObject]@{
snapshotSha256 = Get-SnapshotDirectoryDigest -Path $stagingDocs
sidebarSha256 = (
Get-FileHash $stagingSidebar -Algorithm SHA256
).Hash
}
)
$nativeVersions = @(
$entries |
Where-Object { $_.version -ne "current" -and $_.published } |
Sort-Object {
[System.Management.Automation.SemanticVersion]::Parse(
$_.version
)
} -Descending |
ForEach-Object version
)
Write-SnapshotJson `
-Value $nativeVersions `
-Path $stagingVersions `
-AsArray
Write-SnapshotJson -Value $manifest -Path $stagingManifest
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $stagingManifest `
-WebsiteRoot $WebsiteRoot `
-SkipNativeState
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
Invoke-SnapshotTransaction `
-WebsiteRoot $WebsiteRoot `
-Changes @(
[PSCustomObject]@{
Target = "versioned_docs/version-$Version"
Source = $stagingDocs
},
[PSCustomObject]@{
Target = "versioned_sidebars/version-$Version-sidebars.json"
Source = $stagingSidebar
},
[PSCustomObject]@{
Target = "versions.json"
Source = $stagingVersions
},
[PSCustomObject]@{
Target = "humanizer-versions.json"
Source = $stagingManifest
}
) `
-Validate {
& (Join-Path $PSScriptRoot "verify-manifest.ps1") `
-ManifestPath $ManifestPath `
-WebsiteRoot $WebsiteRoot
}
Write-Host "Created immutable documentation snapshot: $Version"
} finally {
Remove-Item $stagingRoot -Recurse -Force -ErrorAction SilentlyContinue
}
} finally {
if ($null -ne $mutationLock) {
Exit-SnapshotMutation -Lock $mutationLock
}
}