Skip to content

Commit 90f2db9

Browse files
authored
Fix loggers when collecting build output (#45)
Fixes #44
1 parent 5b52034 commit 90f2db9

File tree

5 files changed

+108
-26
lines changed

5 files changed

+108
-26
lines changed

src/MSBuildProjectCreator.UnitTests/BuildTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
//
33
// Licensed under the MIT license.
44

5+
using Microsoft.Build.Evaluation;
56
using Microsoft.Build.Execution;
7+
using Microsoft.Build.Framework;
8+
using Microsoft.Build.Logging;
69
using NuGet.Packaging.Core;
710
using Shouldly;
811
using System.Collections.Generic;
@@ -76,6 +79,47 @@ public void CanRestoreAndBuild()
7679
buildOutput.MessageEvents.High.ShouldContain(i => i.Message == "Building...", buildOutput.GetConsoleLog());
7780
}
7881

82+
[Fact]
83+
public void ProjectCollectionLoggersWork()
84+
{
85+
string binLogPath = Path.Combine(TestRootPath, "test.binlog");
86+
string fileLogPath = Path.Combine(TestRootPath, "test.log");
87+
88+
using (ProjectCollection projectCollection = new ProjectCollection())
89+
{
90+
projectCollection.RegisterLogger(new BinaryLogger
91+
{
92+
Parameters = $"LogFile={binLogPath}",
93+
});
94+
projectCollection.RegisterLogger(new FileLogger
95+
{
96+
Parameters = $"LogFile={fileLogPath}",
97+
Verbosity = LoggerVerbosity.Normal,
98+
ShowSummary = true,
99+
});
100+
101+
ProjectCreator.Templates
102+
.LogsMessage(
103+
text: "$(Property1)",
104+
projectCollection: projectCollection)
105+
.Property("Property1", "2AE492F6EEE04255B31B088051E9AF0F")
106+
.Save(GetTempFileName(".proj"))
107+
.TryBuild(out bool result, out BuildOutput buildOutput);
108+
109+
result.ShouldBeTrue();
110+
111+
buildOutput.MessageEvents.Normal.ShouldContain(i => i.Message == "2AE492F6EEE04255B31B088051E9AF0F", buildOutput.GetConsoleLog());
112+
}
113+
114+
File.Exists(binLogPath).ShouldBeTrue();
115+
116+
File.Exists(fileLogPath).ShouldBeTrue();
117+
118+
string fileLogContents = File.ReadAllText(fileLogPath);
119+
120+
fileLogContents.ShouldContain("2AE492F6EEE04255B31B088051E9AF0F", fileLogContents);
121+
}
122+
79123
[Fact]
80124
public void RestoreTargetCanBeRun()
81125
{

src/MSBuildProjectCreator.UnitTests/ProjectTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void TryGetProjectBuildOutput()
8383
.Import(@"$(MSBuildBinPath)\Microsoft.Common.targets")
8484
.TryGetProject(out Project _, out BuildOutput buildOutput);
8585

86-
buildOutput.WarningEvents.ShouldHaveSingleItem().Code.ShouldBe("MSB4011");
86+
buildOutput.WarningEvents.ShouldHaveSingleItem(buildOutput.GetConsoleLog()).Code.ShouldBe("MSB4011", buildOutput.GetConsoleLog());
8787
}
8888

8989
[Fact]

src/MSBuildProjectCreator/ExtensionMethods.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static IEnumerable<T> AsEnumerable<T>(this T item)
3030
}
3131

3232
/// <summary>
33-
/// Merge's two dictionaries by combining all values and overriding with the first with the second.
33+
/// Merges two dictionaries by combining all values and overriding with the first with the second.
3434
/// </summary>
3535
/// <param name="first">The first dictionary and all of its values to start with.</param>
3636
/// <param name="second">The second dictionary to merge with the first and override its values.</param>
@@ -41,7 +41,7 @@ public static IDictionary<string, string> Merge(this IDictionary<string, string>
4141
}
4242

4343
/// <summary>
44-
/// Merge's two dictionaries by combining all values and overriding with the first with the second.
44+
/// Merges two dictionaries by combining all values and overriding with the first with the second.
4545
/// </summary>
4646
/// <param name="first">The first dictionary and all of its values to start with.</param>
4747
/// <param name="second">The second dictionary to merge with the first and override its values.</param>
@@ -60,5 +60,20 @@ public static IDictionary<string, string> Merge(this IDictionary<string, string>
6060

6161
return result;
6262
}
63+
64+
/// <summary>
65+
/// Gets the current object as an array of objects.
66+
/// </summary>
67+
/// <typeparam name="T">The type of the object.</typeparam>
68+
/// <param name="item">The item to make into an array.</param>
69+
/// <returns>An array of T objects.</returns>
70+
public static T[] ToArrayWithSingleElement<T>(this T item)
71+
where T : class
72+
{
73+
return new[]
74+
{
75+
item,
76+
};
77+
}
6378
}
6479
}

src/MSBuildProjectCreator/ProjectCreator.Build.cs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public ProjectCreator TryBuild(bool restore, string target, out bool result)
4141
}
4242
}
4343

44-
lock (BuildManager.DefaultBuildManager)
45-
{
46-
result = Project.Build(target);
47-
}
44+
Build(target.ToArrayWithSingleElement(), out result, out _, out _);
4845

4946
return this;
5047
}
@@ -85,10 +82,7 @@ public ProjectCreator TryBuild(bool restore, string target, out bool result, out
8582
buildOutput = BuildOutput.Create();
8683
}
8784

88-
lock (BuildManager.DefaultBuildManager)
89-
{
90-
result = Project.Build(target, buildOutput.AsEnumerable());
91-
}
85+
Build(target.ToArrayWithSingleElement(), buildOutput, out result, out _);
9286

9387
return this;
9488
}
@@ -121,10 +115,7 @@ public ProjectCreator TryBuild(bool restore, out bool result)
121115
}
122116
}
123117

124-
lock (BuildManager.DefaultBuildManager)
125-
{
126-
result = Project.Build();
127-
}
118+
Build(null, out result, out _, out _);
128119

129120
return this;
130121
}
@@ -163,10 +154,7 @@ public ProjectCreator TryBuild(bool restore, out bool result, out BuildOutput bu
163154
buildOutput = BuildOutput.Create();
164155
}
165156

166-
lock (BuildManager.DefaultBuildManager)
167-
{
168-
result = Project.Build(buildOutput.AsEnumerable());
169-
}
157+
Build(null, buildOutput, out result, out _);
170158

171159
return this;
172160
}
@@ -236,12 +224,7 @@ public ProjectCreator TryBuild(bool restore, string[] targets, out bool result,
236224
buildOutput = BuildOutput.Create();
237225
}
238226

239-
lock (BuildManager.DefaultBuildManager)
240-
{
241-
ProjectInstance projectInstance = Project.CreateProjectInstance();
242-
243-
result = projectInstance.Build(targets, buildOutput.AsEnumerable(), out targetOutputs);
244-
}
227+
Build(targets, buildOutput, out result, out targetOutputs);
245228

246229
return this;
247230
}
@@ -352,5 +335,45 @@ public ProjectCreator TryRestore(out bool result, out BuildOutput buildOutput, o
352335

353336
return this;
354337
}
338+
339+
private void Build(string[] targets, out bool result, out BuildOutput buildOutput, out IDictionary<string, TargetResult> targetOutputs)
340+
{
341+
buildOutput = BuildOutput.Create();
342+
343+
Build(targets, buildOutput, out result, out targetOutputs);
344+
}
345+
346+
private void Build(string[] targets, BuildOutput buildOutput, out bool result, out IDictionary<string, TargetResult> targetOutputs)
347+
{
348+
lock (BuildManager.DefaultBuildManager)
349+
{
350+
BuildRequestData restoreRequest = new BuildRequestData(
351+
ProjectInstance,
352+
targetsToBuild: targets ?? ProjectInstance.DefaultTargets.ToArray(),
353+
hostServices: null,
354+
flags: BuildRequestDataFlags.ReplaceExistingProjectInstance);
355+
356+
BuildParameters buildParameters = new BuildParameters
357+
{
358+
Loggers = new List<Framework.ILogger>(ProjectCollection.Loggers.Concat(buildOutput.AsEnumerable())),
359+
};
360+
361+
BuildManager.DefaultBuildManager.BeginBuild(buildParameters);
362+
try
363+
{
364+
BuildSubmission buildSubmission = BuildManager.DefaultBuildManager.PendBuildRequest(restoreRequest);
365+
366+
BuildResult buildResult = buildSubmission.Execute();
367+
368+
result = buildResult.OverallResult == BuildResultCode.Success;
369+
370+
targetOutputs = buildResult.ResultsByTarget;
371+
}
372+
finally
373+
{
374+
BuildManager.DefaultBuildManager.EndBuild();
375+
}
376+
}
377+
}
355378
}
356379
}

src/MSBuildProjectCreator/ProjectCreator.Project.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public ProjectCreator TryGetProject(
9696
{
9797
buildOutput = BuildOutput.Create();
9898

99-
projectCollection = projectCollection ?? ProjectCollection ?? new ProjectCollection();
99+
projectCollection = projectCollection ?? new ProjectCollection();
100100

101101
projectCollection.RegisterLogger(buildOutput);
102102

0 commit comments

Comments
 (0)