|
| 1 | +# When run from file with params or when locally debugging: |
| 2 | +param( |
| 3 | +### Latest api docs paths as deployed on dev/staging/release |
| 4 | +[string]$docsLatestRoot, |
| 5 | +### The new type doc that will be deployed. |
| 6 | +[string] $typeDocToDeploy, |
| 7 | +### The json file that contains all versions for dev/staging/release |
| 8 | +[string] $jsonFile, |
| 9 | +### The version tag. |
| 10 | +[string]$tag, |
| 11 | +### Whether this is the latest verisons, and should overwrite the latest folder. |
| 12 | +[bool]$isLatest |
| 13 | +) |
| 14 | + |
| 15 | +# When run as inline script in release pipe: |
| 16 | +# $docsLatestRoot = "$(stagingFilePath_reactAPI)"; |
| 17 | +# $typeDocToDeploy = "$(System.ArtifactsDirectory)/TypeDocOutput"; |
| 18 | +# $jsonFile = "react-api-docs-versions-prod.json"; |
| 19 | +# $tag = "$(pMajor).$(pMinor).$(pBuild)"; |
| 20 | +# $isLatest = $True; |
| 21 | + |
| 22 | +Write-Output "Tag is: " $tag |
| 23 | + |
| 24 | +$versionPath = $docsLatestRoot.Replace("typescript\latest", $tag + "\typescript"); |
| 25 | +$docsLatestTSFolder = [System.IO.Directory]::GetParent($docsLatestRoot); |
| 26 | +$docsLatestRootFolder = [System.IO.Directory]::GetParent($docsLatestTSFolder); |
| 27 | +Write-Output "version path is:" $versionPath; |
| 28 | + |
| 29 | +$filePath = $docsLatestRootFolder.FullName + "\" + $jsonFile; |
| 30 | + |
| 31 | +Write-Output "json file path is:" $filePath; |
| 32 | + |
| 33 | +#######Delete all other folders starting with <Major>.<Minor>######## |
| 34 | +#######Since we are going to keep the latest and greatest######## |
| 35 | +$lastDot = $tag.LastIndexOf('.'); |
| 36 | +$filter = $tag.Substring(0,$lastDot); |
| 37 | +$foldersToDel = Get-ChildItem -Path $docsLatestRootFolder -Directory -Filter $filter* |
| 38 | +Write-Output "Folders to delete: " $foldersToDel |
| 39 | + |
| 40 | +try { |
| 41 | +foreach($f in $foldersToDel){ |
| 42 | + if([System.IO.Directory]::Exists($f.FullName)) |
| 43 | + { Write-Output $f.FullName " to be deleted!" |
| 44 | + Remove-Item $f.FullName -Recurse -Force -Verbose } } |
| 45 | + |
| 46 | + Write-Output $f.FullName " deleted." |
| 47 | + } |
| 48 | +catch { Write-Output "Exception while deleting the old folders" } |
| 49 | + |
| 50 | +Write-Output "Starting copying files from: " $typeDocToDeploy "to: " $versionPath; |
| 51 | + |
| 52 | +New-Item -Path $versionPath -ItemType Directory -Force -Verbose |
| 53 | +Copy-Item -Path $typeDocToDeploy\* -Destination $versionPath -Recurse -Force -Verbose |
| 54 | + |
| 55 | +Write-Output "Files copied." |
| 56 | + |
| 57 | +###Add metatag inside index.html files' head section |
| 58 | +Write-Output "Add metatag inside index.html files' head section"; |
| 59 | +$indexFiles = Get-ChildItem -Path $versionPath -File -Recurse -Filter index.html |
| 60 | +Write-Output "Index files to update:" + $indexFiles.Length; |
| 61 | +foreach($indexFile in $indexFiles) { |
| 62 | + Write-Output "Updating file: " + $indexFile.FullName; |
| 63 | + $newText = [System.IO.File]::ReadAllText($indexFile.FullName).Replace("</head>", " <meta name=`"robots`" content=`"noindex,nofollow`"> |
| 64 | +</head>"); |
| 65 | + [System.IO.File]::WriteAllText($indexFile.FullName, $newText); |
| 66 | + Write-Output "File updated: " $indexFile.FullName; |
| 67 | +} |
| 68 | + |
| 69 | +############Update the typescript folders if isLatest flag is true########### |
| 70 | +Write-Output "Is latest version?: " $isLatest; |
| 71 | +if($isLatest) { |
| 72 | + Write-Output "Updating latest folder since this is latest version."; |
| 73 | + $typeScriptFolder = $versionPath; |
| 74 | + $typeScriptFolderToUpdate = $docsLatestTSFolder.FullName; |
| 75 | + Write-Output "[Latest] typeScriptFolderToUpdate is:" $typeScriptFolderToUpdate; |
| 76 | + |
| 77 | + #Delete the existing content as octopus option - delete all before deployment |
| 78 | + try { |
| 79 | + Remove-Item $typeScriptFolderToUpdate\* -Recurse -Force -Verbose } |
| 80 | + catch { Write-Output "Exception while deleting the $typeScriptFolderToUpdate" } |
| 81 | + Write-Output "Copy from " $typeScriptFolder "to: " $typeScriptFolderToUpdate; |
| 82 | + New-Item -Path $typeScriptFolderToUpdate\latest -ItemType Directory -Force -Verbose |
| 83 | + Copy-Item -Path $typeScriptFolder\* -Destination $typeScriptFolderToUpdate\latest -Recurse -Force -Verbose |
| 84 | + Write-Output "Copy Done."; |
| 85 | +} |
| 86 | + |
| 87 | +###########Json file content Update########### |
| 88 | +Write-Output "Update json file with new version." |
| 89 | +### If file does not exits - create it |
| 90 | +if (![System.IO.File]::Exists($filePath)) { |
| 91 | + Write-Output "File at: " $filePath "does not exist. Creating new file." |
| 92 | + New-Item -Path $filePath -ItemType File -Force -Verbose |
| 93 | +} |
| 94 | + |
| 95 | +Write-Output "Check file exists: " ([System.IO.File]::Exists($filePath)) |
| 96 | +Write-Output "Check dir exists:" ([System.IO.Directory]::Exists($docsLatestRootFolder)); |
| 97 | + |
| 98 | + |
| 99 | +if([System.IO.File]::Exists($filePath) -and [System.IO.Directory]::Exists($docsLatestRootFolder)) { |
| 100 | + $folders = Get-ChildItem -Path $docsLatestRootFolder -Directory -Exclude $tagFolder,"sass","typescript" -Name | Sort-Object @{Expression = {[double]($_.Substring(0, $_.LastIndexOf('.'))) }}; |
| 101 | + Write-Output "Folders found: " + $folders.Length; |
| 102 | + $textToUpdate = ""; |
| 103 | + foreach($item in $folders) { |
| 104 | + $textToUpdate += '"' + $item + '"'; |
| 105 | + } |
| 106 | + $textToUpdate = "[" + $textToUpdate.Replace("`"`"","`"`,`"") + "]" ; |
| 107 | + Write-Output "Text to update: " $textToUpdate; |
| 108 | + |
| 109 | + $content = [System.IO.File]::ReadAllText($filePath); |
| 110 | + if (!$content) { |
| 111 | + ### No Content Yet. Set initial structure. |
| 112 | + Write-Output "File has no previous content. Set initial structure." |
| 113 | + $content = '{"folders": []}'; |
| 114 | + } |
| 115 | + $newContent = $content -replace "\[.*\]", $textToUpdate; |
| 116 | + Write-Output "New file content is: " $newContent; |
| 117 | + [System.IO.File]::WriteAllText($filePath,$newContent); |
| 118 | +} |
0 commit comments