forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatcherEx.cs
More file actions
179 lines (158 loc) · 5.54 KB
/
Copy pathWatcherEx.cs
File metadata and controls
179 lines (158 loc) · 5.54 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.IO;
using System.Text.RegularExpressions;
using PluginCore.Managers;
namespace PluginCore.Bridge
{
public class WatcherEx
{
string path;
string filter;
bool isRemote;
bool enabled;
FileSystemWatcher watcher;
BridgeClient bridge;
public bool IsRemote { get { return isRemote; } }
/// <summary>
/// Either watch a single file (if specified) or an entire directory tree.
/// </summary>
public WatcherEx(string path)
: this(path, null)
{
}
public WatcherEx(string path, string file)
{
this.path = path;
this.filter = file;
isRemote = BridgeManager.Active && path.ToUpper().StartsWithOrdinal(BridgeManager.Settings.SharedDrive);
if (!isRemote) SetupRegularWatcher();
}
public void Dispose()
{
if (watcher != null)
{
watcher.Dispose();
watcher = null;
}
}
#region Tracing
static bool errorDone = false;
public void TraceError()
{
if (errorDone) return;
else errorDone = true;
TraceManager.AddAsync("Unable to connect to FlashDevelop Bridge.");
}
static bool okDone = false;
public void TraceOk()
{
if (okDone) return;
else okDone = true;
TraceManager.AddAsync("Connected successfully to FlashDevelop Bridge.");
}
#endregion
#region FSW emulation
public event FileSystemEventHandler Created;
public event FileSystemEventHandler Changed;
public event FileSystemEventHandler Deleted;
public event RenamedEventHandler Renamed;
public bool EnableRaisingEvents
{
get { return enabled; }
set
{
enabled = value;
if (watcher != null) watcher.EnableRaisingEvents = value;
else if (enabled)
{
bridge = new BridgeClient();
if (!bridge.Connected)
{
enabled = false;
bridge = null;
TraceError();
}
else
{
if (Directory.Exists(path) && !path.EndsWith('\\')) path += "\\";
bridge.DataReceived += new DataReceivedEventHandler(bridge_DataReceived);
if (filter == null) bridge.Send("watch:" + path);
else bridge.Send("watch:" + Path.Combine(path, filter));
TraceOk();
}
}
else if (bridge != null)
{
if (bridge.Connected)
{
//bridge.Send("unwatch:");
try
{
bridge.Disconnect();
}
catch { }
}
bridge = null;
}
}
}
void bridge_DataReceived(object sender, DataReceivedEventArgs e)
{
string fullPath = e.Text;
if (fullPath.StartsWithOrdinal("BRIDGE:"))
{
// Lets expose bridge location...
Environment.SetEnvironmentVariable("FDBRIDGE", fullPath.Replace("BRIDGE:", ""));
return;
}
if (!fullPath.EndsWith('\\')) fullPath += '\\';
if (fullPath.Length < 3) return;
string folder = Path.GetDirectoryName(fullPath);
string name = Path.GetFileName(fullPath);
Changed?.Invoke(this, new FileSystemEventArgs(WatcherChangeTypes.Changed, folder, name));
}
#endregion
#region regular watcher implementation
static private Regex reIgnore = new Regex("[\\\\/][._]svn", RegexOptions.Compiled | RegexOptions.RightToLeft);
private void SetupRegularWatcher()
{
watcher = new FileSystemWatcher(path);
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
if (filter != null)
{
watcher.IncludeSubdirectories = false;
watcher.Filter = filter;
}
else
{
watcher.IncludeSubdirectories = true;
watcher.InternalBufferSize = 4096 * 8;
}
watcher.Created += watcher_Created;
watcher.Changed += watcher_Changed;
watcher.Deleted += watcher_Deleted;
watcher.Renamed += watcher_Renamed;
}
private void watcher_Created(object sender, FileSystemEventArgs e)
{
if (reIgnore.IsMatch(e.FullPath)) return;
Created?.Invoke(this, e);
}
private void watcher_Changed(object sender, FileSystemEventArgs e)
{
if (reIgnore.IsMatch(e.FullPath)) return;
Changed?.Invoke(this, e);
}
private void watcher_Deleted(object sender, FileSystemEventArgs e)
{
if (reIgnore.IsMatch(e.FullPath)) return;
Deleted?.Invoke(this, e);
}
private void watcher_Renamed(object sender, RenamedEventArgs e)
{
if (reIgnore.IsMatch(e.FullPath)) return;
Renamed?.Invoke(this, e);
}
#endregion
}
}