MsBuild Condition Evaluate Property Contains

14,007

Solution 1

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.

Solution 2

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>
Share:
14,007

Related videos on Youtube

Thermionix
Author by

Thermionix

Updated on September 24, 2022

Comments

  • Thermionix
    Thermionix over 1 year

    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?