public IObservable <ChangedFile> CreateFileSystemWatcher(string path, string mask) { WatchPath = path; if (!_fileSystem.Path.IsPathRooted(WatchPath)) { WatchPath = _fileSystem.Path.Combine(Environment.CurrentDirectory, path); } Logger.LogInformation("Watching files under path {Path}", WatchPath); var dir = _fileSystem.Path.GetDirectoryName(WatchPath); var ofsw = new ObservableFileSystemWatcher( new FileSystemWatcher(dir, mask ?? "*.cs") { EnableRaisingEvents = true, IncludeSubdirectories = true, }); Logger.LogInformation("File watcher {FileWatcher} started with config {@Config}", ofsw, _config); var ret = Observable .Merge(ofsw.Changed, ofsw.Created, ofsw.Renamed, ofsw.Deleted) .Where(x => !_config.Ignore.Any(i => x.FullPath.Contains((string)i))) .Select(TryGetChangedFile) .Where(x => x != null) .Do(f => Logger.LogInformation("Changed File: {ChangedFile}", f.Path.Substring(WatchPath.Length))); ofsw.Start(); return(ret); }
// dismissing method public static void Observe(string classImplementation) { var sourcesPath = Path.Combine(Environment.CurrentDirectory, "Sources"); Console.WriteLine($"Running from: {Environment.CurrentDirectory}"); Console.WriteLine($"Sources from: {sourcesPath}"); Console.WriteLine("Modify the sources to compile and run it!"); var compiler = new Compiler(); var runner = new Runner(); using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = @".\Sources"; })) { var changes = watcher.Changed.Throttle(TimeSpan.FromSeconds(.5)).Where(c => c.FullPath.EndsWith(@"DynamicProgram.cs")).Select(c => c.FullPath); changes.Subscribe(filepath => runner.Execute("dynamicNamespace.dynamicClassName", compiler.CompileFile(filepath), new JObject()) ); watcher.Start(); Console.WriteLine("Press any key to exit!"); Console.ReadLine(); } }
public Script() { using var watcher = new ObservableFileSystemWatcher(c => { c.Path = @".\Sources"; }); var changes = watcher.Changed.Throttle(TimeSpan.FromSeconds(.5)) .Where(c => c.FullPath.EndsWith(@"DynamicProgram.cs")).Select(c => c.FullPath); //changes.Subscribe(filepath => _runner.Execute(_compiler.Compile(filepath), new[] {"France"})); watcher.Start(); }
static void Main(string[] args) { using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = @"C:\FolderToWatch\"; c.IncludeSubdirectories = true; })) { watcher.Created.Select(x => $"{x.Name} was {x.ChangeType}").Subscribe(Console.WriteLine); watcher.Changed.Select(x => $"{x.Name} was {x.ChangeType}").Subscribe(Console.WriteLine); watcher.Renamed.Select(x => $"{x.OldName} was {x.ChangeType} to {x.Name}").Subscribe(Console.WriteLine); watcher.Deleted.Select(x => $"{x.Name} was {x.ChangeType}").Subscribe(Console.WriteLine); watcher.Errors.Subscribe(Console.WriteLine); watcher.Start(); Console.ReadLine(); } }
public async Task DeleteMonitoredDirectory_StreamsError() { using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; })) { var firstError = watcher.Errors.FirstAsync().ToTask(); watcher.Start(); Directory.Delete(TempPath); var error = await firstError; Assert.That(error.GetException().Message, Is.EqualTo("Access is denied")); } }
public async Task DeleteMonitoredDirectory_StreamsError() { using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; })) { var firstError = watcher.Errors.FirstAsync().ToTask(); watcher.Start(); Directory.Delete(TempPath); var error = await firstError; Expect(error.GetException().HResult, Is.EqualTo(-2147467259)); } }
public async Task WriteToFile_StreamsChanged() { using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; })) { var firstChanged = watcher.Changed.FirstAsync().ToTask(); watcher.Start(); File.WriteAllText(Path.Combine(TempPath, "Changed.Txt"), "foo"); var changed = await firstChanged; Assert.That(changed.ChangeType, Is.EqualTo(WatcherChangeTypes.Changed)); Assert.That(changed.Name, Is.EqualTo("Changed.Txt")); } }
public async Task CreateFile_StreamsCreated() { using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; })) { var firstCreated = watcher.Created.FirstAsync().ToTask(); var filePath = Path.Combine(TempPath, "Created.Txt"); watcher.Start(); File.WriteAllText(filePath, "foo"); var created = await firstCreated; Expect(created.ChangeType, Is.EqualTo(WatcherChangeTypes.Created)); Expect(created.Name, Is.EqualTo("Created.Txt")); } }
public async Task DeleteFile_StreamsDeleted() { using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; })) { var firstDeleted = watcher.Deleted.FirstAsync().ToTask(); var filePath = Path.Combine(TempPath, "ToDelete.Txt"); File.WriteAllText(filePath, "foo"); watcher.Start(); File.Delete(filePath); var deleted = await firstDeleted; Assert.That(deleted.ChangeType, Is.EqualTo(WatcherChangeTypes.Deleted)); Assert.That(deleted.Name, Is.EqualTo("ToDelete.Txt")); } }
public async Task RenameFile_StreamsRenamed() { using (var watcher = new ObservableFileSystemWatcher(c => { c.Path = TempPath; })) { var firstRenamed = watcher.Renamed.FirstAsync().ToTask(); var originalPath = Path.Combine(TempPath, "Changed.Txt"); File.WriteAllText(originalPath, "foo"); watcher.Start(); var renamedPath = Path.Combine(TempPath, "Renamed.Txt"); File.Move(originalPath, renamedPath); var renamed = await firstRenamed; Assert.That(renamed.OldFullPath, Is.EqualTo(originalPath)); Assert.That(renamed.FullPath, Is.EqualTo(renamedPath)); } }
public void StartWatching() { if (WatchPath == null || !Directory.Exists(WatchPath)) { throw new InvalidOperationException("Invalid watch path provided"); } _canceller = new CancellationTokenSource(); _fileSystemWatcher.Watcher.Path = WatchPath; ChangedAssemblies .GroupBy(ca => ca) .SelectMany(ca => ca.Throttle(TimeSpan.FromSeconds(.5))) .Subscribe( onNext: f => { this.Log().Debug(String.Format("{0} changed, attempting to send.. ", f)); var bytes = File.ReadAllBytes(f); var msg = new NewAssemblyMessage { AssemblyName = Path.GetFileNameWithoutExtension(f), AssemblyBytes = bytes }; _jsonProtocolMessenger.Send(msg); }, onError: ex => this.Log().Error(ex.Message), onCompleted: () => { }, token: _canceller.Token ); _fileSystemWatcher.Start(); IsWatching = true; }