Last active
November 6, 2017 06:52
-
-
Save EtherZa/08fd5f67d03135e19d3b5785bab4cfcc to your computer and use it in GitHub Desktop.
Replace tokens in config transformations. Used for prepping branched deployments.
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
# Submit with a single parameter of a comma delimted list of key=value pairs. | |
# Caveat: no escaping is performed, so the script will go awry if the value contains a ',' and the script is not modified | |
param ( | |
[parameter(Mandatory=$true)] [string]$raw | |
) | |
$filter = '*.setParameters.xml' | |
$lookup = ConvertFrom-StringData ($raw.split(",") | out-string) | |
try { | |
$working = (Get-Location) | |
Write-Host Working: $working | |
Write-Host Filter: $filter | |
Write-Host ($lookup | Out-String) | |
$filesUpdated = 0 | |
foreach($fileName in [System.IO.Directory]::GetFiles($working, $filter, "AllDirectories")) | |
{ | |
$file = New-Object System.IO.FileInfo ($fileName) | |
$global:updated = 0 | |
$regex = [regex]($lookup.Keys -join '|') | |
$callback = { | |
$global:updated++ | |
$lookup[$args[0].Value] | |
} | |
$content = [System.IO.File]::ReadAllText($file.FullName) | |
$content = $regex.Replace($content, $callback) | |
if ($global:updated -gt 0) | |
{ | |
$filesUpdated++ | |
Write-Host Processed: $file.Name [$global:updated tokens found] | |
Set-Content -Path $file.FullName -Value $content | |
} | |
} | |
if ($filesUpdated -eq 0) | |
{ | |
Write-Host No tokens were found. | |
} | |
else | |
{ | |
Write-Host Done. | |
} | |
} Catch { | |
Write-Host $("##teamcity[message text='{0}' status='ERROR']" -f $_.Exception.Message.Replace("]", "|]")) | |
[System.Environment]::Exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment