This repository was archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cs
360 lines (315 loc) · 14.2 KB
/
Application.cs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Permissions;
using System.Timers;
namespace wlpm
{
public class Application
{
public delegate void executionCallback();
private bool VerboseLog = false;
private PackageManager pm;
private ModuleManager mm;
private string Version = "0.0.0.1"; // this is dynamically retrieved from assembly info
private int RetryAttempt = 0;
private List<FileSystemWatcher> Watchers = null;
private Dictionary<string, DateTime> WatcherChangedFilesCache;
public Application(string projectDir, string[] args, string version)
{
Version = version;
ConsoleColorChanger.SetPrimary(Console.ForegroundColor);
var noExit = hasArg(args, "--noexit");
if(args.Length < 1 || noExit) {
Console.WriteLine("Warcraft 3 Lua Package Manager "+Version+" (WLPM) by ScorpioT1000");
Console.WriteLine("Get more info at: https://github.com/Indaxia/WLPM");
Console.WriteLine("Arguments:");
Console.WriteLine(" install <package> [<version>] [file]");
Console.WriteLine(" - adds a new package or file to your package file and installs dependencies. Omit version to require head revision. To add a file type 'file' as a third parameter");
Console.WriteLine(" update");
Console.WriteLine(" - removes any package data and re-downloads it from the internet");
Console.WriteLine(" build");
Console.WriteLine(" - builds all downloaded modules and sources into target lua file");
Console.WriteLine(" update build");
Console.WriteLine(" - runs 'update' then 'build'");
Console.WriteLine(" watch");
Console.WriteLine(" - watches for changes of the sources and target and performs update or build");
Console.WriteLine("Options:");
Console.WriteLine(" --detailed");
Console.WriteLine(" - add this option to get more detailed info about the internal processes");
Console.WriteLine("");
if(noExit) {
ConsoleColorChanger.UseAccent();
Console.Error.WriteLine("Press any key to exit.");
ConsoleColorChanger.UsePrimary();
Console.ReadKey();
}
return;
}
if(hasArg(args, "--detailed")) {
VerboseLog = true;
}
pm = new PackageManager(projectDir, VerboseLog);
mm = new ModuleManager(pm, VerboseLog, Version);
for(; RetryAttempt < 3; ++RetryAttempt) {
try {
if(hasArg(args, "build")) {
pm.RefreshPackages();
mm.RebuildModules();
return;
} else if(hasArg(args, "install")) {
if(args.Length < 2) {
Console.WriteLine("install format: install url [version]");
} else {
pm.InstallDependency(args[1], args.Length > 2 ? args[2].Trim() : "*", args.Length > 3 && args[3] == "file");
}
return;
} else if(hasArg(args, "watch")) {
pm.RefreshPackages();
mm.RebuildModules();
WatchForChanges();
} else if(hasArg(args, "update")) {
pm.RefreshPackages(true);
return;
} else {
Console.WriteLine("Wrong command, execute the program without arguments to get help");
return;
}
} catch(Exception e) {
ConsoleColorChanger.UseWarning();
Console.Error.WriteLine("General Error: " + e.Message);
Console.Error.WriteLine("Source: " + e.Source);
Console.Error.WriteLine("");
Console.Error.WriteLine("Press any key to try again. Press CTRL+C to stop.");
ConsoleColorChanger.UsePrimary();
Console.ReadKey();
pm.Clear();
mm.Clear();
}
Console.Error.WriteLine("Retry attempt: " + RetryAttempt);
}
}
private bool hasArg(string[] args, string key)
{
foreach(string arg in args) {
if(arg == key) { return true; }
}
return false;
}
private void WatchForChanges()
{
if(WatcherChangedFilesCache != null) {
WatcherChangedFilesCache.Clear();
} else {
WatcherChangedFilesCache = new Dictionary<string, DateTime>();
}
if(Watchers != null) {
foreach(var w in Watchers) {
w.Dispose();
}
Watchers.Clear();
} else {
Watchers = new List<FileSystemWatcher>();
}
foreach(string d in pm.ProjectPackage.Sources) {
if(Path.IsPathRooted(d)) {
if(VerboseLog) Console.WriteLine("-- Watching "+d);
WatchDirectory(d);
} else {
WatchDirectory(Path.Combine(pm.ProjectDirectory, d));
if(VerboseLog) Console.WriteLine("-- Watching "+d);
}
}
WatchProjectPackage();
WatchTargetFile();
PrintReadyMessage();
Console.ReadKey();
System.Environment.Exit(0);
}
// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private void WatchDirectory(string path, string filter = "*.lua")
{
string targetPath = Path.GetFullPath(path);
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = targetPath;
watcher.IncludeSubdirectories = true;
watcher.NotifyFilter = NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
watcher.Filter = filter;
watcher.Changed += OnSrcChanged;
watcher.Created += OnSrcChanged;
watcher.Deleted += OnSrcChanged;
watcher.Renamed += OnSrcRenamed;
watcher.EnableRaisingEvents = true;
Watchers.Add(watcher);
}
// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private void WatchProjectPackage()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = pm.ProjectDirectory;
watcher.NotifyFilter = NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
watcher.Changed += OnPackageChanged;
watcher.Created += OnPackageChanged;
watcher.Deleted += OnPackageChanged;
watcher.Renamed += OnPackageRenamed;
watcher.EnableRaisingEvents = true;
Watchers.Add(watcher);
}
// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private void WatchTargetFile()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName(Path.Combine(pm.ProjectDirectory, pm.ProjectPackage.Target));
watcher.NotifyFilter = NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
watcher.Changed += OnTargetChanged;
watcher.Created += OnTargetChanged;
watcher.Deleted += OnTargetChanged;
watcher.Renamed += OnTargetRenamed;
watcher.EnableRaisingEvents = true;
Watchers.Add(watcher);
}
private void OnTargetChanged(object source, FileSystemEventArgs e)
{
if(!CheckIsFileReallyChanged(e.FullPath) || !mm.IsTargetChangedOutside() || !e.Name.EndsWith(pm.ProjectPackage.Target)) { return; }
PrintWatcherEvent("Target", getChangeType(e.ChangeType), e.FullPath);
mm.invokeASAP("ModuleManager.RebuildModules", () => {
mm.RebuildModules();
PrintReadyMessage();
});
}
private void OnTargetRenamed(object source, RenamedEventArgs e)
{
if(!CheckIsFileReallyChanged(e.FullPath) || !mm.IsTargetChangedOutside() || !e.Name.EndsWith(pm.ProjectPackage.Target)) { return; }
PrintWatcherEvent("Target", "renamed", e.OldFullPath, e.FullPath);
mm.invokeASAP("ModuleManager.RebuildModules", () => {
mm.RebuildModules(() => {
PrintReadyMessage();
});
});
}
private void OnSrcChanged(object source, FileSystemEventArgs e)
{
if(!CheckIsFileReallyChanged(e.FullPath)) { return; }
PrintWatcherEvent("Source", getChangeType(e.ChangeType), e.FullPath);
mm.invokeASAP("ModuleManager.RebuildModules", () => {
mm.RebuildModules(() => {
PrintReadyMessage();
});
});
}
private void OnSrcRenamed(object source, RenamedEventArgs e)
{
if(!CheckIsFileReallyChanged(e.FullPath)) { return; }
PrintWatcherEvent("Source", getChangeType(e.ChangeType), e.FullPath);
mm.invokeASAP("ModuleManager.RebuildModules", () => {
mm.RebuildModules(() => {
PrintReadyMessage();
});
});
}
private void OnPackageChanged(object source, FileSystemEventArgs e)
{
if(e.Name.EndsWith(pm.ProjectPackageName)) {
if(!CheckIsFileReallyChanged(e.FullPath)) { return; }
PrintWatcherEvent("Package config", getChangeType(e.ChangeType), e.Name);
if(VerboseLog) Console.WriteLine("-- Package changed: " + e.Name);
pm.invokeASAP("PackageManager.RefreshPackages", () => {
pm.RefreshPackages(false);
mm.RebuildModules(() => {
PrintReadyMessage();
});
});
}
}
private void OnPackageRenamed(object source, RenamedEventArgs e)
{
if(e.Name.EndsWith(pm.ProjectPackageName)) {
if(!CheckIsFileReallyChanged(e.FullPath)) { return; }
PrintWatcherEvent("Package config", "renamed", e.OldName, e.Name);
pm.invokeASAP("PackageManager.RefreshPackages", () => {
pm.RefreshPackages(false);
mm.RebuildModules();
});
}
}
private void PrintReadyMessage()
{
if(Watchers.Count > 0) {
var sources = String.Join(',', pm.ProjectPackage.Sources.ToArray());
Console.WriteLine("");
Console.WriteLine(((new Random()).Next(100) < 50 ? "Nice!" : "Great!") + " Watching for changes:");
ConsoleColorChanger.UseSecondary();
Console.Write(" " + pm.ProjectPackageName);
ConsoleColorChanger.UsePrimary();
Console.WriteLine(" -> refresh packages");
if(sources.Length > 0) {
ConsoleColorChanger.UseSecondary();
Console.Write(" " + sources);
ConsoleColorChanger.UsePrimary();
Console.WriteLine(" -> rebuild modules");
}
ConsoleColorChanger.UseSecondary();
Console.Write(" " + pm.ProjectPackage.Target);
ConsoleColorChanger.UsePrimary();
Console.WriteLine(" -> rebuild modules");
}
Console.WriteLine("");
ConsoleColorChanger.UseAccent();
Console.WriteLine("Now you are free to work with your map directory. Press any key to stop.");
ConsoleColorChanger.UsePrimary();
Console.WriteLine("");
}
private void PrintWatcherEvent(string prefix, string action, string filename = "", string anotherFilename = "")
{
if(prefix != "") Console.Write(" "+prefix+" ");
ConsoleColorChanger.UseAccent();
Console.Write(action+" ");
ConsoleColorChanger.UsePrimary();
if(filename != "") {
ConsoleColorChanger.UseSecondary();
Console.Write(filename+" ");
ConsoleColorChanger.UsePrimary();
}
if(anotherFilename != "") {
Console.Write(" -> ");
ConsoleColorChanger.UseSecondary();
Console.Write(anotherFilename+" ");
ConsoleColorChanger.UsePrimary();
}
Console.WriteLine("");
}
private string getChangeType(WatcherChangeTypes t)
{
switch(t) {
case WatcherChangeTypes.Created:
return "created";
case WatcherChangeTypes.Deleted:
return "deleted";
case WatcherChangeTypes.Changed:
return "changed";
case WatcherChangeTypes.Renamed:
return "renamed";
case WatcherChangeTypes.All:
return "changed";
}
return "(unknown change)";
}
private bool CheckIsFileReallyChanged(string fileFullPath)
{
bool isChanged = false;
DateTime changedAt = File.GetLastAccessTimeUtc(fileFullPath);
if(WatcherChangedFilesCache.ContainsKey(fileFullPath)) {
isChanged = WatcherChangedFilesCache[fileFullPath] < changedAt;
WatcherChangedFilesCache.Remove(fileFullPath);
}
WatcherChangedFilesCache.Add(fileFullPath, changedAt);
return isChanged;
}
}
}