|
// 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
{
public class GetRestoreNuGetAuditSuppressionsTask : Microsoft.Build.Utilities.Task
{
/// <summary>
/// Full path to the msbuild project.
/// </summary>
[Required]
public string ProjectUniqueName { get; set; }
[Required]
public ITaskItem[] NuGetAuditSuppressions { 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 (NuGetAuditSuppressions.Length == 0) return true;
var entries = new List<ITaskItem>(NuGetAuditSuppressions.Length);
var seenIds = new HashSet<string>(NuGetAuditSuppressions.Length, StringComparer.Ordinal);
foreach (var msbuildItem in NuGetAuditSuppressions)
{
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", "NuGetAuditSuppress");
properties.Add("Id", packageId);
if (!string.IsNullOrEmpty(TargetFrameworks))
{
properties.Add("TargetFrameworks", TargetFrameworks);
}
entries.Add(new TaskItem(Guid.NewGuid().ToString(), properties));
}
RestoreGraphItems = entries.ToArray();
return true;
}
}
}
|