How to set 'condition' using a condition stored in a property? - msbuild

I have a condition such as 'a==1' stored in property $(c) and I wanna used it as the condition for task Message like below code:
<PropertyGroup>
<aa>1>2</aa>
</PropertyGroup>
<Target Name="t">
<Message Text="122333" Condition="$(aa)" />
</Target>
Error was raised!
So, how can I do it? Please help!

You can easily use property values for evaluating conditions. Here is an example:
<PropertyGroup>
<aa>1</aa>
</PropertyGroup>
<Target Name="Build">
<Message Text="Some text" Condition=" $(aa) < 2 " />
</Target>
Note that:
Property values are strings, you must evaluate the condition in the Condition attribute. See MSDN Docs on evaluating conditions.
You must escape XML characters (replace < with < )

Related

msbuild - how to remove a string within a property

If I have
<PropertyGroup>
<Prop1>C:\asdfsa\abc;C:\sadf\def;C:\asfddsa\abc;</Prop1>
</PropertyGroup>
How do I remove all entries that contain \abc?
I want final value of $(Prop1) to be C:\sadf\def.
A property doesn't have 'entries', it's merely a string. You could fiddle with string splitting and/or regexes to erase some parts from it. On the other hand MSBuild also has Items which are more like proper lists. Going round via them is probably easier:
<Target Name="RemoveItemsFromProperty">
<PropertyGroup>
<Prop1>C:\asdfsa\abc;C:\sadf\def;C:\asfddsa\abc;</Prop1>
</PropertyGroup>
<ItemGroup>
<Items Include="$(Prop1)"/>
<FilteredItems Include="#(Items)" Condition="! $([System.String]::Copy('%(Identity)').Contains('\abc'))"/>
</ItemGroup>
<PropertyGroup>
<Prop1>#(FilteredItems)</Prop1>
</PropertyGroup>
<Message Text="$(Prop1)" />
</Target>
edit ok the regex way is easier though I'm not 100% sure my pattern covers all cases:
<Target Name="RemoveItemsFromProperty">
<PropertyGroup>
<Prop1>C:\asdfsa\abc;C:\sadf\def;C:\asfddsa\abc;</Prop1>
</PropertyGroup>
<Message Text="$([System.Text.RegularExpressions.Regex]::Replace('$(Prop1)', ';[.^;]\\abc', ''))" />
</Target>

How to check for presence of a string in a property

If I have a property
<PropertyGroup>
<MyProp>abd;efg;hij;klm</MyProp>
</PropertyGroup>
How do I parse $(MyProp) to check the presence of klm?
You can use property functions to invoke the string Contains() method to check for string occurrences. While some other options using the Items (through an Include="$(MyProp)" and checking if an item with the expected identity exists) is also possible, conditions using property functions can be used on any msbuild element, both inside and outside of targets.
Example:
<Project>
<PropertyGroup>
<MyProp>abd;efg;hij;klm</MyProp>
</PropertyGroup>
<Target Name="Build">
<Message Importance="high" Text="klm is included!" Condition="$(MyProp.Contains('klm'))" />
<PropertyGroup>
<MyProp>;$(MyProp);</MyProp>
</PropertyGroup>
<Message Importance="high" Text="exactly klm is included!" Condition="$(MyProp.Contains(';klm;'))" />
</Target>
</Project>
The second approach - pre- and appending ; and checking for ;klm; - makes sure that the string is matched as a whole in the list can deal with ;aklm.

Parse key/value pairs from MSBuild property

Say I have a property like:
<MyProp>Foo=Bar;Hello=World</MyProp>
This seems like a reasonably common property pattern in MSBuild. How would I go about fetching the value "World"? In an ideal world this might look something like:
$(MyProp).(Hello)
Edit: To be clear, the property is not of my own creation, it is the output from another target that is out of my control, so I cannot change the way the property is declared.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MyProp>Foo=Bar;Hello=World</MyProp>
</PropertyGroup>
<Target Name="Foo">
<CreateItem Include="MyProp" AdditionalMetadata="$(MyProp)">
<Output TaskParameter="Include" ItemName="MyProp" />
</CreateItem>
<Message Text="Foo %(MyProp.Foo)" />
<Message Text="Hello %(MyProp.Hello)" />
</Target>
</Project>
You have two routes to follow as far as I am concerned
Declare a Property Group just like the following:
<PropertyGroup>
<Foo>Bar</Foo>
<Hello>World</Hello>
</PropertyGroup>
and then use the following method to access your properties
<Target Name="DoSomething">
<Message Text="Print this : $(Foo)" />
</Target>
or you might want to take ItemGroup Element approach like the following
<ItemGroup>
<MySolutionFiles Include="..\mySolution.sln" />
</ItemGroup>
<Target Name="PrintItems">
<Message Text="My Files: #(MySolutionFiles)" />
</Target>
You can have the following as well
<ItemGroup>
<MyProp
Include="Foo;Hello" />
</ItemGroup>
<Target Name="PrintMyItems">
<Message Text="MyProp: #(MyProp)" />
</Target>
If there is no choice over the input then one possible solution is parsing the input into an array and then taking it from there like the following:
<PropertyGroup>
<MyProp>Foo=Bar;Hello=World</MyProp>
<Split>$(MyProp.Split(';'))</Split>
</PropertyGroup>
and then play with the array items like the following:
<Target Name="DoPrint">
<Message text="$(Split[0])" />
</Target>
Split[0] item contains your Foo=Bar which can be split into two more strings just like above. This should keep you going for now.
You don't need a property group, it's just nice to have default values in case the user doesn't pass them.
For each property you pass, the syntax to access that property is $(PropertyName).
So if you pass:
msbuild.exe /p:P1=V1 /p:P2=V2;P3=V3
You would use the property name $(P1), $(P2), $(P3).
More on MsBuild properties here.

How do I change a property's value based on a conditional in msbuild?

I would like to change the value of a property if it is a certain value. In C# I would write:
if(x=="NotAllowed")
x="CorrectedValue;
This is what I have so far, please don't laugh:
<PropertyGroup>
<BranchName>BranchNameNotSet</BranchName>
</PropertyGroup>
///Other targets set BranchName
<Target Name="CheckPropertiesHaveBeenSet">
<Error Condition="$(BranchName)==BranchNameNotSet" Text="Something has gone wrong.. branch name not entered"/>
<When Condition="$(BranchName)==master">
<PropertyGroup>
<BranchName>MasterBranch</BranchName>
</PropertyGroup>
</When>
</Target>
You can do that using Condition on Property:
<PropertyGroup>
<BranchName>BranchNameNotSet</BranchName>
</PropertyGroup>
<Target Name="CheckPropertiesHaveBeenSet">
<!-- If BranchName equals 'BranchNameNotSet' stop the build with error-->
<Error Condition="'$(BranchName)'=='BranchNameNotSet'" Text="Something has gone wrong.. branch name not entered"/>
<PropertyGroup>
<!-- Change BranchName value if BranchName equals 'master' -->
<BranchName Condition="'$(BranchName)'=='master'">MasterBranch</BranchName>
</PropertyGroup>
</Target>
Info on When and Choose:
The Choose, When, and Otherwise elements are used together to provide a way to select one section of code to execute out of a number of possible alternatives.
Choose elements can be used as child elements of Project, When and Otherwise elements.
In your code sample, you use When without Choose and within a target, that is not possible.
this sets BranchName to the string 'CorrectedValue' if it's value equals 'NotAllowed':
<PropertyGroup>
<BranchName Condition="'$(BranchName)'=='NotAllowed'">CorrectedValue</BranchName>
</PropertyGroup>

MSBuild getting property substring before underscore symbol

In MSBuild I have a property which value is Name_Something. How can I get name part of this property.
With MSBuild 4
If you use MSBuild 4, you could use the new and shiny property functions.
<PropertyGroup>
<MyProperty>Name_Something</MyProperty>
</PropertyGroup>
<Target Name="SubString">
<PropertyGroup>
<PropertyName>$(MyProperty.Substring(0, $(MyProperty.IndexOf('_'))))</PropertyName>
</PropertyGroup>
<Message Text="PropertyName: $(PropertyName)"/>
</Target>
With MSBuild < 4
You could use the RegexReplace task of MSBuild Community Task
<PropertyGroup>
<MyProperty>Name_Something</MyProperty>
</PropertyGroup>
<Target Name="RegexReplace">
<RegexReplace Input="$(MyProperty)" Expression="_.*" Replacement="" Count="1">
<Output ItemName ="PropertyNameRegex" TaskParameter="Output" />
</RegexReplace>
<Message Text="PropertyNameRegex: #(PropertyNameRegex)"/>
</Target>
If I understand your question correctly you are trying to get the substring of a MSBuild property. There is no direct way to do string manipulation in MSBuild, like in NAnt. So you have two options:
1). Create separate variables for each part and combine them:
<PropertyGroup>
<Name>Name</Name>
<Something>Something</Something>
<Combined>$(Name)_$(Something)</Combined>
</PropertyGroup>
This works fine if the parts are known before hand, but not if you need to do this dynamically.
2). Write a customer MSBuild task that does the string manipulation. This would be your only option if it needed to done at runtime.
It looks like you could use Item MetaData instead of a Property:
<ItemGroup>
<Something Include="SomeValue">
<Name>YourName</Name>
<SecondName>Foo</SecondName>
</Something>
</ItemGroup>