forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemWatcherLiveReload.cs
More file actions
119 lines (103 loc) · 3.88 KB
/
FileSystemWatcherLiveReload.cs
File metadata and controls
119 lines (103 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.IO;
using ServiceStack.Logging;
namespace ServiceStack.Razor.Managers
{
public interface ILiveReload
{
void StartWatching(string scanRootPath);
}
public class FileSystemWatcherLiveReload : ILiveReload
{
public static ILog Log = LogManager.GetLogger(typeof(FileSystemWatcherLiveReload));
/// <summary>
/// The purpose of the FileSystemWatcher is to ensure razor pages are
/// consistent with the code generated by the razor engine. The file
/// system watcher will invalidate pages and queue them for recompilation.
/// </summary>
protected FileSystemWatcher FileSystemWatcher;
private readonly RazorViewManager views;
public FileSystemWatcherLiveReload(RazorViewManager views)
{
this.views = views;
}
public void StartWatching(string scanRootPath)
{
//setup the file system watcher for page invalidation
this.FileSystemWatcher = new FileSystemWatcher(scanRootPath, "*.*")
{
IncludeSubdirectories = true,
EnableRaisingEvents = true,
//NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.FileName | NotifyFilters.LastWrite
};
this.FileSystemWatcher.Changed += FileSystemWatcher_Changed;
this.FileSystemWatcher.Created += FileSystemWatcher_Created;
this.FileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
this.FileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
this.FileSystemWatcher.Error += FileSystemWatcher_Error;
}
/// <summary>
/// Avoid throwing unhandled exception when shutting down ASP.NET host
/// </summary>
private void FileSystemWatcher_Error(object sender, ErrorEventArgs e)
{
Log.WarnFormat("FileSystemWatcher Error: ", sender);
}
protected virtual void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
try
{
var oldPagePath = views.GetDictionaryPagePath(views.GetRelativePath(e.OldFullPath));
if (!views.Pages.Remove(oldPagePath))
{
//Debugger.Break();
}
views.AddPage(e.FullPath);
}
catch (Exception ex)
{
Log.Warn("FileSystemWatcher_Renamed error: ", ex);
}
}
protected virtual void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
try
{
var file = views.GetVirtualFile(e.FullPath);
if (file == null || !views.IsWatchedFile(file)) return;
var pathPage = views.GetDictionaryPagePath(file);
views.Pages.Remove(pathPage);
}
catch (Exception ex)
{
Log.Warn("FileSystemWatcher_Deleted error: ", ex);
}
}
protected virtual void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
try
{
views.AddPage(e.FullPath);
}
catch (Exception ex)
{
Log.Warn("FileSystemWatcher_Created error: ", ex);
}
}
protected virtual void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
try
{
var file = views.GetVirtualFile(e.FullPath);
if (file == null || !views.IsWatchedFile(file)) return;
RazorPage page = views.GetPage(file);
if (page != null)
views.InvalidatePage(page);
}
catch (Exception ex)
{
Log.Warn("FileSystemWatcher_Changed error: ", ex);
}
}
}
}