|
| 1 | +function Get-java-OnboardedDocsMsPackages($DocRepoLocation) { |
| 2 | + $packageOnboardingFiles = "$DocRepoLocation/package.json" |
| 3 | + |
| 4 | + $onboardingSpec = ConvertFrom-Json (Get-Content $packageOnboardingFiles -Raw) |
| 5 | + $allPackages = @{} |
| 6 | + foreach ($spec in $onboardingSpec) { |
| 7 | + $spec.packages | ForEach-Object {$allPackages[$_.packageArtifactId] = $null} |
| 8 | + } |
| 9 | + return $allPackages |
| 10 | + } |
| 11 | + |
| 12 | +function Get-java-DocsMsTocData($packageMetadata, $docRepoLocation) { |
| 13 | + # Fallback to get package-level readme name if metadata file info does not exist |
| 14 | + $packageLevelReadmeName = $packageMetadata.Package.Replace('azure-', ''); |
| 15 | + |
| 16 | + # If there is a metadata json for the package use the DocsMsReadmeName from |
| 17 | + # the metadata function |
| 18 | + if ($packageMetadata.PSObject.Members.Name -contains "FileMetadata") { |
| 19 | + $readmeMetadata = &$GetDocsMsMetadataForPackageFn -PackageInfo $packageMetadata.FileMetadata |
| 20 | + $packageLevelReadmeName = $readmeMetadata.DocsMsReadMeName |
| 21 | + } |
| 22 | + |
| 23 | + $packageTocHeader = $packageMetadata.Package |
| 24 | + if ($packageMetadata.DisplayName) { |
| 25 | + $packageTocHeader = $packageMetadata.DisplayName |
| 26 | + } |
| 27 | + |
| 28 | + $children = @() |
| 29 | + # Children here combine namespaces in both preview and GA. |
| 30 | + if($package.VersionPreview) { |
| 31 | + $children += Get-Toc-Children -package $package.Package -groupId $package.GroupId -version $package.VersionPreview ` |
| 32 | + -docRepoLocation $docRepoLocation -folderName "preview" |
| 33 | + } |
| 34 | + if($package.VersionGA) { |
| 35 | + $children += Get-Toc-Children -package $package.Package -groupId $package.GroupId -version $package.VersionGA ` |
| 36 | + -docRepoLocation $docRepoLocation -folderName "latest" |
| 37 | + } |
| 38 | + if (!$children) { |
| 39 | + Write-Host "Did not find the package namespaces for $($packageMetadata.GroupId):$($packageMetadata.Package):$version" |
| 40 | + } |
| 41 | + $output = [PSCustomObject]@{ |
| 42 | + PackageLevelReadmeHref = "~/docs-ref-services/{moniker}/$packageLevelReadmeName-readme.md" |
| 43 | + PackageTocHeader = $packageTocHeader |
| 44 | + TocChildren = $children |
| 45 | + } |
| 46 | + return $output |
| 47 | +} |
| 48 | + |
| 49 | +function Get-java-DocsMsTocChildrenForManagementPackages($packageMetadata, $docRepoLocation) { |
| 50 | + $children = @() |
| 51 | + foreach ($package in $packageMetadata) { |
| 52 | + if($package.VersionPreview) { |
| 53 | + $children += Get-Toc-Children -package $package.Package -groupId $package.GroupId -version $package.VersionPreview ` |
| 54 | + -docRepoLocation $docRepoLocation -folderName "preview" |
| 55 | + } |
| 56 | + if($package.VersionGA) { |
| 57 | + $children += Get-Toc-Children -package $package.Package -groupId $package.GroupId -version $package.VersionGA ` |
| 58 | + -docRepoLocation $docRepoLocation -folderName "latest" |
| 59 | + } |
| 60 | + } |
| 61 | + # Children here combine namespaces in both preview and GA. |
| 62 | + return ($children | Sort-Object | Get-Unique) |
| 63 | +} |
| 64 | + |
| 65 | +# This is a helper function which fetch the java package namespaces from javadoc jar. |
| 66 | +# Here are the major workflow: |
| 67 | +# 1. Read the ${package}.txt under /metadata folder |
| 68 | +# 2. If file not found, then download javadoc jar from maven repository. |
| 69 | +# 3. If there is 'element-list' in javadoc jar, then copy to destination ${package}.txt. |
| 70 | +# 4. If no 'element-list', then parse the 'overview-frame.html' for the namespaces and copy to destination. |
| 71 | +# 5. If no 'overview-frame.html', then read folder 'com/azure/...' for namespaces. E.g some mgmt packages use this structure. |
| 72 | +# 6. Otherwise, return empty children. |
| 73 | +function Get-Toc-Children($package, $groupId, $version, $docRepoLocation, $folderName) { |
| 74 | + # Looking for the txt |
| 75 | + $filePath = Join-Path "$docRepoLocation/metadata/$folderName" "$package.txt" |
| 76 | + if (!(Test-Path $filePath)) { |
| 77 | + # Download from maven |
| 78 | + # javadoc jar url. e.g.: https://repo1.maven.org/maven2/com/azure/azure-core/1.25.0/azure-core-1.25.0-javadoc.jar |
| 79 | + $artifact = "${groupId}:${package}:${version}:javadoc" |
| 80 | + # A temp folder |
| 81 | + $tempDirectory = Join-Path ([System.IO.Path]::GetTempPath()) "javadoc" |
| 82 | + if (!(Test-Path $tempDirectory)) { |
| 83 | + New-Item $tempDirectory -ItemType Directory | Out-Null |
| 84 | + } |
| 85 | + try { |
| 86 | + Write-Host "mvn dependency:copy -Dartifact=$artifact -DoutputDirectory=$tempDirectory" |
| 87 | + $javadocLocation = "$tempDirectory/$package-$version-javadoc.jar" |
| 88 | + & 'mvn' dependency:copy -Dartifact="$artifact" -DoutputDirectory="$tempDirectory" | Out-Null |
| 89 | + Write-Host "Download complete." |
| 90 | + } |
| 91 | + catch { |
| 92 | + Write-Error "Not able to download javadoc jar from $artifact." |
| 93 | + return @() |
| 94 | + } |
| 95 | + Fetch-Namespaces-From-Javadoc -jarFilePath $javadocLocation -destination $filePath |
| 96 | + } |
| 97 | + |
| 98 | + if (!(Test-Path $filePath)) { |
| 99 | + # Log and warn |
| 100 | + Write-Host "Not able to find namespaces from javadoc jar $package-$version-javadoc.jar" |
| 101 | + } |
| 102 | + return (Get-Content $filePath | ForEach-Object {$_.Trim()}) |
| 103 | +} |
| 104 | + |
| 105 | +function Fetch-Namespaces-From-Javadoc ($jarFilePath, $destination) { |
| 106 | + $tempLocation = (Join-Path ([System.IO.Path]::GetTempPath()) "jarFiles") |
| 107 | + if (Test-Path $tempLocation) { |
| 108 | + Remove-Item $tempLocation/* -Recurse -Force |
| 109 | + } |
| 110 | + else { |
| 111 | + New-Item -ItemType Directory -Path $tempLocation -Force | Out-Null |
| 112 | + } |
| 113 | + |
| 114 | + Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 115 | + [System.IO.Compression.ZipFile]::ExtractToDirectory($jarFilePath, $tempLocation) |
| 116 | + if (Test-Path "$tempLocation/element-list") { |
| 117 | + # Rename and move to location |
| 118 | + Write-Host "Copying the element-list to $destination..." |
| 119 | + Copy-Item "$tempLocation/element-list" -Destination $destination |
| 120 | + } |
| 121 | + elseif (Test-Path "$tempLocation/overview-frame.html") { |
| 122 | + Parse-Overview-Frame -filePath "$tempLocation/overview-frame.html" -destination $destination |
| 123 | + } |
| 124 | + elseif (Test-Path "$tempLocation/com") { |
| 125 | + $originLocation = Get-Location |
| 126 | + try { |
| 127 | + Set-Location $tempLocation |
| 128 | + $allFolders = Get-ChildItem "$tempLocation/com" -Recurse -Directory | |
| 129 | + Where-Object {$_.GetFiles().Count -gt 0 -and $_.name -notmatch "class-use"} |
| 130 | + foreach ($path in $allFolders) { |
| 131 | + $path = (Resolve-Path $path -Relative) -replace "\./|\.\\" |
| 132 | + $path = $path -replace "\\|\/", "." |
| 133 | + Add-Content $destination -Value $path.Trim() |
| 134 | + } |
| 135 | + } |
| 136 | + finally { |
| 137 | + Set-Location $originLocation |
| 138 | + } |
| 139 | + } |
| 140 | + else { |
| 141 | + Write-Error "Can't find namespaces from javadoc jar jarFilePath." |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +function Parse-Overview-Frame ($filePath, $destination) { |
| 146 | + $htmlBody = Get-Content $filePath |
| 147 | + $packages = [RegEx]::Matches($htmlBody, "<li><a.*?>(?<package>.*?)<\/a><\/li>") |
| 148 | + |
| 149 | + $namespaces = $packages | ForEach-Object { $_.Groups["package"].Value } |
| 150 | + Add-Content -Path $destination -Value $namespaces |
| 151 | + Get-Content $destination |
| 152 | +} |
0 commit comments