File size: 8,883 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Generates an index page for cataloging different versions of the Docs
[CmdletBinding()]
Param (
    $DocFx,
    $RepoRoot,
    $DocGenDir,
    $DocOutDir = "${RepoRoot}/docfx_project",
    $DocfxJsonPath = "${PSScriptRoot}\docfx.json",
    $MainJsPath = "${PSScriptRoot}\templates\matthews\styles\main.js"
)
. "${PSScriptRoot}\..\scripts\common.ps1"

# Fetch a list of "artifacts" from blob storage corresponding to the given
# language (-storagePrefix). Remove the prefix from the path names to arrive at
# an "artifact" name.
function Get-BlobStorage-Artifacts(
  $blobDirectoryRegex,
  $blobArtifactsReplacement,
  $storageAccountName,
  $storageContainerName,
  $storagePrefix
) {
    LogDebug "Reading artifact from storage blob ..."
    # "--only-show-errors" suppresses warnings about the fact that the az CLI is not authenticated
    # "--query '[].name'" returns a list of only blob names
    # "--num-results *" handles pagination so the caller does not have to
    $artifacts = az storage blob list `
        --account-name $storageAccountName `
        --container-name $storageContainerName `
        --prefix $storagePrefix `
        --delimiter / `
        --only-show-errors `
        --query '[].name' `
        --num-results * | ConvertFrom-Json
    LogDebug "Number of artifacts found: $($artifacts.Length)"

    # example: "python/azure-storage-blob" -> "azure-storage-blob"
    $artifacts = $artifacts.ForEach({ $_ -replace $blobDirectoryRegex, $blobArtifactsReplacement })
    return $artifacts
}

function Get-TocMapping {
    Param (
        [Parameter(Mandatory = $true)] [Object[]] $metadata,
        [Parameter(Mandatory = $true)] [String[]] $artifacts
    )
    # Used for sorting the toc display order
    $orderServiceMapping = @{}

    foreach ($artifact in $artifacts) {
        $packageInfo = $metadata | ? { $_.Package -eq $artifact -and $_.Hide -ne "true" }
      
        $serviceName = ""
        if (!$packageInfo) {
            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. "
            continue
        }
        elseif (!$packageInfo[0].ServiceName) {
            LogWarning "There is no service name for artifact $artifact. Please check csv of Azure/azure-sdk/_data/release/latest repo if this is intended. "
            # If no service name retrieved, print out warning message, and put it into Other page.
            $serviceName = "Other"
        }
        else {
            if ($packageInfo.Length -gt 1) {
                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. "
            }
            $serviceName = $packageInfo[0].ServiceName.Trim()
        }
        
        # Define the order of "New", "Type", if not match, return the length of the array.
        $CustomOrder_New = "true", "false", ""
        $newIndex = $CustomOrder_New.IndexOf($packageInfo[0].New.ToLower())
        $newIndex = $newIndex -eq -1 ?  $CustomOrder_New.Count : $newIndex
        $CustomOrder_Type = "client", "mgmt", "compat", "spring", ""
        $typeIndex = $CustomOrder_Type.IndexOf($packageInfo[0].Type.ToLower())
        $typeIndex = $typeIndex -eq -1 ? $CustomOrder_Type.Count : $typeIndex
        $orderServiceMapping[$artifact] = [PSCustomObject][ordered]@{
            NewIndex = $newIndex
            TypeIndex = $typeIndex
            ServiceName = $serviceName
            DisplayName = $packageInfo[0].DisplayName.Trim()
            Artifact = $artifact
       }
    }
    return $orderServiceMapping
}

function GenerateDocfxTocContent([Hashtable]$tocContent, [String]$lang, [String]$campaignId = "UA-62780441-46") {
    LogDebug "Start generating the docfx toc and build docfx site..."

    LogDebug "Initializing Default DocFx Site..."
    & $($DocFx) init -q -o "${DocOutDir}"
    # The line below is used for testing in local
    #docfx init -q -o "${DocOutDir}"
    LogDebug "Copying template and configuration..."
    New-Item -Path "${DocOutDir}" -Name "templates" -ItemType "directory" -Force
    Copy-Item "${DocGenDir}/templates/*" -Destination "${DocOutDir}/templates" -Force -Recurse

    $headerTemplateLocation = "${DocOutDir}/templates/matthews/partials/head.tmpl.partial"

    if ($campaignId -and (Test-Path $headerTemplateLocation)){
        $headerTemplateContent = Get-Content -Path $headerTemplateLocation -Raw
        $headerTemplateContent = $headerTemplateContent -replace "GA_CAMPAIGN_ID", $campaignId
        Set-Content -Path $headerTemplateLocation -Value $headerTemplateContent -NoNewline
    }

    Copy-Item "${DocGenDir}/docfx.json" -Destination "${DocOutDir}/" -Force
    $YmlPath = "${DocOutDir}/api"
    New-Item -Path $YmlPath -Name "toc.yml" -Force
    $visitedService = @{}
    # Sort and display toc service name by alphabetical order, and then sort artifact by order.
    $sortedToc = $tocContent.Values | Sort-Object ServiceName, NewIndex, TypeIndex, DisplayName, Artifact
    foreach ($serviceMapping in $sortedToc) {
        $artifact = $serviceMapping.Artifact
        $serviceName = $serviceMapping.ServiceName
        $displayName = $serviceMapping.DisplayName

        # handle spaces in service name, EG "Confidential Ledger"
        # handle / in service name, EG "Database for MySQL/PostgreSQL". Leaving a "/" present will generate a bad link location.
        $fileName = ($serviceName -replace '\s', '').Replace("/","").ToLower().Trim()
        if ($visitedService.ContainsKey($serviceName)) {
            if ($displayName) {
                Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact`n##### ($displayName)"
            }
            else {
                Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact"
            }
        }
        else {
            Add-Content -Path "$($YmlPath)/toc.yml" -Value "- name: ${serviceName}`r`n  href: ${fileName}.md"
            New-Item -Path $YmlPath -Name "${fileName}.md" -Force
            if ($displayName) {
                Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact`n##### ($displayName)"
            }
            else {
                Add-Content -Path "$($YmlPath)/${fileName}.md" -Value "#### $artifact"
            }
            $visitedService[$serviceName] = $true
        }
    }

    # Generate toc homepage.
    LogDebug "Creating Site Title and Navigation..."
    New-Item -Path "${DocOutDir}" -Name "toc.yml" -Force
    Add-Content -Path "${DocOutDir}/toc.yml" -Value "- name: Azure SDK for $lang APIs`r`n  href: api/`r`n  homepage: api/index.md"

    LogDebug "Copying root markdowns"
    Copy-Item "$($RepoRoot)/README.md" -Destination "${DocOutDir}/api/index.md" -Force
    Copy-Item "$($RepoRoot)/CONTRIBUTING.md" -Destination "${DocOutDir}/api/CONTRIBUTING.md" -Force

    LogDebug "Building site..."
    & $($DocFx) build "${DocOutDir}/docfx.json"
    # The line below is used for testing in local
    #docfx build "${DocOutDir}/docfx.json"
    Copy-Item "${DocGenDir}/assets/logo.svg" -Destination "${DocOutDir}/_site/" -Force
}

function UpdateDocIndexFiles {
    Param (
        [Parameter(Mandatory=$false)] [String]$appTitleLang = $Language,
        [Parameter(Mandatory=$false)] [String]$lang = $Language,
        [Parameter(Mandatory=$false)] [String]$packageRegex = "`"`"",
        [Parameter(Mandatory=$false)] [String]$regexReplacement = ""
    )
    # Update docfx.json
    $docfxContent = Get-Content -Path $DocfxJsonPath -Raw
    $docfxContent = $docfxContent -replace "`"_appTitle`": `"`"", "`"_appTitle`": `"Azure SDK for $appTitleLang`""
    $docfxContent = $docfxContent -replace "`"_appFooter`": `"`"", "`"_appFooter`": `"Azure SDK for $appTitleLang`""
    Set-Content -Path $DocfxJsonPath -Value $docfxContent -NoNewline
    # Update main.js var lang
    $mainJsContent = Get-Content -Path $MainJsPath -Raw
    $mainJsContent = $mainJsContent -replace "var SELECTED_LANGUAGE = ''", "var SELECTED_LANGUAGE = '$lang'"
    # Update main.js package regex and replacement
    $mainJsContent = $mainJsContent -replace "var PACKAGE_REGEX = ''", "var PACKAGE_REGEX = $packageRegex"
    $mainJsContent = $mainJsContent -replace "var PACKAGE_REPLACEMENT = ''", "var PACKAGE_REPLACEMENT = `"$regexReplacement`""

    Set-Content -Path $MainJsPath -Value $mainJsContent -NoNewline
}

if ($GetGithubIoDocIndexFn -and (Test-Path "function:$GetGithubIoDocIndexFn"))
{
    &$GetGithubIoDocIndexFn
}
else
{
    LogWarning "The function for 'GetGithubIoDocIndexFn' was not found.`
    Make sure it is present in eng/scripts/Language-Settings.ps1 and referenced in eng/common/scripts/common.ps1.`
    See https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/common_engsys.md#code-structure"
}