Ejemplo n.º 1
0
        public async Task AttachFileObserver(IBundleDocument document, string fileName, Func <string, bool, Task> updateBundle)
        {
            _document       = document;
            _bundleFileName = document.FileName;
            fileName        = Path.GetFullPath(fileName);

            if (!File.Exists(fileName))
            {
                return;
            }

            _watcher = new FileSystemWatcher
            {
                Path   = Path.GetDirectoryName(fileName),
                Filter = Path.GetFileName(fileName),
                //_watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.LastWrite | NotifyFilters.Size;
                NotifyFilter = NotifyFilters.Attributes |
                               NotifyFilters.CreationTime |
                               NotifyFilters.FileName |
                               NotifyFilters.LastAccess |
                               NotifyFilters.LastWrite |
                               NotifyFilters.Size
            };

            using (await _rwLock.ReadLockAsync())
            {
                if (WatchedFiles.ContainsKey(_bundleFileName) && WatchedFiles[_bundleFileName].Any(s => s.Item1.Equals(fileName, StringComparison.OrdinalIgnoreCase)))
                {
                    return;
                }
            }

            _changeEvent = (_, __) => Changed(updateBundle);

            _watcher.Changed += _changeEvent;
            _watcher.Deleted += (_, __) => Deleted(fileName);

            if (_extensions.Any(e => fileName.EndsWith(e, StringComparison.OrdinalIgnoreCase)))
            {
                _watcher.Renamed += (_, renamedEventArgument) => Renamed(renamedEventArgument, updateBundle);
            }

            _watcher.EnableRaisingEvents = true;

            using (await _rwLock.WriteLockAsync())
            {
                if (!WatchedFiles.ContainsKey(_bundleFileName))
                {
                    WatchedFiles.Add(_bundleFileName, new HashSet <Tuple <string, FileSystemWatcher> >());
                }

                WatchedFiles[_bundleFileName].Add(new Tuple <string, FileSystemWatcher>(fileName, _watcher));
            }
        }
        public async Task AttachFileObserver(IBundleDocument document, string fileName, Func<string, bool, Task> updateBundle)
        {
            _document = document;
            _bundleFileName = document.FileName;
            fileName = Path.GetFullPath(fileName);

            if (!File.Exists(fileName))
                return;

            _watcher = new FileSystemWatcher
            {
                Path = Path.GetDirectoryName(fileName),
                Filter = Path.GetFileName(fileName),
                //_watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.LastWrite | NotifyFilters.Size;
                NotifyFilter = NotifyFilters.Attributes |
                               NotifyFilters.CreationTime |
                               NotifyFilters.FileName |
                               NotifyFilters.LastAccess |
                               NotifyFilters.LastWrite |
                               NotifyFilters.Size
            };

            using (await _rwLock.ReadLockAsync())
            {
                if (WatchedFiles.ContainsKey(_bundleFileName) && WatchedFiles[_bundleFileName].Any(s => s.Item1.Equals(fileName, StringComparison.OrdinalIgnoreCase)))
                    return;
            }

            _changeEvent = (_, __) => Changed(updateBundle);

            _watcher.Changed += _changeEvent;
            _watcher.Deleted += (_, __) => Deleted(fileName);

            if (_extensions.Any(e => fileName.EndsWith(e, StringComparison.OrdinalIgnoreCase)))
                _watcher.Renamed += (_, renamedEventArgument) => Renamed(renamedEventArgument, updateBundle);

            _watcher.EnableRaisingEvents = true;

            using (await _rwLock.WriteLockAsync())
            {
                if (!WatchedFiles.ContainsKey(_bundleFileName))
                    WatchedFiles.Add(_bundleFileName, new HashSet<Tuple<string, FileSystemWatcher>>());

                WatchedFiles[_bundleFileName].Add(new Tuple<string, FileSystemWatcher>(fileName, _watcher));
            }
        }
Ejemplo n.º 3
0
        private void Renamed(RenamedEventArgs renamedEventArgument, Func <string, bool, Task> updateBundle)
        {
            Task.Run(async() =>
            {
                using (await rwLock.ReadLockAsync())
                {
                    if (!_watchedFiles.ContainsKey(renamedEventArgument.OldFullPath) ||
                        !renamedEventArgument.FullPath.StartsWith(ProjectHelpers.GetSolutionFolderPath(), StringComparison.OrdinalIgnoreCase))
                    {
                        return;
                    }
                }

                HashSet <Tuple <string, FileSystemWatcher> > oldValue;

                using (await rwLock.ReadLockAsync())
                {
                    oldValue = _watchedFiles[renamedEventArgument.OldFullPath];
                }

                using (await rwLock.WriteLockAsync())
                {
                    _watchedFiles.Remove(renamedEventArgument.OldFullPath);
                }

                _document = await _document.LoadFromFile(renamedEventArgument.FullPath);

                foreach (Tuple <string, FileSystemWatcher> tuple in oldValue)
                {
                    tuple.Item2.EnableRaisingEvents = false;

                    tuple.Item2.Dispose();

                    if (_extensions.Any(e => tuple.Item1.EndsWith(e, StringComparison.OrdinalIgnoreCase)))
                    {
                        await AttachFileObserver(_document, _document.FileName, updateBundle);
                    }
                    else
                    {
                        await AttachFileObserver(_document, tuple.Item1, updateBundle);
                    }
                }
            }).Wait();
        }
Ejemplo n.º 4
0
        private void Changed(Func <string, bool, Task> updateBundle)
        {
            _watcher.EnableRaisingEvents = false;
            _watcher.Changed            -= _changeEvent;

            Task.Run(async() =>
            {
                _document = await _document.LoadFromFile(_bundleFileName);

                if (_document == null)
                {
                    return;
                }

                await updateBundle(_bundleFileName, false);

                IEnumerable <Tuple <string, FileSystemWatcher> > tuples;

                using (await _rwLock.ReadLockAsync())
                {
                    tuples = WatchedFiles[_bundleFileName].Where(x => !_extensions.Any(e => x.Item1.EndsWith(e)) && !_document.BundleAssets.Contains(x.Item1, StringComparer.OrdinalIgnoreCase));
                }

                using (await _rwLock.WriteLockAsync())
                {
                    IList <Tuple <string, FileSystemWatcher> > enumerable = tuples as IList <Tuple <string, FileSystemWatcher> > ?? tuples.ToList();
                    StopMonitoring(enumerable);

                    WatchedFiles[_bundleFileName].RemoveWhere(x => enumerable.Contains(x));
                }
            }).Wait();

            try
            {
                _watcher.EnableRaisingEvents = true;
                _watcher.Changed            += _changeEvent;
            }
            catch (FileNotFoundException)
            {
                //Well, if the file doesn't exists anymore, there is no use for this observer.
                Dispose();
            }
        }
        private void Renamed(RenamedEventArgs renamedEventArgument, Func<string, bool, Task> updateBundle)
        {
            Task.Run(async () =>
            {
                using (await rwLock.ReadLockAsync())
                {
                    if (!_watchedFiles.ContainsKey(renamedEventArgument.OldFullPath) ||
                        !renamedEventArgument.FullPath.StartsWith(ProjectHelpers.GetSolutionFolderPath(), StringComparison.OrdinalIgnoreCase))
                        return;
                }

                HashSet<Tuple<string, FileSystemWatcher>> oldValue;

                using (await rwLock.ReadLockAsync())
                {
                    oldValue = _watchedFiles[renamedEventArgument.OldFullPath];
                }

                using (await rwLock.WriteLockAsync())
                {
                    _watchedFiles.Remove(renamedEventArgument.OldFullPath);
                }

                _document = await _document.LoadFromFile(renamedEventArgument.FullPath);

                foreach (Tuple<string, FileSystemWatcher> tuple in oldValue)
                {
                    tuple.Item2.EnableRaisingEvents = false;

                    tuple.Item2.Dispose();

                    if (_extensions.Any(e => tuple.Item1.EndsWith(e, StringComparison.OrdinalIgnoreCase)))
                        await AttachFileObserver(_document, _document.FileName, updateBundle);
                    else
                        await AttachFileObserver(_document, tuple.Item1, updateBundle);
                }
            }).Wait();
        }
        private void Changed(Func<string, bool, Task> updateBundle)
        {
            _watcher.EnableRaisingEvents = false;

            Task.Run(async () =>
            {
                _document = await _document.LoadFromFile(_bundleFileName);

                if (_document == null)
                    return;

                await updateBundle(_bundleFileName, false);

                IEnumerable<Tuple<string, FileSystemWatcher>> tuples;

                using (await rwLock.ReadLockAsync())
                {
                    tuples = _watchedFiles[_bundleFileName].Where(x => !_extensions.Any(e => x.Item1.EndsWith(e)) && !_document.BundleAssets.Contains(x.Item1, StringComparer.OrdinalIgnoreCase));
                }

                using (await rwLock.WriteLockAsync())
                {
                    StopMonitoring(tuples);

                    _watchedFiles[_bundleFileName].RemoveWhere(x => tuples.Contains(x));
                }
            }).Wait();

            try
            {
                _watcher.EnableRaisingEvents = true;
            }
            catch (FileNotFoundException)
            {
                //Well, if the file doesn't exists anymore, there is no use for this observer.
                Dispose();
            }
        }
        private async void Changed(Func<string, bool, Task> updateBundle)
        {
            _watcher.EnableRaisingEvents = false;

            _document = await _document.LoadFromFile(_bundleFileName);

            if (_document == null)
                return;

            await updateBundle(_bundleFileName, false);

            IEnumerable<Tuple<string, FileSystemWatcher>> tuples;

            using (await rwLock.ReadLockAsync())
            {
                tuples = _watchedFiles[_bundleFileName].Where(x => !_extensions.Any(e => x.Item1.EndsWith(e)) && !_document.BundleAssets.Contains(x.Item1, StringComparer.OrdinalIgnoreCase));
            }

            using (await rwLock.WriteLockAsync())
            {
                StopMonitoring(tuples);

                _watchedFiles[_bundleFileName].RemoveWhere(x => tuples.Contains(x));
            }

            _watcher.EnableRaisingEvents = true;
        }