public static bool ExistFile(ProjectFolderInfo projectFolder, string dir)
        {
            if (projectFolder.ProjectFolderRole == TridionVSRazorExtension.ProjectFolderRole.Binary)
            {
                return Extensions.Keys.Select(extension => ExistFile(projectFolder, dir, extension)).Any(x => x);
            }

            return ExistFile(projectFolder, dir, "*.cshtml");
        }
        public static void AddChildItems(ProjectFolderInfo projectFolder, string rootPath)
        {
            if (projectFolder == null)
                return;

            if (String.IsNullOrEmpty(projectFolder.FullPath))
                return;

            if (!Directory.Exists(projectFolder.FullPath))
                Directory.CreateDirectory(projectFolder.FullPath);

            ProjectFolderInfo topFolder = projectFolder.GetTopFolder();
            ProjectFolderRole role = topFolder.ProjectFolderRole;
            string[] extensions = role == TridionVSRazorExtension.ProjectFolderRole.Binary ? Extensions.Keys.ToArray() : new[] { "*.cshtml" };
            string[] directories = Directory.GetDirectories(projectFolder.FullPath);
            string[] files = GetFiles(projectFolder.FullPath, extensions);

            if (directories.Length == 0 && files.Length == 0)
            {
                projectFolder.ChildItems = null;
                return;
            }

            List<ProjectItemInfo> newChildItems = new List<ProjectItemInfo>();

            foreach (string dir in directories)
            {
                ProjectFolderInfo childFolder = null;

                if (projectFolder.ChildItems != null)
                {
                    childFolder = projectFolder.ChildItems.FirstOrDefault(x => x.IsFolder && x.FullPath == dir) as ProjectFolderInfo;
                }

                if (childFolder == null)
                {
                    childFolder = new ProjectFolderInfo { RootPath = rootPath, Path = dir.Replace(rootPath, "").Trim('\\'), Checked = false };
                }

                childFolder.Parent = projectFolder;

                AddChildItems(childFolder, rootPath);

                newChildItems.Add(childFolder);
            }

            foreach (string file in files)
            {
                ProjectFileInfo childFile = null;

                if (projectFolder.ChildItems != null)
                {
                    childFile = projectFolder.ChildItems.FirstOrDefault(x => x.IsFile && x.FullPath == file) as ProjectFileInfo;
                }

                if (childFile == null)
                {
                    childFile = new ProjectFileInfo { RootPath = rootPath, Path = file.Replace(rootPath, "").Trim('\\'), Checked = false };
                }

                childFile.Parent = projectFolder;

                newChildItems.Add(childFile);
            }

            projectFolder.ChildItems = newChildItems.Count > 0 ? newChildItems : null;
        }
        public static void DeleteFileFromMapping(ProjectFolderInfo folder, string filePath)
        {
            if (folder.ChildItems == null)
                return;

            folder.ChildItems.RemoveAll(x => x.IsFile && x.FullPath == filePath);

            foreach (ProjectFolderInfo childFolder in folder.ChildItems.Where(x => x.IsFolder))
            {
                DeleteFileFromMapping(childFolder, filePath);
            }
        }
 public static bool ExistFile(ProjectFolderInfo projectFolder, string dir, string extension)
 {
     return Directory.GetFiles(dir, "*" + extension).Any() || Directory.GetDirectories(dir).Any(x => ExistFile(projectFolder, x, extension));
 }
        private static string GetContainerTcmId(MappingInfo mapping, TridionRole tridionRole, ProjectFolderInfo folder, string path)
        {
            if (folder != null && !String.IsNullOrEmpty(folder.TcmId))
                return folder.TcmId;

            if (mapping.TridionFolders.Any(x => x.TridionRole == tridionRole))
            {
                if (mapping.TridionFolders.Count(x => x.TridionRole == tridionRole) == 1)
                {
                    string tcm = mapping.TridionFolders.First(x => x.TridionRole == tridionRole).TcmId;
                    if (folder != null)
                        folder.TcmId = tcm;
                    return tcm;
                }

                SelectTridionFolderDialogWindow dialog = new SelectTridionFolderDialogWindow();
                dialog.Path = path;
                dialog.TridionFolders = mapping.TridionFolders.Where(x => x.TridionRole == tridionRole).ToList().FillNamedPath(mapping);
                bool res = dialog.ShowDialog() == true;
                if (res)
                {
                    return dialog.SelectedTridionFolder.TcmId;
                }
            }

            return String.Empty;
        }
 private static string GetContainerTcmId(MappingInfo mapping, TridionRole tridionRole, ProjectFolderInfo folder)
 {
     if(folder == null)
         return String.Empty;
     return GetContainerTcmId(mapping, tridionRole, folder, folder.Path);
 }
 public static void SetRootPath(ProjectFolderInfo folder, string rootPath)
 {
     folder.RootPath = rootPath;
     if (folder.ChildItems != null)
     {
         foreach (ProjectItemInfo childItem in folder.ChildItems)
         {
             childItem.Parent = folder;
             if (childItem.IsFile)
             {
                 childItem.RootPath = rootPath;
             }
             if (childItem.IsFolder)
             {
                 SetRootPath((ProjectFolderInfo) childItem, rootPath);
             }
         }
     }
 }
        public static ProjectFolderInfo ClearProjectFolder(this ProjectFolderInfo projectFolder, bool checkChecked)
        {
            if (projectFolder == null)
                return null;

            if (checkChecked && projectFolder.Checked == false)
                return null;

            ProjectFolderInfo clearedProjectFolder = new ProjectFolderInfo();
            clearedProjectFolder.Path = projectFolder.Path;
            clearedProjectFolder.Checked = projectFolder.Checked;
            clearedProjectFolder.SyncTemplate = projectFolder.SyncTemplate;
            clearedProjectFolder.TemplateFormat = projectFolder.TemplateFormat;
            clearedProjectFolder.ProjectFolderRole = projectFolder.ProjectFolderRole;
            clearedProjectFolder.TcmId = projectFolder.TcmId;

            if (projectFolder.ChildItems != null && projectFolder.ChildItems.Count > 0)
            {
                clearedProjectFolder.ChildItems = new List<ProjectItemInfo>();
                foreach (ProjectItemInfo childItem in projectFolder.ChildItems)
                {
                    if (childItem.IsFolder)
                    {
                        ProjectFolderInfo clearedChildProjectFolder = ((ProjectFolderInfo)childItem).ClearProjectFolder(true);
                        if (clearedChildProjectFolder != null)
                            clearedProjectFolder.ChildItems.Add(clearedChildProjectFolder);
                    }
                    if (childItem.IsFile)
                    {
                        if (childItem.Checked == true)
                        {
                            clearedProjectFolder.ChildItems.Add(childItem);
                        }
                    }
                }
                if (clearedProjectFolder.ChildItems.Count == 0)
                    clearedProjectFolder.ChildItems = null;
            }

            return clearedProjectFolder;
        }
        //public static void SetCode(string path, string newCode)
        //{
        //    ProjectItem projectItem = Project.ProjectItems.AddFromFile(path);
        //    EnvDTE.Window editWindow = projectItem.Open(Constants.vsext_vk_Code);
        //    editWindow.Visible = false;
        //    TextDocument textDocument = (TextDocument)editWindow.Document.Object("TextDocument");
        //    EditPoint editPoint = textDocument.StartPoint.CreateEditPoint();
        //    editPoint.Delete(textDocument.EndPoint);
        //    editPoint.Insert(newCode);
        //    editWindow.Close(vsSaveChanges.vsSaveChangesYes);
        //    Marshal.ReleaseComObject(textDocument);
        //    Marshal.ReleaseComObject(editPoint);
        //    Marshal.ReleaseComObject(projectItem);
        //}
        public static void ProcessFolder(MappingInfo mapping, ProjectFolderInfo folder)
        {
            if (folder == null || folder.ChildItems == null)
                return;

            if (folder.Checked == false)
                return;

            ProjectFolderRole role = folder.ProjectFolderRole;
            TridionRole tridionRole = TridionRole.Other;

            if (role == TridionVSRazorExtension.ProjectFolderRole.PageLayout)
            {
                tridionRole = TridionRole.PageLayoutContainer;
            }

            if (role == TridionVSRazorExtension.ProjectFolderRole.ComponentLayout)
            {
                tridionRole = TridionRole.ComponentLayoutContainer;
            }

            if (role == TridionVSRazorExtension.ProjectFolderRole.Binary)
            {
                tridionRole = TridionRole.MultimediaComponentContainer;
            }

            string tcmItemContainer = GetContainerTcmId(mapping, tridionRole, folder);

            if (String.IsNullOrEmpty(tcmItemContainer))
            {
                WriteErrorLog("Could not find Tridion mapping for role " + tridionRole);
                return;
            }

            foreach (ProjectItemInfo item in folder.ChildItems)
            {
                item.Parent = folder;

                if (item.IsFile)
                {
                    ProcessFile(mapping, (ProjectFileInfo)item);
                }
                if (item.IsFolder)
                {
                    ProcessFolder(mapping, (ProjectFolderInfo)item);
                }
            }
        }
        public static ProjectFolderInfo GetRootTree(ProjectFolderInfo projectFolder, string fullPath)
        {
            if (projectFolder == null)
                return null;

            if (!Directory.Exists(fullPath))
                return null;

            if (!ExistFile(projectFolder, fullPath) && projectFolder.FullPath != fullPath)
                return null;

            if (projectFolder.FullPath == fullPath)
            {
                projectFolder.IsSelected = true;
                projectFolder.Expand();
                return projectFolder;
            }

            string rootPath = projectFolder.RootPath;

            ProjectFolderInfo parentFolder = new ProjectFolderInfo { RootPath = rootPath, Path = fullPath.Replace(rootPath, "").Trim('\\'), Checked = false };
            parentFolder.ChildItems = new List<ProjectItemInfo>();

            foreach (string childFolderPath in Directory.GetDirectories(fullPath))
            {
                ProjectFolderInfo childFolder = GetRootTree(projectFolder, childFolderPath);
                if (childFolder != null)
                {
                    childFolder.Parent = parentFolder;
                    parentFolder.ChildItems.Add(childFolder);
                }
            }

            foreach (string childFilePath in GetFiles(fullPath, projectFolder.ProjectFolderRole == TridionVSRazorExtension.ProjectFolderRole.Binary ? Extensions.Keys.ToArray() : new[] { "*.cshtml" }))
            {
                ProjectFileInfo childFile = new ProjectFileInfo { RootPath = rootPath, Path = childFilePath.Replace(rootPath, "").Trim('\\'), Checked = false };
                childFile.Parent = parentFolder;
                parentFolder.ChildItems.Add(childFile);
            }

            if (parentFolder.ChildItems.Count == 0)
                parentFolder.ChildItems = null;

            return parentFolder;
        }
        public static IEnumerable<ProjectFolderInfo> GetFileTree(ProjectFolderInfo projectFolder, string rootPath)
        {
            AddChildItems(projectFolder, rootPath);

            ProjectFolderInfo rootFolder = GetRootTree(projectFolder, rootPath);

            return new List<ProjectFolderInfo> { rootFolder };
        }