-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildWatcher.cs
More file actions
106 lines (100 loc) · 4.02 KB
/
BuildWatcher.cs
File metadata and controls
106 lines (100 loc) · 4.02 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
// -----------------------------------------------------------------------
// <copyright file="BuildWatcher.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
namespace NLB
{
/// <summary>
/// TODO: Update summary.
/// </summary>
public class BuildWatcher
{
internal readonly HashSet<string> BuildArtefacts = new HashSet<string>();
private readonly object _syncLock = new object();
private readonly FileSystemWatcher _watcher = new FileSystemWatcher();
internal bool Completed = false;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="path"> Full pathname of the file.</param>
/// <param name="endFlag">The end flag.</param>
internal BuildWatcher(string path, string endFlag)
{
//Create a new FileSystemWatcher.
_watcher = new FileSystemWatcher {IncludeSubdirectories = true};
_watcher.Created += watcher_FileCreated;
_watcher.Deleted += watcher_Deleted;
_watcher.Renamed += watcher_Renamed;
_watcher.Path = path;
//Enable the FileSystemWatcher events.
_watcher.EnableRaisingEvents = true;
}
/// <summary>
/// Record change event.
/// </summary>
/// <param name="change">The change.</param>
internal void RecordChangeEvent(BuildFileChange change)
{
lock (_syncLock)
{
if (change.ChangeType == WatcherChangeTypes.Deleted && BuildArtefacts.Contains(change.FullPath))
{
BuildArtefacts.Remove(change.FullPath);
}
if (change.ChangeType == WatcherChangeTypes.Renamed && BuildArtefacts.Contains(change.OldFullPath))
{
BuildArtefacts.Remove(change.OldFullPath);
}
if ((change.ChangeType == WatcherChangeTypes.Created || change.ChangeType == WatcherChangeTypes.Renamed) &&
BuildArtefacts.Contains(change.FullPath) == false)
{
BuildArtefacts.Add(change.FullPath);
}
}
}
/// <summary>
/// Event handler. Called by watcher for deleted events.
/// </summary>
/// <param name="sender">Source of the event.</param>
/// <param name="e"> File system event information.</param>
internal void watcher_Deleted(object sender, FileSystemEventArgs e)
{
var change = new BuildFileChange(e);
RecordChangeEvent(change);
}
/// <summary>
/// Event handler. Called by watcher for renamed events.
/// </summary>
/// <param name="sender">Source of the event.</param>
/// <param name="e"> Renamed event information.</param>
internal void watcher_Renamed(object sender, RenamedEventArgs e)
{
var change = new BuildFileChange(e);
RecordChangeEvent(change);
}
/// <summary>
/// Event handler. Called by watcher for file created events.
/// </summary>
/// <param name="sender">Source of the event.</param>
/// <param name="e"> File system event information.</param>
internal void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
var change = new BuildFileChange(e);
RecordChangeEvent(change);
}
/// <summary>
/// Ends monitoring.
/// </summary>
internal void EndMonitoring()
{
_watcher.Created -= watcher_FileCreated;
_watcher.Deleted -= watcher_Deleted;
_watcher.Renamed -= watcher_Renamed;
_watcher.EnableRaisingEvents = false;
}
}
}