|
| 1 | + |
| 2 | +# Efficient script to copy fallback files into version-0.82 with badges removed |
| 3 | +# Iterates each fallback directory ONCE, extracting original_id from frontmatter |
| 4 | + |
| 5 | +Set-Location "G:\react-native-windows-samples\website" |
| 6 | + |
| 7 | +# Step 1: Build set of original_ids already covered in version-0.82 |
| 8 | +$covered = @{} |
| 9 | +Get-ChildItem "versioned_docs\version-0.82" -Filter "*.md" -Recurse | ForEach-Object { |
| 10 | + $lines = Get-Content $_.FullName -TotalCount 10 |
| 11 | + foreach ($line in $lines) { |
| 12 | + if ($line -match '^original_id:\s*(.+)$') { |
| 13 | + $covered[$Matches[1].Trim()] = $true |
| 14 | + break |
| 15 | + } |
| 16 | + } |
| 17 | +} |
| 18 | +Write-Output "Already covered in 0.82: $($covered.Count)" |
| 19 | + |
| 20 | +# Step 2: Get all doc IDs from sidebar to know which ones we need |
| 21 | +$sidebar = Get-Content "versioned_sidebars\version-0.82-sidebars.json" -Raw | ConvertFrom-Json |
| 22 | +$neededIds = @{} |
| 23 | + |
| 24 | +function Extract-Ids($obj) { |
| 25 | + if ($obj -is [string]) { |
| 26 | + $script:neededIds[$obj] = $true |
| 27 | + } elseif ($obj -is [array]) { |
| 28 | + foreach ($item in $obj) { Extract-Ids $item } |
| 29 | + } elseif ($obj.PSObject -and $obj.PSObject.Properties) { |
| 30 | + foreach ($prop in $obj.PSObject.Properties) { |
| 31 | + Extract-Ids $prop.Value |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +Extract-Ids $sidebar |
| 36 | +Write-Output "Total sidebar IDs: $($neededIds.Count)" |
| 37 | + |
| 38 | +# Step 3: Determine which IDs still need to be copied |
| 39 | +$toCopy = @{} |
| 40 | +foreach ($id in $neededIds.Keys) { |
| 41 | + if (-not $covered.ContainsKey($id)) { |
| 42 | + $toCopy[$id] = $true |
| 43 | + } |
| 44 | +} |
| 45 | +Write-Output "IDs still needed: $($toCopy.Count)" |
| 46 | + |
| 47 | +# Step 4: Iterate fallback directories in order (newest first) |
| 48 | +$fallbackDirs = @( |
| 49 | + "version-0.81", "version-0.80", "version-0.79", "version-0.78", |
| 50 | + "version-0.77", "version-0.76", "version-0.75", "version-0.74", |
| 51 | + "version-0.73", "version-0.72", "version-0.71", "version-0.70", |
| 52 | + "version-0.69", "version-0.68", "version-0.67", "version-0.66", |
| 53 | + "version-0.65", "version-0.64", "version-0.63", "version-0.62", |
| 54 | + "version-0.61", "version-0.60" |
| 55 | +) |
| 56 | + |
| 57 | +$copied = 0 |
| 58 | +$noBadge = 0 |
| 59 | +$notFound = @{} |
| 60 | + |
| 61 | +# Copy $toCopy so we can iterate |
| 62 | +foreach ($id in @($toCopy.Keys)) { $notFound[$id] = $true } |
| 63 | + |
| 64 | +foreach ($dir in $fallbackDirs) { |
| 65 | + $dirPath = "versioned_docs\$dir" |
| 66 | + if (-not (Test-Path $dirPath)) { continue } |
| 67 | + |
| 68 | + # Get all .md files in this fallback dir (including native-api subfolder) |
| 69 | + $files = Get-ChildItem $dirPath -Filter "*.md" -Recurse |
| 70 | + |
| 71 | + foreach ($file in $files) { |
| 72 | + # Quick frontmatter parse for original_id |
| 73 | + $lines = Get-Content $file.FullName -TotalCount 10 |
| 74 | + $originalId = $null |
| 75 | + foreach ($line in $lines) { |
| 76 | + if ($line -match '^original_id:\s*(.+)$') { |
| 77 | + $originalId = $Matches[1].Trim() |
| 78 | + break |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (-not $originalId) { continue } |
| 83 | + if (-not $toCopy.ContainsKey($originalId)) { continue } |
| 84 | + |
| 85 | + # This file matches an ID we need! Read full content |
| 86 | + $content = Get-Content $file.FullName -Raw |
| 87 | + |
| 88 | + # Determine target path (preserve subfolder structure) |
| 89 | + $relativePath = $file.FullName.Substring((Resolve-Path $dirPath).Path.Length + 1) |
| 90 | + $targetPath = "versioned_docs\version-0.82\$relativePath" |
| 91 | + |
| 92 | + # Remove badge line (various patterns) |
| 93 | + $newContent = $content -replace '(?m)^\!\[Architecture\]\(https://img\.shields\.io/badge/architecture-[^\)]+\)\r?\n\r?\n', '' |
| 94 | + |
| 95 | + # Update versioned ID |
| 96 | + $newContent = $newContent -replace 'id:\s*version-[\d\.]+-', 'id: version-0.82-' |
| 97 | + |
| 98 | + # Ensure target directory exists |
| 99 | + $targetDir = Split-Path $targetPath -Parent |
| 100 | + if (-not (Test-Path $targetDir)) { |
| 101 | + New-Item -ItemType Directory -Path $targetDir -Force | Out-Null |
| 102 | + } |
| 103 | + |
| 104 | + Set-Content -Path $targetPath -Value $newContent -NoNewline |
| 105 | + $copied++ |
| 106 | + |
| 107 | + # Remove from needed set |
| 108 | + $toCopy.Remove($originalId) |
| 109 | + $notFound.Remove($originalId) |
| 110 | + } |
| 111 | + |
| 112 | + Write-Output "After $dir : copied=$copied, remaining=$($toCopy.Count)" |
| 113 | + |
| 114 | + # Early exit if all covered |
| 115 | + if ($toCopy.Count -eq 0) { break } |
| 116 | +} |
| 117 | + |
| 118 | +# Step 5: Try docs/ folder for any remaining |
| 119 | +if ($toCopy.Count -gt 0) { |
| 120 | + Write-Output "Checking docs/ for $($toCopy.Count) remaining IDs..." |
| 121 | + $docsFiles = Get-ChildItem "..\docs" -Filter "*.md" -Recurse |
| 122 | + foreach ($file in $docsFiles) { |
| 123 | + $lines = Get-Content $file.FullName -TotalCount 10 |
| 124 | + $docId = $null |
| 125 | + foreach ($line in $lines) { |
| 126 | + if ($line -match '^id:\s*(.+)$') { |
| 127 | + $docId = $Matches[1].Trim() |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (-not $docId) { continue } |
| 133 | + if (-not $toCopy.ContainsKey($docId)) { continue } |
| 134 | + |
| 135 | + $content = Get-Content $file.FullName -Raw |
| 136 | + $relativePath = $file.FullName.Substring((Resolve-Path "..\docs").Path.Length + 1) |
| 137 | + $targetPath = "versioned_docs\version-0.82\$relativePath" |
| 138 | + |
| 139 | + # Remove badge |
| 140 | + $newContent = $content -replace '(?m)^\!\[Architecture\]\(https://img\.shields\.io/badge/architecture-[^\)]+\)\r?\n\r?\n', '' |
| 141 | + |
| 142 | + # Replace id with versioned id and add original_id |
| 143 | + $newContent = $newContent -replace '(?m)^(id:\s*)(.+)$', "id: version-0.82-$docId`noriginal_id: $docId" |
| 144 | + |
| 145 | + $targetDir = Split-Path $targetPath -Parent |
| 146 | + if (-not (Test-Path $targetDir)) { |
| 147 | + New-Item -ItemType Directory -Path $targetDir -Force | Out-Null |
| 148 | + } |
| 149 | + |
| 150 | + Set-Content -Path $targetPath -Value $newContent -NoNewline |
| 151 | + $copied++ |
| 152 | + $toCopy.Remove($docId) |
| 153 | + $notFound.Remove($docId) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +Write-Output "" |
| 158 | +Write-Output "=== SUMMARY ===" |
| 159 | +Write-Output "Total copied: $copied" |
| 160 | +Write-Output "Still missing: $($toCopy.Count)" |
| 161 | +if ($toCopy.Count -gt 0) { |
| 162 | + Write-Output "Missing IDs:" |
| 163 | + $toCopy.Keys | Sort-Object | ForEach-Object { Write-Output " $_" } |
| 164 | +} |
0 commit comments