Created
April 13, 2017 18:11
-
-
Save tillig/d72f27f3af6abc97fd14a1eb69a1c775 to your computer and use it in GitHub Desktop.
Format XML configuration files on build with tidy
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
<?xml version="1.0" encoding="utf-8"?> | |
<Project DefaultTargets="All" InitialTargets="_SetVersion" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> | |
<Import Project="$(MSBuildProjectDirectory)\toolset\msbuildcommunitytasks\MSBuild.Community.Tasks.Targets"/> | |
<Target Name="_Tidy"> | |
<ItemGroup> | |
<ConfigFiles | |
Include="$(MSBuildProjectDirectory)\**\*.config" | |
Exclude="$(MSBuildProjectDirectory)\packages\**\*.*; | |
$(MSBuildProjectDirectory)\**\packages.config; | |
$(MSBuildProjectDirectory)\**\NuGet.config; | |
$(MSBuildProjectDirectory)\**\repositories.config; | |
$(MSBuildProjectDirectory)\.vs\**\*.*" /> | |
</ItemGroup> | |
<Message Text="Cleaning up configuration file formatting." /> | |
<!-- | |
* HTML Tidy cleans up the config file XML and places it in a temp location (filename.formatted) | |
* A post-formatting task cleans up key/value pair nodes to be flat and easier to read. | |
* The formatted config file is copied over the top of the original XML file and the temp file is deleted. | |
--> | |
<Exec Command="tidy.exe --input-xml yes --output-xml yes --preserve-entities yes --indent yes --indent-spaces 4 --input-encoding utf8 --indent-attributes yes --wrap 0 "%(ConfigFiles.FullPath)" > "%(ConfigFiles.FullPath).formatted"" WorkingDirectory="$(MSBuildProjectDirectory)\toolset\tidy" Condition="'%(ConfigFiles.FullPath)'!=''" /> | |
<FileUpdate Files="%(ConfigFiles.FullPath).formatted" Regex="<add\s+(key|name)="([^"]*)"\s+value="([^"]*)"\s+/>" ReplacementText="<add $1="$2" value="$3" />" Condition="'%(ConfigFiles.FullPath)'!=''" /> | |
<Copy DestinationFiles="%(ConfigFiles.FullPath)" SourceFiles="%(ConfigFiles.FullPath).formatted" Condition="'%(ConfigFiles.FullPath)'!=''" /> | |
<Delete Files="%(ConfigFiles.FullPath).formatted" Condition="'%(ConfigFiles.FullPath)'!=''" /> | |
</Target> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment