Last active
March 17, 2016 16:34
-
-
Save turowicz/579c77c020c860ebb299 to your computer and use it in GitHub Desktop.
Replace JSON variables based on another json
This file contains 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
Param( | |
[Parameter(Mandatory=$True)] | |
[string]$configFile, | |
[Parameter(Mandatory=$True)] | |
[string]$path | |
) | |
# read configuration file | |
$config = "" | |
foreach($file in Get-ChildItem $path -filter $configFile) | |
{ | |
$config = Get-Content $file.FullName | ConvertFrom-Json | |
} | |
if($config -ne "") | |
{ | |
Write-Host | |
Write-Host 'Configuration:' | |
Write-Host $config | |
Write-Host | |
foreach ($key in $config.psobject.properties.name) | |
{ | |
# for each entity | |
if($key -ne "`$schema") | |
{ | |
Write-Host 'Configuring: '$key | |
foreach($file in Get-ChildItem $path -filter "*.json") | |
{ | |
$target = Get-Content $file.FullName | ConvertFrom-Json | |
# find matching definition | |
if($target.name -eq $key) | |
{ | |
Write-Host | |
Write-Host "Transforming file " $file.Name | |
# replace values | |
foreach($property in $config.$key) | |
{ | |
Write-Host "Setting: " $property.name | |
Write-Host "Value: " $property.value | |
$expression = $property.name -replace "[`$]", "`$target" | |
Invoke-Expression ($expression + " = `"" + $property.value + "`"") | |
} | |
# write result to disk | |
$target | ConvertTo-Json -depth 999 | Out-File $file.FullName | |
Write-Host | |
} | |
} | |
} | |
} | |
} | |
else | |
{ | |
Write-Host "Configuration file not found: " $configFile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful for data factories