How to manually evaluate msbuild condition?

1.2k Views Asked by At

I'm creating a custom msbuild task that will be processing a configuration from custom XML file. I want to allow to use Condition attribute in that xml file. Syntax of that attribute should be the same as MSBuild Conditions (https://msdn.microsoft.com/en-us/library/7szfhaft.aspx)

How can I evaluate value of that attribute? Is there an existing library that automate that or I'm forced to write my own parser?

So far I was able only to get value of all variables that probably will be necessary to evaluate that conditions (How to access the MSBuild 's properties list when coding a custom task?)

1

There are 1 best solutions below

1
On

I’m not sure if this will be helpful for you. I was solving the similar problem c++ projects. I was thinking to use Microsoft.Build.BuildEngine.Project class but later changed my mind. Finally I’ve created mine config in msbuild style (including namespace). I’ve enforced importing my config by msbuild (I’ve misused ForceImportAfterCppTargets property). Msbuild evaluated everything for me. Mine injected config (or props/target file) contained target that was injected into build process by overriding some build property (at the project level) in a way my target was called. Mine custom target called mine custom task with passed all necessary properties and items by parameters.


Following content is response on Uriel Jun 12 at 16:26:

Because you've marked question with tag c# I tried to make sample with C# vs 2010.

I made sample really simple. I put task and xml configuration file into one file named my.props. My custom task just prints values of my configuration provided by item. It prints metadata of item. One think you have to do is to manually modify your .csproj by adding one simple line. After the line where is Microsoft.CSharp.targets imported add import of custom my.props file. This sample expects your my.props is in the same directory as .csproj. Diff style change: +

Content of my.props:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<UsingTask TaskName="MyTool" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
  <ParameterGroup>
    <Cfg ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
  </ParameterGroup>
  <Task>
    <Code Type="Fragment" Language="cs">
<![CDATA[
      if (Cfg.Length > 0)
      {
        for (int i = 0; i < Cfg.Length; ++i)
        {
          ITaskItem item = Cfg[i];
          string value1 = item.GetMetadata("Value1");
          string value2 = item.GetMetadata("Value2");
          Log.LogMessage(MessageImportance.High, "MyTool: {0} - {1}", value1, value2);
        }
      }
]]>
    </Code>
  </Task>
</UsingTask>

<ItemDefinitionGroup Condition=" '$(Configuration)' == 'Release' ">
  <MyConfig>
      <Value1>Hello</Value1>
    <Value2>World</Value2>
  </MyConfig>
</ItemDefinitionGroup>

<ItemDefinitionGroup Condition=" '$(Configuration)' == 'Debug' ">
  <MyConfig>
      <Value1>Hello</Value1>
    <Value2>Debug world</Value2>
  </MyConfig>
</ItemDefinitionGroup>

<ItemGroup>
  <MyConfig Include="MyCfg" />
</ItemGroup>

  <Target Name="AfterBuild">
    <MyTool Cfg="@(MyConfig)" />
  </Target>
</Project>