Skip to content

Instantly share code, notes, and snippets.

@nshenoy
Created June 10, 2016 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nshenoy/79ac16c84edcc65485dff263d1e2781f to your computer and use it in GitHub Desktop.
Save nshenoy/79ac16c84edcc65485dff263d1e2781f to your computer and use it in GitHub Desktop.
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