From 92623d89b80945af13cbba930c6844178e435a01 Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Tue, 7 Jan 2025 16:48:43 -0700 Subject: [PATCH] use `break` and `continue` to reduce nesting --- .../Discover/SdkProjectDiscovery.cs | 92 +++++++++++-------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs index 048eaf622f..6f1f64f931 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs @@ -213,48 +213,62 @@ public static async Task> DiscoverWithBin if (experimentsManager.InstallDotnetSdks) { var projectEvaluation = GetNearestProjectEvaluation(target); - if (projectEvaluation is not null) + if (projectEvaluation is null) + { + break; + } + + var removedReferences = target.Children.OfType().FirstOrDefault(r => r.Name == "Reference"); + var addedReferences = target.Children.OfType().FirstOrDefault(r => r.Name == "Reference"); + if (removedReferences is null || addedReferences is null) + { + break; + } + + foreach (var removedAssembly in removedReferences.Children.OfType()) { - var removedReferences = target.Children.OfType().FirstOrDefault(r => r.Name == "Reference"); - var addedReferences = target.Children.OfType().FirstOrDefault(r => r.Name == "Reference"); - if (removedReferences is not null && addedReferences is not null) + var removedPackageName = GetChildMetadataValue(removedAssembly, "NuGetPackageId"); + var removedFileName = Path.GetFileName(removedAssembly.Name); + if (removedPackageName is null || removedFileName is null) { - foreach (var removedAssembly in removedReferences.Children.OfType()) - { - var removedPackageName = GetChildMetadataValue(removedAssembly, "NuGetPackageId"); - var removedFileName = Path.GetFileName(removedAssembly.Name); - if (removedPackageName is not null && removedFileName is not null) - { - var existingProjectPackagesByTfm = packagesPerProject.GetOrAdd(projectEvaluation.ProjectFile, () => new(PathComparer.Instance)); - var existingProjectPackages = existingProjectPackagesByTfm.GetOrAdd(tfm, () => new(StringComparer.OrdinalIgnoreCase)); - if (existingProjectPackages.ContainsKey(removedPackageName)) - { - var correspondingAddedFile = addedReferences.Children.OfType() - .FirstOrDefault(i => removedFileName.Equals(Path.GetFileName(i.Name), StringComparison.OrdinalIgnoreCase)); - if (correspondingAddedFile is not null) - { - var runtimePackageName = GetChildMetadataValue(correspondingAddedFile, "NuGetPackageId"); - var runtimePackageVersion = GetChildMetadataValue(correspondingAddedFile, "NuGetPackageVersion"); - if (runtimePackageName is not null && - runtimePackageVersion is not null && - SemVersion.TryParse(runtimePackageVersion, out var parsedRuntimePackageVersion)) - { - var packageMapper = DotNetPackageCorrelationManager.GetPackageMapper(); - var replacementPackageVersion = packageMapper.GetPackageVersionThatShippedWithOtherPackage(runtimePackageName, parsedRuntimePackageVersion, removedPackageName); - if (replacementPackageVersion is not null) - { - var packagesPerThisProject = packagesReplacedBySdkPerProject.GetOrAdd(projectEvaluation.ProjectFile, () => new(PathComparer.Instance)); - var packagesPerTfm = packagesPerThisProject.GetOrAdd(tfm, () => new(StringComparer.OrdinalIgnoreCase)); - packagesPerTfm[removedPackageName] = replacementPackageVersion.ToString(); - var relativeProjectPath = Path.GetRelativePath(repoRootPath, projectEvaluation.ProjectFile).NormalizePathToUnix(); - logger.Info($"Re-added SDK managed package [{removedPackageName}/{replacementPackageVersion}] to project [{relativeProjectPath}]"); - } - } - } - } - } - } + continue; + } + + var existingProjectPackagesByTfm = packagesPerProject.GetOrAdd(projectEvaluation.ProjectFile, () => new(PathComparer.Instance)); + var existingProjectPackages = existingProjectPackagesByTfm.GetOrAdd(tfm, () => new(StringComparer.OrdinalIgnoreCase)); + if (!existingProjectPackages.ContainsKey(removedPackageName)) + { + continue; + } + + var correspondingAddedFile = addedReferences.Children.OfType() + .FirstOrDefault(i => removedFileName.Equals(Path.GetFileName(i.Name), StringComparison.OrdinalIgnoreCase)); + if (correspondingAddedFile is null) + { + continue; + } + + var runtimePackageName = GetChildMetadataValue(correspondingAddedFile, "NuGetPackageId"); + var runtimePackageVersion = GetChildMetadataValue(correspondingAddedFile, "NuGetPackageVersion"); + if (runtimePackageName is null || + runtimePackageVersion is null || + !SemVersion.TryParse(runtimePackageVersion, out var parsedRuntimePackageVersion)) + { + continue; + } + + var packageMapper = DotNetPackageCorrelationManager.GetPackageMapper(); + var replacementPackageVersion = packageMapper.GetPackageVersionThatShippedWithOtherPackage(runtimePackageName, parsedRuntimePackageVersion, removedPackageName); + if (replacementPackageVersion is null) + { + continue; } + + var packagesPerThisProject = packagesReplacedBySdkPerProject.GetOrAdd(projectEvaluation.ProjectFile, () => new(PathComparer.Instance)); + var packagesPerTfm = packagesPerThisProject.GetOrAdd(tfm, () => new(StringComparer.OrdinalIgnoreCase)); + packagesPerTfm[removedPackageName] = replacementPackageVersion.ToString(); + var relativeProjectPath = Path.GetRelativePath(repoRootPath, projectEvaluation.ProjectFile).NormalizePathToUnix(); + logger.Info($"Re-added SDK managed package [{removedPackageName}/{replacementPackageVersion}] to project [{relativeProjectPath}]"); } } break;