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

笑着哭i 提交于 2019-12-10 14:58:13

问题


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?


    ]]>

回答1:


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)");
]]>



回答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>



回答3:


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)"/>


来源:https://stackoverflow.com/questions/14636796/msbuild-how-to-access-project-property-value-in-inline-task-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!