Skip to content

Instantly share code, notes, and snippets.

@milannankov
Last active November 7, 2016 09:08
Show Gist options
  • Save milannankov/e22a79dbb3ea24fbcb8dafd121649ec4 to your computer and use it in GitHub Desktop.
Save milannankov/e22a79dbb3ea24fbcb8dafd121649ec4 to your computer and use it in GitHub Desktop.
XML Transforms with Msbuild
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- Plug into the Publish pipeline -->
<OnAfterPipelineCollectFilesPhase>
XmlTransform;
$(OnAfterPipelineCollectFilesPhase);
</OnAfterPipelineCollectFilesPhase>
<XmlTransformDependsOn>
XMLTransformInit;
XMLTransformPrepareTemp;
XMLTransformExecute;
XMLTransformAddForPackaging;
</XmlTransformDependsOn>
<XmlTransformTempPath>$(IntermediateOutputPath)XmlTransform\</XmlTransformTempPath>
</PropertyGroup>
<ItemGroup>
<!-- Enable Build Action "XmlTransform" in Visual Studio -->
<AvailableItemName Include="XmlTransform"/>
</ItemGroup>
<Target Name="XmlTransform" DependsOnTargets="$(XmlTransformDependsOn)">
</Target>
<Target Name="XMLTransformInit">
<Message Text="Preparing files for XMLTransform: @(XmlTransform)" Importance="high"/>
<ItemGroup>
<!-- Generate paths to transform files and output files -->
<XmlTransformInfo Include="@(XmlTransform)">
<TransformFilePath>%(RelativeDir)%(Filename).$(Configuration)%(Extension)</TransformFilePath>
<TempFileDir>$(XmlTransformTempPath)transformed\%(RelativeDir)</TempFileDir>
<TempFilePath>$(XmlTransformTempPath)transformed\%(RelativeDir)%(Filename)%(Extension)</TempFilePath>
<RelativePath>%(RelativeDir)%(Filename)%(Extension)</RelativePath>
</XmlTransformInfo>
</ItemGroup>
<!-- Generate error if any of the transform files for the current configuration is missing -->
<Error Text="XML transformation file %(XmlTransformInfo.TransformFilePath) does not exist."
Condition="!Exists('%(XmlTransformInfo.TransformFilePath)')" />
</Target>
<Target Name="XMLTransformPrepareTemp">
<PropertyGroup>
<PrepareXMLTransformsOutputDirectories>@(_WebConfigsToTransformOuputs->'%(RootDir)%(Directory)')</PrepareXMLTransformsOutputDirectories>
</PropertyGroup>
<ItemGroup>
<_WebConfigsToTransformOuputsDirectories Include="$(_WebConfigsToTransformOuputsDirectories)" />
</ItemGroup>
<RemoveDir Directories="$(XmlTransformTempPath)"/>
<MakeDir Directories="%(XmlTransformInfo.TempFileDir)"/>
</Target>
<Target Name="XMLTransformExecute">
<!-- Perform actual transform -->
<TransformXml
Source="@(XmlTransformInfo)"
Transform="%(TransformFilePath)"
Destination="%(TempFilePath)" />
</Target>
<Target Name="XMLTransformAddForPackaging">
<ItemGroup>
<TransformedFilesToInclude Include="%(XmlTransformInfo.TempFilePath)">
<DestinationRelativePath>%(XmlTransformInfo.RelativePath)</DestinationRelativePath>
</TransformedFilesToInclude>
<FilesForPackagingFromProject Include="@(TransformedFilesToInclude->'%(FullPath)')" KeepMetadata="DestinationRelativePath"/>
</ItemGroup>
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment