protected override void OnClosed(EventArgs e)
        {
            if (_savedChanges)
            {
                // Make sure the current document has the necessary
                // extensions loaded.
                // UNDONE: We should be able to do this with the document
                // closed or open as text as well via a registered service
                // on the ORMDesignerPackage, but this is sufficient for now.
                Dictionary <string, string> requiredExtensions = null;
                string[] loadedExtensions = null;
                foreach (IORMGenerator selectedGenerator in _mainBranch.SelectedGenerators)
                {
                    foreach (string requiredExtension in selectedGenerator.GetRequiredExtensionsForInputFormat("ORM"))
                    {
                        if (loadedExtensions == null)
                        {
                            loadedExtensions = (new ORMExtensionManager(_projectItem)).GetLoadedExtensions(_serviceProvider);
                        }
                        if (Array.BinarySearch <string>(loadedExtensions, requiredExtension) < 0)
                        {
                            if (requiredExtensions == null)
                            {
                                requiredExtensions = new Dictionary <string, string>();
                            }
                            else if (requiredExtensions.ContainsKey(requiredExtension))
                            {
                                continue;
                            }
                            requiredExtensions.Add(requiredExtension, requiredExtension);
                        }
                    }
                }
                if (requiredExtensions != null)
                {
                    _savedChanges = ORMExtensionManager.EnsureExtensions(_projectItem, _serviceProvider, requiredExtensions.Values);
                }
            }
            if (_savedChanges)
            {
                // Delete the removed items from the project
                if (_removedItems != null)
                {
                    EnvDTE.ProjectItems subItems = _projectItem.ProjectItems;
                    foreach (string itemName in _removedItems.Keys)
                    {
                        try
                        {
                            EnvDTE.ProjectItem subItem = subItems.Item(itemName);
                            if (subItem != null)
                            {
                                subItem.Delete();
                            }
                        }
                        catch (ArgumentException)
                        {
                            // Swallow
                        }
                    }
                }
                // throw away the original build item group
                if (_originalItemGroup != null)
                {
                    try
                    {
#if VISUALSTUDIO_10_0
                        _project.RemoveChild(_originalItemGroup);
#else
                        _project.RemoveItemGroup(_originalItemGroup);
#endif
                    }
                    catch (InvalidOperationException)
                    {
                        // Swallow
                    }
                }

#if VISUALSTUDIO_10_0
                Dictionary <string, ProjectItemElement> removeItems = new Dictionary <string, ProjectItemElement>();
#else
                Dictionary <string, BuildItem> removeItems = new Dictionary <string, BuildItem>();
#endif
                string tmpFile = null;
                try
                {
                    EnvDTE.ProjectItems projectItems = _projectItem.ProjectItems;
#if VISUALSTUDIO_10_0
                    string itemDirectory = (new FileInfo((string)_project.FullPath)).DirectoryName;
                    foreach (ProjectItemElement item in _itemGroup.Items)
#else
                    string itemDirectory = (new FileInfo((string)_project.FullFileName)).DirectoryName;
                    foreach (BuildItem item in this._itemGroup)
#endif
                    {
                        string filePath = string.Concat(itemDirectory, Path.DirectorySeparatorChar, item.Include);
                        string fileName = (new FileInfo(item.Include)).Name;
                        if (File.Exists(filePath))
                        {
                            try
                            {
                                projectItems.AddFromFile(filePath);
                            }
                            catch (ArgumentException)
                            {
                                // Swallow
                            }
                        }
                        else
                        {
                            if (tmpFile == null)
                            {
                                tmpFile = Path.GetTempFileName();
                            }
                            EnvDTE.ProjectItem projectItem = projectItems.AddFromTemplate(tmpFile, fileName);
                            string             customTool  = item.GetMetadata(ITEMMETADATA_GENERATOR);
                            if (!string.IsNullOrEmpty(customTool))
                            {
                                projectItem.Properties.Item("CustomTool").Value = customTool;
                            }
                        }
                        removeItems[item.Include] = null;
                    }
                }
                finally
                {
                    if (tmpFile != null)
                    {
                        File.Delete(tmpFile);
                    }
                }

#if VISUALSTUDIO_10_0
                foreach (ProjectItemGroupElement group in this._project.ItemGroups)
#else
                foreach (BuildItemGroup group in this._project.ItemGroups)
#endif
                {
                    if (group.Condition.Trim() == this._itemGroup.Condition.Trim())
                    {
                        continue;
                    }
#if VISUALSTUDIO_10_0
                    foreach (ProjectItemElement item in group.Items)
#else
                    foreach (BuildItem item in group)
#endif
                    {
                        if (removeItems.ContainsKey(item.Include))
                        {
                            removeItems[item.Include] = item;
                        }
                    }
                }
                foreach (string key in removeItems.Keys)
                {
#if VISUALSTUDIO_10_0
                    ProjectItemElement      removeItem;
                    ProjectElementContainer removeFrom;
                    if (null != (removeItem = removeItems[key]) &&
                        null != (removeFrom = removeItem.Parent))
                    {
                        removeFrom.RemoveChild(removeItem);
                    }
#else
                    BuildItem removeItem = removeItems[key];
                    if (removeItem != null)
                    {
                        _project.RemoveItem(removeItem);
                    }
#endif
                }

                VSLangProj.VSProjectItem vsProjectItem = _projectItem.Object as VSLangProj.VSProjectItem;
                if (vsProjectItem != null)
                {
                    vsProjectItem.RunCustomTool();
                }
            }
            else
            {
#if VISUALSTUDIO_10_0
                _project.RemoveChild(_itemGroup);
#else
                _project.RemoveItemGroup(_itemGroup);
#endif
            }
            base.OnClosed(e);
        }
Example #2
0
        static void CopyFilesAndFoldersRecursively(IEnumerable <ProjectItemWrapper> sourceProjectItems, EnvDTE.ProjectItems destinationProjectItems, Context.CopyOrAddAsLink howToDealWithCSharpFiles, int level = 1)
        {
            if (!StaticAbortProcessing)
            {
                foreach (ProjectItemWrapper sourceItem in sourceProjectItems)
                {
                    string sourceItemName     = sourceItem.GetName();
                    string sourceItemFullPath = sourceItem.GetFullPath();

                    //---------------------
                    // If it is a folder:
                    //---------------------
                    if (FoldersHelper.IsAFolder(sourceItemFullPath))
                    {
                        // If it is NOT the "Properties" folder (which exists by default in all projects):
                        if (level != 1 || sourceItemName.ToLower() != "properties")
                        {
                            // Create a new folder with the same name in the destination project:
                            EnvDTE.ProjectItem newFolder = destinationProjectItems.AddFolder(sourceItemName);

                            // Log:
                            AddLogEntryOnUIThread(string.Format(@"Created folder ""{0}"".", sourceItemName));

                            // Continue the recursion:
                            CopyFilesAndFoldersRecursively(sourceItem.GetChildItems(), newFolder.ProjectItems, howToDealWithCSharpFiles, level + 1);
                        }
                    }
                    //---------------------
                    // If it is a file:
                    //---------------------
                    else
                    {
                        EnvDTE.ProjectItem newFile = null;
                        if (sourceItemFullPath.ToLower().EndsWith(".cs"))
                        {
                            //---------------------
                            // C# file:
                            //---------------------

                            // Check if the file exists (this can be the case for example if we previously copied a file such as "UserControl.xaml", which automatically copies its child "UserControl.xaml.cs"):
                            EnvDTE.ProjectItem existingItem = FindProjectItemOrNull(sourceItemName, destinationProjectItems);

                            // Copy the file or add it as link:
                            if (howToDealWithCSharpFiles == Context.CopyOrAddAsLink.Copy)
                            {
                                // Copy only if the file is not already there:
                                if (existingItem == null)
                                {
                                    newFile = destinationProjectItems.AddFromFileCopy(sourceItemFullPath);
                                }

                                // Log:
                                AddLogEntryOnUIThread(string.Format(@"Copied file ""{0}"".", sourceItemName));
                            }
                            else if (howToDealWithCSharpFiles == Context.CopyOrAddAsLink.AddAsLink)
                            {
                                // Delete the copied file in order to replace it with a linked file (this can happen for example if we previously copied a file such as "UserControl.xaml", which automatically copies its child "UserControl.xaml.cs", so we need to delete this child):
                                if (existingItem != null)
                                {
                                    existingItem.Delete();
                                }

                                // Add the file "as link":
                                newFile = destinationProjectItems.AddFromFile(sourceItemFullPath);

                                // Log:
                                AddLogEntryOnUIThread(string.Format(@"Added file ""{0}"" as link.", sourceItemName));
                            }
                            else
                            {
                                throw new NotSupportedException();
                            }
                        }
                        else if (sourceItemFullPath.ToLower().EndsWith(".xaml") ||
                                 DoesFileHaveOneOfTheseExtensions(sourceItemFullPath, ExtensionsOfFilesToCopy)) // JPG, PNG, etc.
                        {
                            //---------------------
                            // XAML file:
                            //---------------------

                            // Copy the file:
                            newFile = destinationProjectItems.AddFromFileCopy(sourceItemFullPath);

                            // Log:
                            AddLogEntryOnUIThread(string.Format(@"Copied file ""{0}"".", sourceItemName));
                        }

                        // Continue the recursion:
                        if (newFile != null)
                        {
                            CopyFilesAndFoldersRecursively(sourceItem.GetChildItems(), newFile.ProjectItems, howToDealWithCSharpFiles, level + 1);
                        }
                    }

                    if (StaticAbortProcessing)
                    {
                        break;
                    }
                }
            }
        }