public static bool ThreadSafeRemoveItem(this Microsoft.Build.Evaluation.Project thisp, ProjectItem item) { lock (thisp) { try { if (thisp != item.Project) { ICollection <ProjectItem> foundItem = thisp.GetItemsByEvaluatedInclude(item.EvaluatedInclude); if (foundItem == null || foundItem.Count != 1) { Trace.WriteLine("Attempt to remove item with wrong project evaluated include returned with more than one item or null item"); return(false); } return(thisp.RemoveItem(foundItem.FirstOrDefault())); } return(thisp.RemoveItem(item)); } catch (Exception) { return(false); } } }
public void ItemsByEvaluatedIncludeDirectRemove() { Project project = new Project(); ProjectItem item1 = project.AddItem("i", "i1;j1")[0]; project.RemoveItem(item1); List<ProjectItem> items = Helpers.MakeList(project.GetItemsByEvaluatedInclude("i1")); Assert.Equal(0, items.Count); }
public void ItemsByEvaluatedIncludeDirectAdd() { Project project = new Project(); project.AddItem("i", "i1"); List<ProjectItem> items = Helpers.MakeList(project.GetItemsByEvaluatedInclude("i1")); Assert.Equal(1, items.Count); }
public void ItemsByEvaluatedIncludeReevaluation() { Project project = new Project(); project.Xml.AddItem("i", "i1"); project.ReevaluateIfNecessary(); List<ProjectItem> items = Helpers.MakeList(project.GetItemsByEvaluatedInclude("i1")); Assert.Equal(1, items.Count); project.Xml.AddItem("j", "i1"); project.ReevaluateIfNecessary(); items = Helpers.MakeList(project.GetItemsByEvaluatedInclude("i1")); Assert.Equal(2, items.Count); }
public void ItemsByEvaluatedInclude_EvaluatedIncludeNeedsEscaping() { Project project = new Project(); project.Xml.AddItem("i", "i%261"); project.Xml.AddItem("j", "i%25261"); project.Xml.AddItem("k", "j1"); project.Xml.AddItem("l", "i&1"); project.ReevaluateIfNecessary(); List<ProjectItem> items = Helpers.MakeList(project.GetItemsByEvaluatedInclude("i&1")); Assert.Equal(2, items.Count); foreach (ProjectItem item in items) { Assert.Equal("i&1", item.EvaluatedInclude); Assert.True( String.Equals(item.ItemType, "i", StringComparison.OrdinalIgnoreCase) || String.Equals(item.ItemType, "l", StringComparison.OrdinalIgnoreCase) ); } }
public void ItemsByEvaluatedInclude() { Project project = new Project(); project.Xml.AddItem("i", "i1"); project.Xml.AddItem("i", "i1"); project.Xml.AddItem("j", "j1"); project.Xml.AddItem("j", "i1"); project.ReevaluateIfNecessary(); List<ProjectItem> items = Helpers.MakeList(project.GetItemsByEvaluatedInclude("i1")); Assert.Equal(3, items.Count); foreach (ProjectItem item in items) { Assert.Equal("i1", item.EvaluatedInclude); } }
public void ChangeItemType() { Project project = new Project(); project.AddItem("i", "i1"); project.ReevaluateIfNecessary(); ProjectItem item = Helpers.GetFirst(project.GetItems("i")); item.ItemType = "j"; Assert.Equal("j", item.ItemType); string expected = ObjectModelHelpers.CleanupFileContents( @"<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""msbuildnamespace""> <ItemGroup> <j Include=""i1"" /> </ItemGroup> </Project>"); Helpers.VerifyAssertProjectContent(expected, project.Xml); ProjectItemGroupElement itemGroupElement = Helpers.GetFirst(project.Xml.ItemGroups); Assert.Equal(1, Helpers.MakeList(itemGroupElement.Items).Count); Assert.Equal(true, Object.ReferenceEquals(itemGroupElement, item.Xml.Parent)); Assert.Equal(1, Helpers.MakeList(project.Items).Count); Assert.Equal(1, Helpers.MakeList(project.ItemsIgnoringCondition).Count); Assert.Equal(0, Helpers.MakeList(project.GetItems("i")).Count); Assert.Equal(0, Helpers.MakeList(project.GetItemsIgnoringCondition("i")).Count); Assert.Equal(true, Object.ReferenceEquals(item, Helpers.GetFirst(project.GetItems("j")))); Assert.Equal(true, Object.ReferenceEquals(item, Helpers.GetFirst(project.GetItemsIgnoringCondition("j")))); Assert.Equal(true, Object.ReferenceEquals(item, Helpers.GetFirst(project.GetItemsByEvaluatedInclude("i1")))); }
public void RenameItemInProjectWithWildcards() { string projectDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(projectDirectory); try { string sourceFile = Path.Combine(projectDirectory, "a.cs"); string renamedSourceFile = Path.Combine(projectDirectory, "b.cs"); File.Create(sourceFile).Dispose(); var project = new Project(); project.AddItem("File", "*.cs"); project.FullPath = Path.Combine(projectDirectory, "test.proj"); // assign a path so the wildcards can lock onto something. project.ReevaluateIfNecessary(); var projectItem = project.Items.Single(); Assert.Equal(Path.GetFileName(sourceFile), projectItem.EvaluatedInclude); Assert.Same(projectItem, project.GetItemsByEvaluatedInclude(projectItem.EvaluatedInclude).Single()); projectItem.Rename(Path.GetFileName(renamedSourceFile)); File.Move(sourceFile, renamedSourceFile); // repro w/ or w/o this project.ReevaluateIfNecessary(); projectItem = project.Items.Single(); Assert.Equal(Path.GetFileName(renamedSourceFile), projectItem.EvaluatedInclude); Assert.Same(projectItem, project.GetItemsByEvaluatedInclude(projectItem.EvaluatedInclude).Single()); } finally { Directory.Delete(projectDirectory, recursive: true); } }
public void AddProjectReferences() { var projects = GetProjects(); var thisProject = projects.FirstOrDefault(c => c.GuidString == "{7AAC4FDB-1C6A-453F-8126-D2E89BC1B6D9}"); var generatorTestProject = projects.FirstOrDefault(c => c.GuidString == "{D30AC162-B004-4946-96EC-13A760A2D8FA}"); if (thisProject == null) throw new Exception("Cannot find Generator Project, operation failed."); projects = projects.Except(new[] { thisProject, generatorTestProject }).ToList(); var gen = new Project(thisProject.AbsolutePath); foreach (var project in projects) { if (!gen.GetItemsByEvaluatedInclude(project.PathToSolution).Any()) { gen.AddItem("ProjectReference", project.PathToSolution, new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("Project", project.GuidString), new KeyValuePair<string, string>("Name", project.Name) }); } } gen.Save(); ProjectCollection.GlobalProjectCollection.UnloadAllProjects(); }