public SolutionItem ReloadItem(IProgressMonitor monitor, SolutionItem sitem)
        {
            if (Items.IndexOf(sitem) == -1)
            {
                throw new InvalidOperationException("Solution item '" + sitem.Name + "' does not belong to folder '" + Name + "'");
            }

            SolutionEntityItem item = sitem as SolutionEntityItem;

            if (item != null)
            {
                // Load the new item

                SolutionEntityItem newItem;
                try {
                    if (ParentSolution.IsSolutionItemEnabled(item.FileName))
                    {
                        newItem = Services.ProjectService.ReadSolutionItem(monitor, item.FileName);
                    }
                    else
                    {
                        UnknownSolutionItem e = new UnknownSolutionItem()
                        {
                            FileName      = item.FileName,
                            UnloadedEntry = true
                        };
                        newItem = e;
                    }
                } catch (Exception ex) {
                    UnknownSolutionItem e = new UnknownSolutionItem();
                    e.LoadError = ex.Message;
                    e.FileName  = item.FileName;
                    newItem     = e;
                }

                // Replace in the file list
                Items.Replace(item, newItem);

                DisconnectChildEntryEvents(item);
                ConnectChildEntryEvents(newItem);

                NotifyModified("Items");
                OnItemRemoved(new SolutionItemChangeEventArgs(item, ParentSolution, true)
                {
                    ReplacedItem = item
                }, true);
                OnItemAdded(new SolutionItemChangeEventArgs(newItem, ParentSolution, true)
                {
                    ReplacedItem = item
                }, true);

                item.Dispose();
                return(newItem);
            }
            else
            {
                return(sitem);
            }
        }
        public void SolutionBuildOrder()
        {
            string solFile = Util.GetSampleProject("solution-build-order", "ConsoleApplication3.sln");

            Solution           sol  = Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile) as Solution;
            SolutionEntityItem p    = sol.FindProjectByName("ConsoleApplication3");
            SolutionEntityItem lib1 = sol.FindProjectByName("ClassLibrary1");
            SolutionEntityItem lib2 = sol.FindProjectByName("ClassLibrary2");

            Assert.IsTrue(p.ItemDependencies.Contains(lib1));
            Assert.IsTrue(p.ItemDependencies.Contains(lib2));
            Assert.AreEqual(2, p.ItemDependencies.Count);

            Assert.IsTrue(lib2.ItemDependencies.Contains(lib1));
            Assert.AreEqual(1, lib2.ItemDependencies.Count);
            Assert.AreEqual(0, lib1.ItemDependencies.Count);

            // Check that dependencies are saved

            var solContent1 = File.ReadAllLines(solFile);

            sol.Save(new NullProgressMonitor());

            var solContent2 = File.ReadAllLines(solFile);

            Assert.AreEqual(solContent1, solContent2);

            // Check that when an item is removed, it is removed from the dependencies list

            lib1.ParentFolder.Items.Remove(lib1);
            lib1.Dispose();

            Assert.IsTrue(p.ItemDependencies.Contains(lib2));
            Assert.AreEqual(1, p.ItemDependencies.Count);
            Assert.AreEqual(0, lib2.ItemDependencies.Count);

            // Check that when an item is reloaded, it is kept from the dependencies list

            var lib2Reloaded = lib2.ParentFolder.ReloadItem(Util.GetMonitor(), lib2);

            Assert.AreNotEqual(lib2, lib2Reloaded);
            Assert.IsTrue(p.ItemDependencies.Contains(lib2Reloaded));
            Assert.AreEqual(1, p.ItemDependencies.Count);
        }
Exemple #3
0
        public SolutionItem ReloadItem(IProgressMonitor monitor, SolutionItem sitem)
        {
            if (Items.IndexOf(sitem) == -1)
            {
                throw new InvalidOperationException("Solution item '" + sitem.Name + "' does not belong to folder '" + Name + "'");
            }

            SolutionEntityItem item = sitem as SolutionEntityItem;

            if (item != null)
            {
                // Load the new item

                SolutionEntityItem newItem;
                try {
                    if (ParentSolution.IsSolutionItemEnabled(item.FileName))
                    {
                        newItem = Services.ProjectService.ReadSolutionItem(monitor, item.FileName);
                    }
                    else
                    {
                        UnknownSolutionItem e = new UnloadedSolutionItem()
                        {
                            FileName = item.FileName
                        };
                        var ch = item.GetItemHandler() as MonoDevelop.Projects.Formats.MSBuild.MSBuildHandler;
                        if (ch != null)
                        {
                            var h = new MonoDevelop.Projects.Formats.MSBuild.MSBuildHandler(ch.TypeGuid, ch.ItemId)
                            {
                                Item = e,
                            };
                            e.SetItemHandler(h);
                        }
                        newItem = e;
                    }
                } catch (Exception ex) {
                    UnknownSolutionItem e = new UnknownSolutionItem();
                    e.LoadError = ex.Message;
                    e.FileName  = item.FileName;
                    newItem     = e;
                }

                if (!Items.Contains(item))
                {
                    // The old item is gone, which probably means it has already been reloaded (BXC20615), or maybe removed.
                    // In this case, there isn't anything else we can do
                    newItem.Dispose();

                    // Find the replacement if it exists
                    return(Items.OfType <SolutionEntityItem> ().FirstOrDefault(it => it.FileName == item.FileName));
                }

                // Replace in the file list
                Items.Replace(item, newItem);

                DisconnectChildEntryEvents(item);
                ConnectChildEntryEvents(newItem);

                NotifyModified("Items");
                OnItemRemoved(new SolutionItemChangeEventArgs(item, ParentSolution, true)
                {
                    ReplacedItem = item
                }, true);
                OnItemAdded(new SolutionItemChangeEventArgs(newItem, ParentSolution, true)
                {
                    ReplacedItem = item
                }, true);

                item.Dispose();
                return(newItem);
            }
            else
            {
                return(sitem);
            }
        }