public void Add <T>(T item) where T : ProjectItem { var group = _project.FindGroup(item.Matches) ?? _project.FindGroup(x => x.Name == item.Name) ?? _project.AddNewItemGroup(); item.Configure(group); }
public void AddImportIfMissing_AddImportToBottomWhenOtherImportIsNotLastElementInProject_ImportAddedAsLastElementInProject() { CreateProject(); var itemGroup = project.AddNewItemGroup(); itemGroup.AddNewItem("File.cs", "File.cs"); project.AddNewImport("test", null, itemGroup); string importFile = @"..\packages\Foo.0.1\build\Foo.targets"; string condition = "Exists('..\\packages\\Foo.0.1\\build\\Foo.targets')"; var lastItemBeforeAddingNewImport = project.GetAllObjects().Last() as MSBuildItemGroup; AddImportIfMissingAtBottom(importFile, condition); var lastItem = project.GetAllObjects().Last() as MSBuildImport; Assert.IsNotNull(lastItem); Assert.AreEqual(importFile, lastItem.Project); Assert.AreEqual(condition, lastItem.Condition); Assert.IsNotNull(lastItemBeforeAddingNewImport); Assert.AreEqual("File.cs", lastItemBeforeAddingNewImport.Items.First().Include); }
static MSBuildItemGroup GetOrAddItemGroup(MSBuildProject project, string originalFramework) { string condition = string.Format("'$(TargetFramework)' == '{0}'", originalFramework); MSBuildItemGroup itemGroup = project .ItemGroups .FirstOrDefault(item => StringComparer.OrdinalIgnoreCase.Equals(item.Condition, condition)); if (itemGroup == null) { itemGroup = project.AddNewItemGroup(); itemGroup.Condition = condition; } return(itemGroup); }