Steven Liekens
Steven Liekens

Reputation: 14088

ItemGroup include files with condition

In MSBuild 12.0, can I include files in an <ItemGroup> only when a condition is met?

My use case is that I want to create a collection of all .csproj files for which a .nuspec file with the same name exists (without extension).

- root_dir\
    - build.proj
    - Project1\
        - Project1.csproj
        - Project1.nuspec
    - Project2\
        - Project2.csproj
    - Project3\
        - Project3.csproj
        - Project3.nuspec

I tried to do this with MSBuild transforms, but that didn't work.

<ItemGroup>
    <ProjectWithNuspec Include="*\*.csproj"
                       Condition="Exists('@(ProjectWithNuspec->'%(Filename).nuspec')')">
</ItemGroup>

The item ProjectWithNuspec does not seem to be initialized before the condition is evaluated.

I did figure out a way to do it in two steps:

  1. Include all files
  2. Remove files that do not meet the condition
<ItemGroup>
    <ProjectWithNuspec Include="*\*.csproj">
    <ProjectWithNuspec Remove="%(ProjectWithNuspec.Identity)"
                       Condition="!Exists('@(ProjectWithNuspec->'%(Filename).nuspec')')">
</ItemGroup>

Ideally, I'd like to be able to do this in a single step.

Upvotes: 9

Views: 8645

Answers (2)

Cogent
Cogent

Reputation: 402

The Condition element can be used regular expressions. Please refer to this answer.

Upvotes: 0

Michael Baker
Michael Baker

Reputation: 3434

You are basically doing this the best way that can be expressed in MSBuild. For these sorts of transforms you almost always need an intermediate item group which you layer additional transforms to. Think of it like a pipeline, first you need all files (these go into group 1), now I need all files from group 1 which also match some other condition (group 2).

<ItemGroup>
    <AllProjects Include="$(MyDir)\**\*.csproj" />        
    <AllProjectsWithNuspec Include="@(AllProjects)"
                           Condition="Exists('%(RecursiveDir)%(FileName).nuspec')"  />

</ItemGroup>

Upvotes: 10

Related Questions