public List <FileItem> GetFiles(HashSet <string> extensions, HashSet <string> blackList, GetFilesUpdate onProgress = null) { var location = Path; var list = new List <FileItem>(); if (Directory.Exists(location)) { var allowedDirs = GetAllowedFolders(); // var gitIgnores = GitIgnoreReader.Find(location, SearchOption.AllDirectories).Select(p => GitIgnoreReader.Load(p)); var queue = new Queue <KeyValuePair <string, IEnumerable <GitIgnoreReader> > >(); var dirsCount = 0; GetFiles(location); void GetFiles(string dir) { var relevantGitFiles = gitIgnores.Where(f => GitIgnoreReader.IsChildedPath(f.BaseDir, dir)); var isAllowedDir = allowedDirs.Count > 0 ? allowedDirs.Any(p => GitIgnoreReader.IsChildedPath(p, dir)) : true; var subs = Directory.EnumerateDirectories(dir) .Where(p => !IsIgnoredForder(p)) .Select(s => s.Replace('\\', '/')); var files = Directory.EnumerateFiles(dir) .Where(s => isAllowedDir) .Where(s => extensions.Contains(PathIO.GetExtension(s))) .Where(s => blackList.All(end => !s.EndsWith(end))) .Select(s => s.Replace('\\', '/')); dirsCount += subs.Count(); onProgress?.Invoke(queue.Count, dirsCount, dir); foreach (var file in files) { queue.Enqueue(new KeyValuePair <string, IEnumerable <GitIgnoreReader> >(file, relevantGitFiles)); } foreach (var sub in subs.Select(s => s + '/')) { var isMatch = relevantGitFiles.All(r => r.IsMatch(sub)); if (isMatch) { GetFiles(sub); } } } while (queue.Count > 0) { var pair = queue.Dequeue(); var isMatch = pair.Value.All(r => r.IsMatch(pair.Key)); list.Add(new FileItem(pair.Key, isMatch)); onProgress?.Invoke(queue.Count, 0, pair.Key); } } return(list); }
public IEnumerable<string> GetFilesData(Project project, HashSet<string> extensions, HashSet<string> blackList, GetFilesUpdate onProgress = null) { if (!Directory.Exists(project.Location)) { throw new System.Exception($"Project '{project.Title}' has no existing folder on path '{project.Location}'"); } var location = project.Location; var repos = FindGitRepositories(location).ToArray(); var allFiles = new List<string>(); if (repos.Any()) { var nonGitFiles = Directory .EnumerateFiles(location, "*", SearchOption.AllDirectories) .Where(x => repos.All(y => !IsChildedPath(y, x))); allFiles.AddRange(nonGitFiles); foreach (var repo in repos) { var files = FindFilesInGitRepo(repo); allFiles.AddRange(files); } } else { var nonGitFiles = Directory .EnumerateFiles(location, "*", SearchOption.AllDirectories); allFiles.AddRange(nonGitFiles); } var filteredFiles = allFiles .Where(x => extensions.Contains(Path.GetExtension(x).Trim(), StringComparer.OrdinalIgnoreCase)) .Where(x => blackList.All(end => !x.EndsWith(end, StringComparison.OrdinalIgnoreCase))) .ToArray(); var allowedFiles = GetAllowedFiles(project, filteredFiles); var result = allowedFiles.AsParallel().ToArray(); return result; }