0

Lets say I have two properties in my MSBuild properties file like this:

<Test1>foo1;foo2</Test1>
<Test2>bar;baz</Test2>

Now I want to let MSBuild set the value of a new property with all the combinations of the values of the two lists, like this (actual order in the final list does not matter, as long as all the combinations are there):

<Test>foo1-bar;foo1-baz;foo2-bar;foo2-baz</Test>

How would I do that? Using Linq (C#) it would be as simple as this:

string.Join(';', Test1.Split(';').SelectMany(t1 => Test2.Split(';').Select(t2 => t1 + '-' + t2)));

But Linq cannot be used in MSBuild properties.

1 Answer 1

0

I finally wound up with using a RoslynCodeTaskFactory task and do it in C#:

    <UsingTask TaskName="Group" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
        <ParameterGroup>
            <first ParameterType="System.String" Required="True" />
            <second ParameterType="System.String" Required="True" />
            <result ParameterType="System.String" Output="True" />
        </ParameterGroup>
        <Task>
            <Using Namespace="System.Linq" />
            <Code Type="Fragment" Language="cs">
                <![CDATA[
                    result = string.Join(@";", first.Split(';').SelectMany(t1 => second.Split(';').Select(t2 => t1 + @"-" + t2)));
                ]]>
            </Code>
        </Task>
    </UsingTask>

And then use the task as follows:

    <Target Name="GroupTest" BeforeTargets="Compile">
        <Group first="$(Test1)" second="$(Test2)">
            <Output PropertyName="Test" TaskParameter="result" />
        </Group>
    </Target>
Sign up to request clarification or add additional context in comments.

1 Comment

It seems in some situations, the target is not yet evaluated in time. Now I am still looking for a pure MSBuild-only solution, not using tasks and targets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.