Any way to update the metadata value of an MSBuild element?

I need to update the item metadata values. Easy to add to value:

<ItemDefinitionGroup> <ClCompile> <PreprocessorDefinitions>FOO;BAR;%(PreprocessorDefinitions)</PreprocessorDefinitions> </ClCompile> </ItemDefinitionGroup> 

However, I need to do delete part of the value. Ideally, something like this would work, but it is not:

 <ItemDefinitionGroup> <ClCompile> <PreprocessorDefinitions>%(PreprocessorDefinitions.Replace('FOO;',''))</PreprocessorDefinitions> </ClCompile> </ItemDefinitionGroup> 

Is there a way to accomplish this in MSBuild 4?

+4
source share
2 answers

I tried to do the same, and until I could figure out how to remove the definitions from the string, I discovered an additional property: UndefinePreprocessorDefinitions .

 <ItemDefinitionGroup> <ClCompile> <UndefinePreprocessorDefinitions>FOO</UndefinePreprocessorDefinitions> </ClCompile> </ItemDefinitionGroup> 

This will override the previous definition of FOO. It might seem a little silly to pass -DFOO -UFOO compiler, not anything, but it works just as well.

+2
source

In the following ItemDefinitionGroup you can create a copy of the current metadata, and then call Replace :

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" DefaultTargets="Dump" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemDefinitionGroup> <SomeItem> <SomeMetaData>foo,bar,baz</SomeMetaData> </SomeItem> </ItemDefinitionGroup> <ItemGroup> <SomeItem Include="one;two" /> </ItemGroup> <ItemDefinitionGroup> <SomeItem> <!-- Remove "bar" --> <SomeMetaData>$([System.String]::Copy('%(SomeMetaData)').Replace('bar',''))</SomeMetaData> </SomeItem> </ItemDefinitionGroup> <Target Name="Dump"> <Message Text="SomeItem.SomeMetaData: @(SomeItem -> '%(Identity)=%(SomeMetaData)') " /> </Target> </Project> 

Here is the output when starting with MSBuild 14:

  > MSBuild .\foo.proj Microsoft (R) Build Engine version 14.0.25420.1 Copyright (C) Microsoft Corporation. All rights reserved. Build started 2/17/2017 7:09:48 PM. Project "D:\temp\mb\foo.proj" on node 1 (default targets). Dump: SomeItem.SomeMetaData: one=foo,,baz;two=foo,,baz Done Building Project "D:\temp\mb\foo.proj" (default targets). Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:00.03 
+1
source

Source: https://habr.com/ru/post/1414203/


All Articles