private IEnumerable <PowershellFileParser> GetFileList(string path, FilesPatternProvider filesPatternProvider, BackgroundIndexer worker)
        {
            PowershellFileParser parser           = null;
            Queue <string>       pathsToEnumerate = new Queue <string>();

            if (File.Exists(path) && filesPatternProvider.DoesFileMatch(path))
            {
                parser = new PowershellFileParser(path, isDirectory: false);
                yield return(parser);

                this.ReportProgress(worker, path);
                yield break;
            }
            if (!Directory.Exists(path) || !filesPatternProvider.DoesDirectoryMatch(path))
            {
                yield break;
            }
            parser = new PowershellFileParser(path, isDirectory: true);
            yield return(parser);

            pathsToEnumerate.Enqueue(path);
            while (pathsToEnumerate.Any())
            {
                IEnumerable <string> dirs = null;
                parser = null;
                string currentPath = pathsToEnumerate.Dequeue();

                foreach (var file in this.GetFilesInDirectory(currentPath, filesPatternProvider))
                {
                    yield return(file);
                }

                try {
                    dirs = Directory.EnumerateDirectories(currentPath).Where(dir => filesPatternProvider.DoesDirectoryMatch(dir));
                } catch (Exception e)
                {
                    parser = new PowershellFileParser(currentPath, isDirectory: true, errorMessage: e.Message);
                }
                if (parser != null)
                {
                    yield return(parser);

                    continue;
                }
                foreach (string dir in dirs)
                {
                    if (filesPatternProvider.DoesDirectoryMatch(dir) && (filesPatternProvider.IncludeAllFiles || filesPatternProvider.IsInAdditonalPaths(dir)))
                    {
                        parser = new PowershellFileParser(dir, isDirectory: true);
                        yield return(parser);
                    }
                    pathsToEnumerate.Enqueue(dir);
                }
                this.ReportProgress(worker, currentPath);
            }
            while (pathsToEnumerate.Any())
            {
                ;
            }
        }
        public static string GetRootDirectoryToSearch(string filePath)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                return(null);
            }
            string driveRoot = Path.GetPathRoot(filePath).ToLowerInvariant();
            string rootDir   = Path.GetDirectoryName(filePath);

            if (String.IsNullOrEmpty(rootDir))
            {
                return(null);
            }
            var    filesPatternProvider = new FilesPatternProvider(false);
            string currentDir           = rootDir;

            while (true)
            {
                var currentDirInfo = Directory.GetParent(currentDir);
                if (currentDirInfo == null || currentDirInfo.FullName.ToLowerInvariant() == driveRoot)
                {
                    // TODO: why root dir is disallowed?
                    return(rootDir.ToLowerInvariant() == driveRoot ? null : rootDir);
                }
                currentDir = currentDirInfo.FullName;
                if (!filesPatternProvider.DoesDirectoryMatch(currentDir))
                {
                    continue;
                }
                IList <string> allFilesInCurrentDir;
                try
                {
                    allFilesInCurrentDir = Directory.GetFiles(currentDir).ToList();
                }
                catch (IOException)
                {
                    return(null);
                }
                foreach (string file in allFilesInCurrentDir)
                {
                    if (filesPatternProvider.DoesFileMatch(file))
                    {
                        rootDir = currentDir;
                    }
                    if (filesPatternProvider.IsModuleFile(file))
                    {
                        // TODO: why root dir is disallowed?
                        return(rootDir.ToLowerInvariant() == driveRoot ? null : rootDir);
                    }
                }
            }
        }
        public static string GetRootDirectoryToSearch(string filePath)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                return null;
            }
            string driveRoot = Path.GetPathRoot(filePath).ToLowerInvariant();
            string rootDir = Path.GetDirectoryName(filePath);
            if (String.IsNullOrEmpty(rootDir))
            {
                return null;
            }
            var filesPatternProvider = new FilesPatternProvider(false);
            string currentDir = rootDir;
            while (true)
            {
                var currentDirInfo = Directory.GetParent(currentDir);
                if (currentDirInfo == null || currentDirInfo.FullName.ToLowerInvariant() == driveRoot)
                {
                    // TODO: why root dir is disallowed?
                    return (rootDir.ToLowerInvariant() == driveRoot ? null : rootDir);
                }
                currentDir = currentDirInfo.FullName;
                if (!filesPatternProvider.DoesDirectoryMatch(currentDir))
                {
                    continue;
                }
                IList<string> allFilesInCurrentDir;
                try
                {
                    allFilesInCurrentDir = Directory.GetFiles(currentDir).ToList();
                }
                catch (IOException)
                {
                    return null;
                }
                foreach (string file in allFilesInCurrentDir)
                {
                    if (filesPatternProvider.DoesFileMatch(file))
                    {
                        rootDir = currentDir;
                    }
                    if (filesPatternProvider.IsModuleFile(file))
                    {
                        // TODO: why root dir is disallowed?
                        return (rootDir.ToLowerInvariant() == driveRoot ? null : rootDir);
                    }
                }

            }
        }
        private IEnumerable<PowershellFileParser> GetFileList(string path, FilesPatternProvider filesPatternProvider, BackgroundIndexer worker)
        {
            PowershellFileParser parser = null;
            Queue<string> pathsToEnumerate = new Queue<string>();

            if (File.Exists(path) && filesPatternProvider.DoesFileMatch(path))
            {
                parser = new PowershellFileParser(path, isDirectory: false);
                yield return parser;
                ReportProgress(worker, path);
                yield break;
            }
            if (!Directory.Exists(path) || !filesPatternProvider.DoesDirectoryMatch(path))
            {
                yield break;
            }
            parser = new PowershellFileParser(path, isDirectory: true);
            yield return parser;
            pathsToEnumerate.Enqueue(path);
            while (pathsToEnumerate.Any())
            {
                IEnumerable<string> dirs = null;
                parser = null;
                string currentPath = pathsToEnumerate.Dequeue();

                foreach (var file in GetFilesInDirectory(currentPath, filesPatternProvider))
                {
                    yield return file;
                }

                try {
                    dirs = Directory.EnumerateDirectories(currentPath).Where(dir => filesPatternProvider.DoesDirectoryMatch(dir));
                } catch (Exception e)
                {
                    parser = new PowershellFileParser(currentPath, isDirectory: true, errorMessage: e.Message);
                }
                if (parser != null)
                {
                    yield return parser;
                    continue;
                }
                foreach (string dir in dirs)
                {
                    if (filesPatternProvider.DoesDirectoryMatch(dir) && (filesPatternProvider.IncludeAllFiles || filesPatternProvider.IsInAdditonalPaths(dir)))
                    {
                        parser = new PowershellFileParser(dir, isDirectory: true);
                        yield return parser;
                    }
                    pathsToEnumerate.Enqueue(dir);
                }
                ReportProgress(worker, currentPath);
            } while (pathsToEnumerate.Any());
        }