Skip to content

Commit

Permalink
Add support for embedding arbitrary files into binlog (#6339)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillOsenkov committed Apr 21, 2021
1 parent b7a88ab commit 23f5b88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Build/Logging/BinaryLogger/BinaryLogger.cs
Expand Up @@ -164,13 +164,26 @@ public void Initialize(IEventSource eventSource)
binaryWriter = new BinaryWriter(stream);
eventArgsWriter = new BuildEventArgsWriter(binaryWriter);

if (projectImportsCollector != null)
{
eventArgsWriter.EmbedFile += EventArgsWriter_EmbedFile;
}

binaryWriter.Write(FileFormatVersion);

LogInitialInfo();

eventSource.AnyEventRaised += EventSource_AnyEventRaised;
}

private void EventArgsWriter_EmbedFile(string filePath)
{
if (projectImportsCollector != null)
{
projectImportsCollector.AddFile(filePath);
}
}

private void LogInitialInfo()
{
LogMessage("BinLogFilePath=" + FilePath);
Expand Down
31 changes: 31 additions & 0 deletions src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs
Expand Up @@ -105,6 +105,11 @@ internal class BuildEventArgsWriter
/// </summary>
private readonly List<KeyValuePair<int, int>> nameValueIndexListBuffer = new List<KeyValuePair<int, int>>(1024);

/// <summary>
/// Raised when an item is encountered with a hint to embed a file into the binlog.
/// </summary>
public event Action<string> EmbedFile;

/// <summary>
/// Initializes a new instance of BuildEventArgsWriter with a BinaryWriter
/// </summary>
Expand Down Expand Up @@ -788,6 +793,7 @@ private void WriteProjectItems(IEnumerable items)
{
WriteDeduplicatedString(itemType);
WriteTaskItemList(itemList);
CheckForFilesToEmbed(itemType, itemList);
});

// signal the end
Expand All @@ -800,6 +806,7 @@ private void WriteProjectItems(IEnumerable items)
{
WriteDeduplicatedString(itemType);
WriteTaskItemList(itemList);
CheckForFilesToEmbed(itemType, itemList);
});

// signal the end
Expand Down Expand Up @@ -827,6 +834,7 @@ private void WriteProjectItems(IEnumerable items)
{
WriteDeduplicatedString(currentItemType);
WriteTaskItemList(reusableProjectItemList);
CheckForFilesToEmbed(currentItemType, reusableProjectItemList);
reusableProjectItemList.Clear();
}
Expand All @@ -839,6 +847,7 @@ private void WriteProjectItems(IEnumerable items)
{
WriteDeduplicatedString(currentItemType);
WriteTaskItemList(reusableProjectItemList);
CheckForFilesToEmbed(currentItemType, reusableProjectItemList);
reusableProjectItemList.Clear();
}

Expand All @@ -847,6 +856,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);
Expand Down
5 changes: 5 additions & 0 deletions src/Shared/Constants.cs
Expand Up @@ -141,6 +141,11 @@ internal static class ItemTypeNames
/// Declares a project cache plugin and its configuration.
/// </summary>
internal const string ProjectCachePlugin = nameof(ProjectCachePlugin);

/// <summary>
/// Embed specified files in the binary log
/// </summary>
internal const string EmbedInBinlog = nameof(EmbedInBinlog);
}

/// <summary>
Expand Down

0 comments on commit 23f5b88

Please sign in to comment.