Exemplo n.º 1
0
        public bool UpdateDocumentHierarchy(IEnumerable <string> pathsToUpdate, FilesPatternProvider filesPatternProvider, BackgroundIndexer worker)
        {
            if (this.DocumentHierarchy == null)
            {
                return(false);
            }
            bool changed = false;

            foreach (string path in pathsToUpdate)
            {
                INode node = this.DocumentHierarchy.GetNode(path);
                bool  nodeShouldBeRemoved = node != null;
                var   fileSystemEntryList = this.GetFileList(path, filesPatternProvider, worker);

                foreach (PowershellParseResult fileSystemEntry in fileSystemEntryList)
                {
                    // this is to prevent from reporting progress after deletion if the node is only updated
                    if (fileSystemEntry.Path == path && node != null)
                    {
                        this.DocumentHierarchy.RemoveNode(node);
                        nodeShouldBeRemoved = false;
                    }
                    this.documentHierarchyIndexer.AddFileSystemNode(this.DocumentHierarchy, fileSystemEntry);
                    changed = true;
                }
                if (nodeShouldBeRemoved)
                {
                    this.DocumentHierarchy.RemoveNode(node);
                    changed = true;
                    this.ReportProgress(worker, path);
                }
            }
            return(changed);
        }
 public void Watch(string path, FilesPatternProvider filesPatternProvider)
 {
     lock (FileSystemChangeNotifier)
     {
         this.Watcher.EnableRaisingEvents = false;
         if (path != RootPath)
         {
             this.FileSystemChangeNotifier.ClearChangePool();
         }
         this.RootPath = path;
         if (String.IsNullOrEmpty(path) || !Directory.Exists(path))
         {
             return;
         }
         this.FilesPatternProvider          = filesPatternProvider;
         this.Watcher.Path                  = path;
         this.Watcher.InternalBufferSize    = 65536;
         this.Watcher.NotifyFilter          = NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Security;
         this.Watcher.IncludeSubdirectories = true;
         this.Watcher.Changed              += OnFileChanged;
         this.Watcher.Created              += OnFileChanged;
         this.Watcher.Deleted              += OnFileChanged;
         this.Watcher.Renamed              += OnFileRenamed;
         this.Watcher.EnableRaisingEvents   = true;
     }
 }
        public bool UpdateDocumentHierarchy(IEnumerable<string> pathsToUpdate, FilesPatternProvider filesPatternProvider, BackgroundIndexer worker)
        {
            if (DocumentHierarchy == null)
            {
                return false;
            }
            var documentHierarchyIndexer = new DocumentHierarchyIndexer(DocumentHierarchy);
            bool changed = false;
            foreach (string path in pathsToUpdate)
            {
                INode node = DocumentHierarchy.GetNode(path);
                bool nodeShouldBeRemoved = node != null;
                var fileSystemEntryList = GetFileList(path, filesPatternProvider, worker);

                foreach (PowershellFileParser fileSystemEntry in fileSystemEntryList)
                {
                    // this is to prevent from reporting progress after deletion if the node is only updated
                    if (fileSystemEntry.Path == path && node != null)
                    {
                        DocumentHierarchy.RemoveNode(node);
                        nodeShouldBeRemoved = false;
                    }
                    documentHierarchyIndexer.AddFileSystemNode(fileSystemEntry);
                    changed = true;
                }
                if (nodeShouldBeRemoved)
                {
                    DocumentHierarchy.RemoveNode(node);
                    changed = true;
                    ReportProgress(worker, path);
                }
            }
            return changed;
        }
Exemplo n.º 4
0
        private IEnumerable <PowershellParseResult> GetFilesInDirectory(string path, FilesPatternProvider filesPatternProvider)
        {
            IEnumerable <string>  files       = null;
            PowershellParseResult parseResult = null;

            try
            {
                files = Directory.GetFiles(path, filesPatternProvider.GetFilesPattern()).Where(f => filesPatternProvider.DoesFileMatch(f));
            }
            catch (Exception e)
            {
                parseResult = this.powershellFileParser.ParseFile(path, isDirectory: true, isExcluded: false, errorMessage: e.Message);
            }

            if (parseResult != null)
            {
                yield return(parseResult);

                yield break;
            }

            foreach (string file in files)
            {
                parseResult = this.powershellFileParser.ParseFile(file, isDirectory: false, isExcluded: filesPatternProvider.IsExcludedFromIndexing(file), errorMessage: null);
                yield return(parseResult);
            }
        }
        private IEnumerable <PowershellFileParser> GetFilesInDirectory(string path, FilesPatternProvider filesPatternProvider)
        {
            IEnumerable <string> files  = null;
            PowershellFileParser parser = null;

            try
            {
                files = Directory.GetFiles(path, filesPatternProvider.GetFilesPattern()).Where(f => filesPatternProvider.DoesFileMatch(f));
            }
            catch (Exception e)
            {
                parser = new PowershellFileParser(path, isDirectory: true, errorMessage: e.Message);
            }

            if (parser != null)
            {
                yield return(parser);

                yield break;
            }

            foreach (string file in files)
            {
                parser = new PowershellFileParser(file, isDirectory: false);
                yield return(parser);
            }
        }
        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 TreeViewModel(FileSystemChangeWatcher fileSystemChangeWatcher, DocumentHierarchyFactory documentHierarchyFactory, FilesPatternProvider filesPatternProvider)
 {
     FileSystemChangeWatcher = fileSystemChangeWatcher;
     DocumentHierarchyFactory = documentHierarchyFactory;
     FilesPatternProvider = filesPatternProvider;
     ItemsMap = new Dictionary<string, TreeViewEntryItemModel>();
 }
 public void Watch(string path, FilesPatternProvider filesPatternProvider)
 {
     lock (FileSystemChangeNotifier)
     {
         Watcher.EnableRaisingEvents = false;
         if (path != RootPath)
         {
             FileSystemChangeNotifier.ClearChangePool();
         }
         RootPath = path;
         if (String.IsNullOrEmpty(path) || !Directory.Exists(path))
         {
             return;
         }
         FilesPatternProvider = filesPatternProvider;
         Watcher.Path = path;
         Watcher.InternalBufferSize = 65536;
         Watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Security;
         Watcher.IncludeSubdirectories = true;
         Watcher.Changed += OnFileChanged;
         Watcher.Created += OnFileChanged;
         Watcher.Deleted += OnFileChanged;
         Watcher.Renamed += OnFileRenamed;
         Watcher.EnableRaisingEvents = true;
     }
 }
 public BackgroundIndexerParams(DocumentHierarchyFactory documentHierarchyFactory, string rootDirectory, IEnumerable<string> pathsChanged, FilesPatternProvider filesPatternProvider)
 {
     DocumentHierarchyFactory = documentHierarchyFactory;
     RootDirectory = rootDirectory;
     PathsChanged = pathsChanged;
     FilesPatternProvider = filesPatternProvider;
 }
Exemplo n.º 10
0
 public TreeViewModel(FileSystemChangeWatcher fileSystemChangeWatcher, DocumentHierarchyFactory documentHierarchyFactory, FilesPatternProvider filesPatternProvider)
 {
     this.FileSystemChangeWatcher = fileSystemChangeWatcher;
     this.DocumentHierarchyFactory = documentHierarchyFactory;
     this.FilesPatternProvider = filesPatternProvider;
     this.ItemsMap = new Dictionary<string, TreeViewEntryItemModel>();
     this.TokenLocator = new TokenLocator();
 }
        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);
                    }
                }

            }
        }
 public PowershellFileParser(string path, bool isDirectory, string errorMessage)
 {
     this.Path         = path;
     this.IsDirectory  = isDirectory;
     this.ErrorMessage = errorMessage;
     if (!this.IsDirectory && FilesPatternProvider.IsPowershellFile(path))
     {
         this.ParseFile();
     }
     else
     {
         this.FileContents = string.Empty;
     }
 }
Exemplo n.º 14
0
        public PowershellParseResult ParseFile(string path, bool isDirectory, bool isExcluded, string errorMessage)
        {
            if (isExcluded || isDirectory || !FilesPatternProvider.IsPowershellFile(path))
            {
                return(new PowershellParseResult(null, errorMessage, path, null, isDirectory, isExcluded));
            }
            string fileContents;

            try
            {
                fileContents = fileReader.ReadFileAsString(path);
            }
            catch (Exception e)
            {
                return(new PowershellParseResult(null, e.Message, path, null, isDirectory, isExcluded));
            }
            if (fileContents != null)
            {
                var rootPowershellItem = this.powershellTokenizer.GetPowershellItems(path, fileContents);
                return(new PowershellParseResult(rootPowershellItem, errorMessage, path, fileContents, isDirectory, isExcluded));
            }
            return(new PowershellParseResult(null, errorMessage, path, fileContents, isDirectory, isExcluded));
        }
Exemplo n.º 15
0
 public FileSystemChangeWatcher(FileSystemOperationsService fileSystemOperationsService, FilesPatternProvider filesPatternProvider)
 {
     this.fileSystemOperationsService = fileSystemOperationsService;
     this.filesPatternProvider        = filesPatternProvider;
 }
        private IEnumerable<PowershellFileParser> GetFilesInDirectory(string path, FilesPatternProvider filesPatternProvider)
        {
            IEnumerable<string> files = null;
            PowershellFileParser parser = null;
            try
            {
                files = Directory.GetFiles(path, filesPatternProvider.GetFilesPattern()).Where(f => filesPatternProvider.DoesFileMatch(f));
            }
            catch (Exception e)
            {
                parser = new PowershellFileParser(path, isDirectory: true, errorMessage: e.Message);
            }

            if (parser != null)
            {
                yield return parser;
                yield break;
            }

            foreach (string file in files)
            {
                parser = new PowershellFileParser(file, isDirectory: false);
                yield return parser;
            }
        }
        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());
        }
 public FileSystemChangeWatcher(FileSystemOperationsService fileSystemOperationsService, FilesPatternProvider filesPatternProvider)
 {
     this.fileSystemOperationsService = fileSystemOperationsService;
     this.filesPatternProvider = filesPatternProvider;
 }