diff --git a/src/StructuredLogger/Analyzers/FileCopyMap.cs b/src/StructuredLogger/Analyzers/FileCopyMap.cs index bed2297f..9e358055 100644 --- a/src/StructuredLogger/Analyzers/FileCopyMap.cs +++ b/src/StructuredLogger/Analyzers/FileCopyMap.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using StructuredLogViewer; namespace Microsoft.Build.Logging.StructuredLogger @@ -225,6 +226,7 @@ public bool TryGetResults(NodeQueryMatcher matcher, IList resultSe if (resultSet.Count == 1) { FoundSingleFileCopy?.Invoke(fileData, resultSet); + TryExplainSingleFileCopy(fileData, resultSet); } } else @@ -238,6 +240,60 @@ public bool TryGetResults(NodeQueryMatcher matcher, IList resultSe return false; } + private void TryExplainSingleFileCopy(FileData fileData, IList resultSet) + { + var fileCopyInfo = fileData.Incoming.FirstOrDefault() ?? fileData.Outgoing.FirstOrDefault(); + var project = fileCopyInfo.Project; + + var filePath = fileData.FilePath; + if (fileData.Incoming.Count == 1) + { + filePath = fileCopyInfo.FileCopyOperation.Source; + } + + var fileName = Path.GetFileName(filePath); + + var build = project.GetRoot() as Build; + if (build == null) + { + return; + } + + var evaluation = build.FindEvaluation(project.EvaluationId); + if (evaluation == null) + { + return; + } + + var itemsFolder = evaluation.FindChild("Items"); + if (itemsFolder == null) + { + return; + } + + FindCopyToOutputDirectoryItem(resultSet, itemsFolder, fileName, "None"); + FindCopyToOutputDirectoryItem(resultSet, itemsFolder, fileName, "Content"); + } + + private static void FindCopyToOutputDirectoryItem(IList resultSet, NamedNode itemsFolder, string fileName, string itemName) + { + var addItem = itemsFolder.FindChild(itemName); + if (addItem != null) + { + foreach (var item in addItem.Children.OfType()) + { + string name = Path.GetFileName(item.Name); + if (fileName.Equals(name, StringComparison.OrdinalIgnoreCase)) + { + if (item.FindChild("CopyToOutputDirectory") is { } metadata && (metadata.Value == "Always" || metadata.Value == "PreserveNewest")) + { + resultSet.Add(new SearchResult(item)); + } + } + } + } + } + private void TryGetFiles(string text, IList resultSet, int maxResults) { var results = new List();