From 25f9a1bbc7877ef78039c129adea97c6c5f8b30b Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Fri, 10 Feb 2017 14:14:04 -0600 Subject: [PATCH] Fix dotnet new classlib to contain the correct NETStandard.Library version. Fix #5638 --- Microsoft.DotNet.Cli.sln | 8 +++ ...rosoft.DotNet.Cli.DependencyVersions.props | 2 +- .../FrameworkDependencyFile.cs | 15 +++++- .../commands/dotnet-new/NewCommandShim.cs | 2 +- .../dotnet-new.Tests/GivenThatIWantANewApp.cs | 53 ++++++++++++++++--- 5 files changed, 68 insertions(+), 12 deletions(-) diff --git a/Microsoft.DotNet.Cli.sln b/Microsoft.DotNet.Cli.sln index e78a69d66..36fe2a898 100644 --- a/Microsoft.DotNet.Cli.sln +++ b/Microsoft.DotNet.Cli.sln @@ -24,14 +24,22 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestPackages", "TestPackage EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{89905EC4-BC0F-443B-8ADF-691321F10108}" ProjectSection(SolutionItems) = preProject + build\Microsoft.DotNet.Cli.BundledSdks.proj = build\Microsoft.DotNet.Cli.BundledSdks.proj build\Microsoft.DotNet.Cli.BundledSdks.props = build\Microsoft.DotNet.Cli.BundledSdks.props + build\Microsoft.DotNet.Cli.BundledTemplates.proj = build\Microsoft.DotNet.Cli.BundledTemplates.proj + build\Microsoft.DotNet.Cli.BundledTemplates.props = build\Microsoft.DotNet.Cli.BundledTemplates.props build\Microsoft.DotNet.Cli.Compile.targets = build\Microsoft.DotNet.Cli.Compile.targets build\Microsoft.DotNet.Cli.DependencyVersions.props = build\Microsoft.DotNet.Cli.DependencyVersions.props + build\Microsoft.DotNet.Cli.GitCommitInfo.targets = build\Microsoft.DotNet.Cli.GitCommitInfo.targets + build\Microsoft.DotNet.Cli.HostInfo.targets = build\Microsoft.DotNet.Cli.HostInfo.targets + build\Microsoft.DotNet.Cli.InitRepo.props = build\Microsoft.DotNet.Cli.InitRepo.props + build\Microsoft.DotNet.Cli.InitRepo.targets = build\Microsoft.DotNet.Cli.InitRepo.targets build\Microsoft.DotNet.Cli.Monikers.props = build\Microsoft.DotNet.Cli.Monikers.props build\Microsoft.DotNet.Cli.Package.targets = build\Microsoft.DotNet.Cli.Package.targets build\Microsoft.DotNet.Cli.Prepare.targets = build\Microsoft.DotNet.Cli.Prepare.targets build\Microsoft.DotNet.Cli.Publish.targets = build\Microsoft.DotNet.Cli.Publish.targets build\Microsoft.DotNet.Cli.Run.targets = build\Microsoft.DotNet.Cli.Run.targets + build\Microsoft.DotNet.Cli.Signing.proj = build\Microsoft.DotNet.Cli.Signing.proj build\Microsoft.DotNet.Cli.tasks = build\Microsoft.DotNet.Cli.tasks build\Microsoft.DotNet.Cli.Test.targets = build\Microsoft.DotNet.Cli.Test.targets EndProjectSection diff --git a/build/Microsoft.DotNet.Cli.DependencyVersions.props b/build/Microsoft.DotNet.Cli.DependencyVersions.props index c073e263d..4c694753e 100644 --- a/build/Microsoft.DotNet.Cli.DependencyVersions.props +++ b/build/Microsoft.DotNet.Cli.DependencyVersions.props @@ -11,6 +11,6 @@ 15.0.0-preview-20170125-04 1.0.0-beta1-20170202-111 1.0.0-beta1-20170131-110 - 1.0.0-beta1-20170207-114 + 1.0.0-beta1-20170209-117 diff --git a/src/Microsoft.DotNet.Cli.Utils/FrameworkDependencyFile.cs b/src/Microsoft.DotNet.Cli.Utils/FrameworkDependencyFile.cs index 5393df880..202c07e9c 100644 --- a/src/Microsoft.DotNet.Cli.Utils/FrameworkDependencyFile.cs +++ b/src/Microsoft.DotNet.Cli.Utils/FrameworkDependencyFile.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System; using System.IO; using System.Linq; using Microsoft.DotNet.PlatformAbstractions; @@ -16,10 +17,14 @@ namespace Microsoft.DotNet.Cli.Utils internal class FrameworkDependencyFile { private readonly string _depsFilePath; + private readonly Lazy _dependencyContext; + + private DependencyContext DependencyContext => _dependencyContext.Value; public FrameworkDependencyFile() { _depsFilePath = Muxer.GetDataFromAppDomain("FX_DEPS_FILE"); + _dependencyContext = new Lazy(CreateDependencyContext); } public bool SupportsCurrentRuntime() @@ -29,9 +34,15 @@ namespace Microsoft.DotNet.Cli.Utils public bool IsRuntimeSupported(string runtimeIdentifier) { - DependencyContext fxDependencyContext = CreateDependencyContext(); + return DependencyContext.RuntimeGraph.Any(g => g.Runtime == runtimeIdentifier); + } - return fxDependencyContext.RuntimeGraph.Any(g => g.Runtime == runtimeIdentifier); + public string GetNetStandardLibraryVersion() + { + return DependencyContext + .RuntimeLibraries + .FirstOrDefault(l => "netstandard.library".Equals(l.Name, StringComparison.OrdinalIgnoreCase)) + ?.Version; } private DependencyContext CreateDependencyContext() diff --git a/src/dotnet/commands/dotnet-new/NewCommandShim.cs b/src/dotnet/commands/dotnet-new/NewCommandShim.cs index ec3b1ad8e..f45615bd5 100644 --- a/src/dotnet/commands/dotnet-new/NewCommandShim.cs +++ b/src/dotnet/commands/dotnet-new/NewCommandShim.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; -using System.Runtime.CompilerServices; using Microsoft.DotNet.Cli.Utils; using Microsoft.TemplateEngine.Abstractions; using Microsoft.TemplateEngine.Cli; @@ -54,6 +53,7 @@ namespace Microsoft.DotNet.Tools.New { "prefs:language", "C#" }, { "dotnet-cli-version", Product.Version }, { "RuntimeFrameworkVersion", new Muxer().SharedFxVersion }, + { "NetStandardImplicitPackageVersion", new FrameworkDependencyFile().GetNetStandardLibraryVersion() }, }; return new DefaultTemplateEngineHost(HostIdentifier, "v" + Product.Version, CultureInfo.CurrentCulture.Name, preferences, builtIns); diff --git a/test/dotnet-new.Tests/GivenThatIWantANewApp.cs b/test/dotnet-new.Tests/GivenThatIWantANewApp.cs index c1700b239..d81eec7e2 100644 --- a/test/dotnet-new.Tests/GivenThatIWantANewApp.cs +++ b/test/dotnet-new.Tests/GivenThatIWantANewApp.cs @@ -1,10 +1,12 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using Microsoft.DotNet.Tools.Test.Utilities; using System; using System.IO; +using System.Linq; +using System.Xml.Linq; using FluentAssertions; +using Microsoft.DotNet.Tools.Test.Utilities; using Xunit; namespace Microsoft.DotNet.New.Tests @@ -16,13 +18,15 @@ namespace Microsoft.DotNet.New.Tests { var rootPath = TestAssetsManager.CreateTestDirectory().Path; - new TestCommand("dotnet") { WorkingDirectory = rootPath } - .Execute($"new console --debug:ephemeral-hive"); + new NewCommand() + .WithWorkingDirectory(rootPath) + .Execute($"console --debug:ephemeral-hive"); DateTime expectedState = Directory.GetLastWriteTime(rootPath); - var result = new TestCommand("dotnet") { WorkingDirectory = rootPath } - .ExecuteWithCapturedOutput($"new console --debug:ephemeral-hive"); + var result = new NewCommand() + .WithWorkingDirectory(rootPath) + .ExecuteWithCapturedOutput($"console --debug:ephemeral-hive"); DateTime actualState = Directory.GetLastWriteTime(rootPath); @@ -55,14 +59,47 @@ namespace Microsoft.DotNet.New.Tests string projectFolder, string packagesDirectory) { - new TestCommand("dotnet") { WorkingDirectory = projectFolder } - .Execute($"new {projectType} --debug:ephemeral-hive") + new NewCommand() + .WithWorkingDirectory(projectFolder) + .Execute($"{projectType} --debug:ephemeral-hive") .Should().Pass(); new RestoreCommand() .WithWorkingDirectory(projectFolder) - .Execute($"--packages {packagesDirectory} /p:SkipInvalidConfigurations=true") + .Execute($"--packages {packagesDirectory}") .Should().Pass(); } + + [Fact] + public void NewClassLibRestoresCorrectNetStandardLibraryVersion() + { + var rootPath = TestAssetsManager.CreateTestDirectory().Path; + var packagesDirectory = Path.Combine(rootPath, "packages"); + var projectName = "Library"; + var projectFileName = $"{projectName}.csproj"; + + new NewCommand() + .WithWorkingDirectory(rootPath) + .Execute($"classlib --name {projectName} -o .") + .Should().Pass(); + + new RestoreCommand() + .WithWorkingDirectory(rootPath) + .Execute($"--packages {packagesDirectory}") + .Should().Pass(); + + var expectedVersion = XDocument.Load(Path.Combine(rootPath, projectFileName)) + .Elements("Project") + .Elements("PropertyGroup") + .Elements("NetStandardImplicitPackageVersion") + .FirstOrDefault() + ?.Value; + + expectedVersion.Should().NotBeNullOrEmpty("Could not find NetStandardImplicitPackageVersion property in a new classlib."); + + new DirectoryInfo(Path.Combine(packagesDirectory, "netstandard.library")) + .Should().Exist() + .And.HaveDirectory(expectedVersion); + } } }