forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatchScriptCommand.cs
More file actions
115 lines (103 loc) · 4.15 KB
/
WatchScriptCommand.cs
File metadata and controls
115 lines (103 loc) · 4.15 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
using System;
using System.Threading;
using ScriptCs.Contracts;
namespace ScriptCs.Command
{
internal class WatchScriptCommand : IScriptCommand
{
private readonly AppDomainSetup _setup = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
};
private readonly Config _config;
private readonly string[] _scriptArgs;
private readonly IConsole _console;
private readonly IFileSystem _fileSystem;
private readonly ILog _logger;
private readonly IFileSystemMigrator _fileSystemMigrator;
private readonly CrossAppDomainExecuteScriptCommand _executeScriptCommand;
public WatchScriptCommand(
Config config,
string[] scriptArgs,
IConsole console,
IFileSystem fileSystem,
ILogProvider logProvider,
IFileSystemMigrator fileSystemMigrator)
{
Guard.AgainstNullArgument("config", config);
Guard.AgainstNullArgument("scriptArgs", scriptArgs);
Guard.AgainstNullArgument("console", console);
Guard.AgainstNullArgument("fileSystem", fileSystem);
Guard.AgainstNullArgument("logProvider", logProvider);
Guard.AgainstNullArgument("fileSystemMigrator", fileSystemMigrator);
_config = config;
_scriptArgs = scriptArgs;
_console = console;
_fileSystem = fileSystem;
_logger = logProvider.ForCurrentType();
_fileSystemMigrator = fileSystemMigrator;
_executeScriptCommand = new CrossAppDomainExecuteScriptCommand
{
Config = _config,
ScriptArgs = _scriptArgs,
};
}
public CommandResult Execute()
{
_fileSystemMigrator.Migrate();
_console.WriteLine("scriptcs (ctrl-c to exit)");
_logger.InfoFormat("Running script '{0}' and watching for changes...", _config.ScriptName);
while (true)
{
using (var fileChanged = new ManualResetEventSlim())
using (var watcher = new FileWatcher(_config.ScriptName, 500, _fileSystem))
{
_logger.DebugFormat("Creating app domain '{0}'...", _config.ScriptName);
var appDomain = AppDomain.CreateDomain(_config.ScriptName, null, _setup);
try
{
watcher.Changed += (sender, e) =>
{
_logger.DebugFormat("Script '{0}' changed.", _config.ScriptName);
EnsureUnloaded(appDomain);
fileChanged.Set();
};
watcher.Start();
_logger.DebugFormat("Executing script '{0}' and watching for changes...", _config.ScriptName);
fileChanged.Reset();
try
{
appDomain.DoCallBack(_executeScriptCommand.Execute);
}
catch (AppDomainUnloadedException ex)
{
_logger.DebugFormat("App domain '{0}' has been unloaded.", ex, _config.ScriptName);
}
}
finally
{
EnsureUnloaded(appDomain);
}
fileChanged.Wait();
_logger.InfoFormat("Script changed. Reloading...", _config.ScriptName);
}
}
}
private void EnsureUnloaded(AppDomain domain)
{
try
{
_logger.DebugFormat("Unloading app domain '{0}'", _config.ScriptName);
AppDomain.Unload(domain);
}
catch (AppDomainUnloadedException ex)
{
_logger.DebugFormat("App domain '{0}' has already been unloaded.", ex, _config.ScriptName);
}
}
public string[] ScriptArgs
{
get { return this._scriptArgs; }
}
}
}