Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 37267bf

Browse files
committed
Addressing code review comments and trying to fix the windows break.
1 parent a4213f7 commit 37267bf

File tree

7 files changed

+234
-292
lines changed

7 files changed

+234
-292
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.DotNet.MSBuildSdkResolver
5+
{
6+
// Note: This is not SemVer (esp., in comparing pre-release part, fx_ver_t does not
7+
// compare multiple dot separated identifiers individually.) ex: 1.0.0-beta.2 vs. 1.0.0-beta.11
8+
// See the original version of this code here: https://github.com/dotnet/core-setup/blob/master/src/corehost/cli/fxr/fx_ver.cpp
9+
internal sealed class FXVersion
10+
{
11+
public int Major { get; }
12+
public int Minor { get; }
13+
public int Patch { get; }
14+
public string Pre { get; }
15+
public string Build { get; }
16+
17+
public FXVersion(int major, int minor, int patch, string pre = "", string build = "")
18+
{
19+
Major = major;
20+
Minor = minor;
21+
Patch = patch;
22+
Pre = pre;
23+
Build = build;
24+
}
25+
26+
public static int Compare(FXVersion s1, FXVersion s2)
27+
{
28+
if (s1.Major != s2.Major)
29+
{
30+
return s1.Major > s2.Major ? 1 : -1;
31+
}
32+
33+
if (s1.Minor != s2.Minor)
34+
{
35+
return s1.Minor > s2.Minor ? 1 : -1;
36+
}
37+
38+
if (s1.Patch != s2.Patch)
39+
{
40+
return s1.Patch > s2.Patch ? 1 : -1;
41+
}
42+
43+
if (string.IsNullOrEmpty(s1.Pre) != string.IsNullOrEmpty(s2.Pre))
44+
{
45+
return string.IsNullOrEmpty(s1.Pre) ? 1 : -1;
46+
}
47+
48+
int preCompare = string.CompareOrdinal(s1.Pre, s2.Pre);
49+
if (preCompare != 0)
50+
{
51+
return preCompare;
52+
}
53+
54+
return string.CompareOrdinal(s1.Build, s2.Build);
55+
}
56+
57+
public static bool TryParse(string fxVersionString, out FXVersion FXVersion)
58+
{
59+
FXVersion = null;
60+
if (string.IsNullOrEmpty(fxVersionString))
61+
{
62+
return false;
63+
}
64+
65+
int majorSeparator = fxVersionString.IndexOf(".");
66+
if (majorSeparator == -1)
67+
{
68+
return false;
69+
}
70+
71+
int major = 0;
72+
if (!int.TryParse(fxVersionString.Substring(0, majorSeparator), out major))
73+
{
74+
return false;
75+
}
76+
77+
int minorStart = majorSeparator + 1;
78+
int minorSeparator = fxVersionString.IndexOf(".", minorStart);
79+
if (minorSeparator == -1)
80+
{
81+
return false;
82+
}
83+
84+
int minor = 0;
85+
if (!int.TryParse(fxVersionString.Substring(minorStart, minorSeparator - minorStart), out minor))
86+
{
87+
return false;
88+
}
89+
90+
int patch = 0;
91+
int patchStart = minorSeparator + 1;
92+
int patchSeparator = fxVersionString.FindFirstNotOf("0123456789", patchStart);
93+
if (patchSeparator == -1)
94+
{
95+
if (!int.TryParse(fxVersionString.Substring(patchStart), out patch))
96+
{
97+
return false;
98+
}
99+
100+
FXVersion = new FXVersion(major, minor, patch);
101+
return true;
102+
}
103+
104+
if (!int.TryParse(fxVersionString.Substring(patchStart, patchSeparator - patchStart), out patch))
105+
{
106+
return false;
107+
}
108+
109+
int preStart = patchSeparator;
110+
int preSeparator = fxVersionString.IndexOf("+", preStart);
111+
if (preSeparator == -1)
112+
{
113+
FXVersion = new FXVersion(major, minor, patch, fxVersionString.Substring(preStart));
114+
}
115+
else
116+
{
117+
int buildStart = preSeparator + 1;
118+
FXVersion = new FXVersion(
119+
major,
120+
minor,
121+
patch,
122+
fxVersionString.Substring(preStart, preSeparator - preStart),
123+
fxVersionString.Substring(buildStart));
124+
}
125+
126+
return true;
127+
}
128+
}
129+
}

src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,16 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext
5353
netcoreSdkVersion = new DirectoryInfo(netcoreSdkDir).Name;;
5454
}
5555

56-
if (!string.IsNullOrEmpty(sdkReference.MinimumVersion) &&
57-
SemanticVersion.Parse(netcoreSdkVersion) < SemanticVersion.Parse(sdkReference.MinimumVersion))
56+
if (!IsNetCoreSDKOveridden(netcoreSdkVersion) &&
57+
IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, sdkReference.MinimumVersion))
5858
{
5959
return factory.IndicateFailure(
6060
new[]
6161
{
6262
$"Version {netcoreSdkVersion} of the SDK is smaller than the minimum version"
6363
+ $" {sdkReference.MinimumVersion} requested. Check that a recent enough .NET Core SDK is"
64-
+ " installed and/or increase the version specified in global.json."
64+
+ " installed, increase the minimum version specified in the project, or increase"
65+
+ " the version specified in global.json."
6566
});
6667
}
6768

@@ -79,6 +80,42 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext
7980
return factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion);
8081
}
8182

83+
private bool IsNetCoreSDKOveridden(string netcoreSdkVersion)
84+
{
85+
return netcoreSdkVersion == null;
86+
}
87+
88+
private bool IsNetCoreSDKSmallerThanTheMinimumVersion(string netcoreSdkVersion, string minimumVersion)
89+
{
90+
FXVersion netCoreSdkFXVersion;
91+
FXVersion minimumFXVersion;
92+
93+
if (string.IsNullOrEmpty(minimumVersion))
94+
{
95+
return false;
96+
}
97+
98+
if (FailsToParseVersions(netcoreSdkVersion, minimumVersion, out netCoreSdkFXVersion, out minimumFXVersion))
99+
{
100+
return true;
101+
}
102+
103+
return FXVersion.Compare(netCoreSdkFXVersion, minimumFXVersion) == -1;
104+
}
105+
106+
private bool FailsToParseVersions(
107+
string netcoreSdkVersion,
108+
string minimumVersion,
109+
out FXVersion netCoreSdkFXVersion,
110+
out FXVersion minimumFXVersion)
111+
{
112+
netCoreSdkFXVersion = null;
113+
minimumFXVersion = null;
114+
115+
return !FXVersion.TryParse(netcoreSdkVersion, out netCoreSdkFXVersion) ||
116+
!FXVersion.TryParse(minimumVersion, out minimumFXVersion);
117+
}
118+
82119
private string ResolveNetcoreSdkDirectory(SdkResolverContext context)
83120
{
84121
foreach (string exeDir in GetDotnetExeDirectoryCandidates())

src/Microsoft.DotNet.MSBuildSdkResolver/SemanticVersion.cs

Lines changed: 0 additions & 181 deletions
This file was deleted.

test/Microsoft.DotNet.MSBuildSdkResolver.Tests/GivenAnMSBuildSdkResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public void ItReturnsNullIfTheVersionFoundDoesNotSatisfyTheMinVersion()
7171
result.Version.Should().BeNull();
7272
result.Warnings.Should().BeNullOrEmpty();
7373
result.Errors.Should().Contain("Version 99.99.99 of the SDK is smaller than the minimum version 999.99.99"
74-
+ " requested. Check that a recent enough .NET Core SDK is installed and/or increase the version"
75-
+ " specified in global.json.");
74+
+ " requested. Check that a recent enough .NET Core SDK is installed, increase the minimum version"
75+
+ " specified in the project, or increase the version specified in global.json.");
7676
}
7777

7878
[Fact]

0 commit comments

Comments
 (0)