Adding a Minimum VS Defined SDK version to the resolver. If it set to 1.0.4 if the minimum SDK version file is not found.

This commit is contained in:
Livar Cunha 2017-07-05 09:54:46 -07:00
parent 0578b62490
commit 2b15cb43cf
4 changed files with 107 additions and 8 deletions

View file

@ -2,10 +2,10 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// NOTE: Currently, only the NET46 build ships (with Visual Studio/desktop msbuild),
// but the netstandard1.3 adaptation here acts a proof-of-concept for cross-platform
// but the netstandard1.5 adaptation here acts a proof-of-concept for cross-platform
// portability of the underlying hostfxr API and gives us build and test coverage
// on non-Windows machines.
#if NETSTANDARD1_3
#if NETSTANDARD1_5
using System.Runtime.InteropServices;
using System.Text;
@ -34,4 +34,4 @@ namespace Microsoft.DotNet.MSBuildSdkResolver
}
}
#endif // NETSTANDARD1_3
#endif // NETSTANDARD1_5

View file

@ -6,6 +6,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Microsoft.DotNet.MSBuildSdkResolver
{
@ -64,6 +65,7 @@ namespace Microsoft.DotNet.MSBuildSdkResolver
+ " the version specified in global.json."
});
}
string minimumMSBuildVersionString = GetMinimumMSBuildVersion(netcoreSdkDir);
var minimumMSBuildVersion = Version.Parse(minimumMSBuildVersionString);
if (context.MSBuildVersion < minimumMSBuildVersion)
@ -77,6 +79,18 @@ namespace Microsoft.DotNet.MSBuildSdkResolver
+ " version currently available."
});
}
string minimumVSDefinedSDKVersion = GetMinimumVSDefinedSDKVersion();
if (IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, minimumVSDefinedSDKVersion))
{
return factory.IndicateFailure(
new[]
{
$"Version {netcoreSdkVersion} of the .NET Core SDK is smaller than the minimum version"
+ $" {minimumVSDefinedSDKVersion} required by Visual Studio. Check that a recent enough"
+ " .NET Core SDK is installed or increase the version specified in global.json."
});
}
}
string msbuildSdkDir = Path.Combine(msbuildSdksDir, sdkReference.Name, "Sdk");
@ -107,6 +121,22 @@ namespace Microsoft.DotNet.MSBuildSdkResolver
return File.ReadLines(minimumVersionFilePath).First().Trim();
}
private static string GetMinimumVSDefinedSDKVersion()
{
string dotnetMSBuildSdkResolverDirectory =
Path.GetDirectoryName(typeof(DotNetMSBuildSdkResolver).GetTypeInfo().Assembly.Location);
string minimumVSDefinedSdkVersionFilePath =
Path.Combine(dotnetMSBuildSdkResolverDirectory, "minimumVSDefinedSDKVersion");
if (!File.Exists(minimumVSDefinedSdkVersionFilePath))
{
// smallest version that is required by VS 15.3.
return "1.0.4";
}
return File.ReadLines(minimumVSDefinedSdkVersionFilePath).First().Trim();
}
private bool IsNetCoreSDKSmallerThanTheMinimumVersion(string netcoreSdkVersion, string minimumVersion)
{
FXVersion netCoreSdkFXVersion;

View file

@ -3,8 +3,8 @@
<PropertyGroup>
<Version>$(SdkVersion)</Version>
<TargetFrameworks>netstandard1.3;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard1.3</TargetFrameworks>
<TargetFrameworks>netstandard1.5;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard1.5</TargetFrameworks>
<PlatformTarget>AnyCPU</PlatformTarget>
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
<WarningsAsErrors>true</WarningsAsErrors>
@ -20,7 +20,7 @@
<PackageReference Include="Microsoft.NETCore.DotNetHostResolver" Version="$(CLI_SharedFrameworkVersion)" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.5'">
<PackageReference Include="NETStandard.Library" Version="1.6.0" />
</ItemGroup>

View file

@ -13,6 +13,8 @@ using Xunit;
using Xunit.Abstractions;
using System;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAnMSBuildSdkResolver : TestBase
@ -106,11 +108,59 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
}
[Fact]
public void ItReturnsTheVersionIfItIsEqualToTheMinVersion()
public void ItReturnsNullWhenTheDefaultVSRequiredSDKVersionIsHigherThanTheSDKVersionAvailable()
{
var environment = new TestEnvironment();
var expected =
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "1.0.1");
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
var resolver = environment.CreateResolver();
var result = (MockResult)resolver.Resolve(
new SdkReference("Some.Test.Sdk", null, "1.0.0"),
new MockContext { ProjectFilePath = environment.TestDirectory.FullName },
new MockFactory());
result.Success.Should().BeFalse();
result.Path.Should().BeNull();
result.Version.Should().BeNull();
result.Warnings.Should().BeNullOrEmpty();
result.Errors.Should().Contain($"Version 1.0.1 of the .NET Core SDK is smaller than the minimum version"
+ " 1.0.4 required by Visual Studio. Check that a recent enough"
+ " .NET Core SDK is installed or increase the version specified in global.json.");
}
[Fact]
public void ItReturnsNullWhenTheTheVSRequiredSDKVersionIsHigherThanTheSDKVersionAvailable()
{
var environment = new TestEnvironment();
var expected =
environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "1.0.1");
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
environment.CreateMinimumVSDefinedSDKVersionFile("2.0.0");
var resolver = environment.CreateResolver();
var result = (MockResult)resolver.Resolve(
new SdkReference("Some.Test.Sdk", null, "1.0.0"),
new MockContext { ProjectFilePath = environment.TestDirectory.FullName },
new MockFactory());
result.Success.Should().BeFalse();
result.Path.Should().BeNull();
result.Version.Should().BeNull();
result.Warnings.Should().BeNullOrEmpty();
result.Errors.Should().Contain($"Version 1.0.1 of the .NET Core SDK is smaller than the minimum version"
+ " 2.0.0 required by Visual Studio. Check that a recent enough"
+ " .NET Core SDK is installed or increase the version specified in global.json.");
}
[Fact]
public void ItReturnsTheVersionIfItIsEqualToTheMinVersionAndTheVSDefinedMinVersion()
{
var environment = new TestEnvironment();
var expected = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "99.99.99");
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
environment.CreateMinimumVSDefinedSDKVersionFile("99.99.99");
var resolver = environment.CreateResolver();
var result = (MockResult)resolver.Resolve(
@ -126,11 +176,12 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
}
[Fact]
public void ItReturnsTheVersionIfItIsHigherThanTheMinVersion()
public void ItReturnsTheVersionIfItIsHigherThanTheMinVersionAndTheVSDefinedMinVersion()
{
var environment = new TestEnvironment();
var expected = environment.CreateSdkDirectory(ProgramFiles.X64, "Some.Test.Sdk", "999.99.99");
environment.CreateMuxerAndAddToPath(ProgramFiles.X64);
environment.CreateMinimumVSDefinedSDKVersionFile("999.99.98");
var resolver = environment.CreateResolver();
var result = (MockResult)resolver.Resolve(
@ -167,6 +218,8 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
identifier: identifier,
callingMethod: callingMethod);
DeleteMinimumVSDefinedSDKVersionFile();
PathEnvironmentVariable = string.Empty;
}
@ -248,6 +301,22 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
return null;
}
}
public void CreateMinimumVSDefinedSDKVersionFile(string version)
{
File.WriteAllText(GetMinimumVSDefinedSDKVersionFilePath(), version);
}
private void DeleteMinimumVSDefinedSDKVersionFile()
{
File.Delete(GetMinimumVSDefinedSDKVersionFilePath());
}
private string GetMinimumVSDefinedSDKVersionFilePath()
{
string baseDirectory = AppContext.BaseDirectory;
return Path.Combine(baseDirectory, "minimumVSDefinedSDKVersion");
}
}
private sealed class MockContext : SdkResolverContext