Skip to content

Instantly share code, notes, and snippets.

@RhysC
Created May 5, 2011 10:47
Show Gist options
  • Save RhysC/956857 to your computer and use it in GitHub Desktop.
Save RhysC/956857 to your computer and use it in GitHub Desktop.
handles swapping out templated values in configs from nested prop files
#config swapper
#load the default values in to a hash
#get the values from the env and override in the hash
#foreach key in the hash replace the {[key]} with the value i.e. (Get-Content $_) -replace "$tokenedKey","$value" | Set-Content -path $_.$env
function Create-ConfigFromTemplate{
param ([string]$envPropFile, #".\$env.properties"
[string]$outputFolder,#".\$env\
[string]$templateFile #".\App.template.config"
)
$outputFile = Join-Path $outputFolder (split-path ($templateFile.Replace(".template", "")) -leaf)
if((Test-Path $outputFolder) -eq $false) {
$newDir = New-Item $outputFolder -type directory
Write-Host "new directory created for output - " $newDir.FullName
}
$hash = @{}
$envXmlData = [xml](Get-Content $envPropFile)
$basePropFile = $envXmlData.Configuration.GetAttribute("Inherits")
if($basePropFile){
$baseXmlData = [xml](Get-Content (join-path `
(Split-Path $envPropFile -Parent) $basePropFile))
#Put the base values in a hashtable
$baseXmlData.Configuration.Properties.Property |
%{
$hash[$_.Key] = $_.Value
}
}
#Add or override the more specific values from the environment config
$envXmlData.Configuration.Properties.Property |
%{
$hash[$_.Key] = $_.Value
}
#create the ouput file ready to have the tokens swapped out
Copy-Item $templateFile $outputFile;
$hash.GetEnumerator() |
%{
$key = $_.Key
$value = $_.Value
$tokenedKey = "\`$\{$key\}" #powershell escape is ` regex escape is \
#continuously replace the values and rewrite to the ouput file - this is standrd replace technique in PS
(Get-Content $outputFile) -replace "$tokenedKey" , "$value" |
Set-Content -path $outputFile
}
#Check for any missed tokens
$missedtokens = Select-String -Pattern \$\`{ -path $outputFile
if($missedtokens -ne $null)
{
Write-Warning "There are tokens that have not been swapped out:"
$missedtokens | %{
Write-Warning $_
}
}
}
function Create-AllConfigFromTemplateFolder
{
param ( [string]$configPath, #"C:\Configurations"
[string]$templateFile,#"C:\projectA\App.template.config"
[string]$ouputRootLocation #"."
)
Get-ChildItem -Path $configPath -Filter *.properties |
%{ $_.BaseName } |
%{
$propFile = join-path $configPath "$_.properties"
Create-ConfigFromTemplate -envPropFile $propFile `
-outputFolder "$ouputRootLocation\$_\" `
-templateFile $templateFile `
}
}
########### example files #####
##App.template.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Abc" value="${abc}"/>
<add key="def" value="${def}"/>
<add key="def" value="${ghi}"/>
</appSettings>
</configuration>
#dev.prop
<?xml version="1.0" encoding="utf-16"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Inherits=".\base.prop">
<Properties>
<Property>
<Key>abc</Key>
<Value>My overriding value</Value>
</Property>
<Property>
<Key>def</Key>
<Value>another overriding value</Value>
</Property>
</Properties>
</Configuration>
#base.prop
<?xml version="1.0" encoding="utf-16"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Inherits=".\base.prop">
<Properties>
<Property>
<Key>abc</Key>
<Value>My base value</Value>
</Property>
<Property>
<Key>def</Key>
<Value>another base value</Value>
</Property>
<Property>
<Key>ghi</Key>
<Value>base value that is not overriden</Value>
</Property>
</Properties>
</Configuration>
@RhysC
Copy link
Author

RhysC commented May 5, 2011

some white spacing issues may arise from entering this in via the web interface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment