From ce3e719a0603df3bd4432715536f019b2b27ce69 Mon Sep 17 00:00:00 2001 From: Troy Dai Date: Mon, 11 Jan 2016 22:26:34 -0800 Subject: [PATCH] Update ProjectModel server 1. Rebase on new LibraryExporter 2. Update dependency name to "fx/" for reference assembly 3. Update framework friendly name 4. Fix dependency message regression 5. Update tests --- .../Helpers/LibraryExtensions.cs | 21 ++++++++++++ .../Helpers/NuGetFrameworkExtensions.cs | 2 +- .../InternalModels/ProjectContextSnapshot.cs | 25 +++++++++------ .../Models/DependencyDescription.cs | 29 +++++++---------- .../Compilation/LibraryExport.cs | 10 +----- .../Constants.cs | 4 +++ .../Resolution/FrameworkReferenceResolver.cs | 32 +++++++++++++++++++ .../Utilities/VersionUtility.cs | 7 +++- .../DthStartupTests.cs | 2 +- 9 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 src/Microsoft.DotNet.ProjectModel.Server/Helpers/LibraryExtensions.cs diff --git a/src/Microsoft.DotNet.ProjectModel.Server/Helpers/LibraryExtensions.cs b/src/Microsoft.DotNet.ProjectModel.Server/Helpers/LibraryExtensions.cs new file mode 100644 index 000000000..2c86daade --- /dev/null +++ b/src/Microsoft.DotNet.ProjectModel.Server/Helpers/LibraryExtensions.cs @@ -0,0 +1,21 @@ +// 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.ProjectModel.Graph; + +namespace Microsoft.DotNet.ProjectModel.Server.Helpers +{ + public static class LibraryExtensions + { + public static string GetUniqueName(this LibraryDescription library) + { + var identity = library.Identity; + return identity.Type != LibraryType.ReferenceAssembly ? identity.Name : $"fx/{identity.Name}"; + } + + public static string GetUniqueName(this LibraryRange range) + { + return range.Target != LibraryType.ReferenceAssembly ? range.Name : $"fx/{range.Name}"; + } + } +} diff --git a/src/Microsoft.DotNet.ProjectModel.Server/Helpers/NuGetFrameworkExtensions.cs b/src/Microsoft.DotNet.ProjectModel.Server/Helpers/NuGetFrameworkExtensions.cs index 279f7c970..5bc8c1d53 100644 --- a/src/Microsoft.DotNet.ProjectModel.Server/Helpers/NuGetFrameworkExtensions.cs +++ b/src/Microsoft.DotNet.ProjectModel.Server/Helpers/NuGetFrameworkExtensions.cs @@ -14,7 +14,7 @@ namespace Microsoft.DotNet.ProjectModel.Server.Models { ShortName = framework.GetShortFolderName(), FrameworkName = framework.DotNetFrameworkName, - FriendlyName = framework.Framework, + FriendlyName = FrameworkReferenceResolver.Default.GetFriendlyFrameworkName(framework), RedistListPath = FrameworkReferenceResolver.Default.GetFrameworkRedistListPath(framework) }; } diff --git a/src/Microsoft.DotNet.ProjectModel.Server/InternalModels/ProjectContextSnapshot.cs b/src/Microsoft.DotNet.ProjectModel.Server/InternalModels/ProjectContextSnapshot.cs index 1bb6fe760..ff26a5301 100644 --- a/src/Microsoft.DotNet.ProjectModel.Server/InternalModels/ProjectContextSnapshot.cs +++ b/src/Microsoft.DotNet.ProjectModel.Server/InternalModels/ProjectContextSnapshot.cs @@ -11,7 +11,7 @@ using NuGet.Frameworks; namespace Microsoft.DotNet.ProjectModel.Server { internal class ProjectContextSnapshot - { + { public string RootDependency { get; set; } public NuGetFramework TargetFramework { get; set; } public IReadOnlyList SourceFiles { get; set; } @@ -24,31 +24,36 @@ namespace Microsoft.DotNet.ProjectModel.Server public static ProjectContextSnapshot Create(ProjectContext context, string configuration, IEnumerable currentSearchPaths) { var snapshot = new ProjectContextSnapshot(); - + var allDependencyDiagnostics = new List(); allDependencyDiagnostics.AddRange(context.LibraryManager.GetAllDiagnostics()); allDependencyDiagnostics.AddRange(DependencyTypeChangeFinder.Diagnose(context, currentSearchPaths)); var diagnosticsLookup = allDependencyDiagnostics.ToLookup(d => d.Source); + var allExports = context.CreateExporter(configuration) + .GetAllExports() + .ToDictionary(export => export.Library.GetUniqueName()); var allSourceFiles = new List(context.ProjectFile.Files.SourceFiles); var allFileReferences = new List(); var allProjectReferences = new List(); var allDependencies = new Dictionary(); - - foreach (var export in context.CreateExporter(configuration).GetDependencies()) + + // All exports are returned. When the same library name have a ReferenceAssembly type export and a Package type export + // both will be listed as dependencies. Prefix "fx/" will be added to ReferenceAssembly type dependency. + foreach (var pair in allExports) { + var export = pair.Value; + allSourceFiles.AddRange(export.SourceReferences); allFileReferences.AddRange(export.CompilationAssemblies.Select(asset => asset.ResolvedPath)); - var library = export.Library; - var diagnostics = diagnosticsLookup[library].ToList(); - var description = DependencyDescription.Create(library, diagnostics); + var diagnostics = diagnosticsLookup[export.Library].ToList(); + var description = DependencyDescription.Create(export.Library, diagnostics, allExports); allDependencies[description.Name] = description; - var projectDescription = library as ProjectDescription; - - if (projectDescription != null) + var projectDescription = export.Library as ProjectDescription; + if (projectDescription != null && projectDescription.Identity.Name != context.ProjectFile.Name) { allProjectReferences.Add(ProjectReferenceDescription.Create(projectDescription)); } diff --git a/src/Microsoft.DotNet.ProjectModel.Server/Models/DependencyDescription.cs b/src/Microsoft.DotNet.ProjectModel.Server/Models/DependencyDescription.cs index 7ae2cfe65..ae1c11865 100644 --- a/src/Microsoft.DotNet.ProjectModel.Server/Models/DependencyDescription.cs +++ b/src/Microsoft.DotNet.ProjectModel.Server/Models/DependencyDescription.cs @@ -3,7 +3,8 @@ using System.Collections.Generic; using System.Linq; -using Microsoft.DotNet.ProjectModel.Graph; +using Microsoft.DotNet.ProjectModel.Compilation; +using Microsoft.DotNet.ProjectModel.Server.Helpers; namespace Microsoft.DotNet.ProjectModel.Server.Models { @@ -51,20 +52,23 @@ namespace Microsoft.DotNet.ProjectModel.Server.Models return base.GetHashCode(); } - public static DependencyDescription Create(LibraryDescription library, IEnumerable diagnostics) + public static DependencyDescription Create(LibraryDescription library, + List diagnostics, + Dictionary allExports) { + var name = library.GetUniqueName(); return new DependencyDescription { - Name = library.Identity.Name, - DisplayName = GetLibraryDisplayName(library), - Version = library.Identity.Version?.ToString(), + Name = name, + DisplayName = library.Identity.Name, + Version = library.Identity.Version?.ToNormalizedString(), Type = library.Identity.Type.Value, Resolved = library.Resolved, Path = library.Path, Dependencies = library.Dependencies.Select(dependency => new DependencyItem { - Name = dependency.Name, - Version = dependency.VersionRange?.ToString() // TODO: review + Name = dependency.GetUniqueName(), + Version = allExports[dependency.GetUniqueName()].Library.Identity.Version?.ToNormalizedString() }), Errors = diagnostics.Where(d => d.Severity == DiagnosticMessageSeverity.Error) .Select(d => new DiagnosticMessageView(d)), @@ -72,16 +76,5 @@ namespace Microsoft.DotNet.ProjectModel.Server.Models .Select(d => new DiagnosticMessageView(d)) }; } - - private static string GetLibraryDisplayName(LibraryDescription library) - { - var name = library.Identity.Name; - if (library.Identity.Type == LibraryType.ReferenceAssembly && name.StartsWith("fx/")) - { - name = name.Substring(3); - } - - return name; - } } } diff --git a/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExport.cs b/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExport.cs index e5f866f75..69163ca0a 100644 --- a/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExport.cs +++ b/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExport.cs @@ -1,10 +1,8 @@ // 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.Collections.Generic; using System.Diagnostics; -using System.Linq; namespace Microsoft.DotNet.ProjectModel.Compilation { @@ -45,12 +43,6 @@ namespace Microsoft.DotNet.ProjectModel.Compilation NativeLibraries = nativeLibraries; } - private string DebuggerDisplay - { - get - { - return Library.Identity.ToString(); - } - } + private string DebuggerDisplay => Library.Identity.ToString(); } } diff --git a/src/Microsoft.DotNet.ProjectModel/Constants.cs b/src/Microsoft.DotNet.ProjectModel/Constants.cs index e7c25d4c0..92c06fc15 100644 --- a/src/Microsoft.DotNet.ProjectModel/Constants.cs +++ b/src/Microsoft.DotNet.ProjectModel/Constants.cs @@ -1,11 +1,15 @@ // 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; + namespace Microsoft.DotNet.ProjectModel { internal static class Constants { public static readonly string DefaultOutputDirectory = "bin"; public static readonly string DefaultConfiguration = "Debug"; + + public static readonly Version Version50 = new Version(5, 0); } } diff --git a/src/Microsoft.DotNet.ProjectModel/Resolution/FrameworkReferenceResolver.cs b/src/Microsoft.DotNet.ProjectModel/Resolution/FrameworkReferenceResolver.cs index 5377648bd..c29252c11 100644 --- a/src/Microsoft.DotNet.ProjectModel/Resolution/FrameworkReferenceResolver.cs +++ b/src/Microsoft.DotNet.ProjectModel/Resolution/FrameworkReferenceResolver.cs @@ -142,6 +142,38 @@ namespace Microsoft.DotNet.ProjectModel.Resolution return information.RedistListPath; } + + public string GetFriendlyFrameworkName(NuGetFramework targetFramework) + { + var frameworkName = new FrameworkName(targetFramework.DotNetFrameworkName); + + // We don't have a friendly name for this anywhere on the machine so hard code it + if (string.Equals(frameworkName.Identifier, VersionUtility.DnxCoreFrameworkIdentifier, StringComparison.OrdinalIgnoreCase)) + { + return "DNX Core 5.0"; + } + else if (string.Equals(frameworkName.Identifier, VersionUtility.DnxFrameworkIdentifier, StringComparison.OrdinalIgnoreCase)) + { + return "DNX " + targetFramework.Version.ToString(); + } + else if (string.Equals(frameworkName.Identifier, VersionUtility.NetPlatformFrameworkIdentifier, StringComparison.OrdinalIgnoreCase)) + { + var version = targetFramework.Version > Constants.Version50 ? + (" " + targetFramework.Version.ToString()) : + string.Empty; + return ".NET Platform" + version; + } + + var information = _cache.GetOrAdd(targetFramework, GetFrameworkInformation); + + if (information == null) + { + return SynthesizeFrameworkFriendlyName(targetFramework); + } + + return information.Name; + } + private FrameworkInformation GetFrameworkInformation(NuGetFramework targetFramework) { string referenceAssembliesPath = ReferenceAssembliesPath; diff --git a/src/Microsoft.DotNet.ProjectModel/Utilities/VersionUtility.cs b/src/Microsoft.DotNet.ProjectModel/Utilities/VersionUtility.cs index 82e79824f..4755457af 100644 --- a/src/Microsoft.DotNet.ProjectModel/Utilities/VersionUtility.cs +++ b/src/Microsoft.DotNet.ProjectModel/Utilities/VersionUtility.cs @@ -1,7 +1,6 @@ // 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.Runtime.Loader; using System.Text; using NuGet.Versioning; @@ -10,10 +9,16 @@ namespace Microsoft.DotNet.ProjectModel.Utilities { public static class VersionUtility { + public static readonly string DnxCoreFrameworkIdentifier = "DNXCore"; + public static readonly string DnxFrameworkIdentifier = "DNX"; + public static readonly string NetPlatformFrameworkIdentifier = ".NETPlatform"; + public static readonly string NetFrameworkIdentifier = ".NETFramework"; + internal static NuGetVersion GetAssemblyVersion(string path) { return new NuGetVersion(AssemblyLoadContext.GetAssemblyName(path).Version); } + public static string RenderVersion(VersionRange range) { if (range == null) diff --git a/test/Microsoft.DotNet.ProjectModel.Server.Tests/DthStartupTests.cs b/test/Microsoft.DotNet.ProjectModel.Server.Tests/DthStartupTests.cs index d5ddb5be7..62149496f 100644 --- a/test/Microsoft.DotNet.ProjectModel.Server.Tests/DthStartupTests.cs +++ b/test/Microsoft.DotNet.ProjectModel.Server.Tests/DthStartupTests.cs @@ -68,7 +68,7 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests } } - [Theory] + [Fact] public void DthStartup_ProtocolNegotiation_ZeroIsNoAllowed() { using (var server = new DthTestServer(_testHelper.LoggerFactory))