|
| 1 | +# Generates an index page for cataloging different versions of the Docs |
| 2 | +[CmdletBinding()] |
| 3 | +Param ( |
| 4 | + $DocFx, |
| 5 | + $RepoRoot, |
| 6 | + $DocGenDir, |
| 7 | + $DocOutDir = "${RepoRoot}/docfx_project", |
| 8 | + $DocfxJsonPath = "${PSScriptRoot}\docfx.json", |
| 9 | + $MainJsPath = "${PSScriptRoot}\templates\matthews\styles\main.js" |
| 10 | +) |
| 11 | +. "${PSScriptRoot}\..\scripts\common.ps1" |
| 12 | +$GetGithubIoDocIndexFn = "Get-${Language}-GithubIoDocIndex" |
| 13 | + |
| 14 | +# Given the metadata url under https://github.com/Azure/azure-sdk/tree/master/_data/releases/latest, |
| 15 | +# the function will return the csv metadata back as part of response. |
| 16 | +function Get-CSVMetadata ([string]$MetadataUri) { |
| 17 | + $metadataResponse = Invoke-RestMethod -Uri $MetadataUri -method "GET" -MaximumRetryCount 3 -RetryIntervalSec 10 | ConvertFrom-Csv |
| 18 | + return $metadataResponse |
| 19 | +} |
| 20 | + |
| 21 | +# Given the github io blob storage url and language regex, |
| 22 | +# the helper function will return a list of artifact names. |
| 23 | +function Get-BlobStorage-Artifacts($blobStorageUrl, $blobDirectoryRegex, $blobArtifactsReplacement) { |
| 24 | + LogDebug "Reading artifact from storage blob ..." |
| 25 | + $returnedArtifacts = @() |
| 26 | + $pageToken = "" |
| 27 | + Do { |
| 28 | + $resp = "" |
| 29 | + if (!$pageToken) { |
| 30 | + # First page call. |
| 31 | + $resp = Invoke-RestMethod -Method Get -Uri $blobStorageUrl |
| 32 | + } |
| 33 | + else { |
| 34 | + # Next page call |
| 35 | + $blobStorageUrlPageToken = $blobStorageUrl + "&marker=$pageToken" |
| 36 | + $resp = Invoke-RestMethod -Method Get -Uri $blobStorageUrlPageToken |
| 37 | + } |
| 38 | + # Convert to xml documents. |
| 39 | + $xmlDoc = [xml](removeBomFromString $resp) |
| 40 | + foreach ($elem in $xmlDoc.EnumerationResults.Blobs.BlobPrefix) { |
| 41 | + # What service return like "dotnet/Azure.AI.Anomalydetector/", needs to fetch out "Azure.AI.Anomalydetector" |
| 42 | + $artifact = $elem.Name -replace $blobDirectoryRegex, $blobArtifactsReplacement |
| 43 | + $returnedArtifacts += $artifact |
| 44 | + } |
| 45 | + # Fetch page token |
| 46 | + $pageToken = $xmlDoc.EnumerationResults.NextMarker |
| 47 | + } while ($pageToken) |
| 48 | + return $returnedArtifacts |
| 49 | + } |
| 50 | + |
| 51 | +# The sequence of Bom bytes differs by different encoding. |
| 52 | +# The helper function here is only to strip the utf-8 encoding system as it is used by blob storage list api. |
| 53 | +# Return the original string if not in BOM utf-8 sequence. |
| 54 | +function RemoveBomFromString([string]$bomAwareString) { |
| 55 | + if ($bomAwareString.length -le 3) { |
| 56 | + return $bomAwareString |
| 57 | + } |
| 58 | + $bomPatternByteArray = [byte[]] (0xef, 0xbb, 0xbf) |
| 59 | + # The default encoding for powershell is ISO-8859-1, so converting bytes with the encoding. |
| 60 | + $bomAwareBytes = [Text.Encoding]::GetEncoding(28591).GetBytes($bomAwareString.Substring(0, 3)) |
| 61 | + if (@(Compare-Object $bomPatternByteArray $bomAwareBytes -SyncWindow 0).Length -eq 0) { |
| 62 | + return $bomAwareString.Substring(3) |
| 63 | + } |
| 64 | + return $bomAwareString |
| 65 | +} |
| 66 | + |
| 67 | +function Get-TocMapping { |
| 68 | + Param ( |
| 69 | + [Parameter(Mandatory = $true)] [Object[]] $metadata, |
| 70 | + [Parameter(Mandatory = $true)] [String[]] $artifacts |
| 71 | + ) |
| 72 | + # Used for sorting the toc display order |
| 73 | + $orderServiceMapping = @{} |
| 74 | + |
| 75 | + foreach ($artifact in $artifacts) { |
| 76 | + $packageInfo = $metadata | ? {$_.Package -eq $artifact} |
| 77 | + |
| 78 | + if ($packageInfo -and $packageInfo[0].Hide -eq 'true') { |
| 79 | + LogDebug "The artifact $artifact set 'Hide' to 'true'." |
| 80 | + continue |
| 81 | + } |
| 82 | + $serviceName = "" |
| 83 | + $displayName = "" |
| 84 | + if (!$packageInfo) { |
| 85 | + LogWarning "There is no artifact $artifact. Please check csv of Azure/azure-sdk/_data/release/latest repo if this is intended. " |
| 86 | + # If no service name retrieved, print out warning message, and put it into Other page. |
| 87 | + $serviceName = "Other" |
| 88 | + } |
| 89 | + elseif (!$packageInfo[0].ServiceName) { |
| 90 | + LogWarning "There is no service name for artifact $artifact. Please check csv of Azure/azure-sdk/_data/release/latest repo if this is intended. " |
| 91 | + # If no service name retrieved, print out warning message, and put it into Other page. |
| 92 | + $serviceName = "Other" |
| 93 | + $displayName = $packageInfo[0].DisplayName.Trim() |
| 94 | + } |
| 95 | + else { |
| 96 | + if ($packageInfo.Length -gt 1) { |
| 97 | + LogWarning "There are more than 1 packages fetched out for artifact $artifact. Please check csv of Azure/azure-sdk/_data/release/latest repo if this is intended. " |
| 98 | + } |
| 99 | + $serviceName = $packageInfo[0].ServiceName.Trim() |
| 100 | + $displayName = $packageInfo[0].DisplayName.Trim() |
| 101 | + } |
| 102 | + $orderServiceMapping[$artifact] = @($serviceName, $displayName) |
| 103 | + } |
| 104 | + return $orderServiceMapping |
| 105 | +} |
| 106 | + |
| 107 | +function GenerateDocfxTocContent([Hashtable]$tocContent, [String]$lang) { |
| 108 | + LogDebug "Start generating the docfx toc and build docfx site..." |
| 109 | + |
| 110 | + LogDebug "Initializing Default DocFx Site..." |
| 111 | + & $($DocFx) init -q -o "${DocOutDir}" |
| 112 | + # The line below is used for testing in local |
| 113 | + #docfx init -q -o "${DocOutDir}" |
| 114 | + LogDebug "Copying template and configuration..." |
| 115 | + New-Item -Path "${DocOutDir}" -Name "templates" -ItemType "directory" -Force |
| 116 | + Copy-Item "${DocGenDir}/templates/*" -Destination "${DocOutDir}/templates" -Force -Recurse |
| 117 | + Copy-Item "${DocGenDir}/docfx.json" -Destination "${DocOutDir}/" -Force |
| 118 | + $YmlPath = "${DocOutDir}/api" |
| 119 | + New-Item -Path $YmlPath -Name "toc.yml" -Force |
| 120 | + $visitedService = @{} |
| 121 | + # Sort and display toc service name by alphabetical order, and then sort artifact by order. |
| 122 | + foreach ($serviceMapping in ($tocContent.GetEnumerator() | Sort-Object Value[0], Key)) { |
| 123 | + $artifact = $serviceMapping.Key |
| 124 | + $serviceName = $serviceMapping.Value[0] |
| 125 | + $displayName = $serviceMapping.Value[1] |
| 126 | + |
| 127 | + $fileName = ($serviceName -replace '\s', '').ToLower().Trim() |
| 128 | + if ($visitedService.ContainsKey($serviceName)) { |
| 129 | + if ($displayName) { |
| 130 | + Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact`n##### ($displayName)" |
| 131 | + } |
| 132 | + else { |
| 133 | + Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact" |
| 134 | + } |
| 135 | + } |
| 136 | + else { |
| 137 | + Add-Content -Path "$($YmlPath)/toc.yml" -Value "- name: ${serviceName}`r`n href: ${fileName}.md" |
| 138 | + New-Item -Path $YmlPath -Name "${fileName}.md" -Force |
| 139 | + if ($displayName) { |
| 140 | + Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact`n##### ($displayName)" |
| 141 | + } |
| 142 | + else { |
| 143 | + Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact" |
| 144 | + } |
| 145 | + $visitedService[$serviceName] = $true |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + # Generate toc homepage. |
| 150 | + LogDebug "Creating Site Title and Navigation..." |
| 151 | + New-Item -Path "${DocOutDir}" -Name "toc.yml" -Force |
| 152 | + Add-Content -Path "${DocOutDir}/toc.yml" -Value "- name: Azure SDK for $lang APIs`r`n href: api/`r`n homepage: api/index.md" |
| 153 | + |
| 154 | + LogDebug "Copying root markdowns" |
| 155 | + Copy-Item "$($RepoRoot)/README.md" -Destination "${DocOutDir}/api/index.md" -Force |
| 156 | + Copy-Item "$($RepoRoot)/CONTRIBUTING.md" -Destination "${DocOutDir}/api/CONTRIBUTING.md" -Force |
| 157 | + |
| 158 | + LogDebug "Building site..." |
| 159 | + & $($DocFx) build "${DocOutDir}/docfx.json" |
| 160 | + # The line below is used for testing in local |
| 161 | + #docfx build "${DocOutDir}/docfx.json" |
| 162 | + Copy-Item "${DocGenDir}/assets/logo.svg" -Destination "${DocOutDir}/_site/" -Force |
| 163 | +} |
| 164 | + |
| 165 | +function Mutate-Files { |
| 166 | + Param ( |
| 167 | + [Parameter(Mandatory=$true)] [String]$appTitle, |
| 168 | + [Parameter(Mandatory=$true)] [String]$lang, |
| 169 | + [Parameter(Mandatory=$true)] [String]$indexhtmlloc, |
| 170 | + [Parameter(Mandatory=$false)] [String]$packageRegex = "`"`"", |
| 171 | + [Parameter(Mandatory=$false)] [String]$regexReplacement = "" |
| 172 | + ) |
| 173 | + # Update docfx.json |
| 174 | + $docfxContent = Get-Content -Path $DocfxJsonPath -Raw |
| 175 | + $docfxContent = $docfxContent -replace "`"_appTitle`": `"`"", "`"_appTitle`": `"$appTitle`"" |
| 176 | + $docfxContent = $docfxContent -replace "`"_appFooter`": `"`"", "`"_appFooter`": `"$appTitle`"" |
| 177 | + Set-Content -Path $DocfxJsonPath -Value $docfxContent |
| 178 | + # Update main.js var lang |
| 179 | + $mainJsContent = Get-Content -Path $MainJsPath -Raw |
| 180 | + $mainJsContent = $mainJsContent -replace "var SELECTED_LANGUAGE = ''", "var SELECTED_LANGUAGE = '$lang'" |
| 181 | + # Update main.js var index html |
| 182 | + $mainJsContent = $mainJsContent -replace "var INDEX_HTML = ''", "var INDEX_HTML = '$indexhtmlloc'" |
| 183 | + # Update main.js package regex and replacement |
| 184 | + $mainJsContent = $mainJsContent -replace "var PACKAGE_REGEX = ''", "var PACKAGE_REGEX = $packageRegex" |
| 185 | + $mainJsContent = $mainJsContent -replace "var PACKAGE_REPLACEMENT = ''", "var PACKAGE_REPLACEMENT = `"$regexReplacement`"" |
| 186 | + Set-Content -Path $MainJsPath -Value $mainJsContent -NoNewline |
| 187 | +} |
| 188 | + |
| 189 | +if ($GetGithubIoDocIndexFn -and (Test-Path "function:$GetGithubIoDocIndexFn")) |
| 190 | +{ |
| 191 | + &$GetGithubIoDocIndexFn |
| 192 | +} |
| 193 | +else |
| 194 | +{ |
| 195 | + LogWarning "The function '$GetGithubIoDocIndexFn' was not found." |
| 196 | +} |
0 commit comments