Skip to content

Commit d1b735e

Browse files
azure-sdksima-zhuweshaggard
authored
Sync eng/common directory with azure-sdk-tools for PR 1219 (Azure#17108)
* Move entire docgeneration into common tools * Move docindex to common * Added the package replacement logic * Fixed on parameters * Fixed param * Change function to dash * Added regex on function * Added display name. * Update eng/common/docgeneration/Generate-DocIndex.ps1 Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com> * Deal with js * Add no new line args * revert some test changes * Need to default to the double quotes for JS regex * Update Generate-DocIndex.ps1 * Added the appTitle * type Co-authored-by: Sima Zhu <sizhu@microsoft.com> Co-authored-by: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Co-authored-by: Wes Haggard <weshaggard@users.noreply.github.com>
1 parent d979d96 commit d1b735e

File tree

11 files changed

+1317
-0
lines changed

11 files changed

+1317
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
}
Lines changed: 76 additions & 0 deletions
Loading
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"metadata": [
3+
{
4+
"src": [
5+
{
6+
"files": [
7+
"src/**.csproj"
8+
]
9+
}
10+
],
11+
"dest": "api",
12+
"disableGitFeatures": false,
13+
"disableDefaultFilter": false
14+
}
15+
],
16+
"build": {
17+
"content": [
18+
{
19+
"files": [
20+
"api/**.yml",
21+
"api/**.md",
22+
"api/index.md"
23+
]
24+
},
25+
{
26+
"files": [
27+
"toc.yml",
28+
"*.md"
29+
]
30+
}
31+
],
32+
"resource": [
33+
{
34+
"files": [
35+
"images/**"
36+
]
37+
}
38+
],
39+
"overwrite": [
40+
{
41+
"files": [
42+
"apidoc/**.md"
43+
],
44+
"exclude": [
45+
"obj/**",
46+
"_site/**"
47+
]
48+
}
49+
],
50+
"dest": "_site",
51+
"globalMetadataFiles": [],
52+
"fileMetadataFiles": [],
53+
"template": [
54+
"default",
55+
"templates/matthews"
56+
],
57+
"postProcessors": [],
58+
"markdownEngineName": "markdig",
59+
"noLangKeyword": false,
60+
"keepFileLink": false,
61+
"cleanupCacheHistory": false,
62+
"disableGitFeatures": false,
63+
"globalMetadata": {
64+
"_appTitle": "",
65+
"_appFooter": "",
66+
"_enableSearch": false,
67+
"_enableNewTab": true,
68+
"_appFaviconPath": "https://c.s-microsoft.com/favicon.ico?v2",
69+
"_disableContribution": true
70+
}
71+
}
72+
}
73+
74+
75+
76+
77+
78+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{^_disableContribution}}
2+
<div class="contribution-panel mobile-hide">
3+
{{#docurl}}
4+
<a href="{{docurl}}" title="{{__global.improveThisDoc}}" class="fab btn-warning pull-right"><i class="glyphicon glyphicon-pencil"></i></a>
5+
{{/docurl}}
6+
{{#sourceurl}}
7+
<a href="{{sourceurl}}" title="{{__global.viewSource}}" class="fab btn-info pull-right"><i class="fa fa-code"></i></a>
8+
{{/sourceurl}}
9+
</div>
10+
{{/_disableContribution}}
11+
12+
<div class="hidden-sm col-md-2" role="complementary">
13+
<div class="sideaffix">
14+
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
15+
</nav>
16+
</div>
17+
</div>

0 commit comments

Comments
 (0)