msbuildcreateitem

MSBuild CreateItem condition include based on config file


I'm trying to select a list of test dlls that contain corresponding config files

MyTest.Tests.dll
MyTest.Tests.config

I have to use a createItem as the dlls are not available at the time of the script loading

<CreateItem Include="$(AssemblyFolder)\*.Tests.dll"
            Condition="???"
  <Output TaskParameter="Include" ItemName="TestBinariesWithConfig"/>
</CreateItem>

Is there a condition I can use or is this the wrong approach?

Thanks
Mac

EDIT:

ok, to clarify, I need to construct a xUnit.Net project file. I need to do this because I'm running the tests through the xUnit.Console runner via nCover (don't ask!) but the long and short of it is I can only use a project file. The problem I'm having is when I have a test dll with an associated .config file. Without the config file, the test runner will fail.

This means I need to conditionally add an extra attribute (config-file) in the test project file.

The project template file:

<?xml version="1.0" encoding="utf-8"?>
<xunit>
  <assemblies>
<!-- SAMPLE    <assembly filename="Tests.dll" shadow-copy="false" config-file="Tests.dll.config" />     -->
<!-- @TARGETS@ -->
  </assemblies>
</xunit>

The FileUpdate task for the test dlls with no config file.

<FileUpdate
  Files="$(AssemblyFolder)\$(XUnitProjectFileName)"
  Regex="&lt;!-- @TARGETS@ --&gt;"
  ReplacementText="&lt;!-- @TARGETS@ --&gt;%0D%0A&lt;assembly filename='$(AssemblyFolder)\%(TestBinaries.FileName)%(TestBinaries.Extension)' shadow-copy='false' /&gt;"
  />

So I need a way to conditionally add the extra attribute in the FileUpdate task depending on whether there is a corresponding config file for the test dll.


Solution

  • You could just use the MSBuild Task output as a source for your CreateItem Task.

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
        <ItemGroup>
            <ProjectReferences Include="*.*proj" />
        </ItemGroup>
    
        <Target Name="BuildMyProjects">
            <MSBuild
                Projects="@(ProjectReferences)"
                Targets="Build">
                <Output
                    TaskParameter="TargetOutputs"
                    ItemName="AssembliesBuiltByChildProjects" />
            </MSBuild>
        </Target>
    
        <Target Name="AddConfigMetadata" DependsOnTargets="BuildMyProjects">
            <CreateItem
                Include="@(AssembliesBuiltByChildProjects)"
                AdditionalMetadata="config-file=%(Identity).config">
                <Output
                    TaskParameter="Include"
                    ItemName="MySourceItemsWithMetadata" />
            </CreateItem>
        </Target>
    
        <Target Name="WhatEverYouLikeToDo" DependsOnTargets="AddConfigMetadata">
            <Message Text="%(MySourceItemsWithMetadata.config-file)" />
        </Target>
    
    </Project>