|
// 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 GetRestoreFrameworkReferencesTask : Microsoft.Build.Utilities.Task
{
/// <summary>
/// Full path to the msbuild project.
/// </summary>
[Required]
public string ProjectUniqueName { get; set; }
[Required]
public ITaskItem[] FrameworkReferences { 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()
{
var entries = new List<ITaskItem>();
var seenIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var msbuildItem in FrameworkReferences)
{
var frameworkReference = msbuildItem.ItemSpec;
if (string.IsNullOrEmpty(frameworkReference) || !seenIds.Add(frameworkReference))
{
// Skip empty or already processed ids
continue;
}
var properties = new Dictionary<string, string>();
properties.Add("ProjectUniqueName", ProjectUniqueName);
properties.Add("Type", "FrameworkReference");
properties.Add("Id", frameworkReference);
if (!string.IsNullOrEmpty(TargetFrameworks))
{
properties.Add("TargetFrameworks", TargetFrameworks);
}
BuildTasksUtility.CopyPropertyIfExists(msbuildItem, properties, "PrivateAssets");
entries.Add(new TaskItem(Guid.NewGuid().ToString(), properties));
}
RestoreGraphItems = entries.ToArray();
return true;
}
}
}
|