MSBuild inline Task 不起作用(不是程序逻辑引起的)

MSBuild inline Task does not work (not caused by program logic)

我创建了一个内联任务,它将 return 版本字符串列表的最新两个版本字符串。

任务:

<UsingTask TaskName="GetLatestVersions" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <AllVersions ParameterType="System.String" Required="true" />
        <CurrentVersion ParameterType="System.String" Output="true" />
        <LastVersion ParameterType="System.String" Output="true" />
    </ParameterGroup>
    <Task>
        <Using Namespace="System" />
        <Code Type="Fragment" Language="cs">
            <![CDATA[
        var currentVersion = "0.0.0.0";
        var lastVersion = "0.0.0.0";

        var compareWithCurrent = new Func<string, string, bool>((toCompare, with) =>
        {
            var convertToIntArray = new Func<string, int[]>(version =>
            {
                int mayor, minor, build, revision;
                var parts = version.Split('.');

                if (int.TryParse(parts[0], out mayor) &&
                    int.TryParse(parts[1], out minor) &&
                    int.TryParse(parts[2], out build) &&
                    int.TryParse(parts[3], out revision))
                {
                    return new[] {mayor, minor, build, revision};
                }
                return new[] {0, 0, 0, 0};

            });
            var current = convertToIntArray(with);
            var toCompareArr = convertToIntArray(toCompare);

            return toCompareArr[0] > current[0] ||
                    toCompareArr[0] == current[0] && toCompareArr[1] > current[1] ||
                    toCompareArr[0] == current[0] && toCompareArr[1] == current[1] && toCompareArr[2] > current[2] ||
                    toCompareArr[0] == current[0] && toCompareArr[1] == current[1] && toCompareArr[2] == current[2] && toCompareArr[3] > current[3];

        });

        foreach (var version in AllVersions.Replace("\", "").Split(';'))
        {
            if (compareWithCurrent(version, currentVersion))
            {
                lastVersion = currentVersion;
                currentVersion = version;
            }
        }

        CurrentVersion = currentVersion;
        LastVersion = lastVersion;
    ]]>
        </Code>
    </Task>
</UsingTask>

致电:

    <GetLatestVersions AllVersions="@Versions">
        <Output PropertyName="CurrentVersionProperty" TaskParameter="CurrentVersion" />
        <Output PropertyName="LastVersionProperty" TaskParameter="LastVersion" />
    </GetLatestVersions>

@Versions 包含这样的字符串(通过消息转储):

\3.7.1.3846;\3.7.2.3884;\3.7.3.3962;\3.7.4.4112;\3.8.0.4274;\3.9.0.4362;\3.9.0.4386

变量CurrentVersionPropertyLastVersionProperty都是0.0.0.0

我试过的

通常你会说 "The code inside the Task must be false" - 这正是我所期望的。

我已经创建并调试了以下源代码(控制台程序),Codelogics 似乎运行良好。

class Program
{
    static void Main(string[] args)
    {
        var versions =
             @" .8.19.0;.9.23.0;.1.3.0;.10.0.2304;.10.0.2326;.10.0.2367;.11.0.2422;.12.0.2543;.14.0.2716;.15.0.2779;.16.0.2881;.16.0.2888;.17.0.2977;.18.0.3093;.2.19.0;.2.27.0;.2.29.0;.3.7.0;.3.8.568;.4.0.941;.5.0.1448;.6.0.1642;.8.0.2008;.8.0.2045;.9.0.2176;.9.0.2222;.0.0.3174;.0.0.3174-Prof;.0.0.3229;.1.0.3255;.10.0.4470;.10.0.4480;.10.1.4538;.11.0.4656;.12.0.4769;.12.1.4872;.13.0.4985;.13.0.5009;.13.0.5019;.13.1.5094;.13.1.5171;.14.0.5217;.2.0.3342;.2.0.3370;.3.0.3408;.4.0.3546;.5.0.3602;.6.0.3664;.7.0.3794;.7.1.3846;.7.2.3884;.7.3.3962;.7.4.4112;.8.0.4274;.9.0.4362;.9.0.4386";

        string current, last;
        GetLatestVersions(out current, out last, versions);

    }

    public static void GetLatestVersions(out string CurrentVersion, out string LastVersion, string AllVersions)
    {
        //EXACTLY the same code as in Task - omitted to save space
    }
}

我也试过更改变量名。

我的猜测:

问题一定出在 MSBuild 的某个地方。也许 Outparameters 或其他问题存在问题。我将不胜感激任何帮助! 谢谢。

似乎是一个简单的错字:

<GetLatestVersions AllVersions="@Versions">

当然应该是一个适当的 ItemGroup 扩展所以

<GetLatestVersions AllVersions="@(Versions)">

与其重新发明轮子,我还建议您使用 Version class,因为它已经提供了 parsing/less than/greater 比/...所以您的 compareWithCurrent可以改写为

var compareWithCurrent = new Func<string, string, bool>( ( toCompare, with ) =>
{
  return new Version( toCompare ) > new Version( with );
} );

顺便说一句,如果我在您的代码中输入 1.5.0.0;1.2.0.0;1.3.0.0,它会告诉我当前版本是 1.5.0.0,最后版本是 0.0.0.0。不完全是我所期望的 - 再一次,我不确定代码应该做什么。