diff --git a/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs b/src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs index 2470c2c4e83..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.Dependencies, dependency.Id, range); + AddDependency(spec.Dependencies, dependency.Id, range, spec.RestoreMetadata?.CentralPackageVersionsEnabled ?? false); } } @@ -52,7 +52,12 @@ public static void AddOrUpdateDependency( foreach (var list in lists) { - AddOrUpdateDependencyInDependencyList(list, dependency.Id, dependency.VersionRange); + AddOrUpdateDependencyInDependencyList(spec, list, dependency.Id, dependency.VersionRange); + } + + foreach (IDictionary centralPackageVersionList in GetCentralPackageVersionLists(spec, frameworksToAdd)) + { + centralPackageVersionList[dependency.Id] = new CentralPackageVersion(dependency.Id, dependency.VersionRange); } } @@ -117,6 +122,22 @@ private static IEnumerable> GetDependencyLists( } } + private static IEnumerable> GetCentralPackageVersionLists( + PackageSpec spec, + IEnumerable frameworksToConsider) + { + if (spec.RestoreMetadata?.CentralPackageVersionsEnabled ?? false) + { + 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 +147,7 @@ private static List GetExistingDependencies(PackageSpec spec, } private static void AddOrUpdateDependencyInDependencyList( + PackageSpec spec, IList list, string packageId, VersionRange range) @@ -142,12 +164,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 ?? false); } } @@ -155,11 +172,13 @@ private static void AddOrUpdateDependencyInDependencyList( private static void AddDependency( IList list, string packageId, - VersionRange range) + VersionRange range, + bool centralPackageVersionsEnabled) { var dependency = new LibraryDependency { - LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package) + LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package), + VersionCentrallyManaged = centralPackageVersionsEnabled }; 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..a559a2fbe30 100644 --- a/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs +++ b/test/NuGet.Core.Tests/NuGet.ProjectModel.Test/PackageSpecOperationsTests.cs @@ -253,6 +253,102 @@ public void AddOrUpdateDependency_ToSpecificFrameworks_AddsNewDependency() spec.TargetFrameworks[1].Dependencies[0].LibraryRange.VersionRange.MinVersion); } + [Fact] + 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"; + 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() {