Home > Software design >  How to display Text on MSBuild only when ItemGroup Count > 0?
How to display Text on MSBuild only when ItemGroup Count > 0?

Time:07-19

I have following Target:

<Target Name="RemoveUnusedCopiedfiles" AfterTargets="CopyFilesToOutputDirectory" Condition="'$(Configuration)' == 'Release'">

    <ItemGroup>
        <FileToDelete Include="@(_SourceItemsToCopyToOutputDirectory -> '$(TargetDir)\%(FileName)%(Extension)')" Condition="%(Extension) == '.pdb' OR %(Extension) == '.xml'"/>
    </ItemGroup>
    <Delete Files="@(FileToDelete)"/>
    <Message Text="Deleted Files @(FileToDelete -> '%(FullPath)', ', ')"
             Importance="high"/>  // Problem Here I need only show Deleted Files if FileToDelete.Count > 0

    <!-- Remove other unused files -->
    <Delete Files="$(MSBuildProjectDirectory)\$(MSBuildProjectName).nuspec"/>
</Target>

I don't know exactly how to make above <Message> only appeard if FileToDelete variable Count > 0? How to do that in MSBuild?

I always get this line: Image are here

Is there something in MSBuild collection value: i.e: Condition = "@(FileToDelete.Count > 0)"

CodePudding user response:

You can check it like this:

<Message Text="Deleted Files @(FileToDelete -> '%(FullPath)', ', ')"
         Importance="high" Condition="'@(FileToDelete)' != ''"/>

The @(...), in a "string context", will expand to the items, delimited by a semicolon. Or an empty string, if the ItemGroup is empty.

  • Related