diff --git a/src/MSBuildProjectCreator.UnitTests/ItemTests.cs b/src/MSBuildProjectCreator.UnitTests/ItemTests.cs index cf61347..ef3fe2f 100644 --- a/src/MSBuildProjectCreator.UnitTests/ItemTests.cs +++ b/src/MSBuildProjectCreator.UnitTests/ItemTests.cs @@ -5,6 +5,7 @@ using Microsoft.Build.Evaluation; using Shouldly; using System.Collections.Generic; +using System.Linq; using Xunit; namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests @@ -292,6 +293,54 @@ public void RemoveItem() StringCompareShould.IgnoreLineEndings); } + [Fact] + public void TryGetItemsCustomSelector() + { + ProjectCreator.Create(projectFileOptions: NewProjectFileOptions.None) + .ItemInclude("MyItem", "3F114C1509CF4D499A44F986BEBD5707") + .ItemInclude("MyItem", "8A9654A639F5429AB1C7A8F2AE9639D8") + .TryGetItems("MyItem", i => i.EvaluatedInclude, out IReadOnlyCollection items); + + items.ShouldBe(new List + { + "3F114C1509CF4D499A44F986BEBD5707", + "8A9654A639F5429AB1C7A8F2AE9639D8" + }); + } + + [Fact] + public void TryGetItemsDictionary() + { + ProjectCreator.Create(projectFileOptions: NewProjectFileOptions.None) + .ItemInclude("MyItem", "A9AA54F5E51E4C0A966A5F5CDBD5EC9D", metadata: new Dictionary { ["MyMetadata"] = "A3A33B3B55A841D883A05F6CA920AC1F" }) + .ItemInclude("MyItem", "910E2DA9075043AA850D923E8E3EE398", metadata: new Dictionary { ["MyMetadata"] = "EF8E0862B0A946D98DA7082E70AEC3E9" }) + .TryGetItems("MyItem", "MyMetadata", out IReadOnlyDictionary items); + + items.ShouldBe(new Dictionary + { + ["A9AA54F5E51E4C0A966A5F5CDBD5EC9D"] = "A3A33B3B55A841D883A05F6CA920AC1F", + ["910E2DA9075043AA850D923E8E3EE398"] = "EF8E0862B0A946D98DA7082E70AEC3E9" + }); + } + + [Fact] + public void TryGetItemsProjectItems() + { + ProjectCreator.Create(projectFileOptions: NewProjectFileOptions.None) + .ItemInclude("MyItem", "B39E047FC97A48E3964EEA70C46F4E35") + .ItemInclude("MyItem", "E0FA8DA2551F4EE18D2102149D3049D4") + .TryGetItems("MyItem", out IReadOnlyCollection items); + + items + .Select(i => i.EvaluatedInclude) + .ToList() + .ShouldBe(new List + { + "B39E047FC97A48E3964EEA70C46F4E35", + "E0FA8DA2551F4EE18D2102149D3049D4" + }); + } + [Fact] public void UpdateItem() { diff --git a/src/MSBuildProjectCreator.UnitTests/PropertyTests.cs b/src/MSBuildProjectCreator.UnitTests/PropertyTests.cs index 19cf537..fdec05e 100644 --- a/src/MSBuildProjectCreator.UnitTests/PropertyTests.cs +++ b/src/MSBuildProjectCreator.UnitTests/PropertyTests.cs @@ -93,5 +93,18 @@ public void PropertySimple() ", StringCompareShould.IgnoreLineEndings); } + + [Fact] + public void TryGetPropertyValueSimple() + { + ProjectCreator.Create(projectFileOptions: NewProjectFileOptions.None) + .Property("E7A39154F5AB476A928067251F88FFCE", "E8F579A7E3374F389120CF6D888E74B9") + .Property("FAB58E5B32D14990ACE2490D7593FDF6", "60F55FB14D2E44B2BA4EC91488D9FF8F") + .TryGetPropertyValue("E7A39154F5AB476A928067251F88FFCE", out string property1) + .TryGetPropertyValue("FAB58E5B32D14990ACE2490D7593FDF6", out string property2); + + property1.ShouldBe("E8F579A7E3374F389120CF6D888E74B9"); + property2.ShouldBe("60F55FB14D2E44B2BA4EC91488D9FF8F"); + } } } \ No newline at end of file diff --git a/src/MSBuildProjectCreator/ProjectCreator.Build.cs b/src/MSBuildProjectCreator/ProjectCreator.Build.cs index 8e25df0..86d5088 100644 --- a/src/MSBuildProjectCreator/ProjectCreator.Build.cs +++ b/src/MSBuildProjectCreator/ProjectCreator.Build.cs @@ -2,6 +2,8 @@ // // Licensed under the MIT license. +using Microsoft.Build.Execution; + namespace Microsoft.Build.Utilities.ProjectCreation { public partial class ProjectCreator @@ -14,7 +16,10 @@ public partial class ProjectCreator /// The current . public ProjectCreator TryBuild(string target, out bool result) { - result = Project.Build(target); + lock (BuildManager.DefaultBuildManager) + { + result = Project.Build(target); + } return this; } @@ -30,7 +35,10 @@ public ProjectCreator TryBuild(string target, out bool result, out BuildOutput b { buildOutput = BuildOutput.Create(); - result = Project.Build(target, buildOutput.AsEnumerable()); + lock (BuildManager.DefaultBuildManager) + { + result = Project.Build(target, buildOutput.AsEnumerable()); + } return this; } @@ -42,7 +50,10 @@ public ProjectCreator TryBuild(string target, out bool result, out BuildOutput b /// The current . public ProjectCreator TryBuild(out bool result) { - result = Project.Build(); + lock (BuildManager.DefaultBuildManager) + { + result = Project.Build(); + } return this; } @@ -57,7 +68,10 @@ public ProjectCreator TryBuild(out bool result, out BuildOutput buildOutput) { buildOutput = BuildOutput.Create(); - result = Project.Build(buildOutput.AsEnumerable()); + lock (BuildManager.DefaultBuildManager) + { + result = Project.Build(buildOutput.AsEnumerable()); + } return this; } diff --git a/src/MSBuildProjectCreator/ProjectCreator.Items.cs b/src/MSBuildProjectCreator/ProjectCreator.Items.cs index a3f581f..0e8e1be 100644 --- a/src/MSBuildProjectCreator/ProjectCreator.Items.cs +++ b/src/MSBuildProjectCreator/ProjectCreator.Items.cs @@ -4,6 +4,7 @@ using Microsoft.Build.Construction; using Microsoft.Build.Evaluation; +using System; using System.Collections.Generic; using System.Linq; @@ -314,6 +315,58 @@ public ProjectCreator ItemUpdate( condition: condition); } + /// + /// Gets a list of items in the project. + /// + /// The item type. + /// A containing the items. + /// The current . + public ProjectCreator TryGetItems(string itemType, out IReadOnlyCollection items) + { + items = Project.GetItems(itemType).ToList(); + + return this; + } + + /// + /// Gets a list of items in the project. + /// + /// The type of the object that the selector will return. + /// The item type. + /// A that gets the desired return object. + /// A containing the items. + /// The current . + public ProjectCreator TryGetItems(string itemType, Func selector, out IReadOnlyCollection items) + { + if (selector == null) + { + throw new ArgumentNullException(nameof(selector)); + } + + items = Project.GetItems(itemType).Select(selector).ToList(); + + return this; + } + + /// + /// Gets a list of items in the project. + /// + /// The item type. + /// The name of a metadata item to get. + /// A containing the ItemSpec and metadata value. + /// The current . + public ProjectCreator TryGetItems(string itemType, string metadataName, out IReadOnlyDictionary items) + { + if (String.IsNullOrWhiteSpace(metadataName)) + { + throw new ArgumentNullException(nameof(metadataName)); + } + + items = Project.GetItems(itemType).ToDictionary(i => i.EvaluatedInclude, i => i.GetMetadataValue(metadataName)); + + return this; + } + /// /// Adds an item to the current item group. /// diff --git a/src/MSBuildProjectCreator/ProjectCreator.Properties.cs b/src/MSBuildProjectCreator/ProjectCreator.Properties.cs index 2ba0fc8..321552c 100644 --- a/src/MSBuildProjectCreator/ProjectCreator.Properties.cs +++ b/src/MSBuildProjectCreator/ProjectCreator.Properties.cs @@ -24,6 +24,19 @@ public ProjectCreator Property(string name, string unevaluatedValue, string cond return Property(LastPropertyGroup, name, unevaluatedValue, condition, setIfEmpty); } + /// + /// Attempts to get the specified evaluated property value. + /// + /// The name of the property. + /// The evaluated value of the property + /// The unescaped value of the property if it exists, otherwise an empty string. + public ProjectCreator TryGetPropertyValue(string name, out string value) + { + value = Project.GetPropertyValue(name); + + return this; + } + /// /// Adds a property element to the current <PropertyGroup />. A property group is automatically added if necessary. if is null, the property is not added. ///