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

Refactor CentralPackageManagementFile #4708

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9236,8 +9236,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectA.AddPackageToAllFrameworks(packageX100NullVersion);

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("x", "1.0.0")
.AddPackageVersion("y", "1.0.0");
.SetPackageVersion("x", "1.0.0")
.SetPackageVersion("y", "1.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand All @@ -9255,7 +9255,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
r = Util.RestoreSolution(pathContext);

// Update the cpvm
cpvmFile.UpdatePackageVersion("x", "2.0.0");
cpvmFile.SetPackageVersion("x", "2.0.0");
cpvmFile.Save();

// Expect exit code 1 on this restore
Expand Down Expand Up @@ -9321,8 +9321,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectA.AddPackageToAllFrameworks(packageX100NullVersion);

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("x", "1.0.0")
.AddPackageVersion("y", "1.0.0");
.SetPackageVersion("x", "1.0.0")
.SetPackageVersion("y", "1.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand All @@ -9337,7 +9337,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
Assert.True(File.Exists(projectA.NuGetLockFileOutputPath));

// Update the transitive dependency in cpvm
cpvmFile.UpdatePackageVersion("y", "2.0.0");
cpvmFile.SetPackageVersion("y", "2.0.0");
cpvmFile.Save();

// Expect exit code 1 on this restore
Expand Down Expand Up @@ -9396,8 +9396,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectA.AddPackageToAllFrameworks(packageX100NullVersion);

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("x", "1.0.0")
.AddPackageVersion("y", "1.0.0");
.SetPackageVersion("x", "1.0.0")
.SetPackageVersion("y", "1.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand Down Expand Up @@ -9470,8 +9470,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectA.AddPackageToAllFrameworks(packageX100NullVersion);

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("x", "1.0.0")
.AddPackageVersion("y", "1.0.0");
.SetPackageVersion("x", "1.0.0")
.SetPackageVersion("y", "1.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand Down Expand Up @@ -9544,7 +9544,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectA.AddPackageToAllFrameworks(packageX100NullVersion);

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("x", "1.0.0");
.SetPackageVersion("x", "1.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand All @@ -9559,7 +9559,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
Assert.True(File.Exists(projectA.NuGetLockFileOutputPath));

// Update the cpvm
cpvmFile.AddPackageVersion("y", "1.0.0");
cpvmFile.SetPackageVersion("y", "1.0.0");
cpvmFile.Save();

// Expect exit code 1 on this restore
Expand Down Expand Up @@ -9626,8 +9626,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectA.AddPackageToAllFrameworks(packageX100NullVersion);

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("x", "1.0.0")
.AddPackageVersion("y", "1.0.0");
.SetPackageVersion("x", "1.0.0")
.SetPackageVersion("y", "1.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand All @@ -9642,7 +9642,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
Assert.True(File.Exists(projectA.NuGetLockFileOutputPath));

// Add new package version the cpvm
cpvmFile.AddPackageVersion("random", "1.0.0");
cpvmFile.SetPackageVersion("random", "1.0.0");
cpvmFile.Save();

// the addition should not impact this restore
Expand Down Expand Up @@ -9742,8 +9742,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectC.AddPackageToAllFrameworks(packageX);

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("x", "1.0.0")
.AddPackageVersion("y", "2.0.0");
.SetPackageVersion("x", "1.0.0")
.SetPackageVersion("y", "2.0.0");

solution.Projects.Add(projectA);
solution.Projects.Add(projectB);
Expand Down Expand Up @@ -9924,19 +9924,19 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectA.AddPackageToAllFrameworks(packagesForProject.ToArray());

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("B", version100)
.AddPackageVersion("F", version100)
.AddPackageVersion("G", version100)
.AddPackageVersion("E", version300)
.AddPackageVersion("D", version200)
.AddPackageVersion("M", version200)
.AddPackageVersion("P", version300)
.AddPackageVersion("Z", version300)
.AddPackageVersion("T", version300)
.AddPackageVersion("X", version100)
.AddPackageVersion("U", version100)
.AddPackageVersion("R", version300)
.AddPackageVersion("S", version300);
.SetPackageVersion("B", version100)
.SetPackageVersion("F", version100)
.SetPackageVersion("G", version100)
.SetPackageVersion("E", version300)
.SetPackageVersion("D", version200)
.SetPackageVersion("M", version200)
.SetPackageVersion("P", version300)
.SetPackageVersion("Z", version300)
.SetPackageVersion("T", version300)
.SetPackageVersion("X", version100)
.SetPackageVersion("U", version100)
.SetPackageVersion("R", version300)
.SetPackageVersion("S", version300);

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand Down Expand Up @@ -10103,8 +10103,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
projectA.AddPackageToAllFrameworks(packagesForProject.ToArray());

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("B", "1.0.0")
.AddPackageVersion("C", "2.0.0");
.SetPackageVersion("B", "1.0.0")
.SetPackageVersion("C", "2.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand Down Expand Up @@ -10184,8 +10184,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
});

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("A", "1.0.0")
.AddPackageVersion("B", "1.0.0");
.SetPackageVersion("A", "1.0.0")
.SetPackageVersion("B", "1.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand Down Expand Up @@ -10266,8 +10266,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async(
});

var cpvmFile = CentralPackageVersionsManagementFile.Create(pathContext.SolutionRoot)
.AddPackageVersion("A", "1.0.0")
.AddPackageVersion("B", "1.0.0");
.SetPackageVersion("A", "1.0.0")
.SetPackageVersion("B", "1.0.0");

solution.Projects.Add(projectA);
solution.CentralPackageVersionsManagementFile = cpvmFile;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,98 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using NuGet.Frameworks;
using NuGet.ProjectManagement;

namespace NuGet.Test.Utility
{
/// <summary>
/// Represents a central package management file.
/// </summary>
public class CentralPackageVersionsManagementFile
{
private const string Name = "Directory.Build.props";
private string _path;
private const string DirectoryPackagesProps = "Directory.Packages.props";

private Dictionary<string, string> _packageVersions;
private readonly bool _managePackageVersionsCentrally;

private CentralPackageVersionsManagementFile(string path)
{
_path = path;
_packageVersions = new Dictionary<string, string>();
}
private readonly Dictionary<string, string> _packageVersions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

public static CentralPackageVersionsManagementFile Create(string path)
{
return new CentralPackageVersionsManagementFile(path);
}
private readonly FileInfo _path;

public CentralPackageVersionsManagementFile AddPackageVersion(string packageId, string packageVersion)
private CentralPackageVersionsManagementFile(string directoryPath, bool managePackageVersionsCentrally)
{
_packageVersions.Add(packageId, packageVersion);
return this;
_path = new FileInfo(Path.Combine(directoryPath, DirectoryPackagesProps));
_managePackageVersionsCentrally = managePackageVersionsCentrally;
}

public CentralPackageVersionsManagementFile UpdatePackageVersion(string packageId, string packageVersion)
/// <summary>
/// Gets the full path of the central package management file.
/// </summary>
public string FullPath => _path.FullName;

/// <summary>
/// Gets a value indicating whether or not there are any unsaved changes to the current central package management file.
/// </summary>
public bool IsDirty { get; private set; }

/// <summary>
/// Creates a new central package management file (Directory.Packages.props) in the specified directory.
/// </summary>
/// <param name="directoryPath">The path to a directory to create the central package management in.</param>
/// <param name="managePackageVersionsCentrally"><c>true</c> to enable central package management (default), or <c>false</c> to disable it.</param>
/// <returns></returns>
public static CentralPackageVersionsManagementFile Create(string directoryPath, bool managePackageVersionsCentrally = true)
{
_packageVersions[packageId] = packageVersion;
return this;
return new CentralPackageVersionsManagementFile(directoryPath, managePackageVersionsCentrally);
}

/// <summary>
/// Removes a central package version, you must call <see cref="Save" /> after modifying the package versions.
/// </summary>
/// <param name="packageId">The ID of the package.</param>
/// <returns>The current <see cref="CentralPackageVersionsManagementFile" />.</returns>
public CentralPackageVersionsManagementFile RemovePackageVersion(string packageId)
{
_packageVersions.Remove(packageId);

IsDirty = true;

return this;
}

/// <summary>
/// Saves the current central package management file.
/// </summary>
public void Save()
{
XDocument cpvm = XDocument.Parse(
"<Project>" +
"<PropertyGroup>" +
"<CentralPackageVersionsFileImported>true</CentralPackageVersionsFileImported>" +
"</PropertyGroup>" +
"</Project>");
NuGetFramework framework = null;
foreach (var pv in _packageVersions)
{
ProjectFileUtils.AddItem(cpvm, "PackageVersion", pv.Key, framework: framework, properties: new Dictionary<string, string>(), new Dictionary<string, string>() { ["Version"] = pv.Value });
}

var filePath = Path.Combine(_path, Name);
if (File.Exists(filePath))
{
File.Delete(filePath);
}

File.WriteAllText(filePath, cpvm.ToString());
XDocument cpvm = new XDocument(
new XElement("Project",
new XElement("PropertyGroup",
new XElement(ProjectBuildProperties.ManagePackageVersionsCentrally, new XText(_managePackageVersionsCentrally.ToString()))),
new XElement("ItemGroup", _packageVersions.Select(i => new XElement("PackageVersion", new XAttribute("Include", i.Key), new XAttribute("Version", i.Value))))));

cpvm.Save(_path.FullName);

IsDirty = false;
}

/// <summary>
/// Adds or sets a central package version, you must call <see cref="Save" /> after modifying the package versions.
/// </summary>
/// <param name="packageId">The ID of the package.</param>
/// <param name="packageVersion">The version of the package.</param>
/// <returns>The current <see cref="CentralPackageVersionsManagementFile" />.</returns>
public CentralPackageVersionsManagementFile SetPackageVersion(string packageId, string packageVersion)
{
_packageVersions[packageId] = packageVersion;

IsDirty = true;

return this;
}
}
}