Created
March 20, 2020 00:12
-
-
Save scbedd/ead142ad268cd9573f3b9b4b1a387fd8 to your computer and use it in GitHub Desktop.
Updates js files in place so that the previously deployed sites can access the new versioning strategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# transition old style versioning strategy to new. inclusive of existing versions in both strategies | |
param ( | |
$AzCopy, | |
$SASKey, | |
$Language, | |
$BlobName, | |
$DocLocation, | |
$AccountKey | |
) | |
$AccountName = ($BlobName.Replace("https://", "") -Split "\.")[0] | |
$DocDest = "$($BlobName)/`$web/$($Language)" | |
$replacementTemplates = @{ | |
"javascript" = "assets/js/get_options.js"; | |
"dotnet" = "styles/main.js"; | |
"c" = "menu.js"; | |
"python" = "_static/js/get_options.js"; | |
} | |
function Get-Target-Uri | |
{ | |
Param ( | |
[Parameter(Mandatory=$true)] [String]$PkgName, | |
[Parameter(Mandatory=$true)] [String]$Version | |
) | |
$replacementTemplate = $replacementTemplates[$Language] | |
return "$BlobName/`$web/$Language/$PkgName/$Version/$replacementTemplate$SASKey" | |
} | |
function Get-Existing-Versions | |
{ | |
Param ( | |
[Parameter(Mandatory=$true)] [String]$PkgName | |
) | |
$versionUri = "$($BlobName)/`$web/$($Language)/$($PkgName)/versioning/versions" | |
Write-Host "Heading to $versionUri to retrieve known versions" | |
try { | |
return ((Invoke-RestMethod -Uri $versionUri -MaximumRetryCount 3 -RetryIntervalSec 5) -Split "\n" | % {$_.Trim()} | ? { return $_ }) | |
} | |
catch { | |
# Handle 404. If it's 404, this is the first time we've published this package. | |
if ($_.Exception.Response.StatusCode.value__ -eq 404){ | |
Write-Host "Version file does not exist. This is the first time we have published this package." | |
} | |
else { | |
# If it's not a 404. exit. We don't know what's gone wrong. | |
Write-Host "Exception getting version file. Aborting" | |
Write-Host $_ | |
exit(1) | |
} | |
} | |
} | |
if (!(Test-Path "$Language.json")) | |
{ | |
Write-Host "No cached listing" | |
az storage blob list --account-name $AccountName --account-key $AccountKey --container-name "`$web" --prefix "$Language/" --timeout 60 --num-results * --delimiter '/' --query '[].name | [?ends_with(@, `/`)]' | Out-File "$Language.json" | |
} | |
# write it to a json, get ready to partay! | |
$data = Get-Content "$Language.json" | ConvertFrom-Json | |
# at this point we have stuff of the form <language>/<packagename>/<other directory levels we don't care bout> | |
# this means that the first element of a split will ALWAYS be the package name | |
$pkgs = $data | % { ($_ -split "/")[1] } | Select-Object -Unique | |
foreach ($pkgName in $pkgs){ | |
Write-Host "Working on $pkgName" | |
$versions = Get-Existing-Versions $pkgName | |
foreach($version in $versions) | |
{ | |
$fileName = Get-Target-Uri -PkgName $pkgName -Version $version | |
# Write-Host "Replacing js in url $fileName" | |
# Write-Host "$($DocLocation)/$Language/get_options.js" | |
Write-Host "Replacing JS for $version" | |
& $($AzCopy) cp "$($DocLocation)/$Language/get_options.js" $fileName | |
if($lastExitCode) | |
{ | |
Write-Host "Error Detected running $pkgName!" | |
exit(1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment