Skip to content

Instantly share code, notes, and snippets.

@meehanman
Created May 14, 2019 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meehanman/7947b9076845c6c2dd606eb258bfbcec to your computer and use it in GitHub Desktop.
Save meehanman/7947b9076845c6c2dd606eb258bfbcec to your computer and use it in GitHub Desktop.
DevOps | Updates Configuration stored in a separate location to support the new "inline" configuration in newer versions of C# automatically
#Params
$projLoc = "C:\Repositories\trunk\Dev4.0\Direct\src\Web\Direct.Web.csproj"
$confLoc = "C:\Repositories\trunk\Dev4.0\Direct\src\Configuration"
#$projLoc = "%ApplicationProject%"
#$confLoc = "Configuration/%ApplicationName%"
#Load .csproj
$proj = [xml](get-content $projLoc)
#Where to save within the csproj
$itemGroup = $proj.Project.ItemGroup[$proj.Project.ItemGroup.Count-1]
#Get names of all configs
$configsToAdd = Get-ChildItem -Path "$confLoc" -Recurse -Forc
#Add all configs found
foreach($configFile in $configsToAdd)
{
#Split configFile into parts ie. ab.bc.DevOnline.config
$configFile = $($configFile.Name).Split(".")
#config
$configFileExtension = $($configFile[$configFile.count-1])
#DevOnline
$configFileEnvironment = $($configFile[$configFile.count-2])
#ab.bc
$configFileName = $($configFile[0..($configFile.count-3)]) -join "."
#ab.bc.DevOnline.config
$configFile = $configFile -join "."
<# Adds the following XML to a
<Content Include="web.DevOnline.config">
<DependentUpon>web.config</DependentUpon>
</Content>
#>
$Content = $proj.CreateElement("Content", $proj.DocumentElement.NamespaceURI)
$Content.SetAttribute("Include",$configFile)
$DependentUpon = $proj.CreateElement("DependentUpon", $proj.DocumentElement.NamespaceURI)
$DependentUpon.InnerText = "$configFileName.$configFileExtension"
$Content.AppendChild($DependentUpon) | Out-Null
$itemGroup.AppendChild($Content) | Out-Null
Write-Output "Added Configuration file: $configFile : $configFileName.$configFileExtension"
}
#Save and Write
Copy-Item "$($confLoc)\*" -Destination (Get-Item $projLoc).Directory.FullName
$proj.Save($projLoc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment