diff --git a/src/Build/Logging/BinaryLogger/BinaryLogger.cs b/src/Build/Logging/BinaryLogger/BinaryLogger.cs index c9515e817f5..e2ae1c1e0f8 100644 --- a/src/Build/Logging/BinaryLogger/BinaryLogger.cs +++ b/src/Build/Logging/BinaryLogger/BinaryLogger.cs @@ -161,6 +161,11 @@ public void Initialize(IEventSource eventSource) binaryWriter = new BinaryWriter(stream); eventArgsWriter = new BuildEventArgsWriter(binaryWriter); + if (projectImportsCollector != null) + { + eventArgsWriter.EmbedFile += EventArgsWriter_EmbedFile; + } + binaryWriter.Write(FileFormatVersion); LogInitialInfo(); @@ -168,6 +173,14 @@ public void Initialize(IEventSource eventSource) eventSource.AnyEventRaised += EventSource_AnyEventRaised; } + private void EventArgsWriter_EmbedFile(string filePath) + { + if (projectImportsCollector != null) + { + projectImportsCollector.AddFile(filePath); + } + } + private void LogInitialInfo() { LogMessage("BinLogFilePath=" + FilePath); diff --git a/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs b/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs index 5774a80a27f..4079c2c2ce0 100644 --- a/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs +++ b/src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs @@ -104,6 +104,11 @@ internal class BuildEventArgsWriter /// private readonly List> nameValueIndexListBuffer = new List>(1024); + /// + /// Raised when an item is encountered with a hint to embed a file into the binlog. + /// + public event Action EmbedFile; + /// /// Initializes a new instance of BuildEventArgsWriter with a BinaryWriter /// @@ -764,6 +769,7 @@ private void WriteProjectItems(IEnumerable items) { WriteDeduplicatedString(itemType); WriteTaskItemList(itemList); + CheckForFilesToEmbed(itemType, itemList); }); // signal the end @@ -776,6 +782,7 @@ private void WriteProjectItems(IEnumerable items) { WriteDeduplicatedString(itemType); WriteTaskItemList(itemList); + CheckForFilesToEmbed(itemType, itemList); }); // signal the end @@ -803,6 +810,7 @@ private void WriteProjectItems(IEnumerable items) { WriteDeduplicatedString(currentItemType); WriteTaskItemList(reusableProjectItemList); + CheckForFilesToEmbed(currentItemType, reusableProjectItemList); reusableProjectItemList.Clear(); } @@ -815,6 +823,7 @@ private void WriteProjectItems(IEnumerable items) { WriteDeduplicatedString(currentItemType); WriteTaskItemList(reusableProjectItemList); + CheckForFilesToEmbed(currentItemType, reusableProjectItemList); reusableProjectItemList.Clear(); } @@ -823,6 +832,28 @@ private void WriteProjectItems(IEnumerable items) } } + private void CheckForFilesToEmbed(string itemType, object itemList) + { + if (EmbedFile == null || + !string.Equals(itemType, ItemTypeNames.EmbedInBinlog, StringComparison.OrdinalIgnoreCase) || + itemList is not IEnumerable list) + { + return; + } + + foreach (var item in list) + { + if (item is ITaskItem taskItem && !string.IsNullOrEmpty(taskItem.ItemSpec)) + { + EmbedFile.Invoke(taskItem.ItemSpec); + } + else if (item is string itemSpec && !string.IsNullOrEmpty(itemSpec)) + { + EmbedFile.Invoke(itemSpec); + } + } + } + private void Write(ITaskItem item, bool writeMetadata = true) { WriteDeduplicatedString(item.ItemSpec); diff --git a/src/Shared/Constants.cs b/src/Shared/Constants.cs index eea2401dca9..e0c4da0540e 100644 --- a/src/Shared/Constants.cs +++ b/src/Shared/Constants.cs @@ -141,6 +141,11 @@ internal static class ItemTypeNames /// Declares a project cache plugin and its configuration. /// internal const string ProjectCachePlugin = nameof(ProjectCachePlugin); + + /// + /// Embed specified files in the binary log + /// + internal const string EmbedInBinlog = nameof(EmbedInBinlog); } ///