| File: GetRestorePrunedPackageReferencesTask.cs | Web Access |
| Project: src\nuget-client\src\NuGet.Core\NuGet.Build.Tasks\NuGet.Build.Tasks.csproj (NuGet.Build.Tasks) |
// 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. #nullable disable using System; using System.Collections.Generic; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using Newtonsoft.Json; namespace NuGet.Build.Tasks { [MSBuildMultiThreadableTask] public class GetRestorePrunePackageReferencesTask : Task { /// <summary> /// Full path to the msbuild project. /// </summary> [Required] public string ProjectUniqueName { get; set; } [Required] public ITaskItem[] PrunePackageReferences { get; set; } /// <summary> /// Target frameworks to apply this for. If empty this applies to all. /// </summary> public string TargetFrameworks { get; set; } /// <summary> /// Output items /// </summary> [Output] public ITaskItem[] RestoreGraphItems { get; set; } public override bool Execute() { if (PrunePackageReferences.Length == 0) return true; var entries = new List<ITaskItem>(PrunePackageReferences.Length); // Important to use Ordinal comparer to allow the deduplication task report duplicate items. var seenIds = new HashSet<string>(PrunePackageReferences.Length, StringComparer.Ordinal); foreach (var msbuildItem in PrunePackageReferences) { var packageId = msbuildItem.ItemSpec; if (string.IsNullOrEmpty(packageId) || !seenIds.Add(packageId)) { // Skip empty or already processed ids continue; } var properties = new Dictionary<string, string>(); properties.Add("ProjectUniqueName", ProjectUniqueName); properties.Add("Type", "PrunePackageReference"); properties.Add("Id", packageId); BuildTasksUtility.CopyPropertyIfExists(msbuildItem, properties, "Version", "VersionRange"); if (!string.IsNullOrEmpty(TargetFrameworks)) { properties.Add("TargetFrameworks", TargetFrameworks); } entries.Add(new TaskItem(Guid.NewGuid().ToString(), properties)); } RestoreGraphItems = entries.ToArray(); return true; } } }