From e344062c82b498ff30165894e053e18788c2f6ca Mon Sep 17 00:00:00 2001 From: Jeff Kluge Date: Tue, 24 May 2022 08:13:46 -0700 Subject: [PATCH 1/3] Set VersionCentrallyManaged when installing a package in Visual Studio --- .../PackageSpecOperations.cs | 41 ++++++++++--- .../PackageSpecOperationsTests.cs | 61 +++++++++++++++++++ 2 files changed, 92 insertions(+), 10 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs index 2470c2c4e83..a70eb1f8f92 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs @@ -26,7 +26,7 @@ public static void AddOrUpdateDependency(PackageSpec spec, PackageDependency dep if (!existing.Any()) { - AddDependency(spec.Dependencies, dependency.Id, range); + AddDependency(spec.Dependencies, dependency.Id, range, spec.RestoreMetadata.CentralPackageVersionsEnabled); } } @@ -52,7 +52,17 @@ public static void AddOrUpdateDependency( foreach (var list in lists) { - AddOrUpdateDependencyInDependencyList(list, dependency.Id, dependency.VersionRange); + AddOrUpdateDependencyInDependencyList(spec, list, dependency.Id, dependency.VersionRange); + } + + if (spec.RestoreMetadata.CentralPackageVersionsEnabled) + { + var centralPackageVersionLists = GetCentralPackageVersions(spec, frameworksToAdd); + + foreach (var centralPackageVersionList in centralPackageVersionLists) + { + centralPackageVersionList[dependency.Id] = new CentralPackageVersion(dependency.Id, dependency.VersionRange); + } } } @@ -117,6 +127,19 @@ private static IEnumerable> GetDependencyLists( } } + private static IEnumerable> GetCentralPackageVersions( + PackageSpec spec, + IEnumerable frameworksToConsider) + { + foreach (var targetFramework in spec.TargetFrameworks) + { + if (frameworksToConsider == null || frameworksToConsider.Contains(targetFramework.FrameworkName)) + { + yield return targetFramework.CentralPackageVersions; + } + } + } + private static List GetExistingDependencies(PackageSpec spec, string packageId) { return GetDependencyLists(spec, frameworksToConsider: null, includeGenericDependencies: true) @@ -126,6 +149,7 @@ private static List GetExistingDependencies(PackageSpec spec, } private static void AddOrUpdateDependencyInDependencyList( + PackageSpec spec, IList list, string packageId, VersionRange range) @@ -142,12 +166,7 @@ private static void AddOrUpdateDependencyInDependencyList( } else { - var dependency = new LibraryDependency - { - LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package) - }; - - list.Add(dependency); + AddDependency(list, packageId, range, spec.RestoreMetadata.CentralPackageVersionsEnabled); } } @@ -155,11 +174,13 @@ private static void AddOrUpdateDependencyInDependencyList( private static void AddDependency( IList list, string packageId, - VersionRange range) + VersionRange range, + bool versionCentrallyManaged) { var dependency = new LibraryDependency { - LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package) + LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package), + VersionCentrallyManaged = versionCentrallyManaged }; list.Add(dependency); diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs index d3c45ba09e2..bf3d28fd004 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs @@ -253,6 +253,67 @@ public void AddOrUpdateDependency_ToSpecificFrameworks_AddsNewDependency() spec.TargetFrameworks[1].Dependencies[0].LibraryRange.VersionRange.MinVersion); } + [Fact] + public void AddOrUpdateDependency_WithCentralPackageManagementEnabled_AddsNewDependency() + { + // Arrange + var packageId = "NuGet.Versioning"; + var oldVersion = new NuGetVersion("1.0.0"); + var newVersion = new NuGetVersion("2.0.0"); + + var frameworkA = new TargetFrameworkInformation + { + FrameworkName = FrameworkConstants.CommonFrameworks.Net45 + }; + + var ld = new LibraryDependency + { + LibraryRange = new LibraryRange(packageId, new VersionRange(oldVersion), LibraryDependencyTarget.Package), + VersionCentrallyManaged = true + }; + + var frameworkB = new TargetFrameworkInformation + { + FrameworkName = FrameworkConstants.CommonFrameworks.NetStandard16, + Dependencies = new List() { ld }, + }; + + frameworkB.CentralPackageVersions[ld.Name] = new CentralPackageVersion(ld.Name, ld.LibraryRange.VersionRange); + + var spec = new PackageSpec(new[] { frameworkA, frameworkB }) + { + RestoreMetadata = new ProjectRestoreMetadata + { + CentralPackageVersionsEnabled = true + } + }; + var identity = new PackageIdentity(packageId, newVersion); + + //Preconditions + Assert.Equal( + oldVersion, + spec.TargetFrameworks[1].Dependencies[0].LibraryRange.VersionRange.MinVersion); + + // Act + PackageSpecOperations.AddOrUpdateDependency( + spec, + identity, + new[] { frameworkB.FrameworkName }); + + // Assert + Assert.Empty(spec.Dependencies); + + Assert.Empty(spec.TargetFrameworks[0].Dependencies); + + Assert.Equal(1, spec.TargetFrameworks[1].Dependencies.Count); + Assert.Equal(identity.Id, spec.TargetFrameworks[1].Dependencies[0].LibraryRange.Name); + Assert.Equal(identity.Version, spec.TargetFrameworks[1].Dependencies[0].LibraryRange.VersionRange.MinVersion); + Assert.True(spec.TargetFrameworks[1].Dependencies[0].VersionCentrallyManaged); + + Assert.True(spec.TargetFrameworks[1].CentralPackageVersions.ContainsKey(identity.Id)); + Assert.Equal(identity.Version, spec.TargetFrameworks[1].CentralPackageVersions[identity.Id].VersionRange.MinVersion); + } + [Fact] public void RemoveDependency_RemovesFromAllFrameworkLists() { From 94aace252f8ffdf3a8bf6b244604a0d3551bfc9a Mon Sep 17 00:00:00 2001 From: Jeff Kluge Date: Fri, 27 May 2022 08:59:11 -0700 Subject: [PATCH 2/3] Update logic to handle null RestoreMetadata and add test --- .../PackageSpecOperations.cs | 30 +++++++-------- .../PackageSpecOperationsTests.cs | 37 ++++++++++++++++++- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs index a70eb1f8f92..22e84595a57 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs @@ -26,7 +26,7 @@ public static void AddOrUpdateDependency(PackageSpec spec, PackageDependency dep if (!existing.Any()) { - AddDependency(spec.Dependencies, dependency.Id, range, spec.RestoreMetadata.CentralPackageVersionsEnabled); + AddDependency(spec, spec.Dependencies, dependency.Id, range); } } @@ -55,14 +55,9 @@ public static void AddOrUpdateDependency( AddOrUpdateDependencyInDependencyList(spec, list, dependency.Id, dependency.VersionRange); } - if (spec.RestoreMetadata.CentralPackageVersionsEnabled) + foreach (IDictionary centralPackageVersionList in GetCentralPackageVersionLists(spec, frameworksToAdd)) { - var centralPackageVersionLists = GetCentralPackageVersions(spec, frameworksToAdd); - - foreach (var centralPackageVersionList in centralPackageVersionLists) - { - centralPackageVersionList[dependency.Id] = new CentralPackageVersion(dependency.Id, dependency.VersionRange); - } + centralPackageVersionList[dependency.Id] = new CentralPackageVersion(dependency.Id, dependency.VersionRange); } } @@ -127,15 +122,18 @@ private static IEnumerable> GetDependencyLists( } } - private static IEnumerable> GetCentralPackageVersions( + private static IEnumerable> GetCentralPackageVersionLists( PackageSpec spec, IEnumerable frameworksToConsider) { - foreach (var targetFramework in spec.TargetFrameworks) + if (spec.RestoreMetadata?.CentralPackageVersionsEnabled ?? false) { - if (frameworksToConsider == null || frameworksToConsider.Contains(targetFramework.FrameworkName)) + foreach (var targetFramework in spec.TargetFrameworks) { - yield return targetFramework.CentralPackageVersions; + if (frameworksToConsider == null || frameworksToConsider.Contains(targetFramework.FrameworkName)) + { + yield return targetFramework.CentralPackageVersions; + } } } } @@ -166,21 +164,21 @@ private static void AddOrUpdateDependencyInDependencyList( } else { - AddDependency(list, packageId, range, spec.RestoreMetadata.CentralPackageVersionsEnabled); + AddDependency(spec, list, packageId, range); } } private static void AddDependency( + PackageSpec spec, IList list, string packageId, - VersionRange range, - bool versionCentrallyManaged) + VersionRange range) { var dependency = new LibraryDependency { LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package), - VersionCentrallyManaged = versionCentrallyManaged + VersionCentrallyManaged = spec.RestoreMetadata?.CentralPackageVersionsEnabled ?? false }; list.Add(dependency); diff --git a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs index bf3d28fd004..a559a2fbe30 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs @@ -254,7 +254,42 @@ public void AddOrUpdateDependency_ToSpecificFrameworks_AddsNewDependency() } [Fact] - public void AddOrUpdateDependency_WithCentralPackageManagementEnabled_AddsNewDependency() + public void AddOrUpdateDependency_WithCentralPackageManagementEnabled_AddsDependency() + { + // Arrange + var packageIdentity = new PackageIdentity("NuGet.Versioning", new NuGetVersion("1.0.0")); + + var targetFrameworkInformation = new TargetFrameworkInformation + { + FrameworkName = FrameworkConstants.CommonFrameworks.Net45 + }; + + var spec = new PackageSpec(new[] { targetFrameworkInformation }) + { + RestoreMetadata = new ProjectRestoreMetadata + { + CentralPackageVersionsEnabled = true + } + }; + + // Act + PackageSpecOperations.AddOrUpdateDependency( + spec, + packageIdentity, + new[] { targetFrameworkInformation.FrameworkName }); + + // Assert + Assert.Equal(1, spec.TargetFrameworks[0].Dependencies.Count); + Assert.Equal(packageIdentity.Id, spec.TargetFrameworks[0].Dependencies[0].LibraryRange.Name); + Assert.Equal(packageIdentity.Version, spec.TargetFrameworks[0].Dependencies[0].LibraryRange.VersionRange.MinVersion); + Assert.True(spec.TargetFrameworks[0].Dependencies[0].VersionCentrallyManaged); + + Assert.True(spec.TargetFrameworks[0].CentralPackageVersions.ContainsKey(packageIdentity.Id)); + Assert.Equal(packageIdentity.Version, spec.TargetFrameworks[0].CentralPackageVersions[packageIdentity.Id].VersionRange.MinVersion); + } + + [Fact] + public void AddOrUpdateDependency_WithCentralPackageManagementEnabled_UpdatesDependency() { // Arrange var packageId = "NuGet.Versioning"; From b4f9e921846ba9c9da50d2858a668a9dd12f4312 Mon Sep 17 00:00:00 2001 From: Jeff Kluge Date: Tue, 14 Jun 2022 15:09:43 -0700 Subject: [PATCH 3/3] Address comments --- .../NuGet.ProjectModel/PackageSpecOperations.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs index 22e84595a57..8de93df3039 100644 --- a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs +++ b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs @@ -26,7 +26,7 @@ public static void AddOrUpdateDependency(PackageSpec spec, PackageDependency dep if (!existing.Any()) { - AddDependency(spec, spec.Dependencies, dependency.Id, range); + AddDependency(spec.Dependencies, dependency.Id, range, spec.RestoreMetadata?.CentralPackageVersionsEnabled ?? false); } } @@ -164,21 +164,21 @@ private static void AddOrUpdateDependencyInDependencyList( } else { - AddDependency(spec, list, packageId, range); + AddDependency(list, packageId, range, spec.RestoreMetadata?.CentralPackageVersionsEnabled ?? false); } } private static void AddDependency( - PackageSpec spec, IList list, string packageId, - VersionRange range) + VersionRange range, + bool centralPackageVersionsEnabled) { var dependency = new LibraryDependency { LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package), - VersionCentrallyManaged = spec.RestoreMetadata?.CentralPackageVersionsEnabled ?? false + VersionCentrallyManaged = centralPackageVersionsEnabled }; list.Add(dependency);