diff options
author | Michael Hutchinson <m.j.hutchinson@gmail.com> | 2011-11-23 10:52:01 +0400 |
---|---|---|
committer | Michael Hutchinson <m.j.hutchinson@gmail.com> | 2011-11-23 10:52:14 +0400 |
commit | d823430f7f983497176c5677e284c88ec41e0304 (patch) | |
tree | 7d192017b4cfc37f353aadf4521c8d21f94f3814 /main/src | |
parent | 9ea149a2401f755f5aa80a0941ea0352907611bb (diff) |
[Core] Fix removal of duplicate msbuild items
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/core/MonoDevelop.Core/MonoDevelop.Projects.Formats.MSBuild/MSBuildProjectHandler.cs | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/main/src/core/MonoDevelop.Core/MonoDevelop.Projects.Formats.MSBuild/MSBuildProjectHandler.cs b/main/src/core/MonoDevelop.Core/MonoDevelop.Projects.Formats.MSBuild/MSBuildProjectHandler.cs index fd1f4182a3..e9d184277e 100644 --- a/main/src/core/MonoDevelop.Core/MonoDevelop.Projects.Formats.MSBuild/MSBuildProjectHandler.cs +++ b/main/src/core/MonoDevelop.Core/MonoDevelop.Projects.Formats.MSBuild/MSBuildProjectHandler.cs @@ -287,7 +287,9 @@ namespace MonoDevelop.Projects.Formats.MSBuild it.FileName = fileName; it.Name = System.IO.Path.GetFileNameWithoutExtension (fileName); - + + RemoveDuplicateItems (p, fileName); + Load (monitor, p); return it; @@ -396,6 +398,54 @@ namespace MonoDevelop.Projects.Formats.MSBuild return new FileFormat (TargetFormat, TargetFormat.Id, TargetFormat.Name); } + void RemoveDuplicateItems (MSBuildProject msproject, string fileName) + { + timer.Trace ("Checking for duplicate items"); + + var uniqueIncludes = new Dictionary<string,object> (); + var toRemove = new List<MSBuildItem> (); + foreach (MSBuildItem bi in msproject.GetAllItems ()) { + object existing; + string key = bi.Name + "<" + bi.Include; + if (!uniqueIncludes.TryGetValue (key, out existing)) { + uniqueIncludes[key] = bi; + continue; + } + var exBi = existing as MSBuildItem; + if (exBi != null) { + if (exBi.Condition != bi.Condition || exBi.Element.InnerXml != bi.Element.InnerXml) { + uniqueIncludes[key] = new List<MSBuildItem> { exBi, bi }; + } else { + toRemove.Add (bi); + } + continue; + } + + var exList = (List<MSBuildItem>)existing; + bool found = false; + foreach (var m in (exList)) { + if (m.Condition == bi.Condition && m.Element.InnerXml == bi.Element.InnerXml) { + found = true; + break; + } + } + if (!found) { + exList.Add (bi); + } else { + toRemove.Add (bi); + } + } + if (toRemove.Count == 0) + return; + + timer.Trace ("Removing duplicate items"); + + foreach (var t in toRemove) + msproject.RemoveItem (t); + fileContent = msproject.Save (); + MonoDevelop.Projects.Text.TextFile.WriteFile (fileName, fileContent, "UTF-8"); + } + void Load (IProgressMonitor monitor, MSBuildProject msproject) { timer.Trace ("Initialize serialization"); @@ -418,9 +468,6 @@ namespace MonoDevelop.Projects.Formats.MSBuild ProjectItem it = ReadItem (ser, buildItem); if (it != null) { EntityItem.Items.Add (it); - int i = EntityItem.Items.IndexOf (it); - if (i != -1 && EntityItem.Items [i] != it && EntityItem.Items [i].Condition == it.Condition) - EntityItem.Items.RemoveAt (i); // Remove duplicates } } |