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 IEnumerable<ProjectFolderInfo> GetFileTree(ProjectFolderInfo projectFolder, string rootPath) { AddChildItems(projectFolder, rootPath); ProjectFolderInfo rootFolder = GetRootTree(projectFolder, rootPath); return new List<ProjectFolderInfo> { rootFolder }; }
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 IO.Service.GetFiles(fullPath, projectFolder.ProjectFolderRole == Misc.ProjectFolderRole.Binary ? Const.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 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 == Misc.ProjectFolderRole.Binary ? Const.Extensions.Keys.ToArray() : new[] { "*.cshtml" }; string[] directories = Directory.GetDirectories(projectFolder.FullPath); string[] files = IO.Service.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 bool ExistFile(ProjectFolderInfo projectFolder, string dir) { if (projectFolder.ProjectFolderRole == Misc.ProjectFolderRole.Binary) { return Const.Extensions.Keys.Select(extension => ExistFile(projectFolder, dir, extension)).Any(x => x); } return ExistFile(projectFolder, dir, "*.cshtml"); }
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)); }
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 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; }