forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptExecutor.cs
More file actions
262 lines (216 loc) · 9.53 KB
/
ScriptExecutor.cs
File metadata and controls
262 lines (216 loc) · 9.53 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using ScriptCs.Contracts;
namespace ScriptCs
{
public class ScriptExecutor : IScriptExecutor
{
private readonly ILog _log;
public static readonly string[] DefaultReferences =
{
"System",
"System.Core",
"System.Data",
"System.Data.DataSetExtensions",
"System.Xml",
"System.Xml.Linq",
"System.Net.Http",
"Microsoft.CSharp",
typeof(ScriptExecutor).Assembly.Location,
typeof(IScriptEnvironment).Assembly.Location
};
public static readonly string[] DefaultNamespaces =
{
"System",
"System.Collections.Generic",
"System.Linq",
"System.Text",
"System.Threading.Tasks",
"System.IO",
"System.Net.Http",
"System.Dynamic"
};
private const string ScriptLibrariesInjected = "ScriptLibrariesInjected";
public IFileSystem FileSystem { get; private set; }
public IFilePreProcessor FilePreProcessor { get; private set; }
public IScriptEngine ScriptEngine { get; private set; }
[Obsolete("Support for Common.Logging types was deprecated in version 0.15.0 and will soon be removed.")]
public Common.Logging.ILog Logger { get; private set; }
public AssemblyReferences References { get; private set; }
public ICollection<string> Namespaces { get; private set; }
public ScriptPackSession ScriptPackSession { get; protected set; }
public IScriptLibraryComposer ScriptLibraryComposer { get; protected set; }
[Obsolete("Support for Common.Logging types was deprecated in version 0.15.0 and will soon be removed.")]
public ScriptExecutor(
IFileSystem fileSystem, IFilePreProcessor filePreProcessor, IScriptEngine scriptEngine, Common.Logging.ILog logger)
: this(fileSystem, filePreProcessor, scriptEngine, new CommonLoggingLogProvider(logger), new NullScriptLibraryComposer())
{
}
public ScriptExecutor(
IFileSystem fileSystem, IFilePreProcessor filePreProcessor, IScriptEngine scriptEngine, ILogProvider logProvider)
: this(fileSystem, filePreProcessor, scriptEngine, logProvider, new NullScriptLibraryComposer())
{
}
[Obsolete("Support for Common.Logging types was deprecated in version 0.15.0 and will soon be removed.")]
public ScriptExecutor(
IFileSystem fileSystem,
IFilePreProcessor filePreProcessor,
IScriptEngine scriptEngine,
Common.Logging.ILog logger,
IScriptLibraryComposer composer)
: this(fileSystem, filePreProcessor, scriptEngine, new CommonLoggingLogProvider(logger), composer)
{
}
public ScriptExecutor(
IFileSystem fileSystem,
IFilePreProcessor filePreProcessor,
IScriptEngine scriptEngine,
ILogProvider logProvider,
IScriptLibraryComposer composer)
{
Guard.AgainstNullArgument("fileSystem", fileSystem);
Guard.AgainstNullArgumentProperty("fileSystem", "BinFolder", fileSystem.BinFolder);
Guard.AgainstNullArgumentProperty("fileSystem", "DllCacheFolder", fileSystem.DllCacheFolder);
Guard.AgainstNullArgument("filePreProcessor", filePreProcessor);
Guard.AgainstNullArgument("scriptEngine", scriptEngine);
Guard.AgainstNullArgument("logProvider", logProvider);
Guard.AgainstNullArgument("composer", composer);
References = new AssemblyReferences(DefaultReferences);
Namespaces = new Collection<string>();
ImportNamespaces(DefaultNamespaces);
FileSystem = fileSystem;
FilePreProcessor = filePreProcessor;
ScriptEngine = scriptEngine;
_log = logProvider.ForCurrentType();
#pragma warning disable 618
Logger = new ScriptCsLogger(_log);
#pragma warning restore 618
ScriptLibraryComposer = composer;
}
public void ImportNamespaces(params string[] namespaces)
{
Guard.AgainstNullArgument("namespaces", namespaces);
foreach (var @namespace in namespaces)
{
Namespaces.Add(@namespace);
}
}
public void RemoveNamespaces(params string[] namespaces)
{
Guard.AgainstNullArgument("namespaces", namespaces);
foreach (var @namespace in namespaces)
{
Namespaces.Remove(@namespace);
}
}
public virtual void AddReferences(params Assembly[] assemblies)
{
Guard.AgainstNullArgument("assemblies", assemblies);
References = References.Union(assemblies);
}
public virtual void RemoveReferences(params Assembly[] assemblies)
{
Guard.AgainstNullArgument("assemblies", assemblies);
References = References.Except(assemblies);
}
public virtual void AddReferences(params string[] paths)
{
Guard.AgainstNullArgument("paths", paths);
References = References.Union(paths);
}
public virtual void RemoveReferences(params string[] paths)
{
Guard.AgainstNullArgument("paths", paths);
References = References.Except(paths);
}
public virtual void Initialize(
IEnumerable<string> paths, IEnumerable<IScriptPack> scriptPacks, params string[] scriptArgs)
{
AddReferences(paths.ToArray());
var bin = Path.Combine(FileSystem.CurrentDirectory, FileSystem.BinFolder);
var cache = Path.Combine(FileSystem.CurrentDirectory, FileSystem.DllCacheFolder);
ScriptEngine.BaseDirectory = bin;
ScriptEngine.CacheDirectory = cache;
_log.Debug("Initializing script packs");
var scriptPackSession = new ScriptPackSession(scriptPacks, scriptArgs);
ScriptPackSession = scriptPackSession;
scriptPackSession.InitializePacks();
}
public virtual void Reset()
{
References = new AssemblyReferences(DefaultReferences);
Namespaces.Clear();
ImportNamespaces(DefaultNamespaces);
ScriptPackSession.State.Clear();
}
public virtual void Terminate()
{
_log.Debug("Terminating packs");
ScriptPackSession.TerminatePacks();
}
public virtual ScriptResult Execute(string script, params string[] scriptArgs)
{
var path = Path.IsPathRooted(script) ? script : Path.Combine(FileSystem.CurrentDirectory, script);
var result = FilePreProcessor.ProcessFile(path);
ScriptEngine.FileName = Path.GetFileName(path);
return EngineExecute(Path.GetDirectoryName(path), scriptArgs, result);
}
public virtual ScriptResult ExecuteScript(string script, params string[] scriptArgs)
{
var result = FilePreProcessor.ProcessScript(script);
return EngineExecute(FileSystem.CurrentDirectory, scriptArgs, result);
}
protected internal virtual ScriptResult EngineExecute(
string workingDirectory,
string[] scriptArgs,
FilePreProcessorResult result
)
{
InjectScriptLibraries(workingDirectory, result, ScriptPackSession.State);
var namespaces = Namespaces.Union(result.Namespaces);
var references = References.Union(result.References);
_log.Debug("Starting execution in engine");
return ScriptEngine.Execute(result.Code, scriptArgs, references, namespaces, ScriptPackSession);
}
protected internal virtual void InjectScriptLibraries(
string workingDirectory,
FilePreProcessorResult result,
IDictionary<string, object> state
)
{
Guard.AgainstNullArgument("result", result);
Guard.AgainstNullArgument("state", state);
if (state.ContainsKey(ScriptLibrariesInjected))
{
return;
}
var scriptLibrariesPreProcessorResult = LoadScriptLibraries(workingDirectory);
if (scriptLibrariesPreProcessorResult != null)
{
result.Code = scriptLibrariesPreProcessorResult.Code + Environment.NewLine + result.Code;
result.References.AddRange(scriptLibrariesPreProcessorResult.References);
result.Namespaces.AddRange(scriptLibrariesPreProcessorResult.Namespaces);
}
state.Add(ScriptLibrariesInjected, null);
}
protected internal virtual FilePreProcessorResult LoadScriptLibraries(string workingDirectory)
{
if (string.IsNullOrWhiteSpace(ScriptLibraryComposer.ScriptLibrariesFile))
{
return null;
}
var scriptLibrariesPath = Path.Combine(workingDirectory, FileSystem.PackagesFolder,
ScriptLibraryComposer.ScriptLibrariesFile);
if (FileSystem.FileExists(scriptLibrariesPath))
{
_log.DebugFormat("Found Script Library at {0}", scriptLibrariesPath);
return FilePreProcessor.ProcessFile(scriptLibrariesPath);
}
return null;
}
}
}