This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (78 loc) · 3.29 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (78 loc) · 3.29 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
using FileWatcher;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
namespace VSCode.FileSystem
{
class Program
{
private static BlockingCollection<FileEvent> fileEventQueue = new BlockingCollection<FileEvent>();
static int Main(string[] args)
{
// We want Unicode for the output
Console.OutputEncoding = System.Text.Encoding.UTF8;
// Validate args length
if (args.Length == 0 || args.Length > 2)
{
Console.Error.WriteLine("CodeHelper needs exactly one argument of the directory to watch recursively.");
return 1;
}
// Validate provided path
var path = args[0];
if (!Directory.Exists(path))
{
Console.Error.WriteLine("Path '{0}' does not exist.", path);
return 1;
}
var verboseLogging = (args.Length > 1 && args[1] == "-verbose");
// Event processor deals with buffering and normalization of events
var processor = new EventProcessor((e) => {
Console.WriteLine("{0}|{1}", e.changeType, e.path);
if (verboseLogging)
{
Console.WriteLine("{0}| >> normalized {1} {2}", (int)ChangeType.LOG, e.changeType == (int)ChangeType.CREATED ? "[ADDED]" : e.changeType == (int)ChangeType.DELETED ? "[DELETED]" : "[CHANGED]", e.path);
}
}, (msg) => {
Console.WriteLine("{0}|{1}", (int)ChangeType.LOG, msg);
});
// Use a thread to unblock producer
var thread = new Thread(() =>
{
while (true)
{
var e = fileEventQueue.Take();
processor.ProcessEvent(e);
if (verboseLogging)
{
Console.WriteLine("{0}|{1} {2}", (int)ChangeType.LOG, e.changeType == (int)ChangeType.CREATED ? "[ADDED]" : e.changeType == (int)ChangeType.DELETED ? "[DELETED]" : "[CHANGED]", e.path);
}
}
});
thread.IsBackground = true; // this ensures the thread does not block the process from terminating!
thread.Start();
// Log each event in our special format to output queue
Action<FileEvent> onEvent = (e) =>
{
fileEventQueue.Add(e);
};
Action<ErrorEventArgs> onError = (e) =>
{
if (e != null)
{
Console.WriteLine("{0}|{1}", (int)ChangeType.LOG, e.GetException().ToString());
}
};
// Start watching
var watcher = new FileWatcher();
var watcherImpl = watcher.Create(path, onEvent, onError);
watcherImpl.EnableRaisingEvents = true;
// Quit after any input
Console.Read();
return 0;
}
}
}