12

Is there a neat way to create "boolean" properties to use in MSBuild? I can evaluate the expression inside a Condition attribute, but not inside the Value attribute of a CreateProperty task.

I'd like to do something like this:

<CreateProperty Value="'$(IncludeInBuild)'=='' OR 
    '$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectFullPath), 
    $(IncludeInBuild)'=='True'">
    <Output TaskParameter="Value" PropertyName="MatchesInclude" />
</CreateProperty>

What that gives me is not True or False, but

''=='' OR '$([System.Text...

Can I evaluate a boolean expression and set a property with the result? My workaround now is just to repeat the expression in Condition attributes wherever I need it.

Rob
  • 4,327
  • 6
  • 29
  • 55
  • There's something wrong with the parentheses in the boolean expression you give for an example, so it's unclear where the `RegEx` *pattern* string is coming from and/or what input string you're trying to match against the `$(MSBuildProjectFullPath)` to generate a boolean value. Maybe these unrelated problems are why you (incorrectly?) thought you needed to use the MSBuild `CreateProperty` task at all in the first place? – Glenn Slayden Feb 17 '22 at 01:58

1 Answers1

17

How about creating a default property 'false' with a condition to assign true if the condition passes?

<PropertyGroup>
    <MatchesInclude>false</MatchesInclude>
    <MatchesInclude Condition="'$(IncludeInBuild)'=='' OR 
    '$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectFullPath), 
    $(IncludeInBuild)'=='True'">true</MatchesInclude>
</PropertyGroup>
Nicodemeus
  • 4,005
  • 20
  • 23
  • This is in fact what I ended up doing. – Rob Oct 02 '13 at 08:29
  • 1
    However, it is inelegant, as using the variables in conditionals must be of the form '$(MatchesInclude)' == 'true'. We would like something like the Exists function which is evaluation to boolean directly so that one can use something like Condition="$(MatchesInclude)" to enhance readability – Tom West Jan 08 '20 at 17:32
  • 2
    @TomWest just convert it `$([System.Convert]::ToBoolean(false))` and `$([System.Convert]::ToBoolean(true))` – OwnageIsMagic Apr 02 '20 at 12:50