|
| 1 | +# |
| 2 | +# UpdateNativeApiDocs.ps1 is a PowerShell script designed to download the |
| 3 | +# API docs from the specified RNW CI build in ADO and integrate them into |
| 4 | +# our docs folder, properly tagged with New/Old architecture tags |
| 5 | + |
| 6 | +param( |
| 7 | + [Parameter(Mandatory=$true)] |
| 8 | + [int]$BuildId, |
| 9 | + [boolean]$Clean = $True |
| 10 | +) |
| 11 | + |
| 12 | +Function Download-BuildArtifact([int]$BuildId, [string] $ArtifactName, [string]$DestPath = $env:TEMP) |
| 13 | +{ |
| 14 | + Write-Host "Downloading `"$ArtifactName`" from build $BuildId" |
| 15 | + [string]$ArtifactUrl = "https://dev.azure.com/ms/8d8e50dd-c5b4-4b44-8668-86558c5aa5b7/_apis/build/builds/$BuildId/artifacts?artifactName=$ArtifactName&api-version=7.1&%24format=zip" |
| 16 | + [string]$ArtifactZipFile = Join-Path $DestPath "$ArtifactName.zip" |
| 17 | + |
| 18 | + Write-Debug "Downloading `"$ArtifactUrl`"" |
| 19 | + Invoke-WebRequest $ArtifactUrl -OutFile $ArtifactZipFile |
| 20 | + |
| 21 | + Write-Debug "Extracting `"$ArtifactZipFile`"" |
| 22 | + Expand-Archive -LiteralPath $ArtifactZipFile -DestinationPath $DestPath -Force |
| 23 | + |
| 24 | + [string]$ExtractedPath = Join-Path $DestPath $ArtifactName |
| 25 | + |
| 26 | + Write-Debug "Artifact extracted to `"$ExtractedPath`"" |
| 27 | + return $ExtractedPath |
| 28 | +} |
| 29 | + |
| 30 | +Function Create-WinRtApiDocWithBadge([string]$SourceDocPath, [string]$DestDocPath, [string]$BadgeMd) |
| 31 | +{ |
| 32 | + Write-Host "Creating `"$DestDocPath`"" |
| 33 | + |
| 34 | + # Load content |
| 35 | + $Content = (Get-Content $SourceDocPath -Raw) |
| 36 | + |
| 37 | + # Add Badge |
| 38 | + $Content = $Content -replace "---\r\n\r\n", "---`r`n`r`n$($BadgeMd)`r`n`r`n" |
| 39 | + |
| 40 | + # Clean up spacing |
| 41 | + $Content = $Content -replace "(\r\n){3,}", "`r`n`r`n" |
| 42 | + |
| 43 | + # Fix links |
| 44 | + $Content = $Content -replace "https://docs.microsoft.com/uwp/api/Windows.UI.Xaml", "https://learn.microsoft.com/uwp/api/Windows.UI.Xaml" # Workaround for https://github.com/asklar/winmd2md/issues/8 |
| 45 | + $Content = $Content -replace "https://docs.microsoft.com/uwp/api/Microsoft.UI", "https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/Microsoft.UI" # Workaround for https://github.com/asklar/winmd2md/issues/7 |
| 46 | + $Content = $Content -replace "https://docs.microsoft.com/uwp/api/Microsoft.ReactNative\.(\w+\.)*(\w+)", '$2' # Workaround for https://github.com/asklar/winmd2md/issues/9 |
| 47 | + |
| 48 | + $Content = $Content.Trim() |
| 49 | + |
| 50 | + # Write final file |
| 51 | + $Content | Out-File -Encoding utf8NoBOM $DestDocPath |
| 52 | +} |
| 53 | + |
| 54 | +Function Get-DocKind([string]$DocPath) { |
| 55 | + [string]$Kind = "unknown" |
| 56 | + $KindMatches = ((Get-Content $DocPath -Raw) | Select-String -Pattern 'Kind: `(\w+)`').Matches |
| 57 | + if (($KindMatches.Length -gt 0) -and ($KindMatches[0].Groups.Length -gt 1)) { |
| 58 | + $Kind = $KindMatches[0].Groups[1] |
| 59 | + } |
| 60 | + return $Kind |
| 61 | +} |
| 62 | + |
| 63 | +Function Merge-WinRtApiDocs([string]$OldArchDocsPath, [string]$NewArchDocsPath, [string]$OutputDocsPath) |
| 64 | +{ |
| 65 | + Write-Host "Merging WinRT API docs" |
| 66 | + |
| 67 | + [string]$NewAndOldArchBadgeMd = "" |
| 68 | + [string]$NewArchOnlyBadgeMd = "" |
| 69 | + [string]$OldArchOnlyBadgeMd = "" |
| 70 | + |
| 71 | + $OldArchFiles = Get-ChildItem -Path $OldArchDocsPath -File -Filter *.md |
| 72 | + $NewArchFiles = Get-ChildItem -Path $NewArchDocsPath -File -Filter *.md |
| 73 | + |
| 74 | + $AllFilesSet = New-Object System.Collections.Generic.HashSet[string]; |
| 75 | + $($OldArchFiles; $NewArchFiles) | ForEach-Object { $AllFilesSet.Add($_.Name) | Out-Null } |
| 76 | + |
| 77 | + $AllTypesByKind = @{} |
| 78 | + |
| 79 | + # Find and process docs that are Old Arch Only |
| 80 | + Compare-Object $OldArchFiles $NewArchFiles -Property Name | Where-Object { $_.SideIndicator -eq "<=" } | |
| 81 | + ForEach-Object { |
| 82 | + Write-Debug "Old Arch Only: $($_.Name)" |
| 83 | + Create-WinRtApiDocWithBadge -SourceDocPath (Join-Path $OldArchDocsPath $_.Name) -DestDocPath (Join-Path $OutputDocsPath $_.Name) -BadgeMd $OldArchOnlyBadgeMd |
| 84 | + if ($AllFilesSet.Remove($_.Name)) { |
| 85 | + $Kind = Get-DocKind -DocPath (Join-Path $OutputDocsPath $_.Name) |
| 86 | + if ($AllTypesByKind[$Kind] -eq $Null) { $AllTypesByKind[$Kind] = @() } |
| 87 | + $AllTypesByKind[$Kind] += $_.Name.Replace("-api-windows.md", "") |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + # Find and process docs that are New Arch Only |
| 92 | + Compare-Object $NewArchFiles $OldArchFiles -Property Name | Where-Object { $_.SideIndicator -eq "<=" } | |
| 93 | + ForEach-Object { |
| 94 | + Write-Debug "New Arch Only: $($_.Name)" |
| 95 | + Create-WinRtApiDocWithBadge -SourceDocPath (Join-Path $NewArchDocsPath $_.Name) -DestDocPath (Join-Path $OutputDocsPath $_.Name) -BadgeMd $NewArchOnlyBadgeMd |
| 96 | + if ($AllFilesSet.Remove($_.Name)) { |
| 97 | + $Kind = Get-DocKind -DocPath (Join-Path $OutputDocsPath $_.Name) |
| 98 | + if ($AllTypesByKind[$Kind] -eq $Null) { $AllTypesByKind[$Kind] = @() } |
| 99 | + $AllTypesByKind[$Kind] += $_.Name.Replace("-api-windows.md", "") |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + # Process remaining docs are both Old and New Arch |
| 104 | + $AllFilesSet | |
| 105 | + ForEach-Object { |
| 106 | + Write-Debug "Old&New Arch: $_" |
| 107 | + $OldContent = (Get-Content (Join-Path $OldArchDocsPath $_) -Raw) |
| 108 | + $NewContent = (Get-Content (Join-Path $NewArchDocsPath $_) -Raw) |
| 109 | + if ($OldContent -eq $NewContent) |
| 110 | + { |
| 111 | + Write-Debug "Same Content: $_" |
| 112 | + Create-WinRtApiDocWithBadge -SourceDocPath (Join-Path $OldArchDocsPath $_) -DestDocPath (Join-Path $OutputDocsPath $_) -BadgeMd $NewAndOldArchBadgeMd |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + Write-Debug "Diff Content: $_" |
| 117 | + $MergedFilePath = (Join-Path $DownloadPath $_) |
| 118 | + |
| 119 | + $NewContent = $NewContent -replace "---\r\n\r\n", "---`r`n`r`n# New Architecture`r`n`r`n" |
| 120 | + $NewContent = $NewContent -replace "\r\n#", "`r`n##" |
| 121 | + |
| 122 | + $OldContent = $OldContent -replace "---(.*\r\n){1,}---\r\n\r\n", "`r`n`r`n# Old Architecture`r`n`r`n" |
| 123 | + $OldContent = $OldContent -replace "\r\n#", "`r`n##" |
| 124 | + |
| 125 | + ($NewContent + $OldContent) | Out-File -Encoding utf8NoBOM -NoNewline $MergedFilePath |
| 126 | + Create-WinRtApiDocWithBadge -SourceDocPath $MergedFilePath -DestDocPath (Join-Path $OutputDocsPath $_) -BadgeMd $NewAndOldArchBadgeMd |
| 127 | + } |
| 128 | + $Kind = Get-DocKind -DocPath (Join-Path $OutputDocsPath $_) |
| 129 | + if ($AllTypesByKind[$Kind] -eq $Null) { $AllTypesByKind[$Kind] = @() } |
| 130 | + $AllTypesByKind[$Kind] += $_.Replace("-api-windows.md", "") |
| 131 | + } |
| 132 | + |
| 133 | + # Clean up links to enum type values (workaround for https://github.com/asklar/winmd2md/issues/10) |
| 134 | + (Get-ChildItem -Path $OutputDocsPath -File -Filter *-api-windows.md) | ForEach-Object { |
| 135 | + $FilePath = $_ |
| 136 | + $FileContent = (Get-Content $FilePath -Raw) |
| 137 | + $AllTypesByKind['enum'] | ForEach-Object { |
| 138 | + $FileContent = $FileContent -replace "\($_#\w+\)", "($_)" |
| 139 | + } |
| 140 | + $FileContent | Out-File -Encoding utf8NoBOM -NoNewLine $FilePath |
| 141 | + } |
| 142 | + |
| 143 | + # Recreate index (workaround for https://github.com/asklar/winmd2md/issues/10) |
| 144 | + [string]$IndexContent = "" |
| 145 | + $IndexContent += "---`r`n" |
| 146 | + $IndexContent += "id: Native-API-Reference`r`n" |
| 147 | + $IndexContent += "title: namespace Microsoft.ReactNative`r`n" |
| 148 | + $IndexContent += "sidebar_label: Full reference`r`n" |
| 149 | + $IndexContent += "---`r`n" |
| 150 | + $IndexContent += "`r`n" |
| 151 | + $IndexContent += $NewAndOldArchBadgeMd + "`r`n" |
| 152 | + $IndexContent += "`r`n" |
| 153 | + |
| 154 | + $AllTypesByKind.Keys | Where-Object { $_ -ne 'unknown' } | Sort-Object | ForEach-Object { |
| 155 | + $IndexContent += "## $($_.Substring(0,1).ToUpper())$($_.Substring(1))$($_.EndsWith("s") ? "es" : "s")`r`n" |
| 156 | + $AllTypesByKind[$_] | Sort-Object | ForEach-Object { |
| 157 | + $IndexContent += "- [$_]($_)`r`n" |
| 158 | + } |
| 159 | + $IndexContent += "`r`n" |
| 160 | + } |
| 161 | + |
| 162 | + $IndexContent = $IndexContent.Trim() |
| 163 | + |
| 164 | + $IndexPath = (Join-Path $OutputDocsPath "index-api-windows.md") |
| 165 | + |
| 166 | + Write-Host "Creating `"$IndexPath`"" |
| 167 | + $IndexContent | Out-File -Encoding utf8NoBOM $IndexPath |
| 168 | +} |
| 169 | + |
| 170 | +[string] $RepoRoot = Resolve-Path "$PSScriptRoot/../.." |
| 171 | + |
| 172 | +$StartingLocation = Get-Location |
| 173 | +Set-Location -Path $RepoRoot |
| 174 | + |
| 175 | +try |
| 176 | +{ |
| 177 | + Write-Host "UpdateNativeApiDocs -BuildId $BuildId" |
| 178 | + |
| 179 | + $DownloadPath = Join-Path $env:TEMP "UpdateNativeApiDocs-$BuildId" |
| 180 | + Write-Debug "Downloading artifacts to `"$DownloadPath`"" |
| 181 | + if (Test-Path $DownloadPath) |
| 182 | + { |
| 183 | + Write-Debug "Deleting existing `"$DownloadPath`"" |
| 184 | + Remove-Item -Path $DownloadPath -Recurse -Force |
| 185 | + } |
| 186 | + |
| 187 | + Write-Debug "Creating new `"$DownloadPath`"" |
| 188 | + New-Item $DownloadPath -ItemType "Directory" | Out-Null |
| 189 | + |
| 190 | + Write-Debug "Downloading WinRT API docs from $BuildId" |
| 191 | + [string]$OldArchDocsPath = Download-BuildArtifact -BuildId $BuildId -ArtifactName "WinRT Api docs - Universal Build X64Debug-1" -DestPath $DownloadPath |
| 192 | + [string]$NewArchDocsPath = Download-BuildArtifact -BuildId $BuildId -ArtifactName "WinRT Api docs - Universal Build X64DebugFabric-1" -DestPath $DownloadPath |
| 193 | + |
| 194 | + [string]$OutputDocsPath = Join-Path $RepoRoot "docs/native-api" |
| 195 | + |
| 196 | + if ($Clean) { |
| 197 | + Write-Debug "Cleaning `"$OutputDocsPath`"" |
| 198 | + Remove-Item -Path $OutputDocsPath -Recurse -Force |
| 199 | + New-Item $OutputDocsPath -ItemType "Directory" | Out-Null |
| 200 | + } |
| 201 | + |
| 202 | + Merge-WinRtApiDocs -OldArchDocsPath $OldArchDocsPath -NewArchDocsPath $NewArchDocsPath -OutputDocsPath $OutputDocsPath |
| 203 | +} |
| 204 | +finally |
| 205 | +{ |
| 206 | + Set-Location -Path $StartingLocation |
| 207 | +} |
| 208 | + |
| 209 | +exit 0 |
0 commit comments