...

Text file src/github.com/Azure/azure-sdk-for-go/eng/common/docgeneration/Generate-DocIndex.ps1

Documentation: github.com/Azure/azure-sdk-for-go/eng/common/docgeneration

     1# Generates an index page for cataloging different versions of the Docs
     2[CmdletBinding()]
     3Param (
     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
    13# Given the github io blob storage url and language regex,
    14# the helper function will return a list of artifact names.
    15function Get-BlobStorage-Artifacts($blobStorageUrl, $blobDirectoryRegex, $blobArtifactsReplacement) {
    16    LogDebug "Reading artifact from storage blob ..."
    17    $returnedArtifacts = @()
    18    $pageToken = ""
    19    Do {
    20      $resp = ""
    21      if (!$pageToken) {
    22        # First page call.
    23        $resp = Invoke-RestMethod -Method Get -Uri $blobStorageUrl
    24      }
    25      else {
    26        # Next page call
    27        $blobStorageUrlPageToken = $blobStorageUrl + "&marker=$pageToken"
    28        $resp = Invoke-RestMethod -Method Get -Uri $blobStorageUrlPageToken
    29      }
    30      # Convert to xml documents.
    31      $xmlDoc = [xml](removeBomFromString $resp)
    32      foreach ($elem in $xmlDoc.EnumerationResults.Blobs.BlobPrefix) {
    33        # What service return like "dotnet/Azure.AI.Anomalydetector/", needs to fetch out "Azure.AI.Anomalydetector"
    34        $artifact = $elem.Name -replace $blobDirectoryRegex, $blobArtifactsReplacement
    35        $returnedArtifacts += $artifact
    36      }
    37      # Fetch page token
    38      $pageToken = $xmlDoc.EnumerationResults.NextMarker
    39    } while ($pageToken)
    40    return $returnedArtifacts
    41  }
    42
    43# The sequence of Bom bytes differs by different encoding.
    44# The helper function here is only to strip the utf-8 encoding system as it is used by blob storage list api.
    45# Return the original string if not in BOM utf-8 sequence.
    46function RemoveBomFromString([string]$bomAwareString) {
    47    if ($bomAwareString.length -le 3) {
    48        return $bomAwareString
    49    }
    50    $bomPatternByteArray = [byte[]] (0xef, 0xbb, 0xbf)
    51    # The default encoding for powershell is ISO-8859-1, so converting bytes with the encoding.
    52    $bomAwareBytes = [Text.Encoding]::GetEncoding(28591).GetBytes($bomAwareString.Substring(0, 3))
    53    if (@(Compare-Object $bomPatternByteArray $bomAwareBytes -SyncWindow 0).Length -eq 0) {
    54        return $bomAwareString.Substring(3)
    55    }
    56    return $bomAwareString
    57}
    58
    59function Get-TocMapping {
    60    Param (
    61        [Parameter(Mandatory = $true)] [Object[]] $metadata,
    62        [Parameter(Mandatory = $true)] [String[]] $artifacts
    63    )
    64    # Used for sorting the toc display order
    65    $orderServiceMapping = @{}
    66
    67    foreach ($artifact in $artifacts) {
    68        $packageInfo = $metadata | ? { $_.Package -eq $artifact -and $_.Hide -ne "true" }
    69      
    70        $serviceName = ""
    71        if (!$packageInfo) {
    72            LogDebug "There is no service name for artifact $artifact or it is marked as hidden. Please check csv of Azure/azure-sdk/_data/release/latest repo if this is intended. "
    73            continue
    74        }
    75        elseif (!$packageInfo[0].ServiceName) {
    76            LogWarning "There is no service name for artifact $artifact. Please check csv of Azure/azure-sdk/_data/release/latest repo if this is intended. "
    77            # If no service name retrieved, print out warning message, and put it into Other page.
    78            $serviceName = "Other"
    79        }
    80        else {
    81            if ($packageInfo.Length -gt 1) {
    82                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. "
    83            }
    84            $serviceName = $packageInfo[0].ServiceName.Trim()
    85        }
    86        
    87        # Define the order of "New", "Type", if not match, return the length of the array.
    88        $CustomOrder_New = "true", "false", ""
    89        $newIndex = $CustomOrder_New.IndexOf($packageInfo[0].New.ToLower())
    90        $newIndex = $newIndex -eq -1 ?  $CustomOrder_New.Count : $newIndex
    91        $CustomOrder_Type = "client", "mgmt", "compat", "spring", ""
    92        $typeIndex = $CustomOrder_Type.IndexOf($packageInfo[0].Type.ToLower())
    93        $typeIndex = $typeIndex -eq -1 ? $CustomOrder_Type.Count : $typeIndex
    94        $orderServiceMapping[$artifact] = [PSCustomObject][ordered]@{
    95            NewIndex = $newIndex
    96            TypeIndex = $typeIndex
    97            ServiceName = $serviceName
    98            DisplayName = $packageInfo[0].DisplayName.Trim()
    99            Artifact = $artifact
   100       }
   101    }
   102    return $orderServiceMapping
   103}
   104
   105function GenerateDocfxTocContent([Hashtable]$tocContent, [String]$lang, [String]$campaignId = "UA-62780441-46") {
   106    LogDebug "Start generating the docfx toc and build docfx site..."
   107
   108    LogDebug "Initializing Default DocFx Site..."
   109    & $($DocFx) init -q -o "${DocOutDir}"
   110    # The line below is used for testing in local
   111    #docfx init -q -o "${DocOutDir}"
   112    LogDebug "Copying template and configuration..."
   113    New-Item -Path "${DocOutDir}" -Name "templates" -ItemType "directory" -Force
   114    Copy-Item "${DocGenDir}/templates/*" -Destination "${DocOutDir}/templates" -Force -Recurse
   115
   116    $headerTemplateLocation = "${DocOutDir}/templates/matthews/partials/head.tmpl.partial"
   117
   118    if ($campaignId -and (Test-Path $headerTemplateLocation)){
   119        $headerTemplateContent = Get-Content -Path $headerTemplateLocation -Raw
   120        $headerTemplateContent = $headerTemplateContent -replace "GA_CAMPAIGN_ID", $campaignId
   121        Set-Content -Path $headerTemplateLocation -Value $headerTemplateContent -NoNewline
   122    }
   123
   124    Copy-Item "${DocGenDir}/docfx.json" -Destination "${DocOutDir}/" -Force
   125    $YmlPath = "${DocOutDir}/api"
   126    New-Item -Path $YmlPath -Name "toc.yml" -Force
   127    $visitedService = @{}
   128    # Sort and display toc service name by alphabetical order, and then sort artifact by order.
   129    $sortedToc = $tocContent.Values | Sort-Object ServiceName, NewIndex, TypeIndex, DisplayName, Artifact
   130    foreach ($serviceMapping in $sortedToc) {
   131        $artifact = $serviceMapping.Artifact
   132        $serviceName = $serviceMapping.ServiceName
   133        $displayName = $serviceMapping.DisplayName
   134
   135        # handle spaces in service name, EG "Confidential Ledger"
   136        # handle / in service name, EG "Database for MySQL/PostgreSQL". Leaving a "/" present will generate a bad link location.
   137        $fileName = ($serviceName -replace '\s', '').Replace("/","").ToLower().Trim()
   138        if ($visitedService.ContainsKey($serviceName)) {
   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        }
   146        else {
   147            Add-Content -Path "$($YmlPath)/toc.yml" -Value "- name: ${serviceName}`r`n  href: ${fileName}.md"
   148            New-Item -Path $YmlPath -Name "${fileName}.md" -Force
   149            if ($displayName) {
   150                Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact`n##### ($displayName)"
   151            }
   152            else {
   153                Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact"
   154            }
   155            $visitedService[$serviceName] = $true
   156        }
   157    }
   158
   159    # Generate toc homepage.
   160    LogDebug "Creating Site Title and Navigation..."
   161    New-Item -Path "${DocOutDir}" -Name "toc.yml" -Force
   162    Add-Content -Path "${DocOutDir}/toc.yml" -Value "- name: Azure SDK for $lang APIs`r`n  href: api/`r`n  homepage: api/index.md"
   163
   164    LogDebug "Copying root markdowns"
   165    Copy-Item "$($RepoRoot)/README.md" -Destination "${DocOutDir}/api/index.md" -Force
   166    Copy-Item "$($RepoRoot)/CONTRIBUTING.md" -Destination "${DocOutDir}/api/CONTRIBUTING.md" -Force
   167
   168    LogDebug "Building site..."
   169    & $($DocFx) build "${DocOutDir}/docfx.json"
   170    # The line below is used for testing in local
   171    #docfx build "${DocOutDir}/docfx.json"
   172    Copy-Item "${DocGenDir}/assets/logo.svg" -Destination "${DocOutDir}/_site/" -Force
   173}
   174
   175function UpdateDocIndexFiles {
   176    Param (
   177        [Parameter(Mandatory=$false)] [String]$appTitleLang = $Language,
   178        [Parameter(Mandatory=$false)] [String]$lang = $Language,
   179        [Parameter(Mandatory=$false)] [String]$packageRegex = "`"`"",
   180        [Parameter(Mandatory=$false)] [String]$regexReplacement = ""
   181    )
   182    # Update docfx.json
   183    $docfxContent = Get-Content -Path $DocfxJsonPath -Raw
   184    $docfxContent = $docfxContent -replace "`"_appTitle`": `"`"", "`"_appTitle`": `"Azure SDK for $appTitleLang`""
   185    $docfxContent = $docfxContent -replace "`"_appFooter`": `"`"", "`"_appFooter`": `"Azure SDK for $appTitleLang`""
   186    Set-Content -Path $DocfxJsonPath -Value $docfxContent -NoNewline
   187    # Update main.js var lang
   188    $mainJsContent = Get-Content -Path $MainJsPath -Raw
   189    $mainJsContent = $mainJsContent -replace "var SELECTED_LANGUAGE = ''", "var SELECTED_LANGUAGE = '$lang'"
   190    # Update main.js package regex and replacement
   191    $mainJsContent = $mainJsContent -replace "var PACKAGE_REGEX = ''", "var PACKAGE_REGEX = $packageRegex"
   192    $mainJsContent = $mainJsContent -replace "var PACKAGE_REPLACEMENT = ''", "var PACKAGE_REPLACEMENT = `"$regexReplacement`""
   193
   194    Set-Content -Path $MainJsPath -Value $mainJsContent -NoNewline
   195}
   196
   197if ($GetGithubIoDocIndexFn -and (Test-Path "function:$GetGithubIoDocIndexFn"))
   198{
   199    &$GetGithubIoDocIndexFn
   200}
   201else
   202{
   203    LogWarning "The function for 'GetGithubIoDocIndexFn' was not found.`
   204    Make sure it is present in eng/scripts/Language-Settings.ps1 and referenced in eng/common/scripts/common.ps1.`
   205    See https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/common_engsys.md#code-structure"
   206}

View as plain text