-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cs
More file actions
455 lines (403 loc) · 18.8 KB
/
Copy pathApplication.cs
File metadata and controls
455 lines (403 loc) · 18.8 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Permissions;
using System.Timers;
using imp.ModuleManager;
namespace imp
{
public class Application
{
public delegate void executionCallback();
private bool VerboseLog = false;
private PackageManager pm = null;
private AbstractModuleManager mm = null;
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 || hasArg(args, "--help")) {
Console.WriteLine("Indaxia Modules & Packages "+Version+" (IMP) by ScorpioT1000");
Console.WriteLine("Get more info at: https://github.com/Indaxia/imp");
Console.WriteLine("Arguments:");
Console.WriteLine(" init build.lua");
Console.WriteLine(" init src build.lua");
Console.WriteLine(" init src war3map.lua");
Console.WriteLine(" init includes/src war3map.as includes/packages");
Console.WriteLine(" - initializes a new project with the source dir (optional), the target file name and remote sources dir (optional, default is .imp/packages)");
Console.WriteLine(" init-clean");
Console.WriteLine(" init-clean build.lua");
Console.WriteLine(" - initializes a new project with a minimal configuration and without IMP Module Manager");
Console.WriteLine(" update");
Console.WriteLine(" - removes any package data and re-downloads it from the internet");
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(" build");
Console.WriteLine(" - builds all downloaded modules and sources into a target 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);
for(; RetryAttempt < 3; ++RetryAttempt) {
try {
if(hasArg(args, "build")) {
pm.RefreshPackages();
TryCreateModuleManager();
if(mm != null) {
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();
TryCreateModuleManager();
if(mm != null) {
mm.RebuildModules();
}
WatchForChanges();
} else if(hasArg(args, "update")) {
pm.RefreshPackages(true);
return;
} else if(hasArg(args, "init")) {
if(args.Length < 2) {
Console.WriteLine("init format: init [sourceDir] filename [remoteSourcesDir]");
} else {
pm.RefreshPackages(
true,
false,
args.Length > 2 ? args[2] : args[1],
args.Length > 2 ? args[1] : "",
args.Length > 3 ? args[3] : ""
);
TryCreateModuleManager();
if(mm != null && mm.GetModuleManagerPackageURL().Length > 0) {
pm.InstallDependency(mm.GetModuleManagerPackageURL(), "*");
}
}
return;
} else if(hasArg(args, "init-clean")) {
pm.RefreshPackages(true, false, args.Length > 1 ? args[1] : "");
return;
} else {
Console.WriteLine("Wrong command. Execute the program without arguments or with --help 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();
if(mm != null) {
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 TryCreateModuleManager()
{
string language = pm.ProjectPackage.GetLanguage();
if(language == "lua") {
mm = new LuaModuleManager(pm, VerboseLog, Version);
} else if(language == "angelscript") {
mm = new AngelscriptModuleManager(pm, VerboseLog, Version);
} else {
mm = null;
ConsoleColorChanger.UseWarning();
Console.Error.WriteLine("Warning: Cannot find ModuleManager for the language: " + language + ". Please specify \"language\" option in your imp-config.json");
ConsoleColorChanger.UsePrimary();
}
}
private void WatchForChanges()
{
if(mm == null) {
throw new Exception("Cannot watch without ModuleManager");
}
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>();
}
string filter = (pm.ProjectPackage.Source.CustomExtensions == "")
? mm.GetSourceExtensions()
: pm.ProjectPackage.Source.CustomExtensions;
foreach(string d in pm.ProjectPackage.Source.Sources) {
if(Path.IsPathRooted(d)) {
if(VerboseLog) Console.WriteLine("-- Watching "+d);
WatchDirectory(d, filter);
} else {
if(VerboseLog) Console.WriteLine("-- Watching "+d);
WatchDirectory(Path.Combine(pm.ProjectDirectory, d), filter);
}
}
WatchProjectPackage();
WatchTargetFile();
foreach(string fileToWatch in pm.ProjectPackage.WatchExtra) {
WatchExtraFile(fileToWatch);
}
PrintReadyMessage();
Console.ReadKey();
System.Environment.Exit(0);
}
// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private void WatchDirectory(string path, string filter)
{
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);
}
// [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private void WatchExtraFile(string relativeName)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName(Path.Combine(pm.ProjectDirectory, relativeName));
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = Path.GetFileName(relativeName);
watcher.Changed += OnExtraChanged;
watcher.Created += OnExtraChanged;
watcher.Deleted += OnExtraChanged;
watcher.EnableRaisingEvents = true;
Watchers.Add(watcher);
}
private void OnExtraChanged(object source, FileSystemEventArgs e)
{
if(!CheckIsFileReallyChanged(e.FullPath)) { return; }
PrintWatcherEvent("Extra", getChangeType(e.ChangeType), e.FullPath);
mm.invokeASAP("ModuleManager.RebuildModules", () => {
mm.RebuildModules();
PrintReadyMessage();
});
}
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.Source.Sources.ToArray());
var watchExtra = String.Join(',', pm.ProjectPackage.WatchExtra.ToArray());
Console.WriteLine("");
Console.WriteLine("Nice! 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");
if(watchExtra.Length > 0) {
ConsoleColorChanger.UseSecondary();
Console.Write(" " + watchExtra);
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.Length > 0) Console.Write(" "+prefix+" ");
ConsoleColorChanger.UseAccent();
Console.Write(action+" ");
ConsoleColorChanger.UsePrimary();
if(filename.Length > 0) {
ConsoleColorChanger.UseSecondary();
Console.Write(filename+" ");
ConsoleColorChanger.UsePrimary();
}
if(anotherFilename.Length > 0) {
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;
}
}
}