我创建了一个内联任务,它将返回版本字符串列表的最新两个版本字符串。
<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
包含这样的字符串(通过Message转储):
\ 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
变量CurrentVersionProperty
和LastVersionProperty
都是0.0.0.0
通常你会说&#34;任务中的代码必须为false&#34; - 这就是我的预期。
我创建并调试了以下源代码(控制台程序),Codelogics似乎运行良好。
class Program
{
static void Main(string[] args)
{
var versions =
@" \1.8.19.0;\1.9.23.0;\2.1.3.0;\2.10.0.2304;\2.10.0.2326;\2.10.0.2367;\2.11.0.2422;\2.12.0.2543;\2.14.0.2716;\2.15.0.2779;\2.16.0.2881;\2.16.0.2888;\2.17.0.2977;\2.18.0.3093;\2.2.19.0;\2.2.27.0;\2.2.29.0;\2.3.7.0;\2.3.8.568;\2.4.0.941;\2.5.0.1448;\2.6.0.1642;\2.8.0.2008;\2.8.0.2045;\2.9.0.2176;\2.9.0.2222;\3.0.0.3174;\3.0.0.3174-Prof;\3.0.0.3229;\3.1.0.3255;\3.10.0.4470;\3.10.0.4480;\3.10.1.4538;\3.11.0.4656;\3.12.0.4769;\3.12.1.4872;\3.13.0.4985;\3.13.0.5009;\3.13.0.5019;\3.13.1.5094;\3.13.1.5171;\3.14.0.5217;\3.2.0.3342;\3.2.0.3370;\3.3.0.3408;\3.4.0.3546;\3.5.0.3602;\3.6.0.3664;\3.7.0.3794;\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";
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
}
}
我也尝试过更改Variablenames。
问题必须是MSBuild中的问题。也许存在Outparameters或者其他问题。我很感激任何帮助! 谢谢。
答案 0 :(得分:2)
似乎是一个简单的拼写错误:
<GetLatestVersions AllVersions="@Versions">
当然应该是一个正确的ItemGroup扩展,所以
<GetLatestVersions AllVersions="@(Versions)">
我没有重新发明轮子,而是建议你使用Version类,因为它已经提供了解析/小于/大于/ ...所以你的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,它会告诉我currentVersion是1.5.0.0而lastVersion是0.0.0.0。不完全是我所期望的 - 然后再说一遍,我不确定代码应该做什么。