How to restart property evaluation in the target MSBuild environment?

I have a custom target MSBuild, a partial snippet as follows.

<Target Name="PublishHtm"> <PropertyGroup> <PublishHtmTemplateContents>$([System.IO.File]::ReadAllText($(PublishHtmTemplatePath)))</PublishHtmTemplateContents> <PublishHtm>$(PublishHtmTemplateContents)</PublishHtm> </PropertyGroup> <WriteLinesToFile Lines="$(PublishHtm)" File="$(PublishDir)\publish.htm" Overwrite="true"/> </Target> 

This is an attempt to reuse this solution in that I am trying to isolate this template in an external file. The template contains links to MSBuild properties, such as $(ApplicationName) . Performing this exactly as described in the related solution , it works fine, but when loading a template as a string, none of these property expressions are evaluated by the time it gets to the file.

 <SPAN CLASS="BannerTextApplication">$(ApplicationName)</SPAN> 

Is there an MSBuild expression / function that I can use to get the string reevaluated based on the context in which Target is called?

BTW I would rather not get around the problem using find / replace or regex replace, and stick with the MSBuild expression mechanism.

Using Visual Studio 2012 and the .NET Framework 4.5.

+2
source share
1 answer

Sorry for not returning to this question for a while. Initially, I thought that to solve this problem we would need to bend MSBuild in a very unusual way (the plan for today was to write a complex built-in task that would do regex-replace in an external file using the Msbuild properties as tokens). But I think this can be solved more easily using the CDATA section, which is valid in the property definition:

Here is the main script:

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <MyOtherProperty>$([System.DateTime]::Now)</MyOtherProperty> <Version>1.0.1b</Version> <ProjectName>MSBuild Rox</ProjectName> <Author>Alexey Shcherbak</Author> </PropertyGroup> <Target Name="Build"> <ItemGroup> <PropsToPass Include="MyOtherProperty=$(MyOtherProperty)" /> <PropsToPass Include="Version=$(Version)" /> <PropsToPass Include="ProjectName=$(ProjectName)" /> <PropsToPass Include="Author=$(Author)" /> </ItemGroup> <MSBuild Projects="TransformHTML.Template.proj" Properties="@(PropsToPass)" /> </Target> </Project> 

And here is your template. This is not pure html, it is still an msbuild file, but at least without ugly encoding for html tags in xml. This is just a block in CDATA

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Transform"> <PropertyGroup> <HtmlProperty><![CDATA[ <body> <div>$(MyOtherProperty)</div> <div>$(Version)</div> <div>$(ProjectName)</div> <div>$(Author)</div> </body> ]]></HtmlProperty> </PropertyGroup> <Target Name="Transform"> <Message Text="HtmlProperty: $(HtmlProperty)" Importance="High" /> </Target> </Project> 

It may not be very elegant (I personally do not like the section with @PropsToPass), but it will do the job. You can put all the built-in in a single file, and then you do not need to pass properties to the MSBuild task. I do not like the massive html encoding from the proposed "this solution", but I would prefer to save the HTML template in the same script where it will be converted, only in a good html format, without encoding.

Single file example:

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <MyOtherProperty>$([System.DateTime]::Now)</MyOtherProperty> <Version>1.0.1b</Version> <ProjectName>MSBuild Rox</ProjectName> <Author>Alexey Shcherbak</Author> </PropertyGroup> <Target Name="Build"> <PropertyGroup> <HtmlProperty><![CDATA[ <body> <div>$(MyOtherProperty)</div> <div>$(Version)</div> <div>$(ProjectName)</div> <div>$(Author)</div> </body> ]]></HtmlProperty> </PropertyGroup> <Message Text="HtmlProperty: $(HtmlProperty)" Importance="High" /> </Target> </Project> 

You can also download these files here.

+2
source

All Articles