Instantly share code, notes, and snippets.

Embed
What would you like to do?
function transform($xml, $xdt, $output)
{
Add-Type -LiteralPath "Microsoft.Web.XmlTransform.dll"
$xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
$xmldoc.PreserveWhitespace = $true
$xmldoc.Load($xml);
$transform = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt);
if ($transform.Apply($xmldoc) -eq $false)
{
throw "Transformation failed."
}
$xmldoc.Save($output)
}
function substitute($line, $octopusValues)
{
$regex = [regex] "(#\{\b[a-zA-Z0-9-_.]+\})"
$groups = $regex.Matches($line)
if ($groups.Count -eq 0)
{
return $line
}
foreach($group in $groups)
{
$octVariable = $group.Value.Trim("#{").Trim("}")
write-host "[DEBUG] group.Value:" $octVariable
Try
{
$token = $octopusValues | select -ExpandProperty "$octVariable" -ErrorAction Stop
$token = substitute $token $octopusValues
$line = $line.Replace($group.Value, $token)
}
Catch
{
Write-Host "[WARNING] Could not find value of $octVariable"
Break
}
}
return $line
}
function Transform-OctopusConfig($json, $xml, $xdt, $output)
{
if (!$json -or !(Test-Path -path $json -PathType Leaf)) {
throw "File not found. $json";
}
if (!$xml -or !(Test-Path -path $xml -PathType Leaf)) {
throw "File not found. $xml";
}
if (!$xdt -or !(Test-Path -path $xdt -PathType Leaf)) {
throw "File not found. $xdt";
}
$outputTemp = $output + ".tmp"
transform $xml $xdt $outputTemp
$octopusValues = (Get-Content $json | ConvertFrom-Json)
$lines = Get-Content $outputTemp
foreach($line in $lines)
{
$line = substitute $line $octopusValues
$line | Out-File -filePath $output -Append
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment