-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split concerns: collect events as-is for further processing later.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Collections.Concurrent; | ||
using Microsoft.Build.Framework; | ||
|
||
namespace Buildalyzer; | ||
|
||
public sealed class BuildEventCollector : IDisposable | ||
{ | ||
public BuildEventCollector(IEventSource eventSource) | ||
{ | ||
EventSource = Guard.NotNull(eventSource); | ||
|
||
EventSource.BuildStarted += BuildStarted; | ||
EventSource.ProjectStarted += ProjectStarted; | ||
EventSource.TaskStarted += TaskStarted; | ||
EventSource.TargetStarted += TargetStarted; | ||
|
||
EventSource.CustomEventRaised += CustomEventRaised; | ||
EventSource.MessageRaised += MessageRaised; | ||
EventSource.ErrorRaised += ErrorRaised; | ||
EventSource.StatusEventRaised += StatusEventRaised; | ||
|
||
EventSource.BuildFinished += BuildFinished; | ||
EventSource.ProjectFinished += ProjectFinished; | ||
EventSource.TaskFinished += TaskFinished; | ||
EventSource.TargetFinished += TargetFinished; | ||
} | ||
|
||
public ImmutableArray<BuildEventArgs> Events => Bag.ToImmutableArray(); | ||
|
||
private readonly IEventSource EventSource; | ||
|
||
private readonly ConcurrentBag<BuildEventArgs> Bag = []; | ||
|
||
private void Add(BuildEventArgs e) => Bag.Add(e); | ||
|
||
private void TaskStarted(object? sender, TaskStartedEventArgs e) => Add(e); | ||
|
||
private void TargetStarted(object? sender, TargetStartedEventArgs e) => Add(e); | ||
|
||
private void TargetFinished(object? sender, TargetFinishedEventArgs e) => Add(e); | ||
|
||
private void ProjectStarted(object? sender, ProjectStartedEventArgs e) => Add(e); | ||
|
||
private void MessageRaised(object? sender, BuildMessageEventArgs e) => Add(e); | ||
|
||
private void StatusEventRaised(object? sender, BuildStatusEventArgs e) => Add(e); | ||
|
||
private void BuildStarted(object? sender, BuildStartedEventArgs e) => Add(e); | ||
|
||
private void ErrorRaised(object? sender, BuildErrorEventArgs e) => Add(e); | ||
|
||
private void CustomEventRaised(object? sender, CustomBuildEventArgs e) => Add(e); | ||
|
||
private void TaskFinished(object? sender, TaskFinishedEventArgs e) => Add(e); | ||
|
||
private void ProjectFinished(object? sender, ProjectFinishedEventArgs e) => Add(e); | ||
|
||
private void BuildFinished(object? sender, BuildFinishedEventArgs e) => Add(e); | ||
|
||
public void Dispose() | ||
{ | ||
if (!Disposed) | ||
{ | ||
EventSource.BuildStarted -= BuildStarted; | ||
EventSource.ProjectStarted -= ProjectStarted; | ||
EventSource.TaskStarted -= TaskStarted; | ||
EventSource.TargetStarted -= TargetStarted; | ||
|
||
EventSource.CustomEventRaised -= CustomEventRaised; | ||
EventSource.MessageRaised -= MessageRaised; | ||
EventSource.ErrorRaised -= ErrorRaised; | ||
EventSource.StatusEventRaised -= StatusEventRaised; | ||
|
||
EventSource.BuildFinished -= BuildFinished; | ||
EventSource.ProjectFinished -= ProjectFinished; | ||
EventSource.TaskFinished -= TaskFinished; | ||
EventSource.TargetFinished -= TargetFinished; | ||
Disposed = true; | ||
} | ||
} | ||
|
||
private bool Disposed; | ||
} |