Skip to content

Commit

Permalink
Renames
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Nov 6, 2024
1 parent 47f84ee commit c3d59b9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 58 deletions.
17 changes: 5 additions & 12 deletions src/BuiltInTools/dotnet-watch/Internal/FileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace Microsoft.DotNet.Watcher.Internal
{
internal sealed class FileWatcher(IReadOnlyDictionary<string, FileItem> fileSet, IReporter reporter) : IDisposable
{
private readonly Dictionary<string, IFileSystemWatcher> _watchers = [];
// Directory watcher for each watched directory
private readonly Dictionary<string, IDirectoryWatcher> _watchers = [];

private bool _disposed;
public event Action<string, ChangeKind>? OnFileChange;
Expand All @@ -31,7 +32,7 @@ public void Dispose()

public void StartWatching()
{
EnsureNotDisposed();
ObjectDisposedException.ThrowIf(_disposed, this);

foreach (var (filePath, _) in fileSet)
{
Expand Down Expand Up @@ -67,9 +68,9 @@ public void StartWatching()

private void WatcherErrorHandler(object? sender, Exception error)
{
if (sender is IFileSystemWatcher watcher)
if (sender is IDirectoryWatcher watcher)
{
reporter.Warn($"The file watcher observing '{watcher.BasePath}' encountered an error: {error.Message}");
reporter.Warn($"The file watcher observing '{watcher.Directory}' encountered an error: {error.Message}");
}
}

Expand All @@ -90,14 +91,6 @@ private void DisposeWatcher(string directory)
watcher.Dispose();
}

private void EnsureNotDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(FileWatcher));
}
}

private static string EnsureTrailingSlash(string path)
=> (path is [.., var last] && last != Path.DirectorySeparatorChar) ? path + Path.DirectorySeparatorChar : path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,28 @@

namespace Microsoft.DotNet.Watcher.Internal
{
internal class DotnetFileWatcher : IFileSystemWatcher
internal sealed class EventBasedDirectoryWatcher : IDirectoryWatcher
{
public event EventHandler<(string filePath, ChangeKind kind)>? OnFileChange;

public event EventHandler<Exception>? OnError;

public string Directory { get; }

internal Action<string>? Logger { get; set; }

private volatile bool _disposed;

private readonly Func<string, FileSystemWatcher> _watcherFactory;

private FileSystemWatcher? _fileSystemWatcher;

private readonly object _createLock = new();

public DotnetFileWatcher(string watchedDirectory)
: this(watchedDirectory, DefaultWatcherFactory)
internal EventBasedDirectoryWatcher(string watchedDirectory)
{
}

internal DotnetFileWatcher(string watchedDirectory, Func<string, FileSystemWatcher> fileSystemWatcherFactory)
{
Ensure.NotNull(fileSystemWatcherFactory, nameof(fileSystemWatcherFactory));
Ensure.NotNullOrEmpty(watchedDirectory, nameof(watchedDirectory));

BasePath = watchedDirectory;
_watcherFactory = fileSystemWatcherFactory;
Directory = watchedDirectory;
CreateFileSystemWatcher();
}

public event EventHandler<(string filePath, ChangeKind kind)>? OnFileChange;

public event EventHandler<Exception>? OnError;

public string BasePath { get; }

private static FileSystemWatcher DefaultWatcherFactory(string watchedDirectory)
{
Ensure.NotNullOrEmpty(watchedDirectory, nameof(watchedDirectory));

return new FileSystemWatcher(watchedDirectory);
}

private void WatcherErrorHandler(object sender, ErrorEventArgs e)
{
if (_disposed)
Expand All @@ -62,7 +44,7 @@ private void WatcherErrorHandler(object sender, ErrorEventArgs e)
// Win32Exception may be triggered when setting EnableRaisingEvents on a file system type
// that is not supported, such as a network share. Don't attempt to recreate the watcher
// in this case as it will cause a StackOverflowException
if (!(exception is Win32Exception))
if (exception is not Win32Exception)
{
// Recreate the watcher if it is a recoverable error.
CreateFileSystemWatcher();
Expand All @@ -83,9 +65,9 @@ private void WatcherRenameHandler(object sender, RenamedEventArgs e)
NotifyChange(e.OldFullPath, ChangeKind.Delete);
NotifyChange(e.FullPath, ChangeKind.Add);

if (Directory.Exists(e.FullPath))
if (System.IO.Directory.Exists(e.FullPath))
{
foreach (var newLocation in Directory.EnumerateFileSystemEntries(e.FullPath, "*", SearchOption.AllDirectories))
foreach (var newLocation in System.IO.Directory.EnumerateFileSystemEntries(e.FullPath, "*", SearchOption.AllDirectories))
{
// Calculated previous path of this moved item.
var oldLocation = Path.Combine(e.OldFullPath, newLocation.Substring(e.FullPath.Length + 1));
Expand Down Expand Up @@ -147,8 +129,10 @@ private void CreateFileSystemWatcher()
DisposeInnerWatcher();
}

_fileSystemWatcher = _watcherFactory(BasePath);
_fileSystemWatcher.IncludeSubdirectories = true;
_fileSystemWatcher = new FileSystemWatcher(Directory)
{
IncludeSubdirectories = true
};

_fileSystemWatcher.Created += WatcherAddedHandler;
_fileSystemWatcher.Deleted += WatcherDeletedHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Microsoft.DotNet.Watcher.Internal
{
internal static class FileWatcherFactory
{
public static IFileSystemWatcher CreateWatcher(string watchedDirectory)
public static IDirectoryWatcher CreateWatcher(string watchedDirectory)
=> CreateWatcher(watchedDirectory, EnvironmentVariables.IsPollingEnabled);

public static IFileSystemWatcher CreateWatcher(string watchedDirectory, bool usePollingWatcher)
public static IDirectoryWatcher CreateWatcher(string watchedDirectory, bool usePollingWatcher)
{
return usePollingWatcher ?
new PollingFileWatcher(watchedDirectory) :
new DotnetFileWatcher(watchedDirectory);
new PollingDirectoryWatcher(watchedDirectory) :
new EventBasedDirectoryWatcher(watchedDirectory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace Microsoft.DotNet.Watcher.Internal
{
internal interface IFileSystemWatcher : IDisposable
internal interface IDirectoryWatcher : IDisposable
{
event EventHandler<(string filePath, ChangeKind kind)> OnFileChange;

event EventHandler<Exception> OnError;

string BasePath { get; }
string Directory { get; }

bool EnableRaisingEvents { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.DotNet.Watcher.Internal
{
internal class PollingFileWatcher : IFileSystemWatcher
internal sealed class PollingDirectoryWatcher : IDirectoryWatcher
{
// The minimum interval to rerun the scan
private static readonly TimeSpan _minRunInternal = TimeSpan.FromSeconds(.5);
Expand All @@ -22,17 +22,17 @@ internal class PollingFileWatcher : IFileSystemWatcher

private bool _disposed;

public PollingFileWatcher(string watchedDirectory)
public PollingDirectoryWatcher(string watchedDirectory)
{
Ensure.NotNullOrEmpty(watchedDirectory, nameof(watchedDirectory));

_watchedDirectory = new DirectoryInfo(watchedDirectory);
BasePath = _watchedDirectory.FullName;
Directory = _watchedDirectory.FullName;

_pollingThread = new Thread(new ThreadStart(PollingLoop))
{
IsBackground = true,
Name = nameof(PollingFileWatcher)
Name = nameof(PollingDirectoryWatcher)
};

CreateKnownFilesSnapshot();
Expand All @@ -46,7 +46,7 @@ public PollingFileWatcher(string watchedDirectory)
public event EventHandler<Exception>? OnError;
#pragma warning restore

public string BasePath { get; }
public string Directory { get; }

public bool EnableRaisingEvents
{
Expand Down Expand Up @@ -215,7 +215,7 @@ private void EnsureNotDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(PollingFileWatcher));
throw new ObjectDisposedException(nameof(PollingDirectoryWatcher));
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/dotnet-watch.Tests/FileWatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private async Task TestOperation(
Action operation)
{
using var watcher = FileWatcherFactory.CreateWatcher(dir, usePolling);
if (watcher is DotnetFileWatcher dotnetWatcher)
if (watcher is EventBasedDirectoryWatcher dotnetWatcher)
{
dotnetWatcher.Logger = m => output.WriteLine(m);
}
Expand Down Expand Up @@ -291,7 +291,7 @@ public async Task MultipleTriggers(bool usePolling)
watcher.EnableRaisingEvents = false;
}

private async Task AssertFileChangeRaisesEvent(string directory, IFileSystemWatcher watcher)
private async Task AssertFileChangeRaisesEvent(string directory, IDirectoryWatcher watcher)
{
var changedEv = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var expectedPath = Path.Combine(directory, Path.GetRandomFileName());
Expand Down

0 comments on commit c3d59b9

Please sign in to comment.