Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH703: Add FileLogger switches to MSBuild Runner #1215

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,69 @@ public void Should_Append_Logger_To_Process_Arguments()
Assert.Equal("/v:normal /target:Build /logger:B,A;C /logger:E,D /logger:F " +
"\"/Working/src/Solution.sln\"", result.Args);
}

[Fact]
public void Should_Append_FileLogger_To_Process_Arguments()
{
// Given
var fixture = new MSBuildRunnerFixture(false);
fixture.Settings.AddFileLogger(new MSBuildFileLogger { AppendToLogFile = false, Encoding = "E", HideVerboseItemAndPropertyList = false, LogFile = "A", MSBuildFileLoggerOutput = MSBuildFileLoggerOutput.All, PerformanceSummaryEnabled = false, ShowCommandLine = false, ShowEventId = false, ShowTimestamp = false, SummaryDisabled = false, Verbosity = Verbosity.Diagnostic });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

fixture.Settings.AddFileLogger(new MSBuildFileLogger { AppendToLogFile = true, HideVerboseItemAndPropertyList = true, MSBuildFileLoggerOutput = MSBuildFileLoggerOutput.ErrorsOnly, PerformanceSummaryEnabled = true, ShowCommandLine = true, ShowEventId = true, ShowTimestamp = true, SummaryDisabled = true, Verbosity = Verbosity.Minimal });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

fixture.Settings.AddFileLogger(new MSBuildFileLogger { MSBuildFileLoggerOutput = MSBuildFileLoggerOutput.WarningsOnly, Verbosity = Verbosity.Normal });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

fixture.Settings.AddFileLogger(new MSBuildFileLogger { Verbosity = Verbosity.Quiet });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

fixture.Settings.AddFileLogger(new MSBuildFileLogger { Verbosity = Verbosity.Verbose });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

fixture.Settings.AddFileLogger(new MSBuildFileLogger { });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

fixture.Settings.AddFileLogger();

// When
var result = fixture.Run();
// Then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add line break.

Assert.Equal(@"/v:normal /target:Build /fl /flp:logfile=A;Encoding=E;Verbosity=Diagnostic /fl1 /flp1:Append;PerformanceSummary;NoSummary;ErrorsOnly;NoItemAndPropertyList;ShowCommandLine;ShowTimestamp;ShowEventId;Verbosity=Minimal /fl2 /flp2:WarningsOnly;Verbosity=Normal /fl3 /flp3:Verbosity=Quiet /fl4 /flp4:Verbosity=Verbose /fl5 /fl6 ""/Working/src/Solution.sln""", result.Args);
}

[Fact]
public void Should_Append_Default_FileLogger_To_Process_Arguments()
{
// Given
var fixture = new MSBuildRunnerFixture(false);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

fixture.Settings.AddFileLogger();

// When
var result = fixture.Run();
// Then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add line break.

Assert.Equal(@"/v:normal /target:Build /fl ""/Working/src/Solution.sln""", result.Args);
}

[Fact]
public void Should_Throw_Exception_For_Too_Many_FileLoggers()
{
// Given
var fixture = new MSBuildRunnerFixture(false);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();
fixture.Settings.AddFileLogger();

// When
var ex = Assert.Throws<System.InvalidOperationException>(() => fixture.Run());
// Then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add line break.

Assert.Equal(@"Too Many FileLoggers", ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,43 @@ public void Should_Return_The_Same_Configuration()
Assert.Equal(settings, result);
}
}

public sealed class TheAddFileLoggersMethod
{
[Fact]
public void Should_Add_Logger()
{
// Given
var settings = new MSBuildSettings();
var fileLogger = new MSBuildFileLogger();
var fileLogger2 = new MSBuildFileLogger { LogFile = "A" };

// When
settings.AddFileLogger(fileLogger);
settings.AddFileLogger(fileLogger2);

// Then
var loggers = settings.FileLoggers.ToArray();
Assert.Equal(2, loggers.Length);
Assert.Equal(fileLogger, loggers[0]);
Assert.Equal(fileLogger2, loggers[1]);
Assert.Equal("A", loggers[1].LogFile);
}

[Fact]
public void Should_Return_The_Same_Configuration()
{
// Given
var settings = new MSBuildSettings();

// When
var result = settings.AddFileLogger(new MSBuildFileLogger());
var result1 = settings.AddFileLogger();

// Then
Assert.Equal(settings, result);
Assert.Equal(settings, result1);
}
}
}
}
13 changes: 13 additions & 0 deletions src/Cake.Common.Tests/Unit/Tools/MSBuild/MSBuildSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,18 @@ public void Should_Be_Empty_By_Default()
Assert.Empty(settings.Loggers);
}
}

public sealed class TheFileLoggersProperty
{
[Fact]
public void Should_Be_Empty_By_Default()
{
// Given
var settings = new MSBuildSettings();

// Then
Assert.Empty(settings.FileLoggers);
}
}
}
}
106 changes: 106 additions & 0 deletions src/Cake.Common/Tools/MSBuild/MSBuildFileLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using Cake.Core.Diagnostics;

namespace Cake.Common.Tools.MSBuild
{
/// <summary>
/// Contains settings for specifying a MSBuild file logger.
/// </summary>
public class MSBuildFileLogger
{
/// <summary>
/// Initializes a new instance of the <see cref="MSBuildFileLogger"/> class.
/// </summary>
public MSBuildFileLogger()
{
}

/// <summary>
/// Gets or sets a value indicating whether PerformanceSummary will Show the time that’s spent in tasks, targets, and projects.
/// </summary>
public bool PerformanceSummaryEnabled { get; set; }

/// <summary>
/// Gets or sets a value indicating whether Summary will Show the error and warning summary at the end.
/// </summary>
public bool SummaryDisabled { get; set; }

/// <summary>
/// Gets or sets show ErrorsOnly, WarningsOnly, or All
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing period.

/// </summary>
public MSBuildFileLoggerOutput MSBuildFileLoggerOutput { get; set; }

/// <summary>
/// Gets or sets a value indicating whether NoItemAndPropertyList will be set to Don't show the list of items and properties that would appear at the start of each project build if the verbosity level is set to diagnostic.
/// </summary>
public bool HideVerboseItemAndPropertyList { get; set; }

/// <summary>
/// Gets or sets a value indicating whether ShowCommandLine. Show TaskCommandLineEvent messages.
/// </summary>
public bool ShowCommandLine { get; set; }

/// <summary>
/// Gets or sets a value indicating whether ShowTimestamp. Show the timestamp as a prefix to any message.
/// </summary>
public bool ShowTimestamp { get; set; }

/// <summary>
/// Gets or sets a value indicating whether ShowEventId. Show the event ID for each started event, finished event, and message.
/// </summary>
public bool ShowEventId { get; set; }

/// <summary>
/// Gets or sets Verbosity. Override the /verbosity setting for this logger.
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

/// specify the following verbosity levels: q[uiet], m[inimal], n[ormal], v[erbose] (detailed), and diag[nostic].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify should begin with capital letter.

/// </summary>
public Verbosity? Verbosity { get; set; }

/// <summary>
/// Gets or sets LogFile. The path to the log file into which the build log is written.
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

/// an empty string will use msbuild.log
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// </summary>
public string LogFile { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the build log is appended to the log file or overwrites it. When true, the build log is appended to the log file.
/// </summary>
public bool AppendToLogFile { get; set; }

/// <summary>
/// Gets or sets Specifies the encoding for the file (for example, UTF-8, Unicode, or ASCII).
/// </summary>
public string Encoding { get; set; }

/// <summary>
/// process the file logger config and return parameters as a string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// </summary>
/// <returns>The parameters separated by semi-colons.</returns>
public string GetParameters()
{
var parameters = new List<string>();
parameters.Add(!string.IsNullOrWhiteSpace(LogFile) ? $"logfile={LogFile}" : null);
parameters.Add(!string.IsNullOrWhiteSpace(Encoding) ? $"Encoding={Encoding}" : null);
parameters.Add(AppendToLogFile ? "Append" : null);
parameters.Add(PerformanceSummaryEnabled ? "PerformanceSummary" : null);
parameters.Add(SummaryDisabled ? "NoSummary" : null);
parameters.Add(MSBuildFileLoggerOutput == MSBuildFileLoggerOutput.ErrorsOnly ? "ErrorsOnly" : null);
parameters.Add(MSBuildFileLoggerOutput == MSBuildFileLoggerOutput.WarningsOnly ? "WarningsOnly" : null);
parameters.Add(HideVerboseItemAndPropertyList ? "NoItemAndPropertyList" : null);
parameters.Add(ShowCommandLine ? "ShowCommandLine" : null);
parameters.Add(ShowTimestamp ? "ShowTimestamp" : null);
parameters.Add(ShowEventId ? "ShowEventId" : null);
parameters.Add(Verbosity != null ? $"Verbosity={Verbosity.Value.ToString()}" : null);

return string.Join(";", parameters.Where(p => p != null));
}
}
}
23 changes: 23 additions & 0 deletions src/Cake.Common/Tools/MSBuild/MSBuildFileLoggerOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Cake.Common.Tools.MSBuild
{
/// <summary>
/// the type of file logger output to generate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// </summary>
public enum MSBuildFileLoggerOutput
{
/// <summary>
/// show errors and warnings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// </summary>
All = 0,

/// <summary>
/// show errors only
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// </summary>
ErrorsOnly = 1,

/// <summary>
/// show warnings only
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// </summary>
WarningsOnly = 2,
}
}
32 changes: 32 additions & 0 deletions src/Cake.Common/Tools/MSBuild/MSBuildRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,44 @@ private ProcessArgumentBuilder GetArguments(FilePath solution, MSBuildSettings s
}
}

// Got any file loggers?
if (settings.FileLoggers.Count > 0)
{
var arguments = settings.FileLoggers.Select((logger, indx) =>
{
return GetLoggerArgument(indx, logger);
});

foreach (var argument in arguments)
{
builder.Append(argument);
}
}

// Add the solution as the last parameter.
builder.AppendQuoted(solution.MakeAbsolute(_environment).FullPath);

return builder;
}

private static string GetLoggerArgument(int indx, MSBuildFileLogger logger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indx should be called index

{
if (indx >= 10)
{
throw new InvalidOperationException("Too Many FileLoggers");
}

var cntr = indx == 0 ? string.Empty : indx.ToString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cntr should be called counter.

var argument = $"/fl{cntr}";

var parameters = logger.GetParameters();
if (!string.IsNullOrWhiteSpace(parameters))
{
argument = $"{argument} /flp{cntr}:{parameters}";
}
return argument;
}

private static string GetLoggerArgument(MSBuildLogger logger)
{
string argument = "/logger:";
Expand Down
7 changes: 7 additions & 0 deletions src/Cake.Common/Tools/MSBuild/MSBuildSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class MSBuildSettings : ToolSettings
private readonly HashSet<string> _targets;
private readonly Dictionary<string, IList<string>> _properties;
private readonly List<MSBuildLogger> _loggers;
private readonly List<MSBuildFileLogger> _fileLoggers;

/// <summary>
/// Gets the targets.
Expand Down Expand Up @@ -81,6 +82,11 @@ public sealed class MSBuildSettings : ToolSettings
/// </summary>
public ICollection<MSBuildLogger> Loggers => _loggers;

/// <summary>
/// Gets the file loggers
/// </summary>
public ICollection<MSBuildFileLogger> FileLoggers => _fileLoggers;

/// <summary>
/// Initializes a new instance of the <see cref="MSBuildSettings"/> class.
/// </summary>
Expand All @@ -89,6 +95,7 @@ public MSBuildSettings()
_targets = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
_properties = new Dictionary<string, IList<string>>(StringComparer.OrdinalIgnoreCase);
_loggers = new List<MSBuildLogger>();
_fileLoggers = new List<MSBuildFileLogger>();

ToolVersion = MSBuildToolVersion.Default;
Configuration = string.Empty;
Expand Down
45 changes: 45 additions & 0 deletions src/Cake.Common/Tools/MSBuild/MSBuildSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,50 @@ public static MSBuildSettings WithLogger(this MSBuildSettings settings, string l
});
return settings;
}

/// <summary>
/// Adds a file logger.
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

/// each file logger will be declared in the order added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// the first file logger will match up to the /fl parameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// the next nine (max) file loggers will match up to the /fl1 through /fl9 respectively
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="fileLoggerParameters">Parameters to be passed to the logger.</param>
/// <returns>The same <see cref="MSBuildSettings"/> instance so that multiple calls can be chained.</returns>
public static MSBuildSettings AddFileLogger(this MSBuildSettings settings, MSBuildFileLogger fileLoggerParameters)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

if (fileLoggerParameters == null)
{
throw new ArgumentNullException(nameof(fileLoggerParameters));
}
settings.FileLoggers.Add(fileLoggerParameters);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add line break.

return settings;
}

/// <summary>
/// Adds a file logger with all the default settings.
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break.

/// each file logger will be declared in the order added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// the first file logger will match up to the /fl parameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// the next nine (max) file loggers will match up to the /fl1 through /fl9 respectively
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence should begin with a capital letter and end with a period.

/// </summary>
/// <param name="settings">The settings.</param>
/// <returns>The same <see cref="MSBuildSettings"/> instance so that multiple calls can be chained.</returns>
public static MSBuildSettings AddFileLogger(this MSBuildSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

settings.FileLoggers.Add(new MSBuildFileLogger());
return settings;
}
}
}