public IItemContainer Unfold(string name, IItemContainer parent)
        {
            Guard.NotNullOrEmpty(() => name, name);

            var container = parent.As<dynamic>();
            var item = container.ProjectItems.AddFromDirectory(this.SourcePath);

            return parent.Items.FirstOrDefault(i => i.Name == item.Name);
        }
示例#2
0
        public IItemContainer Unfold(string name, IItemContainer parent)
        {
            Guard.NotNullOrEmpty(() => name, name);

            var container = parent.As <dynamic>();
            var item      = container.ProjectItems.AddFromDirectory(this.SourcePath);

            return(parent.Items.FirstOrDefault(i => i.Name == item.Name));
        }
示例#3
0
        public virtual IItemContainer Unfold(string name, IItemContainer parent)
        {
            Guard.NotNullOrEmpty(() => name, name);
            Guard.NotNull(() => parent, parent);

            if (!typeof(HierarchyItem).IsAssignableFrom(parent.GetType()))
            {
                throw new NotSupportedException(Resources.VsFileTemplate_ErrorParentNotHierarchy);
            }

            Guard.NotNullOrEmpty(() => parent.PhysicalPath, parent.PhysicalPath);

            var targetPath = Path.Combine(Path.GetDirectoryName(parent.PhysicalPath), name);

            if (File.Exists(targetPath))
            {
                VsHelper.CheckOut(targetPath);
            }

            if (!this.SourcePath.Equals(targetPath, StringComparison.OrdinalIgnoreCase))
            {
                File.Copy(this.SourcePath, targetPath, this.overwrite);
            }

            var container = parent.As <dynamic>();

            EnvDTE.ProjectItem newlyAddedFile = null;

            if (!parent.Items.Any(i => i.Name == name))
            {
                newlyAddedFile = container.ProjectItems.AddFromFile(targetPath) as EnvDTE.ProjectItem;
            }

            if (this.openFile)
            {
                container.DTE.ItemOperations.OpenFile(targetPath);
            }
            else if (newlyAddedFile != null)
            {
                //
                // The file may have opened anyway, if we're not supposed to open it, we'll search for
                // the matching window and close it
                //
                foreach (EnvDTE.Window w in container.DTE.Windows)
                {
                    if (newlyAddedFile.Equals(w.ProjectItem))
                    {
                        w.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                        break;
                    }
                }
            }

            return(parent.Items.FirstOrDefault(item => item.Kind == ItemKind.Item && item.Name == name));
        }
示例#4
0
        public virtual IItemContainer Unfold(string name, IItemContainer parent)
        {
            Guard.NotNullOrEmpty(() => name, name);
            Guard.NotNull(() => parent, parent);

            if (!typeof(HierarchyItem).IsAssignableFrom(parent.GetType()))
                throw new NotSupportedException(Resources.VsFileTemplate_ErrorParentNotHierarchy);

            Guard.NotNullOrEmpty(() => parent.PhysicalPath, parent.PhysicalPath);

            var targetPath = Path.Combine(Path.GetDirectoryName(parent.PhysicalPath), name);
            if (File.Exists(targetPath))
            {
                VsHelper.CheckOut(targetPath);
            }

            if (!this.SourcePath.Equals(targetPath, StringComparison.OrdinalIgnoreCase))
            {
                File.Copy(this.SourcePath, targetPath, this.overwrite);
            }

            var container = parent.As<dynamic>();
            EnvDTE.ProjectItem newlyAddedFile = null;

            if (!parent.Items.Any(i => i.Name == name))
            {
                newlyAddedFile = container.ProjectItems.AddFromFile(targetPath) as EnvDTE.ProjectItem;
            }

            if (this.openFile)
            {
                container.DTE.ItemOperations.OpenFile(targetPath);
            }
            else if (newlyAddedFile != null)
            {
                //
                // The file may have opened anyway, if we're not supposed to open it, we'll search for
                // the matching window and close it
                //
                foreach (EnvDTE.Window w in container.DTE.Windows)
                {
                    if (newlyAddedFile.Equals(w.ProjectItem))
                    {
                        w.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                        break;
                    }
                }

            }

            return parent.Items.FirstOrDefault(item => item.Kind == ItemKind.Item && item.Name == name);
        }
 private static IItem GetTemplateItemFromTT(IItemContainer item)
 {
     return ExtensionMatches(item) ? item.As<IItem>() : item.Traverse().First(i => ExtensionMatches(i)).As<IItem>();
 }
示例#6
0
        public static string Rename(this IItemContainer item, string proposedItemName, bool ensureUniqueness = false, IVsUIShell uiService = null)
        {
            Guard.NotNull(() => item, item);
            Guard.NotNullOrEmpty(() => proposedItemName, proposedItemName);

            // Ensure new name is cleaned (i.e. valid solution name)
            var newItemName = proposedItemName;

            if (!DataFormats.IsValidSolutionItemName(proposedItemName))
            {
                newItemName = DataFormats.MakeValidSolutionItemName(proposedItemName);
            }

            // Ensure rename required
            if (newItemName.Equals(item.Name, StringComparison.OrdinalIgnoreCase))
            {
                return(newItemName);
            }

            // If Item, then ensure the new name has an extension (borrowed from existing).
            if (item is IItem)
            {
                var proposedExtension = Path.GetExtension(proposedItemName);
                if (string.IsNullOrEmpty(proposedExtension))
                {
                    var existingExtension = Path.GetExtension(item.Name);
                    if (!string.IsNullOrEmpty(existingExtension))
                    {
                        newItemName = Path.ChangeExtension(newItemName, existingExtension);
                    }
                }
            }

            // Check to see if new named item already exists (within parent)
            bool newNamedItemExists =
                (item.Parent.Items.Where(i => i.Name.Equals(newItemName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault() != null);

            if (newNamedItemExists)
            {
                if (ensureUniqueness)
                {
                    // Calculate new unique name
                    var uniqueItemName = string.Empty;
                    uniqueItemName = (item is IItem) ? item.Parent.CalculateNextUniqueChildItemName <IItem>(newItemName) : uniqueItemName;
                    uniqueItemName = (item is IProject) ? item.Parent.CalculateNextUniqueChildItemName <IProject>(newItemName) : uniqueItemName;
                    if (string.IsNullOrEmpty(uniqueItemName))
                    {
                        throw new InvalidOperationException(
                                  string.Format(CultureInfo.CurrentCulture, Resources.SolutionExtensions_ErrorRenameUnknownType, item.Kind));
                    }

                    // Prompt user interactively
                    if (newNamedItemExists)
                    {
                        if (uiService != null)
                        {
                            uiService.ShowWarning(Resources.SolutionExtensions_PromptItemAlreadyExists_Title,
                                                  string.Format(CultureInfo.CurrentCulture,
                                                                Resources.SolutionExtensions_PromptItemAlreadyExists_Message, newItemName, uniqueItemName));
                        }
                    }

                    newItemName = uniqueItemName;
                }
                else
                {
                    throw new InvalidOperationException(
                              string.Format(CultureInfo.CurrentCulture, Resources.SolutionExtensions_ErrorRenamedItemExists, newItemName));
                }
            }

            // Rename item
            if (item is IItem || item is IFolder)
            {
                var vsItem = item.As <ProjectItem>();
                if (vsItem != null)
                {
                    vsItem.Name = newItemName;
                }
            }

            // Rename project
            var project = item as IProject;

            if (project != null)
            {
                var vsProject = project.As <Project>();
                if (vsProject != null)
                {
                    // Rename project Name
                    vsProject.Name = newItemName;

                    // Rename AssemblyName
                    if (project.Data != null)
                    {
                        if (project.Data.AssemblyName != null)
                        {
                            var assemblyName = project.Data.AssemblyName as string;

                            if (assemblyName.IndexOf(project.Name, StringComparison.OrdinalIgnoreCase) >= 0)
                            {
                                project.Data.AssemblyName = assemblyName.Replace(project.Name, newItemName);
                            }
                        }

                        // Rename Rootnamespace
                        if (project.Data.DefaultNamespace != null)
                        {
                            var defaultNamespace = project.Data.DefaultNamespace as string;

                            if (defaultNamespace.IndexOf(project.Name, StringComparison.OrdinalIgnoreCase) >= 0)
                            {
                                project.Data.DefaultNamespace = defaultNamespace.Replace(project.Name, newItemName);
                            }
                        }
                    }
                }
            }

            return(newItemName);
        }
示例#7
0
 private static IItem GetTemplateItemFromTT(IItemContainer item)
 {
     return(ExtensionMatches(item) ? item.As <IItem>() : item.Traverse().First(i => ExtensionMatches(i)).As <IItem>());
 }