Skip to content

Instantly share code, notes, and snippets.

@johnmmoss
Created September 27, 2019 17:12
Show Gist options
  • Save johnmmoss/2aae62214aa8bd3d84b653c22c7c9a78 to your computer and use it in GitHub Desktop.
Save johnmmoss/2aae62214aa8bd3d84b653c22c7c9a78 to your computer and use it in GitHub Desktop.
Add-WebTransform -ProjectPath "WebApplication1.csproj" -Environment "Dev"
Add-WebTransform -ProjectPath "WebApplication1.csproj" -Environment "Live"
Add-WebTransform -ProjectPath "WebApplication1.csproj" -Environment "PreLive"
Add-WebTransform -ProjectPath "WebApplication1.csproj" -Environment "Staging"
function Add-WebTransform() {
Param(
$ProjectPath = "WebApplication1.csproj",
$Environment = "Dev"
)
# TODO Add some param guards
$webTransformFile = ".\Web.$Environment.config"
if ( -not (Test-Path $webTransformFile ))
{
$content =
'<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
</configuration>'
$content | out-file -FilePath $webTransformFile
}
$csprojFile = get-content -path $ProjectPath
if ($csprojFile -notmatch "^.*<Content.*Web\.Dev\.config.*$")
{
$csprojIncludeWebConfig = '<Content Include="Web.config" />'
$csprojIncludeTransform = "`t<Content Include=`"Web.$Environment.config`">`n`t`t<DependentUpon>Web.config</DependentUpon>`n`t</Content>"
$csprojFileUpdated = $csprojFile -replace $csprojIncludeWebConfig, "$csprojIncludeWebConfig`n $csprojIncludeTransform"
set-content -Path $ProjectPath -Value $csprojFileUpdated
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment