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
137 lines (108 loc) · 4.77 KB
/
ScriptExecutor.cs
File metadata and controls
137 lines (108 loc) · 4.77 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
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" };
public static readonly string[] DefaultNamespaces = new[] { "System", "System.Collections.Generic", "System.Linq", "System.Text", "System.Threading.Tasks", "System.IO" };
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 Collection<string> 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)
{
References = new Collection<string>();
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 string[] paths)
{
Guard.AgainstNullArgument("paths", paths);
foreach (var path in paths)
{
References.Add(path);
}
}
public void RemoveReferences(params string[] paths)
{
Guard.AgainstNullArgument("paths", paths);
foreach (var path in paths)
{
References.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, Constants.BinFolder);
var cache = Path.Combine(FileSystem.CurrentDirectory, Constants.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.Clear();
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);
var references = References.Union(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);
var references = References.Union(result.References);
var namespaces = Namespaces.Union(result.Namespaces);
Logger.Debug("Starting execution in engine");
return ScriptEngine.Execute(result.Code, scriptArgs, references, namespaces, ScriptPackSession);
}
}
}