Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MsBuild Condition Evaluate Property Contains

Tags:

msbuild

Attempting to create an itemgroup for use in a target where the file types are - Filename.CSS.ASPX

<Target Name="Test" AfterTargets="Build">
    <Message Text="Project $(ProjectName) Test PostBuild" Importance="high" />
    <Message Text="%(Content.FullPath)" Condition="%(Extension) == '.aspx' AND %(Filename.Contains(css))" Importance="high" />
</Target>

On Compile;

Error   1   Expected "%(Filename.Contains(css))" to evaluate to a boolean instead of "%(Filename.Contains(css))", in condition "%(Extension) == '.aspx' AND %   (Filename.Contains(css))".  C:\Projects\TestProj\TestProj\TestProj.csproj   58  38  TestProj

Any advice on expanding properties for evaluation?

like image 896
Thermionix Avatar asked Nov 14 '11 23:11

Thermionix


2 Answers

As far as I know, you can use string functions (like Contains in your code) only for properties, not for item metadata. But you tried to invoke Contains for %(Filename) and this is a metadata of Content item.

To get more details see link to MSDN. The last paragraph strictly says:

Property functions may not appear within metadata values. For example, %(Compile.FullPath.Substring(0,3)) is not allowed.

However, you can use static methods of Regex class. I believe the following code is what you wanted:

<Target Name="Test" AfterTargets="Build">
    <Message Text="Project $(ProjectName) Test PostBuild" Importance="high" />
    <Message Text="%(Content.FullPath)" Condition=" $([System.Text.RegularExpressions.Regex]::IsMatch('%(FullPath)', '.+\.css\.aspx')) " Importance="high" />
</Target>

If not, you can modify the regular expression in any way you need.

like image 194
vharavy Avatar answered Oct 16 '22 18:10

vharavy


In answer to similar question In MSBuild, can I use the String.Replace function on a MetaData item? was suggestion to use [System.String]::Copy() as workaround to access non static System.String methods.

So code can be rewritten to:

    <Target Name="Test" AfterTargets="Build">
        <Message Text="Project $(ProjectName) Test PostBuild" Importance="high" />
        <Message Text="%(Content.FullPath)" Condition="%(Content.Extension) == '.aspx' AND $([System.String]::Copy(%(Content.Filename)).Contains('css'))" Importance="high" />
    </Target>
like image 25
Stadub Dima Avatar answered Oct 16 '22 19:10

Stadub Dima