MSbuild, How to access project property value in Inline Task code?
Asked Answered
S

3

7

I have inline Task code written in csharp

I wonder if there is anyway to access propect property in inline Task code

For ex. I am trying to replace string match with project property value. is it possible?

<![CDATA[
      MatchCollection matches = Regex.Matches(SourceStr, Pattern);

      for (int i = 0; i < matches.Count; i++)
           // replace the match value with project property... possible?


    ]]>
Solfa answered 31/1, 2013 at 23:5 Comment(0)
C
5

Pass it as a parameter, like you would with a compiled task?

<ParameterGroup>
    <Foo ParameterType="System.Bar" Required="true" />
<ParameterGroup>

Edit: Looks like simple inline tokens work too.

<![CDATA[
    Console.WriteLine("$(Foo)");
]]>
Cotto answered 31/1, 2013 at 23:21 Comment(1)
Additional background: Inline tokens work because MSBuild replaces them with their value before compiling the task.Outstretched
C
2

Too big to reply as comment.

Reason it's empty is as in your other question -- evaluation order. The code is evaluated and tokenized prior to hitting the target, I assume that's where you set the value, and setting the property.

If I understood what you're trying to achieve with the task, have a look below at an example, just pass them in as properties and out as output.

That said, I think you should look into Property Functions first, save youself a bunch of trouble.

http://msdn.microsoft.com/en-us/library/dd633440.aspx

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="Foo" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
            <SourceStr Required="true" Output="true" />
            <Pattern Required="true" />
            <Value Required="true" />
            <Macros ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
        </ParameterGroup>
        <Task>
            <Using Namespace="System.Linq" />
            <Using Namespace="System.Text.RegularExpressions" />
            <Code Type="Fragment" Language="cs">
            <![CDATA[
                var regex = new Regex(Pattern);
                var matches = regex.Matches(SourceStr).Cast<Match>().Select(m => m.Value).ToList();
                matches.ForEach(m => Log.LogMessage("{0} -> {1}", m, Value));
                Macros = matches.Select(m => new TaskItem(m)).ToArray();
                SourceStr = regex.Replace(SourceStr, Value);
            ]]>
            </Code>
        </Task>
    </UsingTask>
    <Target Name="Foo">
        <PropertyGroup>
            <SourceStr>Bar</SourceStr>
            <Value>z</Value>
        </PropertyGroup>
        <Message Text="in $(SourceStr)" />
        <Foo SourceStr="$(SourceStr)" Pattern="r$" Value="$(Value)">
            <Output TaskParameter="SourceStr" PropertyName="SourceStr" />
            <Output TaskParameter="Macros" ItemName="Macros" />
        </Foo>
        <Message Text="out $(SourceStr)" />
        <Message Text="sans %(Macros.Identity)" />
    </Target>
</Project>
Cotto answered 1/2, 2013 at 1:18 Comment(2)
hmmm... the reason I can't pass it as a parameter is 'valueSolfa
value will be unknown until I find 'matches'. value is decided while searching the pattern because I want to replace multiple different pattern of strings...Solfa
N
0

There are examples in the MSDN documentation. For example:

<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion="4.0">
    <UsingTask TaskName="TokenReplace" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
            <Path ParameterType="System.String" Required="true" />
            <Token ParameterType="System.String" Required="true" />
            <Replacement ParameterType="System.String" Required="true" />
        </ParameterGroup>
        <Task>
            <Code Type="Fragment" Language="cs">
                <![CDATA[
    string content = File.ReadAllText(Path);
    content = content.Replace(Token, Replacement);
    File.WriteAllText(Path, content);
    ]]>
            </Code>
        </Task>
    </UsingTask>

    <Target Name='Demo' >
        <TokenReplace Path="C:\Project\Target.config" Token="$MyToken$" Replacement="MyValue"/>
    </Target>
</Project>

If $(MyValue) was a project property you could do something like:

<TokenReplace Path="C:\Project\Target.config" Token="$MyToken$" Replacement="$(MyValue)"/>
Nonrepresentational answered 31/1, 2013 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.