开发者

Dynamic Metadata Assignment in an ItemGroup

I have an ItemGroup defined as:

<ItemGroup>
    <ProtoFiles Include="Protos\*.proto"/>
</ItemGroup>
开发者_如何学编程

It yields a list of all .proto files in a directory of my project. I want each item in the group to include a piece of metadata that specifies the name of the file that will be generated based on the .proto file. I know I can do this:

<ItemGroup>
    <ProtoFiles Include="Protos\*.proto">
        <OutputFile>%(ProtoFiles.Filename).cs</OutputFile>
    </ProtoFiles>
</ItemGroup>

But my problem is that it is not a simple mapping from .proto filename to output filename. There is some tricky logic involved that I need to encapsulate somewhere and call that when assigning metadata. I need something like:

<ItemGroup>
    <ProtoFiles Include="Protos\*.proto">
        <OutputFile><GetOutputFilename ProtoFilename="%(ProtoFiles.Filename)"/></OutputFile>
    </ProtoFiles>
</ItemGroup>

The idea being that my custom GetOutputFilename task would be called in order to get the metadata value.

Is this possible? Am I barking up the wrong tree?


I think it's not, try instead passing the ItemGroup to a task to generate this metadata. Property Functions can operate on metadata values, but unfortunately cannot be used to define metadata.


It's hard to know if the logic is too tricky for MSBuild without knowing exactly what it is. Do you have a custom task that operates on @(ProtoFiles) to generate the output files? If so, why not alter your task (or refactor to a new one) that just calculates the output files without creating them, something like this,

<ProtoTask
   Files="@(ProtoFiles)"
   ... other params
   DryRun="true">
   <Output
      TaskParameter="OutputFiles"
      ItemName="ProtoFiles" />
</ProtoFiles>

The task can clone the item array, calculate the metadata value, and assign it to the output item array, which in the example here overwrites the original item array passed into the task.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜