Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>
like image 417
Noel Kennedy Avatar asked Jun 15 '10 10:06

Noel Kennedy


2 Answers

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.

like image 142
Julien Hoarau Avatar answered Oct 10 '22 21:10

Julien Hoarau


this sets BranchName to the string 'CorrectedValue' if it's value equals 'NotAllowed':

<PropertyGroup>
   <BranchName Condition="'$(BranchName)'=='NotAllowed'">CorrectedValue</BranchName>
</PropertyGroup>
like image 30
stijn Avatar answered Oct 10 '22 21:10

stijn