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

Commit f61d1ff

Browse files
committed
Adding a check for the min version in the CLI Resolver.
1 parent 947c8da commit f61d1ff

File tree

7 files changed

+386
-1
lines changed

7 files changed

+386
-1
lines changed

src/Microsoft.DotNet.MSBuildSdkResolver/MSBuildSdkResolver.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,25 @@ public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext
5353
netcoreSdkVersion = new DirectoryInfo(netcoreSdkDir).Name;;
5454
}
5555

56+
if (SemanticVersion.Parse(netcoreSdkVersion) < SemanticVersion.Parse(sdkReference.MinimumVersion))
57+
{
58+
return factory.IndicateFailure(
59+
new[]
60+
{
61+
$"Version {netcoreSdkVersion} of the SDK is smaller than the minimum version"
62+
+ $" {sdkReference.MinimumVersion} requested. Check that a recent enough .NET Core SDK is"
63+
+ " installed and/or increase the version specified in global.json."
64+
});
65+
}
66+
5667
string msbuildSdkDir = Path.Combine(msbuildSdksDir, sdkReference.Name, "Sdk");
5768
if (!Directory.Exists(msbuildSdkDir))
5869
{
5970
return factory.IndicateFailure(
6071
new[]
6172
{
6273
$"{msbuildSdkDir} not found. Check that a recent enough .NET Core SDK is installed"
63-
+ " and/or increase the version specified in global.json. "
74+
+ " and/or increase the version specified in global.json."
6475
});
6576
}
6677

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
using System.Reflection;
5+
using System.Runtime.CompilerServices;
6+
7+
[assembly: InternalsVisibleTo("Microsoft.DotNet.MSBuildSdkResolver.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
internal sealed class SemanticVersion
7+
{
8+
public int Major { get; private set; }
9+
public int Minor { get; private set; }
10+
public int Patch { get; private set; }
11+
public string Pre { get; private set; }
12+
public string Build { get; private set; }
13+
14+
public SemanticVersion(int major, int minor, int patch) : this(major, minor, patch, string.Empty, string.Empty)
15+
{
16+
}
17+
18+
public SemanticVersion(int major, int minor, int patch, string pre) :
19+
this(major, minor, patch, pre, string.Empty)
20+
{
21+
}
22+
23+
public SemanticVersion(int major, int minor, int patch, string pre, string build)
24+
{
25+
Major = major;
26+
Minor = minor;
27+
Patch = patch;
28+
Pre = pre;
29+
Build = build;
30+
}
31+
32+
public static bool operator ==(SemanticVersion s1, SemanticVersion s2)
33+
{
34+
return Compare(s1, s2) == 0;
35+
}
36+
37+
public static bool operator !=(SemanticVersion s1, SemanticVersion s2)
38+
{
39+
return !(s1 == s2);
40+
}
41+
42+
public static bool operator <(SemanticVersion s1, SemanticVersion s2)
43+
{
44+
return Compare(s1, s2) < 0;
45+
}
46+
47+
public static bool operator >(SemanticVersion s1, SemanticVersion s2)
48+
{
49+
return Compare(s1, s2) > 0;
50+
}
51+
52+
public static bool operator >=(SemanticVersion s1, SemanticVersion s2)
53+
{
54+
return Compare(s1, s2) >= 0;
55+
}
56+
57+
public static bool operator <=(SemanticVersion s1, SemanticVersion s2)
58+
{
59+
return Compare(s1, s2) <= 0;
60+
}
61+
62+
public static SemanticVersion Parse(string semanticVersionString)
63+
{
64+
int majorSeparator = semanticVersionString.IndexOf(".");
65+
if (majorSeparator == -1)
66+
{
67+
return null;
68+
}
69+
70+
int major = 0;
71+
if (!int.TryParse(semanticVersionString.Substring(0, majorSeparator), out major))
72+
{
73+
return null;
74+
}
75+
76+
int minorStart = majorSeparator + 1;
77+
int minorSeparator = semanticVersionString.IndexOf(".", minorStart);
78+
if (minorSeparator == -1)
79+
{
80+
return null;
81+
}
82+
83+
int minor = 0;
84+
if (!int.TryParse(semanticVersionString.Substring(minorStart, minorSeparator - minorStart), out minor))
85+
{
86+
return null;
87+
}
88+
89+
int patch = 0;
90+
int patchStart = minorSeparator + 1;
91+
int patchSeparator = semanticVersionString.FindFirstNotOf("0123456789", patchStart);
92+
if (patchSeparator == -1)
93+
{
94+
if (!int.TryParse(semanticVersionString.Substring(patchStart), out patch))
95+
{
96+
return null;
97+
}
98+
99+
return new SemanticVersion(major, minor, patch);
100+
}
101+
102+
if (!int.TryParse(semanticVersionString.Substring(patchStart, patchSeparator - patchStart), out patch))
103+
{
104+
return null;
105+
}
106+
107+
int preStart = patchSeparator;
108+
int preSeparator = semanticVersionString.IndexOf("+", preStart);
109+
if (preSeparator == -1)
110+
{
111+
return new SemanticVersion(major, minor, patch, semanticVersionString.Substring(preStart));
112+
}
113+
else
114+
{
115+
int buildStart = preSeparator + 1;
116+
return new SemanticVersion(
117+
major,
118+
minor,
119+
patch,
120+
semanticVersionString.Substring(preStart, preSeparator - preStart),
121+
semanticVersionString.Substring(buildStart));
122+
}
123+
}
124+
125+
public override bool Equals(object obj)
126+
{
127+
if (obj == null)
128+
{
129+
return false;
130+
}
131+
132+
var other = obj as SemanticVersion;
133+
if (other == null)
134+
{
135+
return false;
136+
}
137+
138+
return this == other;
139+
}
140+
141+
public override int GetHashCode()
142+
{
143+
return Major.GetHashCode() ^
144+
Minor.GetHashCode() ^
145+
Patch.GetHashCode() ^
146+
Pre.GetHashCode() ^
147+
Build.GetHashCode();
148+
}
149+
150+
private static int Compare(SemanticVersion s1, SemanticVersion s2)
151+
{
152+
if (s1.Major != s2.Major)
153+
{
154+
return s1.Major > s2.Major ? 1 : -1;
155+
}
156+
157+
if (s1.Minor != s2.Minor)
158+
{
159+
return s1.Minor > s2.Minor ? 1 : -1;
160+
}
161+
162+
if (s1.Patch != s2.Patch)
163+
{
164+
return s1.Patch > s2.Patch ? 1 : -1;
165+
}
166+
167+
if (string.IsNullOrEmpty(s1.Pre) != string.IsNullOrEmpty(s2.Pre))
168+
{
169+
return string.IsNullOrEmpty(s1.Pre) ? 1 : -1;
170+
}
171+
172+
int preCompare = string.Compare(s1.Pre, s2.Pre);
173+
if (preCompare != 0)
174+
{
175+
return preCompare;
176+
}
177+
178+
return string.Compare(s1.Build, s2.Build);
179+
}
180+
}
181+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Microsoft.DotNet.MSBuildSdkResolver
2+
{
3+
internal static class StringExtensions
4+
{
5+
public static int FindFirstNotOf(this string s, string chars, int startIndex)
6+
{
7+
for (int i = startIndex; i < s.Length; i++)
8+
{
9+
if (chars.IndexOf(s[i]) == -1) return i;
10+
}
11+
12+
return -1;
13+
}
14+
}
15+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
using Xunit;
5+
using Microsoft.DotNet.MSBuildSdkResolver;
6+
using FluentAssertions;
7+
8+
namespace Microsoft.DotNet.Cli.Utils.Tests
9+
{
10+
public class GivenThatWeWantToCompareSemanticVersions
11+
{
12+
[Theory]
13+
[InlineData("2.0.0", "1.0.0")]
14+
[InlineData("1.1.0", "1.0.0")]
15+
[InlineData("1.0.1", "1.0.0")]
16+
[InlineData("1.0.0", "1.0.0-pre")]
17+
[InlineData("1.0.0-pre+2", "1.0.0-pre+1")]
18+
public void OneSemanticVersionIsBiggerThanTheOther(string s1, string s2)
19+
{
20+
var biggerThan = SemanticVersion.Parse(s1) > SemanticVersion.Parse(s2);
21+
var smallerThan = SemanticVersion.Parse(s1) < SemanticVersion.Parse(s2);
22+
biggerThan.Should().BeTrue();
23+
smallerThan.Should().BeFalse();
24+
}
25+
26+
[Theory]
27+
[InlineData("1.0.0", "2.0.0")]
28+
[InlineData("1.0.0", "1.1.0")]
29+
[InlineData("1.0.0", "1.0.1")]
30+
[InlineData("1.0.0-pre", "1.0.0")]
31+
[InlineData("1.0.0-pre+1", "1.0.0-pre+2")]
32+
public void OneSemanticVersionIsSmallerThanTheOther(string s1, string s2)
33+
{
34+
var smallerThan = SemanticVersion.Parse(s1) < SemanticVersion.Parse(s2);
35+
var biggerThan = SemanticVersion.Parse(s1) > SemanticVersion.Parse(s2);
36+
smallerThan.Should().BeTrue();
37+
biggerThan.Should().BeFalse();
38+
}
39+
40+
[Theory]
41+
[InlineData("2.0.0", "1.0.0")]
42+
[InlineData("1.0.0", "1.0.0")]
43+
public void OneSemanticVersionIsBiggerThanOrEqualsTheOther(string s1, string s2)
44+
{
45+
var biggerThanOrEquals = SemanticVersion.Parse(s1) >= SemanticVersion.Parse(s2);
46+
biggerThanOrEquals.Should().BeTrue();
47+
}
48+
49+
[Theory]
50+
[InlineData("1.0.0", "2.0.0")]
51+
[InlineData("1.0.0", "1.0.0")]
52+
public void OneSemanticVersionIsSmallerThanOrEqualsTheOther(string s1, string s2)
53+
{
54+
var smallerThanOrEquals = SemanticVersion.Parse(s1) <= SemanticVersion.Parse(s2);
55+
smallerThanOrEquals.Should().BeTrue();
56+
}
57+
58+
[Theory]
59+
[InlineData("1.2.3", "1.2.3")]
60+
[InlineData("1.2.3-pre", "1.2.3-pre")]
61+
[InlineData("1.2.3-pre+1", "1.2.3-pre+1")]
62+
public void SemanticVersionsCanBeEqual(string s1, string s2)
63+
{
64+
var equals = SemanticVersion.Parse(s1) == SemanticVersion.Parse(s2);
65+
var different = SemanticVersion.Parse(s1) != SemanticVersion.Parse(s2);
66+
equals.Should().BeTrue();
67+
different.Should().BeFalse();
68+
}
69+
70+
[Theory]
71+
[InlineData("1.2.3", "1.2.0")]
72+
[InlineData("1.2.3-pre", "1.2.3-pra")]
73+
[InlineData("1.2.3-pre+1", "1.2.3-pre+2")]
74+
public void SemanticVersionsCanBeDifferent(string s1, string s2)
75+
{
76+
var different = SemanticVersion.Parse(s1) != SemanticVersion.Parse(s2);
77+
var equals = SemanticVersion.Parse(s1) == SemanticVersion.Parse(s2);
78+
different.Should().BeTrue();
79+
equals.Should().BeFalse();
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)