Skip to content

Commit

Permalink
Set VersionCentrallyManaged when installing a package in Visual Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkl committed May 27, 2022
1 parent dbb62f4 commit e344062
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/NuGet.Core/NuGet.ProjectModel/PackageSpecOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -117,6 +127,19 @@ private static IEnumerable<IList<LibraryDependency>> GetDependencyLists(
}
}

private static IEnumerable<IDictionary<string, CentralPackageVersion>> GetCentralPackageVersions(
PackageSpec spec,
IEnumerable<NuGetFramework> frameworksToConsider)
{
foreach (var targetFramework in spec.TargetFrameworks)
{
if (frameworksToConsider == null || frameworksToConsider.Contains(targetFramework.FrameworkName))
{
yield return targetFramework.CentralPackageVersions;
}
}
}

private static List<LibraryDependency> GetExistingDependencies(PackageSpec spec, string packageId)
{
return GetDependencyLists(spec, frameworksToConsider: null, includeGenericDependencies: true)
Expand All @@ -126,6 +149,7 @@ private static List<LibraryDependency> GetExistingDependencies(PackageSpec spec,
}

private static void AddOrUpdateDependencyInDependencyList(
PackageSpec spec,
IList<LibraryDependency> list,
string packageId,
VersionRange range)
Expand All @@ -142,24 +166,21 @@ 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);
}

}

private static void AddDependency(
IList<LibraryDependency> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LibraryDependency>() { 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()
{
Expand Down

0 comments on commit e344062

Please sign in to comment.