19

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?

Thermionix
  • 669
  • 1
  • 6
  • 17
  • 1
    You'll probably want to make sure that any string literals that you pass to property functions as parameters are properly quoted. `$(Property.Contains('literal'))` not `$(Property.Contains(literal))`. – Michael Price Nov 16 '11 at 17:05

2 Answers2

36

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.

Andrew Arnott
  • 80,040
  • 26
  • 132
  • 171
vharavy
  • 4,881
  • 23
  • 30
14

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>
Community
  • 1
  • 1
Stadub Dima
  • 858
  • 10
  • 24