-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSelectRuntimeIdentifierSpecificItems.cs
More file actions
78 lines (63 loc) · 2.55 KB
/
SelectRuntimeIdentifierSpecificItems.cs
File metadata and controls
78 lines (63 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
using NuGet.RuntimeModel;
namespace Microsoft.NET.Build.Tasks;
/// <summary>
/// MSBuild task that filters a set of Items by matching on compatible RuntimeIdentifier.
/// This task filters an Item list by those items that contain a specific Metadata that is
/// compatible with a specified Runtime Identifier, according to a given RuntimeIdentifierGraph file.
/// </summary>
public class SelectRuntimeIdentifierSpecificItems : TaskBase
{
/// <summary>
/// The target runtime identifier to check compatibility against.
/// </summary>
[Required]
public string TargetRuntimeIdentifier { get; set; } = null!;
/// <summary>
/// The list of candidate items to filter.
/// </summary>
[Required]
public ITaskItem[] Items { get; set; } = null!;
/// <summary>
/// The name of the MSBuild metadata to check on each item. Defaults to "RuntimeIdentifier".
/// </summary>
public string? RuntimeIdentifierItemMetadata { get; set; } = "RuntimeIdentifier";
/// <summary>
/// Path to the RuntimeIdentifierGraph file.
/// </summary>
[Required]
public string RuntimeIdentifierGraphPath { get; set; } = null!;
/// <summary>
/// The filtered items that are compatible with the <see cref="TargetRuntimeIdentifier"/>
/// </summary>
[Output]
public ITaskItem[]? SelectedItems { get; set; }
protected override void ExecuteCore()
{
if (Items.Length == 0)
{
SelectedItems = Array.Empty<ITaskItem>();
return;
}
string ridMetadata = RuntimeIdentifierItemMetadata ?? "RuntimeIdentifier";
RuntimeGraph runtimeGraph = new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeIdentifierGraphPath);
var selectedItems = new List<ITaskItem>();
foreach (var item in Items)
{
string? itemRuntimeIdentifier = item.GetMetadata(ridMetadata);
if (string.IsNullOrEmpty(itemRuntimeIdentifier))
{
// Item doesn't have the runtime identifier metadata, skip it
continue;
}
// Check if the item's runtime identifier is compatible with the target runtime identifier
if (runtimeGraph.AreCompatible(TargetRuntimeIdentifier, itemRuntimeIdentifier))
{
selectedItems.Add(item);
}
}
SelectedItems = selectedItems.ToArray();
}
}