5

In my build script I have peace of code:

<CreateItem Include="src\packages\**\nunit-console.exe">
  <Output TaskParameter="Include" ItemName="NUnitRunners"/>
</CreateItem>

It actually find all nunit-console.exe in my packages folder. Let it used something like this:

<Exec Command="&quot;@(NUnitRunners)&quot; ..." />

It works when only one nunit-console.exe, but obviously it just concatenates paths if more then one exits.

  1. Is there any way to get last item from items collection (NUnitRunners in my case)?
  2. Any other technics how to get path of latest nunit-console.exe in packages folder?

3 Answers 3

3

This might be a bit hacky but you can use batching to extract the last item by just assigning %(Items.Identity) to a property.

Extracting the first item is a little trickier, you have to reverse the order of the original items by creating new items <Reversed Include="@(Items->Reverse())" />. Then you assign %(Reversed.Identity) to a property.

I'm not sure if this could break but looks good for me even when parallel builds are enabled.

See an example here: https://gist.github.com/shadow-cs/cb5499b010bdacd1f778be29daf7f04c

2

You could try to create an ItemGroup instead and then to use MSBuild.ExtensionPack.Framework.MsBuildHelper to extract elements from it.

Some links:

1

I was trying to solve the exact same problem. I wrote an inline task which solves the problem:

<UsingTask TaskName="GetFirstItem" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
  <ParameterGroup>
    <Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
    <FirstItem ParameterType="System.String" Output="true" Required="false" />
  </ParameterGroup>
  <Task>
    <Using Namespace="System.Linq"/>
    <Code Type="Fragment" Language="cs">FirstItem = Items.First().ItemSpec;</Code>
  </Task>
</UsingTask>

See an example here: https://gist.github.com/Serivy/74fff320c93176e6ca7f76e1cc139367

But in the end I didn't want the overhead and settled for a error message if there were too many/not enough.

<Error Condition="@(NUnitRunners->Count()) != 1" Text="@(NUnitRunners->Count()) nunit runners found." />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.