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
161 lines (127 loc) · 5.67 KB
/
ScriptExecutor.cs
File metadata and controls
161 lines (127 loc) · 5.67 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
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using Common.Logging;
using ScriptCs.Contracts;
namespace ScriptCs
{
public class ScriptExecutor : IScriptExecutor
{
public static readonly string[] DefaultReferences = new[] { "System", "System.Core", "System.Data", "System.Data.DataSetExtensions", "System.Xml", "System.Xml.Linq", "System.Net.Http" };
public static readonly string[] DefaultNamespaces = new[] { "System", "System.Collections.Generic", "System.Linq", "System.Text", "System.Threading.Tasks", "System.IO", "System.Net.Http" };
public IFileSystem FileSystem { get; private set; }
public IFilePreProcessor FilePreProcessor { get; private set; }
public IScriptEngine ScriptEngine { get; private set; }
public ILog Logger { get; private set; }
public AssemblyReferences References { get; private set; }
public Collection<string> Namespaces { get; private set; }
public ScriptPackSession ScriptPackSession { get; protected set; }
public ScriptExecutor(IFileSystem fileSystem, IFilePreProcessor filePreProcessor, IScriptEngine scriptEngine, ILog logger)
{
Guard.AgainstNullArgument("fileSystem", fileSystem);
Guard.AgainstNullArgumentProperty("fileSystem", "BinFolder", fileSystem.BinFolder);
Guard.AgainstNullArgumentProperty("fileSystem", "DllCacheFolder", fileSystem.DllCacheFolder);
References = new AssemblyReferences();
AddReferences(DefaultReferences);
Namespaces = new Collection<string>();
ImportNamespaces(DefaultNamespaces);
FileSystem = fileSystem;
FilePreProcessor = filePreProcessor;
ScriptEngine = scriptEngine;
Logger = logger;
}
public void ImportNamespaces(params string[] namespaces)
{
Guard.AgainstNullArgument("namespaces", namespaces);
foreach (var @namespace in namespaces)
{
Namespaces.Add(@namespace);
}
}
public void AddReferences(params Assembly[] assemblies)
{
Guard.AgainstNullArgument("assemblies", assemblies);
foreach (var assembly in assemblies)
{
References.Assemblies.Add(assembly);
}
}
public void RemoveReferences(params Assembly[] assemblies)
{
Guard.AgainstNullArgument("assemblies", assemblies);
foreach (var assembly in assemblies)
{
References.Assemblies.Remove(assembly);
}
}
public void AddReferences(params string[] paths)
{
Guard.AgainstNullArgument("paths", paths);
foreach (var path in paths)
{
References.PathReferences.Add(path);
}
}
public void RemoveReferences(params string[] paths)
{
Guard.AgainstNullArgument("paths", paths);
foreach (var path in paths)
{
References.PathReferences.Remove(path);
}
}
public void RemoveNamespaces(params string[] namespaces)
{
Guard.AgainstNullArgument("namespaces", namespaces);
foreach (var @namespace in namespaces)
{
Namespaces.Remove(@namespace);
}
}
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;
Logger.Debug("Initializing script packs");
var scriptPackSession = new ScriptPackSession(scriptPacks, scriptArgs);
scriptPackSession.InitializePacks();
ScriptPackSession = scriptPackSession;
}
public virtual void Reset()
{
References = new AssemblyReferences();
AddReferences(DefaultReferences);
Namespaces.Clear();
ImportNamespaces(DefaultNamespaces);
ScriptPackSession.State.Clear();
}
public virtual void Terminate()
{
Logger.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);
References.PathReferences.UnionWith(result.References);
var namespaces = Namespaces.Union(result.Namespaces);
ScriptEngine.FileName = Path.GetFileName(path);
Logger.Debug("Starting execution in engine");
return ScriptEngine.Execute(result.Code, scriptArgs, References, namespaces, ScriptPackSession);
}
public virtual ScriptResult ExecuteScript(string script, params string[] scriptArgs)
{
var result = FilePreProcessor.ProcessScript(script);
References.PathReferences.UnionWith(result.References);
var namespaces = Namespaces.Union(result.Namespaces);
Logger.Debug("Starting execution in engine");
return ScriptEngine.Execute(result.Code, scriptArgs, References, namespaces, ScriptPackSession);
}
}
}