forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonScriptEngine.cs
More file actions
206 lines (167 loc) · 8.03 KB
/
CommonScriptEngine.cs
File metadata and controls
206 lines (167 loc) · 8.03 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using ScriptCs.Contracts;
namespace ScriptCs.Engine.Roslyn
{
// note this class is a base for future VB engine
public abstract class CommonScriptEngine : IScriptEngine
{
protected ScriptOptions ScriptOptions { get; set; }
protected ScriptMetadataResolver ScriptMetadataResolver { get; private set; }
private readonly IScriptHostFactory _scriptHostFactory;
protected ILog Log;
public const string SessionKey = "Session";
protected CommonScriptEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider)
{
Guard.AgainstNullArgument("logProvider", logProvider);
ScriptMetadataResolver = ScriptMetadataResolver.Default;
ScriptOptions = ScriptOptions.Default.
WithReferences(typeof(object).Assembly, typeof(TupleElementNamesAttribute).Assembly). // System.ValueTuple
WithMetadataResolver(ScriptMetadataResolver);
_scriptHostFactory = scriptHostFactory;
Log = logProvider.ForCurrentType();
SetCSharpVersionToLatest();
}
public string BaseDirectory
{
get => ScriptMetadataResolver.BaseDirectory;
set => ScriptMetadataResolver = ScriptMetadataResolver.WithBaseDirectory(value);
}
public string CacheDirectory { get; set; }
public string FileName { get; set; }
public string ScriptPath { get; set; }
public ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable<string> namespaces, ScriptPackSession scriptPackSession)
{
if (scriptPackSession == null)
{
throw new ArgumentNullException(nameof(scriptPackSession));
}
if (references == null)
{
throw new ArgumentNullException(nameof(references));
}
Log.Debug("Starting to create execution components");
Log.Debug("Creating script host");
var executionReferences = new AssemblyReferences(references.Assemblies, references.Paths);
executionReferences.Union(scriptPackSession.References);
ScriptResult scriptResult;
SessionState<ScriptState> sessionState;
var isFirstExecution = !scriptPackSession.State.ContainsKey(SessionKey);
if (isFirstExecution)
{
var host = _scriptHostFactory.CreateScriptHost(
new ScriptPackManager(scriptPackSession.Contexts), scriptArgs);
ScriptLibraryWrapper.SetHost(host);
Log.Debug("Creating session");
var hostType = host.GetType();
ScriptOptions = ScriptOptions.AddReferences(hostType.Assembly);
var allNamespaces = namespaces.Union(scriptPackSession.Namespaces).Distinct();
foreach (var reference in executionReferences.Paths)
{
Log.DebugFormat("Adding reference to {0}", reference);
ScriptOptions = ScriptOptions.AddReferences(reference);
}
foreach (var assembly in executionReferences.Assemblies)
{
Log.DebugFormat("Adding reference to {0}", assembly.FullName);
ScriptOptions = ScriptOptions.AddReferences(assembly);
}
foreach (var @namespace in allNamespaces)
{
Log.DebugFormat("Importing namespace {0}", @namespace);
ScriptOptions = ScriptOptions.AddImports(@namespace);
}
sessionState = new SessionState<ScriptState> { References = executionReferences, Namespaces = new HashSet<string>(allNamespaces) };
scriptPackSession.State[SessionKey] = sessionState;
scriptResult = Execute(code, host, sessionState);
}
else
{
Log.Debug("Reusing existing session");
sessionState = (SessionState<ScriptState>)scriptPackSession.State[SessionKey];
if (sessionState.References == null)
{
sessionState.References = new AssemblyReferences();
}
if (sessionState.Namespaces == null)
{
sessionState.Namespaces = new HashSet<string>();
}
var newReferences = executionReferences.Except(sessionState.References);
foreach (var reference in newReferences.Paths)
{
Log.DebugFormat("Adding reference to {0}", reference);
ScriptOptions = ScriptOptions.AddReferences(reference);
sessionState.References = sessionState.References.Union(new[] { reference });
}
foreach (var assembly in newReferences.Assemblies)
{
Log.DebugFormat("Adding reference to {0}", assembly.FullName);
ScriptOptions = ScriptOptions.AddReferences(assembly);
sessionState.References = sessionState.References.Union(new[] { assembly });
}
var newNamespaces = namespaces.Except(sessionState.Namespaces);
foreach (var @namespace in newNamespaces)
{
Log.DebugFormat("Importing namespace {0}", @namespace);
ScriptOptions = ScriptOptions.AddImports(@namespace);
sessionState.Namespaces.Add(@namespace);
}
if (string.IsNullOrWhiteSpace(code))
{
return ScriptResult.Empty;
}
scriptResult = Execute(code, sessionState.Session, sessionState);
}
return scriptResult;
//todo handle namespace failures
//https://github.com/dotnet/roslyn/issues/1012
}
protected virtual ScriptResult Execute(string code, object globals, SessionState<ScriptState> sessionState)
{
try
{
Log.Debug("Starting execution");
var result = GetScriptState(code, globals);
Log.Debug("Finished execution");
sessionState.Session = result;
return new ScriptResult(returnValue: result.ReturnValue);
}
catch (AggregateException ex)
{
return new ScriptResult(executionException: ex.InnerException);
}
catch (CompilationErrorException ex)
{
return new ScriptResult(compilationException: ex);
}
catch (Exception ex)
{
return new ScriptResult(executionException: ex);
}
}
protected abstract ScriptState GetScriptState(string code, object globals);
private void SetCSharpVersionToLatest()
{
try
{
// reset default scripting mode to latest language version to enable C# 7.1 features
// this is not needed once https://github.com/dotnet/roslyn/pull/21331 ships
var csharpScriptCompilerType = typeof(CSharpScript).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScriptCompiler");
var parseOptionsField = csharpScriptCompilerType?.GetField("s_defaultOptions", BindingFlags.Static | BindingFlags.NonPublic);
parseOptionsField?.SetValue(null, new CSharpParseOptions(LanguageVersion.Latest, kind: SourceCodeKind.Script));
}
catch (Exception)
{
Log.Warn("Unable to set C# language version to latest, will use the default version.");
}
}
}
}