-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathCopyLocalAnalyzer.cs
69 lines (66 loc) · 3.38 KB
/
CopyLocalAnalyzer.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Build.Logging.StructuredLogger
{
/// <summary>
/// Adds the value of Private metadata if present to source items of dependencies output by RAR
/// (I know, I need a longer explanation here)
/// TODO: link to article or blog post explaining this
/// </summary>
public class CopyLocalAnalyzer
{
public static void AnalyzeResolveAssemblyReference(Task rar)
{
var results = rar.FindChild<Folder>(c => c.Name == "Results");
if (results != null)
{
foreach (var reference in results.Children.OfType<Parameter>())
{
if (reference.Name.StartsWith("Dependency ") || reference.Name.StartsWith("Unified Dependency "))
{
bool foundNotCopyLocalBecauseMetadata = false;
var requiredBy = new List<Item>();
foreach (var message in reference.Children.OfType<Item>())
{
string text = message.Text;
if (text.StartsWith("Required by \""))
{
requiredBy.Add(message);
}
else if (text == @"This reference is not ""CopyLocal"" because at least one source item had ""Private"" set to ""false"" and no source items had ""Private"" set to ""true"".")
{
foundNotCopyLocalBecauseMetadata = true;
}
}
if (foundNotCopyLocalBecauseMetadata)
{
var assemblies = rar.FindChild<Folder>("Parameters")?.FindChild<Parameter>("Assemblies");
if (assemblies != null)
{
var dictionary = assemblies.Children.OfType<Item>().ToDictionary(a => a.Text, StringComparer.OrdinalIgnoreCase);
foreach (var sourceItem in requiredBy)
{
int prefixLength = "Required by \"".Length;
string text = sourceItem.Text;
var referenceName = text.Substring(prefixLength, text.Length - prefixLength - 2);
Item foundSourceItem;
if (dictionary.TryGetValue(referenceName, out foundSourceItem))
{
foreach (var metadata in foundSourceItem.Children.OfType<Metadata>())
{
if (metadata.Name == "Private")
{
sourceItem.AddChild(new Metadata() { Name = metadata.Name, Value = metadata.Value });
}
}
}
}
}
}
}
}
}
}
}
}