From e8b28a4f64a2ef22d7384c898a0f417b8ba25fd0 Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Tue, 15 Oct 2024 18:08:21 +1300 Subject: [PATCH 1/3] Refactor package.config and PackageReference separation for better clarity; add documentation --- ...Sdk.cs => UpdateWorkerTests.PackageReference.cs} | 2 +- .../Updater/BindingRedirectManager.cs | 13 ++++++++++++- ...PackageUpdater.cs => PackageReferenceUpdater.cs} | 13 ++++++++++++- .../Updater/PackagesConfigUpdater.cs | 8 ++++++++ .../NuGetUpdater.Core/Updater/UpdaterWorker.cs | 2 +- 5 files changed, 34 insertions(+), 4 deletions(-) rename nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/{UpdateWorkerTests.Sdk.cs => UpdateWorkerTests.PackageReference.cs} (99%) rename nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/{SdkPackageUpdater.cs => PackageReferenceUpdater.cs} (97%) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.Sdk.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.PackageReference.cs similarity index 99% rename from nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.Sdk.cs rename to nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.PackageReference.cs index 20bc39ebf5..758a532647 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.Sdk.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.PackageReference.cs @@ -10,7 +10,7 @@ namespace NuGetUpdater.Core.Test.Update; public partial class UpdateWorkerTests { - public class Sdk : UpdateWorkerTestBase + public class PackageReference : UpdateWorkerTestBase { [Theory] [InlineData("net472")] diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/BindingRedirectManager.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/BindingRedirectManager.cs index bfc669b9e5..b1ca292a69 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/BindingRedirectManager.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/BindingRedirectManager.cs @@ -18,6 +18,16 @@ internal static class BindingRedirectManager private static readonly XName DependentAssemblyName = AssemblyBinding.GetQualifiedName("dependentAssembly"); private static readonly XName BindingRedirectName = AssemblyBinding.GetQualifiedName("bindingRedirect"); + /// + /// Updates assembly binding redirects for a project build file. + /// + /// + /// Assembly binding redirects are only applicable to projects targeting .NET Framework. + /// .NET Framework is restricted to non-SDK-style project files using either packages.config OR `` MSBuild items. + /// See: https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions + /// https://learn.microsoft.com/en-us/nuget/resources/check-project-format + /// + /// The project build file (*.xproj) to be updated public static async ValueTask UpdateBindingRedirectsAsync(ProjectBuildFile projectBuildFile) { var configFile = await TryGetRuntimeConfigurationFile(projectBuildFile); @@ -33,7 +43,8 @@ public static async ValueTask UpdateBindingRedirectsAsync(ProjectBuildFile proje var bindings = BindingRedirectResolver.GetBindingRedirects(projectBuildFile.Path, references.Select(static x => x.Include)); if (!bindings.Any()) { - // no bindings to update + // no bindings are configured, nothing to update + // TODO: This assumption may not always be correct; A project could have no binding redirects prior to the update, but now requires them after the update. return; } diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/SdkPackageUpdater.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs similarity index 97% rename from nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/SdkPackageUpdater.cs rename to nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs index b3da76eaaa..e5650226a8 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/SdkPackageUpdater.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs @@ -6,7 +6,18 @@ namespace NuGetUpdater.Core; -internal static class SdkPackageUpdater +/// +/// Handles package updates for projects containing `` MSBuild items. +/// +/// +/// PackageReference items can appear in both SDK-style AND non-SDK-style project files. +/// By default, PackageReference is used by [SDK-style] projects targeting .NET Core, .NET Standard, and UWP. +/// By default, packages.config is used by [non-SDK-style] projects targeting .NET Framework; However, they can be migrated to PackageReference too. +/// See: https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#project-type-support +/// https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference +/// https://learn.microsoft.com/en-us/nuget/resources/check-project-format +/// +internal static class PackageReferenceUpdater { public static async Task UpdateDependencyAsync( string repoRootPath, diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs index ae96cf3671..ddaf94e067 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs @@ -13,6 +13,14 @@ namespace NuGetUpdater.Core; +/// +/// Handles package updates for projects that use packages.config. +/// +/// +/// packages.config can only be used in non-SDK-style project files targeting .NET Framework. +/// See: https://learn.microsoft.com/en-us/nuget/reference/packages-config +/// https://learn.microsoft.com/en-us/nuget/resources/check-project-format +/// internal static class PackagesConfigUpdater { public static async Task UpdateDependencyAsync( diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/UpdaterWorker.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/UpdaterWorker.cs index bf1092faf9..5eadf4f475 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/UpdaterWorker.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/UpdaterWorker.cs @@ -221,7 +221,7 @@ private async Task RunUpdaterAsync( } // Some repos use a mix of packages.config and PackageReference - await SdkPackageUpdater.UpdateDependencyAsync(repoRootPath, projectPath, dependencyName, previousDependencyVersion, newDependencyVersion, isTransitive, _logger); + await PackageReferenceUpdater.UpdateDependencyAsync(repoRootPath, projectPath, dependencyName, previousDependencyVersion, newDependencyVersion, isTransitive, _logger); // Update lock file if exists if (File.Exists(Path.Combine(Path.GetDirectoryName(projectPath), "packages.lock.json"))) From c8cd63f66316f9addaaaa8c7f758cc78de28102b Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Tue, 15 Oct 2024 21:09:00 +1300 Subject: [PATCH 2/3] Refactor package.config and PackageReference separation for better clarity; add documentation --- .../NuGetUpdater.Core/Updater/BindingRedirectManager.cs | 5 ++--- .../NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs | 4 ++-- .../NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs | 5 ++--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/BindingRedirectManager.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/BindingRedirectManager.cs index b1ca292a69..be0cf0ed8f 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/BindingRedirectManager.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/BindingRedirectManager.cs @@ -23,7 +23,7 @@ internal static class BindingRedirectManager /// /// /// Assembly binding redirects are only applicable to projects targeting .NET Framework. - /// .NET Framework is restricted to non-SDK-style project files using either packages.config OR `` MSBuild items. + /// .NET Framework targets can appear in SDK-style OR non-SDK-style project files, using either packages.config OR `` MSBuild items. /// See: https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions /// https://learn.microsoft.com/en-us/nuget/resources/check-project-format /// @@ -43,8 +43,7 @@ public static async ValueTask UpdateBindingRedirectsAsync(ProjectBuildFile proje var bindings = BindingRedirectResolver.GetBindingRedirects(projectBuildFile.Path, references.Select(static x => x.Include)); if (!bindings.Any()) { - // no bindings are configured, nothing to update - // TODO: This assumption may not always be correct; A project could have no binding redirects prior to the update, but now requires them after the update. + // no bindings found in the project file, nothing to update return; } diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs index e5650226a8..f73371c7d6 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackageReferenceUpdater.cs @@ -28,8 +28,8 @@ public static async Task UpdateDependencyAsync( bool isTransitive, ILogger logger) { - // SDK-style project, modify the XML directly - logger.Log(" Running for SDK-style project"); + // PackageReference project; modify the XML directly + logger.Log(" Running 'PackageReference' project direct XML update"); (ImmutableArray buildFiles, string[] tfms) = await MSBuildHelper.LoadBuildFilesAndTargetFrameworksAsync(repoRootPath, projectPath); diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs index ddaf94e067..dea1b596cb 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs @@ -33,9 +33,8 @@ public static async Task UpdateDependencyAsync( ILogger logger ) { - logger.Log($" Found {NuGetHelper.PackagesConfigFileName}; running with NuGet.exe"); - - // use NuGet.exe to perform update + // packages.config project; use NuGet.exe to perform update + logger.Log($" Found '{NuGetHelper.PackagesConfigFileName}' project; running NuGet.exe update"); // ensure local packages directory exists var projectBuildFile = ProjectBuildFile.Open(repoRootPath, projectPath); From 2e2c8bd934d1fa07b03bd1c0de6af0e8655ae703 Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Tue, 15 Oct 2024 21:28:16 +1300 Subject: [PATCH 3/3] Refactor package.config and PackageReference separation for better clarity; add documentation --- .../NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs index dea1b596cb..f4d32f8aeb 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/PackagesConfigUpdater.cs @@ -17,7 +17,7 @@ namespace NuGetUpdater.Core; /// Handles package updates for projects that use packages.config. /// /// -/// packages.config can only be used in non-SDK-style project files targeting .NET Framework. +/// packages.config can appear in non-SDK-style projects, but not in SDK-style projects. /// See: https://learn.microsoft.com/en-us/nuget/reference/packages-config /// https://learn.microsoft.com/en-us/nuget/resources/check-project-format ///