Using MSBuild, how to construct a dynamic string from iterating over files in an ItemGroup?

喜你入骨 提交于 2019-12-06 00:59:11

问题


I need to create multiple /testcontainer: parameters to feed into a task that exec's MsTest.

I have the following :

  <ItemGroup>
    <TestFiles Include="$(ProjectPath)\**\UnitTest.*.dll" />
  </ItemGroup>

for each match in TestFiles I would like to build a string like so:

"/testcontainer:UnitTest.SomeLibrary1.dll"
"/testcontainer:UnitTest.SomeLibrary2.dll"
"/testcontainer:UnitTest.SomeLibrary3.dll"

I am trying to use the internals of MSBuild without having to create a custom task, is this possible ?

TIA


回答1:


It really depends on the usage of this afterwards. For example the task that you are sending it to, does it accept in an item list and do you want to invoke it once or multiple times?

If you want to invoke it once then you use the @(...) syntax, and if you want to invoke it many times then you do batching with the %(...) syntax.

To invoke once

<Message Text="Test Files: @(TestFiles->'/testcontainer:%(RecursiveDir)%(Filename)%(Extension)')"/>

To invoke many times

<Message Text="Test Files: /testcontainer:%(TestFiles.RecursiveDir)%(TestFiles.Filename)%(TestFiles.Extension)"/>

More info on batching at http://sedotech.com/Resources#batching




回答2:


Try this:?

<Message Text="TestFiles= @(TestFiles-> '&quot;%(Fullpath)&quot;', ' ')" />

References:

  • MSBuild transforms
  • MSBuild: How to display an item list, separated by a comma.


来源:https://stackoverflow.com/questions/2768595/using-msbuild-how-to-construct-a-dynamic-string-from-iterating-over-files-in-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!