Skip to content

Commit e04a311

Browse files
committed
Use lock on BuildManager for TryBuild(), add TryGetPropertyValue(), and add TryGetItems() (#14)
* Use lock on BuildManager for TryBuild * Add TryGetPropertyValue() and TryGetItems()
1 parent 8dc65f5 commit e04a311

File tree

6 files changed

+147
-5
lines changed

6 files changed

+147
-5
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ build_script:
1111
1212
MSBuild MSBuildProjectCreator.sln /BinaryLogger /Logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll"
1313
artifacts:
14-
- path: '**\*.nupkg'
14+
- path: 'src\**\*.nupkg'
1515
name: NuGet Packages
1616
on_failure:
1717
- ps: Get-ChildItem .\*log -Recurse | % { Push-AppveyorArtifact $_.FullName -FileName $_.Name }

src/MSBuildProjectCreator.UnitTests/ItemTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Build.Evaluation;
66
using Shouldly;
77
using System.Collections.Generic;
8+
using System.Linq;
89
using Xunit;
910

1011
namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
@@ -292,6 +293,54 @@ public void RemoveItem()
292293
StringCompareShould.IgnoreLineEndings);
293294
}
294295

296+
[Fact]
297+
public void TryGetItemsCustomSelector()
298+
{
299+
ProjectCreator.Create(projectFileOptions: NewProjectFileOptions.None)
300+
.ItemInclude("MyItem", "3F114C1509CF4D499A44F986BEBD5707")
301+
.ItemInclude("MyItem", "8A9654A639F5429AB1C7A8F2AE9639D8")
302+
.TryGetItems("MyItem", i => i.EvaluatedInclude, out IReadOnlyCollection<string> items);
303+
304+
items.ShouldBe(new List<string>
305+
{
306+
"3F114C1509CF4D499A44F986BEBD5707",
307+
"8A9654A639F5429AB1C7A8F2AE9639D8"
308+
});
309+
}
310+
311+
[Fact]
312+
public void TryGetItemsDictionary()
313+
{
314+
ProjectCreator.Create(projectFileOptions: NewProjectFileOptions.None)
315+
.ItemInclude("MyItem", "A9AA54F5E51E4C0A966A5F5CDBD5EC9D", metadata: new Dictionary<string, string> { ["MyMetadata"] = "A3A33B3B55A841D883A05F6CA920AC1F" })
316+
.ItemInclude("MyItem", "910E2DA9075043AA850D923E8E3EE398", metadata: new Dictionary<string, string> { ["MyMetadata"] = "EF8E0862B0A946D98DA7082E70AEC3E9" })
317+
.TryGetItems("MyItem", "MyMetadata", out IReadOnlyDictionary<string, string> items);
318+
319+
items.ShouldBe(new Dictionary<string, string>
320+
{
321+
["A9AA54F5E51E4C0A966A5F5CDBD5EC9D"] = "A3A33B3B55A841D883A05F6CA920AC1F",
322+
["910E2DA9075043AA850D923E8E3EE398"] = "EF8E0862B0A946D98DA7082E70AEC3E9"
323+
});
324+
}
325+
326+
[Fact]
327+
public void TryGetItemsProjectItems()
328+
{
329+
ProjectCreator.Create(projectFileOptions: NewProjectFileOptions.None)
330+
.ItemInclude("MyItem", "B39E047FC97A48E3964EEA70C46F4E35")
331+
.ItemInclude("MyItem", "E0FA8DA2551F4EE18D2102149D3049D4")
332+
.TryGetItems("MyItem", out IReadOnlyCollection<ProjectItem> items);
333+
334+
items
335+
.Select(i => i.EvaluatedInclude)
336+
.ToList()
337+
.ShouldBe(new List<string>
338+
{
339+
"B39E047FC97A48E3964EEA70C46F4E35",
340+
"E0FA8DA2551F4EE18D2102149D3049D4"
341+
});
342+
}
343+
295344
[Fact]
296345
public void UpdateItem()
297346
{

src/MSBuildProjectCreator.UnitTests/PropertyTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,18 @@ public void PropertySimple()
9393
</Project>",
9494
StringCompareShould.IgnoreLineEndings);
9595
}
96+
97+
[Fact]
98+
public void TryGetPropertyValueSimple()
99+
{
100+
ProjectCreator.Create(projectFileOptions: NewProjectFileOptions.None)
101+
.Property("E7A39154F5AB476A928067251F88FFCE", "E8F579A7E3374F389120CF6D888E74B9")
102+
.Property("FAB58E5B32D14990ACE2490D7593FDF6", "60F55FB14D2E44B2BA4EC91488D9FF8F")
103+
.TryGetPropertyValue("E7A39154F5AB476A928067251F88FFCE", out string property1)
104+
.TryGetPropertyValue("FAB58E5B32D14990ACE2490D7593FDF6", out string property2);
105+
106+
property1.ShouldBe("E8F579A7E3374F389120CF6D888E74B9");
107+
property2.ShouldBe("60F55FB14D2E44B2BA4EC91488D9FF8F");
108+
}
96109
}
97110
}

src/MSBuildProjectCreator/ProjectCreator.Build.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// Licensed under the MIT license.
44

5+
using Microsoft.Build.Execution;
6+
57
namespace Microsoft.Build.Utilities.ProjectCreation
68
{
79
public partial class ProjectCreator
@@ -14,7 +16,10 @@ public partial class ProjectCreator
1416
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
1517
public ProjectCreator TryBuild(string target, out bool result)
1618
{
17-
result = Project.Build(target);
19+
lock (BuildManager.DefaultBuildManager)
20+
{
21+
result = Project.Build(target);
22+
}
1823

1924
return this;
2025
}
@@ -30,7 +35,10 @@ public ProjectCreator TryBuild(string target, out bool result, out BuildOutput b
3035
{
3136
buildOutput = BuildOutput.Create();
3237

33-
result = Project.Build(target, buildOutput.AsEnumerable());
38+
lock (BuildManager.DefaultBuildManager)
39+
{
40+
result = Project.Build(target, buildOutput.AsEnumerable());
41+
}
3442

3543
return this;
3644
}
@@ -42,7 +50,10 @@ public ProjectCreator TryBuild(string target, out bool result, out BuildOutput b
4250
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
4351
public ProjectCreator TryBuild(out bool result)
4452
{
45-
result = Project.Build();
53+
lock (BuildManager.DefaultBuildManager)
54+
{
55+
result = Project.Build();
56+
}
4657

4758
return this;
4859
}
@@ -57,7 +68,10 @@ public ProjectCreator TryBuild(out bool result, out BuildOutput buildOutput)
5768
{
5869
buildOutput = BuildOutput.Create();
5970

60-
result = Project.Build(buildOutput.AsEnumerable());
71+
lock (BuildManager.DefaultBuildManager)
72+
{
73+
result = Project.Build(buildOutput.AsEnumerable());
74+
}
6175

6276
return this;
6377
}

src/MSBuildProjectCreator/ProjectCreator.Items.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using Microsoft.Build.Construction;
66
using Microsoft.Build.Evaluation;
7+
using System;
78
using System.Collections.Generic;
89
using System.Linq;
910

@@ -314,6 +315,58 @@ public ProjectCreator ItemUpdate(
314315
condition: condition);
315316
}
316317

318+
/// <summary>
319+
/// Gets a list of items in the project.
320+
/// </summary>
321+
/// <param name="itemType">The item type.</param>
322+
/// <param name="items">A <see cref="IReadOnlyCollection{ProjectItem}"/> containing the items.</param>
323+
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
324+
public ProjectCreator TryGetItems(string itemType, out IReadOnlyCollection<ProjectItem> items)
325+
{
326+
items = Project.GetItems(itemType).ToList();
327+
328+
return this;
329+
}
330+
331+
/// <summary>
332+
/// Gets a list of items in the project.
333+
/// </summary>
334+
/// <typeparam name="T">The type of the object that the selector will return.</typeparam>
335+
/// <param name="itemType">The item type.</param>
336+
/// <param name="selector">A <see cref="Func{ProjectItem, T}"/> that gets the desired return object.</param>
337+
/// <param name="items">A <see cref="IReadOnlyCollection{T}"/> containing the items.</param>
338+
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
339+
public ProjectCreator TryGetItems<T>(string itemType, Func<ProjectItem, T> selector, out IReadOnlyCollection<T> items)
340+
{
341+
if (selector == null)
342+
{
343+
throw new ArgumentNullException(nameof(selector));
344+
}
345+
346+
items = Project.GetItems(itemType).Select(selector).ToList();
347+
348+
return this;
349+
}
350+
351+
/// <summary>
352+
/// Gets a list of items in the project.
353+
/// </summary>
354+
/// <param name="itemType">The item type.</param>
355+
/// <param name="metadataName">The name of a metadata item to get.</param>
356+
/// <param name="items">A <see cref="IReadOnlyDictionary{String,String}"/> containing the ItemSpec and metadata value.</param>
357+
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
358+
public ProjectCreator TryGetItems(string itemType, string metadataName, out IReadOnlyDictionary<string, string> items)
359+
{
360+
if (String.IsNullOrWhiteSpace(metadataName))
361+
{
362+
throw new ArgumentNullException(nameof(metadataName));
363+
}
364+
365+
items = Project.GetItems(itemType).ToDictionary(i => i.EvaluatedInclude, i => i.GetMetadataValue(metadataName));
366+
367+
return this;
368+
}
369+
317370
/// <summary>
318371
/// Adds an item to the current item group.
319372
/// </summary>

src/MSBuildProjectCreator/ProjectCreator.Properties.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public ProjectCreator Property(string name, string unevaluatedValue, string cond
2424
return Property(LastPropertyGroup, name, unevaluatedValue, condition, setIfEmpty);
2525
}
2626

27+
/// <summary>
28+
/// Attempts to get the specified evaluated property value.
29+
/// </summary>
30+
/// <param name="name">The name of the property.</param>
31+
/// <param name="value">The evaluated value of the property</param>
32+
/// <returns>The unescaped value of the property if it exists, otherwise an empty string.</returns>
33+
public ProjectCreator TryGetPropertyValue(string name, out string value)
34+
{
35+
value = Project.GetPropertyValue(name);
36+
37+
return this;
38+
}
39+
2740
/// <summary>
2841
/// Adds a property element to the current &lt;PropertyGroup /&gt;. A property group is automatically added if necessary. if <paramref name="unevaluatedValue"/> is <code>null</code>, the property is not added.
2942
/// </summary>

0 commit comments

Comments
 (0)