In MSBuild, we may define Item metadata as:
<ItemGroup>
<DProjs Include="$(GroupProjPath)app.dproj">
<DCP>test1</DCP>
</DProjs>
</ItemGroup>
I may also define duplicate Item metadata:
<ItemGroup>
<DProjs Include="$(GroupProjPath)app.dproj">
<DCP>test1</DCP>
<DCP>test2</DCP>
<DCP>test3</DCP>
</DProjs>
</ItemGroup>
But how would I access 3 distinct item metadata value?
<Message Text="%(DProjs.DCP)" />
always return test3.
You could make the metadata value <DCP>test1;test2;test3</DCP> which is what you would expect if repeated values were allowed. You can use the CreateItem task to turn it into a list of items that can then be batched (looped over), or use it however you meant.
Related
I tried to have a condition on a Target tag, but resulted with the error:
target has a reference to item metadata. References
to item metadata are not allowed in target conditions unless they are part of an item transform.
So i found this work around:
How to add item transform to VS2012 .proj msbuild file
and tried to implement it, but i can't figure up what i am doing wrong because it's not working as expected.
<CallTarget Targets="CopyOldWebConfigJs" />
<Target Name="CopyOldWebConfigJs"
Inputs="#(ContentFiltered)"
Outputs="%(Identity).Dummy"
DependsOnTargets="webConfigJsCase">
<Message Text="web.config.js Case" />
</Target>
<!-- New target to pre-filter list -->
<Target Name="webConfigJsCase"
Inputs="#(FileToPublish)"
Outputs="%(Identity).Dummy">
<ItemGroup>
<ContentFiltered Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('%(FileToPublish.Filename)%(FileToPublish.Extension)', 'web.config.js'))" />
</ItemGroup>
</Target>
I thought that Inputs="#(ContentFiltered)" will contain the lines that DependsOnTargets="webConfigJsCase" find.
But when i run it , i am getting this message: Skipping target "CopyOldWebConfigJs" because it has no inputs.
I know for a fact that the regex work, and it do find a filename_ext that equals web.config.js so it return True
What do i do or understand wrong?
In <ItemGroup><Item/></ItemGroup>, no change will be made to the Item item because no action was specified. If you want to add entries to the item, you must specify Include="".
The <Item/> documentation describes the various attributes for item elements inside of an <ItemGroup/>. Note that at the top-level of an MSBuild file, directly under the <Project/> element, you would use the attributes Include and Exclude while in a <Target/> you would use the attributes Include and Remove. Not including any attributes at all is nonsensical and—as far as I know—no different than simply deleting the entire line. I am surprised MSBuild doesn’t throw an error or warning this is almost certainly a mistake and not intentional.
The Inputs and Outputs attributes on your <Target Name="webConfigJsCase"/> are unnecessary. In fact, they slow MSBuild down by making it loop over the target unnecessarily. You can filter just in the <Item/> like this:
<Target Name="webConfigJsCase">
<ItemGroup>
<ContentFiltered Condition="'%(FileToPublish.Filename)%(FileToPublish.Extension)' == 'web.config.js'" Include="#(FileToPublish)" />
</ItemGroup>
</Target>
Additionally, I assume that you intended your regular expression to match web.config.js but not match webaconfigbjs. You don’t need to use an advanced feature like Regular Expressions here because MSBuild’s built-in condition operators already support simple string comparison. If fixed the condition above to be more readable.
I'm currently working using MSBuild, and in one of the vcxproj files, I'd like to have different ItemDefinitionGroup for different ItemGroup, without overriding each other, so that each ItemGroup has its own specific definition, e.g.
<ItemDefinitionGroup Label="ItemDefGroupA">
<CLCompile>
<AdditionalOptions> /option_for_item_group_A</AdditionalOptions>
</CLCompile>
</ItemDefinitionGroup>
<ItemGroup Label="ItemGroupA">
<CLCompile Include="src\main.cpp" />
</ItemDefinitionGroup>
<ItemDefinitionGroup>
<CLCompile>
<AdditionalOptions> /option_for_item_group_B</AdditionalOptions>
</CLCompile>
</ItemDefinitionGroup>
<ItemGroup Label="ItemGroupB">
<CLCompile Include="src\main2.cpp" />
</ItemDefinitionGroup>
Is this even possible?
Thank you very much in advance, and have a nice day.
The Label attribute is ignored by MSBuild execution engine. The only place where it is used is by IDE code that needs to know a location inside project file to insert new entities, which happens if you modify your project in Visual Studio. The MSDN blog gives some information on how Labels are used by IDE.
So, what you are doing would not work. You only have one CLCompile item group, and your multiple item definition groups override each other. Whatever definition group is evaluated last, it wins. Note that the Item Definitions are evaluated first, after that Items are evaluated on next pass (link).
One possible workaround for your scenario is to create couple of auxiliary groups, like this:
<ItemGroup>
<GroupA Include="src\file1.cpp" />
<GroupA Include="src\file2.cpp" />
</ItemDefinitionGroup>
<ItemGroup>
<GroupB Include="src\file3.cpp" />
<GroupB Include="src\file4.cpp" />
</ItemDefinitionGroup>
Then you initialize your CLCompile group from a combination of the two with different metadata values:
<ItemGroup>
<CLCompile Include="#(GroupA)">
<AdditionalOptions> /option_for_item_group_A</AdditionalOptions>
</CLCompile>
<CLCompile Include="#(GroupB)">
<AdditionalOptions> /option_for_item_group_B</AdditionalOptions>
</CLCompile>
</ItemDefinitionGroup>
I have not found a correct syntax that would allow a transform modifiers to use property functions. For example, if we want to trim each filename in a list starting with the "lib" string:
<ListWithoutLib>#(MyOriginalList->%(Filename.TrimStart("lib"))</ListWithoutLib>
Is there any msbuild voodoo that could be written to accomplish this?
There is more than one issue in the question.
First, TrimStart doesn't have an overload that accepts a string and it doesn't remove a substring from the beginning of a string. Instead TrimStart accepts a set of chars and remove all instances of each char from the beginning of string.
For example in C#
"libFoo.dll".TrimStart('l', 'i', 'b')
will produce 'Foo.dll' and
"bilbobaggins.dll".TrimStart('l', 'i', 'b')
will produce 'obaggins.dll'.
But that issue is secondary to the question being asked.
You cannot use a property function inside of a metadata transform but you can apply a method of the string class as an item function.
<ItemGroup>
<MyOriginalList Include="libapple;libboat;cat;dog" />
</ItemGroup>
<PropertyGroup>
<ListWithoutLib>#(MyOriginalList->TrimStart('l'))</ListWithoutLib>
</PropertyGroup>
<Target Name="Default">
<Message Text="ListWithoutLib = $(ListWithoutLib)" />
</Target>
<!--
Output:
Default:
ListWithoutLib = ibapple;ibboat;cat;dog
-->
See String item functions.
This example shows how TrimStart can be called on an item collection but it doesn't do what you indicated that you need.
A solution to what you seem to need might be as follows:
<ItemGroup>
<SourceList Include="libapple;libboat;cat;dog"/>
</ItemGroup>
<PropertyGroup>
<libPrefix>lib</libPrefix>
</PropertyGroup>
<ItemGroup>
<MyOriginalList Include="#(SourceList)">
<IsStartsWithLibPrefix>$([System.String]::Copy('%(Filename)').StartsWith('$(libPrefix)'))</IsStartsWithLibPrefix>
<NameWithoutPrefix Condition="!'%(IsStartsWithLibPrefix)'">%(Filename)</NameWithoutPrefix>
<NameWithoutPrefix Condition="'%(IsStartsWithLibPrefix)'">$([System.String]::Copy('%(Filename)').Substring($(libPrefix.Length)))</NameWithoutPrefix>
</MyOriginalList>
</ItemGroup>
<Target Name="Default">
<Message Text="List of Filename:" />
<Message Text="#(MyOriginalList->' %(Filename)','%0d%0a')" />
<Message Text="List of NameWithoutPrefix:" />
<Message Text="#(MyOriginalList->' %(NameWithoutPrefix)','%0d%0a')" />
</Target>
<!--
Output:
Default:
List of Filename:
libapple
libboat
cat
dog
List of NameWithoutPrefix:
apple
boat
cat
dog
-->
There is a difference between item functions and property functions. Item functions are limited to being called on a collection reference, e.g. #(MyOriginalList). Item batching could be used to split the collection into references to the items with and without the lib prefix. Batching requires a target or task to batch over. The code that is shown forgoes using item functions and instead uses property functions. This allows all the 'work' to be done in the ItemGroup definition.
Assuming the inputs are not static, we need to determine which filenames have the prefix.
Although metadata values are strings, string methods can't be called on metadata values. A well known work-around is to create a new string object from the metadata value and then call a string method. [System.String]::Copy('%(Filename)') is creating a string object that is a copy of the value of the Filename metadata. The code $([System.String]::Copy('%(Filename)').StartsWith('$(libPrefix)')) is a property function call and the IsStartsWithLibPrefix metadata will be set to either True or False.
Because ItemGroups can be self-referential, we can use the IsStartsWithLibPrefix metadata to define the NameWithoutPrefix metadata. The NameWithoutPrefix definition has a condition. For a given item in the MyOriginalList ItemGroup only one variant of the NameWithoutPrefix definition will be used. For Items that have the prefix another property function call is used. Substring is called to create a new string instance that doesn't have the prefix.
I have the following problem in scripting with MSBuild:
I create a default item "itemA" with two metadata "metadata1" and "metadata2", whereby metadata2 refers to metadata1.
When I define itemA later and overwrite metadata1, the metadata2 contains still the default value of metadata1. How can I make the metadata2 to refer to the "new" metadata1?
Illustration in code as below:
<ItemDefinitionGroup>
<itemA>
<Metadata1>default</Metadata1>
<Metadata2>%(itemA.Metadata1)</Metadata2>
</itemA>
</ItemDefinitionGroup>
<ItemGroup>
<itemA Include="first" >
<Metadata1>m_data1</Metadata1>
</itemA>
</ItemGroup>
But see the print
<Message Text="itemA.Metadata1 = %(itemA.Metadata1)" />
<Message Text="itemA.Metadata2 = %(itemA.Metadata2)" />
delivers:
itemA.Metadata1 = m_data1 ***<-- correctly updated***
itemA.Metadata2 = default ***<-- why showing the default value, not* m_data1??**
how can I make itemA.Metadata2 to have the same value as itemA.Metadata1 after it has been updated?
I think this is not possible because order of evaluation Item Definitions - Value Sources - Note:
Item metadata from an ItemGroup is not useful in an ItemDefinitionGroup metadata declaration because ItemDefinitionGroup elements are processed before ItemGroup elements.
You have to override itemA's Metadata2 value in ItemGroup
<ItemDefinitionGroup>
<itemA>
<Metadata1>default</Metadata1>
<Metadata2>%(Metadata1)</Metadata2>
</itemA>
</ItemDefinitionGroup>
<ItemGroup>
<itemA Include="first" >
<Metadata1>m_data1</Metadata1>
<Metadata2>%(Metadata1)</Metadata2>
</itemA>
</ItemGroup>
As palo states, since Metadata2 has already been evaluated, you'll have to explicitly overwrite the value. Your change to Metadata1 won't automatically propagate to other places where it was referenced during initialization.
However, you can "re-evaluate" your items' metadata by starting a new instance of MSBuild and passing the updated metadata in as a property. Running msbuild /t:Wrapper on this project from the command line will result in Metadata1 and Metadata2 printing the same value:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DefaultMetadata1 Condition="DefaultMetadata1==''">default</DefaultMetadata1>
</PropertyGroup>
<ItemDefinitionGroup>
<itemA>
<Metadata1>$(DefaultMetadata1)</Metadata1>
<Metadata2>%(itemA.Metadata1)</Metadata2>
</itemA>
</ItemDefinitionGroup>
<ItemGroup>
<itemA Include="first" >
<Metadata1>m_data1</Metadata1>
</itemA>
</ItemGroup>
<Target Name="Wrapper">
<MSBuild
Projects="$(MSBuildProjectFile)"
Targets="Worker"
Properties="DefaultMetadata1=%(itemA.Metadata1)"
/>
</Target>
<Target Name="Worker">
<Message Text="itemA.Metadata1 = %(itemA.Metadata1)" />
<Message Text="itemA.Metadata2 = %(itemA.Metadata2)" />
</Target>
</Project>
The usefulness of this approach will depend on what you're trying to accomplish. You can undoubtedly find an alternate solution using properties instead of item metadata.
While the solution above works for the case you describe, it can quickly get out of hand. There's probably a more simple solution that may involve some redundant code.
My recommendation would be to use the simple solution and eliminate as much redundancy as you reasonably can without inventing new ways to get around MSBuild's small feature set. Clever tricks here probably won't save you that many LOC at the end of the day and may result in less readable code, making it more difficult for newcomers to understand what's going on.
I have an item group that includes a location which may or may not contain files. If there are no files present at the point the item group is declared, is it possible to re-evaluate the item group at a later time to pick up files that may have been generated in the new location, or will I have to declare an identical item group at this time and use that?
Item groups declared statically (outside of a Target, as a child element of the ) will be evaluated when the file is loaded. Item groups declared dynamically (within a <Target>) will be evaluated at the moment the execution passes through the target. For cases where the files are created during the build, you really should use a dynamic Item group.
I think you will have to create a new itemgroup. They are evaluated once and the value is saved, not the formula used to select files. Thus you can't "re-evaluate" these items.
You can re-define the ItemGroup by first removing the items, and the re-including the items:
<Target Name="Later on" >
<ItemGroup>
<ClCompile Remove="#(ClCompile)" />
<ClCompile Include="something here of your choice" />
</ItemGroup>
</Target>
Or if you don't want nor need to remove items, you can always simply append or add to the previously existing item group:
<Target Name="Later on" >
<ItemGroup>
<ClCompile Include="Add Even more stuff" />
</ItemGroup>
</Target>