Skip to content

Commit a2a3755

Browse files
authored
Add TryBuild() overload that has target outputs (#27)
1 parent a749872 commit a2a3755

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Jeff Kluge. All rights reserved.
2+
//
3+
// Licensed under the MIT license.
4+
5+
using Microsoft.Build.Execution;
6+
using Shouldly;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
10+
using Xunit;
11+
12+
namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
13+
{
14+
public class BuildTests : TestBase
15+
{
16+
[Fact]
17+
public void BuildTargetOutputsTest()
18+
{
19+
ProjectCreator
20+
.Create(Path.Combine(TestRootPath, "project1.proj"))
21+
.Target("Build", returns: "@(MyItems)")
22+
.TargetItemInclude("MyItems", "E32099C7AF4E481885B624E5600C718A")
23+
.TargetItemInclude("MyItems", "7F38E64414104C6182F492B535926187")
24+
.Save()
25+
.TryBuild("Build", out bool result, out BuildOutput _, out IDictionary<string, TargetResult> targetOutputs);
26+
27+
result.ShouldBeTrue();
28+
29+
KeyValuePair<string, TargetResult> item = targetOutputs.ShouldHaveSingleItem();
30+
31+
item.Key.ShouldBe("Build");
32+
33+
item.Value.Items.Select(i => i.ItemSpec).ShouldBe(new[] { "E32099C7AF4E481885B624E5600C718A", "7F38E64414104C6182F492B535926187" });
34+
}
35+
}
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Jeff Kluge. All rights reserved.
2+
//
3+
// Licensed under the MIT license.
4+
5+
using System;
6+
using System.IO;
7+
8+
namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
9+
{
10+
public abstract class TestBase : MSBuildTestBase
11+
{
12+
private readonly string _testRootPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
13+
14+
public string TestRootPath
15+
{
16+
get
17+
{
18+
Directory.CreateDirectory(_testRootPath);
19+
return _testRootPath;
20+
}
21+
}
22+
23+
public void Dispose()
24+
{
25+
Dispose(true);
26+
GC.SuppressFinalize(this);
27+
}
28+
29+
protected virtual void Dispose(bool disposing)
30+
{
31+
if (disposing)
32+
{
33+
if (Directory.Exists(TestRootPath))
34+
{
35+
Directory.Delete(TestRootPath, recursive: true);
36+
}
37+
}
38+
}
39+
40+
protected string GetTempFileName(string extension = null)
41+
{
42+
Directory.CreateDirectory(TestRootPath);
43+
44+
return Path.Combine(TestRootPath, $"{Path.GetRandomFileName()}{extension ?? String.Empty}");
45+
}
46+
}
47+
}

src/MSBuildProjectCreator/ProjectCreator.Build.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Licensed under the MIT license.
44

55
using Microsoft.Build.Execution;
6+
using System.Collections.Generic;
7+
using System.Linq;
68

79
namespace Microsoft.Build.Utilities.ProjectCreation
810
{
@@ -75,5 +77,53 @@ public ProjectCreator TryBuild(out bool result, out BuildOutput buildOutput)
7577

7678
return this;
7779
}
80+
81+
/// <summary>
82+
/// Attempts to build the current project.
83+
/// </summary>
84+
/// <param name="target">The name of the target to build.</param>
85+
/// <param name="result">A value indicating the result of the build.</param>
86+
/// <param name="buildOutput">A <see cref="BuildOutput"/> object that captured the logging from the build.</param>
87+
/// <param name="targetOutputs">A <see cref="IDictionary{String,TargetResult}" /> containing the target outputs.</param>
88+
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
89+
public ProjectCreator TryBuild(string target, out bool result, out BuildOutput buildOutput, out IDictionary<string, TargetResult> targetOutputs)
90+
{
91+
return TryBuild(new[] { target }, out result, out buildOutput, out targetOutputs);
92+
}
93+
94+
/// <summary>
95+
/// Attempts to build the current project.
96+
/// </summary>
97+
/// <param name="targets">The names of the targets to build.</param>
98+
/// <param name="result">A value indicating the result of the build.</param>
99+
/// <param name="buildOutput">A <see cref="BuildOutput"/> object that captured the logging from the build.</param>
100+
/// <param name="targetOutputs">A <see cref="IDictionary{String,TargetResult}" /> containing the target outputs.</param>
101+
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
102+
public ProjectCreator TryBuild(string[] targets, out bool result, out BuildOutput buildOutput, out IDictionary<string, TargetResult> targetOutputs)
103+
{
104+
buildOutput = BuildOutput.Create();
105+
106+
lock (BuildManager.DefaultBuildManager)
107+
{
108+
ProjectInstance projectInstance = Project.CreateProjectInstance();
109+
110+
result = projectInstance.Build(targets, buildOutput.AsEnumerable(), out targetOutputs);
111+
}
112+
113+
return this;
114+
}
115+
116+
/// <summary>
117+
/// Attempts to build the current project.
118+
/// </summary>
119+
/// <param name="targets">The names of the targets to build.</param>
120+
/// <param name="result">A value indicating the result of the build.</param>
121+
/// <param name="buildOutput">A <see cref="BuildOutput"/> object that captured the logging from the build.</param>
122+
/// <param name="targetOutputs">A <see cref="IDictionary{String,TargetResult}" /> containing the target outputs.</param>
123+
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
124+
public ProjectCreator TryBuild(IEnumerable<string> targets, out bool result, out BuildOutput buildOutput, out IDictionary<string, TargetResult> targetOutputs)
125+
{
126+
return TryBuild(targets.ToArray(), out result, out buildOutput, out targetOutputs);
127+
}
78128
}
79129
}

0 commit comments

Comments
 (0)