Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

25 Aug 2010

Making the TemplateFileTask easier to use...

One of the disadvantages of the TemplateFile task (msbuildtasks) is the fact that it requires a lot of typing to define template values

<ItemGroup Condition= " '$(ConfigurationEnvironment)'=='build' ">
	<Tokens Include="a">
	<Tokens Include="b">

Here is a format proposition to make this a lot more finger friendly

	<variables env="build">
		<x name="a">localhost</x>
		<x name="b><mynode/></x>

Here is the msbuild script we need to achieve that


<!-- Retreive all template values for the specific environment -->
<XmlQuery XmlFileName="$(ConfigurationFile)" XPath = "//variables[@env='$(ConfigurationEnvironment)']/*">
	<Output TaskParameter="Values" ItemName="Values" />

<-- Construct @Tokens -->
	<Tokens Include="%(Values.name)">

<!-- Generate the configuration files -->
<Message Text="Available variables:" />
<Message Text="====================" />
<Message Text="%(Tokens.Identity): %(Tokens.ReplacementValue)" />

Happy coding!