From 194f0fabdd61863fa5458f045e23d38348f15b73 Mon Sep 17 00:00:00 2001 From: Jeff Kluge Date: Tue, 22 May 2018 15:25:50 -0700 Subject: [PATCH] Improve BuildOutput to get message event Message as a list of strings Separate messages as strings and messages as events --- .../BuildOutputTests.cs | 12 ++-- src/MSBuildProjectCreator/BuildOutput.cs | 50 ++++++++-------- .../BuildOutputMessageEvents.cs | 59 +++++++++++++++++++ .../BuildOutputMessages.cs | 59 +++++++++++++++++++ version.json | 2 +- 5 files changed, 151 insertions(+), 31 deletions(-) create mode 100644 src/MSBuildProjectCreator/BuildOutputMessageEvents.cs create mode 100644 src/MSBuildProjectCreator/BuildOutputMessages.cs diff --git a/src/MSBuildProjectCreator.UnitTests/BuildOutputTests.cs b/src/MSBuildProjectCreator.UnitTests/BuildOutputTests.cs index ffc053a..d9c98f3 100644 --- a/src/MSBuildProjectCreator.UnitTests/BuildOutputTests.cs +++ b/src/MSBuildProjectCreator.UnitTests/BuildOutputTests.cs @@ -42,7 +42,7 @@ public void Errors(string expectedMessage, string expectedCode) { BuildOutput buildOutput = GetProjectLoggerWithEvents(eventSource => { eventSource.OnErrorRaised(expectedMessage, expectedCode); }); - BuildErrorEventArgs args = buildOutput.Errors.ShouldHaveSingleItem(); + BuildErrorEventArgs args = buildOutput.ErrorEvents.ShouldHaveSingleItem(); args.Message.ShouldBe(expectedMessage); @@ -61,20 +61,20 @@ public void MessagesByImportance(MessageImportance importance) var actualItem = buildOutput.Messages.ShouldHaveSingleItem(); - actualItem.Message.ShouldBe(expectedMessage); + actualItem.ShouldBe(expectedMessage); switch (importance) { case MessageImportance.High: - buildOutput.MessagesHighImportance.ShouldHaveSingleItem().ShouldBe(actualItem); + buildOutput.Messages.High.ShouldHaveSingleItem().ShouldBe(actualItem); break; case MessageImportance.Normal: - buildOutput.MessagesNormalImportance.ShouldHaveSingleItem().ShouldBe(actualItem); + buildOutput.Messages.Normal.ShouldHaveSingleItem().ShouldBe(actualItem); break; case MessageImportance.Low: - buildOutput.MessagesLowImportance.ShouldHaveSingleItem().ShouldBe(actualItem); + buildOutput.Messages.Low.ShouldHaveSingleItem().ShouldBe(actualItem); break; } } @@ -110,7 +110,7 @@ public void Warnings(string expectedMessage, string expectedCode) { BuildOutput buildOutput = GetProjectLoggerWithEvents(eventSource => { eventSource.OnWarningRaised(expectedMessage, expectedCode); }); - BuildWarningEventArgs args = buildOutput.Warnings.ShouldHaveSingleItem(); + BuildWarningEventArgs args = buildOutput.WarningEvents.ShouldHaveSingleItem(); args.Message.ShouldBe(expectedMessage); diff --git a/src/MSBuildProjectCreator/BuildOutput.cs b/src/MSBuildProjectCreator/BuildOutput.cs index 4fdc0f4..1e603d5 100644 --- a/src/MSBuildProjectCreator/BuildOutput.cs +++ b/src/MSBuildProjectCreator/BuildOutput.cs @@ -20,12 +20,12 @@ public sealed class BuildOutput : ILogger, IDisposable /// /// Stores the errors that were logged. /// - private readonly List _errors = new List(); + private readonly List _errorEvents = new List(); /// /// Stores the messages that were logged. /// - private readonly List _messages = new List(50); + private readonly List _messageEvents = new List(50); /// /// Stores the results by project. @@ -35,7 +35,7 @@ public sealed class BuildOutput : ILogger, IDisposable /// /// Stores the warnings that were logged. /// - private readonly List _warnings = new List(); + private readonly List _warningEvents = new List(); /// /// Stores all build events. @@ -49,6 +49,8 @@ public sealed class BuildOutput : ILogger, IDisposable private BuildOutput() { + MessageEvents = new BuildOutputMessageEvents(_messageEvents); + Messages = new BuildOutputMessages(this); } /// @@ -57,29 +59,24 @@ private BuildOutput() public IReadOnlyCollection AllEvents => _allEvents; /// - /// Gets the errors that were logged. + /// Gets the error events that were logged. /// - public IReadOnlyCollection Errors => _errors; + public IReadOnlyCollection ErrorEvents => _errorEvents; /// - /// Gets the messages that were logged. - /// - public IReadOnlyCollection Messages => _messages; - - /// - /// Gets the messages that were logged with . + /// Gets the error messages that were logged. /// - public IReadOnlyCollection MessagesHighImportance => _messages.Where(i => i.Importance == MessageImportance.High).ToList(); + public IReadOnlyCollection Errors => _errorEvents.Select(i => i.Message).ToList(); /// - /// Gets the messages that were logged with . + /// Gets the messages that were logged. /// - public IReadOnlyCollection MessagesLowImportance => _messages.Where(i => i.Importance == MessageImportance.Low).ToList(); + public BuildOutputMessageEvents MessageEvents { get; } /// - /// Gets the messages that were logged with . + /// Gets a object that gets the messages from the build. /// - public IReadOnlyCollection MessagesNormalImportance => _messages.Where(i => i.Importance == MessageImportance.Normal).ToList(); + public BuildOutputMessages Messages { get; } /// public string Parameters { get; set; } @@ -98,9 +95,14 @@ private BuildOutput() public LoggerVerbosity Verbosity { get; set; } /// - /// Gets the warnings that were logged. + /// Gets the warning events that were logged. + /// + public IReadOnlyCollection WarningEvents => _warningEvents; + + /// + /// Gets the warning messages that were logged. /// - public IReadOnlyCollection Warnings => _warnings; + public IReadOnlyCollection Warnings => _warningEvents.Select(i => i.Message).ToList(); /// /// Creates an instance of the class. @@ -115,9 +117,9 @@ public static BuildOutput Create() public void Dispose() { _buildFinished = null; - _errors.Clear(); - _messages.Clear(); - _warnings.Clear(); + _errorEvents.Clear(); + _messageEvents.Clear(); + _warningEvents.Clear(); _allEvents = null; } @@ -212,12 +214,12 @@ private void OnAnyEventRaised(object sender, BuildEventArgs e) private void OnBuildFinished(object sender, BuildFinishedEventArgs args) => _buildFinished = args; - private void OnErrorRaised(object sender, BuildErrorEventArgs args) => _errors.Add(args); + private void OnErrorRaised(object sender, BuildErrorEventArgs args) => _errorEvents.Add(args); - private void OnMessageRaised(object sender, BuildMessageEventArgs args) => _messages.Add(args); + private void OnMessageRaised(object sender, BuildMessageEventArgs args) => _messageEvents.Add(args); private void OnProjectFinished(object sender, ProjectFinishedEventArgs e) => _resultsByProject.AddOrUpdate(e.ProjectFile, e.Succeeded, (projectFile, succeeded) => succeeded && e.Succeeded); - private void OnWarningRaised(object sender, BuildWarningEventArgs args) => _warnings.Add(args); + private void OnWarningRaised(object sender, BuildWarningEventArgs args) => _warningEvents.Add(args); } } \ No newline at end of file diff --git a/src/MSBuildProjectCreator/BuildOutputMessageEvents.cs b/src/MSBuildProjectCreator/BuildOutputMessageEvents.cs new file mode 100644 index 0000000..f05cd7c --- /dev/null +++ b/src/MSBuildProjectCreator/BuildOutputMessageEvents.cs @@ -0,0 +1,59 @@ +// Copyright (c) Jeff Kluge. All rights reserved. +// +// Licensed under the MIT license. + +using Microsoft.Build.Framework; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.Build.Utilities.ProjectCreation +{ + /// + /// Represents the that were logged during a build. + /// + public sealed class BuildOutputMessageEvents : IReadOnlyCollection + { + private readonly IReadOnlyCollection _messageEvents; + + /// + /// Initializes a new instance of the class. + /// + /// A containing the logged message events. + internal BuildOutputMessageEvents(IReadOnlyCollection messageEvents) + { + _messageEvents = messageEvents ?? throw new ArgumentNullException(nameof(messageEvents)); + } + + /// + public int Count => _messageEvents.Count; + + /// + /// Gets the that were logged with . + /// + public IReadOnlyCollection High => _messageEvents.Where(i => i.Importance == MessageImportance.High).ToList(); + + /// + /// Gets the that were logged with . + /// + public IReadOnlyCollection Low => _messageEvents.Where(i => i.Importance == MessageImportance.Low).ToList(); + + /// + /// Gets the that were logged with . + /// + public IReadOnlyCollection Normal => _messageEvents.Where(i => i.Importance == MessageImportance.Normal).ToList(); + + /// + public IEnumerator GetEnumerator() + { + return _messageEvents.GetEnumerator(); + } + + /// + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } +} \ No newline at end of file diff --git a/src/MSBuildProjectCreator/BuildOutputMessages.cs b/src/MSBuildProjectCreator/BuildOutputMessages.cs new file mode 100644 index 0000000..71e2253 --- /dev/null +++ b/src/MSBuildProjectCreator/BuildOutputMessages.cs @@ -0,0 +1,59 @@ +// Copyright (c) Jeff Kluge. All rights reserved. +// +// Licensed under the MIT license. + +using Microsoft.Build.Framework; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.Build.Utilities.ProjectCreation +{ + /// + /// Represents the messages that were logged during a build. + /// + public sealed class BuildOutputMessages : IReadOnlyCollection + { + private readonly BuildOutput _buildOutput; + + /// + /// Initializes a new instance of the class. + /// + /// The object that has message events. + internal BuildOutputMessages(BuildOutput buildOutput) + { + _buildOutput = buildOutput ?? throw new ArgumentNullException(nameof(buildOutput)); + } + + /// + public int Count => _buildOutput.MessageEvents.Count; + + /// + /// Gets the messages that were logged with . + /// + public IReadOnlyCollection High => _buildOutput.MessageEvents.High.Select(i => i.Message).ToList(); + + /// + /// Gets the messages that were logged with . + /// + public IReadOnlyCollection Low => _buildOutput.MessageEvents.Low.Select(i => i.Message).ToList(); + + /// + /// Gets the messages that were logged with . + /// + public IReadOnlyCollection Normal => _buildOutput.MessageEvents.Normal.Select(i => i.Message).ToList(); + + /// + public IEnumerator GetEnumerator() + { + return _buildOutput.MessageEvents.Select(i => i.Message).ToList().GetEnumerator(); + } + + /// + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } +} \ No newline at end of file diff --git a/version.json b/version.json index ac6e7c5..9e7df40 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,5 @@ { - "version": "1.0", + "version": "1.1", "assemblyVersion": "1.0", "nugetPackageVersion": { "semVer": 1