Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set CPM metadata during preview restore when installing a package in Visual Studio #4642

Merged
merged 3 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 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, spec.Dependencies, dependency.Id, range);
jeffkl marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -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<string, CentralPackageVersion> centralPackageVersionList in GetCentralPackageVersionLists(spec, frameworksToAdd))
{
centralPackageVersionList[dependency.Id] = new CentralPackageVersion(dependency.Id, dependency.VersionRange);
jeffkl marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

private static IEnumerable<IDictionary<string, CentralPackageVersion>> GetCentralPackageVersionLists(
PackageSpec spec,
IEnumerable<NuGetFramework> 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<LibraryDependency> GetExistingDependencies(PackageSpec spec, string packageId)
{
return GetDependencyLists(spec, frameworksToConsider: null, includeGenericDependencies: true)
Expand All @@ -126,6 +147,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 +164,21 @@ private static void AddOrUpdateDependencyInDependencyList(
}
else
{
var dependency = new LibraryDependency
{
LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package)
};

list.Add(dependency);
AddDependency(spec, list, packageId, range);
}

}

private static void AddDependency(
PackageSpec spec,
jeffkl marked this conversation as resolved.
Show resolved Hide resolved
IList<LibraryDependency> list,
string packageId,
VersionRange range)
{
var dependency = new LibraryDependency
{
LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package)
LibraryRange = new LibraryRange(packageId, range, LibraryDependencyTarget.Package),
VersionCentrallyManaged = spec.RestoreMetadata?.CentralPackageVersionsEnabled ?? false
};

list.Add(dependency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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