Skip to content

Commit 78ce471

Browse files
committed
修改部分版本号格式对比失败的问题
1 parent 654dae5 commit 78ce471

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/JiuLing.CommonLibs.UnitTests/VersionUtilsTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public class VersionUtilsTests
99

1010
[TestMethod()]
1111
[DataRow("1.0.0", "1.0.0", false)]
12+
[DataRow("1.0.0", "1.0.0.0", false)]
13+
[DataRow("1.0.0.0", "1.0.0", false)]
14+
[DataRow("1.0", "1.0.0", false)]
15+
[DataRow("1.0.0", "1.0", false)]
1216
[DataRow("1.0.0", "1.0.2", true)]
1317
[DataRow("2.0.0.0", "2.0.0.0", false)]
1418
[DataRow("2.1.0.0", "3.0.0.0", true)]

src/JiuLing.CommonLibs/VersionUtils.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,30 @@ public static class VersionUtils
1515
/// <returns>返回是否需要更新</returns>
1616
public static bool CheckNeedUpdate(string currentVersion, string newVersion)
1717
{
18-
Version current = new Version(currentVersion);
19-
Version version = new Version(newVersion);
18+
Version current = ToVersionWithBuild(currentVersion);
19+
Version version = ToVersionWithBuild(newVersion);
2020
return CheckNeedUpdate(current, version);
2121
}
2222

23+
private static Version ToVersionWithBuild(string versionString)
24+
{
25+
if (Version.TryParse(versionString, out var version))
26+
{
27+
var build = version.Build;
28+
if (build == -1)
29+
{
30+
build = 0;
31+
}
32+
var revision = version.Revision;
33+
if (revision == -1)
34+
{
35+
revision = 0;
36+
}
37+
version = new Version(version.Major, version.Minor, build, revision);
38+
}
39+
return version;
40+
}
41+
2342
/// <summary>
2443
/// 检查当前版本是否需要更新
2544
/// </summary>

0 commit comments

Comments
 (0)