-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptcs.csx
131 lines (110 loc) · 4.33 KB
/
scriptcs.csx
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
//Modify to change to a different scriptcs version
#r ".\.svm\versions\0.15.0\ScriptCs.Hosting.dll"
#r ".\.svm\versions\0.15.0\ScriptCs.Engine.Roslyn.dll"
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ScriptCs;
using ScriptCs.Contracts;
using ScriptCs.Engine.Roslyn;
using ScriptCs.Hosting;
//necessary hack in order to prevent conflicts with the contracts assembly
public class InteractiveHostAssemblyResolver : IAssemblyResolver {
private IAssemblyResolver _resolver;
public InteractiveHostAssemblyResolver(
IFileSystem fileSystem,
IPackageAssemblyResolver packageAssemblyResolver,
IAssemblyUtility assemblyUtility,
ILogProvider logProvider)
{
_resolver = new AssemblyResolver(fileSystem, packageAssemblyResolver,assemblyUtility,logProvider);
}
public IEnumerable<string> GetAssemblyPaths(string path, bool binariesOnly = false)
{
return _resolver.GetAssemblyPaths(path, binariesOnly).Where(p=>!p.Contains("ScriptCs.Contracts"));
}
}
private class ScriptCsHost
{
private IScriptExecutor _executor;
private ScriptServices _services;
private IDictionary<string, IScriptPackContext> _contexts;
private IConsole _console;
private IFileSystem _fs;
private IEnumerable<IScriptPack> _scriptPacks;
public ScriptCsHost()
{
var console = (IConsole)new ScriptConsole();
_console = console;
var logProvider = new ColoredConsoleLogProvider(LogLevel.Info, console);
var overrides = new Dictionary<Type,Object>();
overrides[typeof(IAssemblyResolver)] = typeof(InteractiveHostAssemblyResolver);
var initializationServices = new InitializationServices(logProvider, overrides);
var builder = new ScriptServicesBuilder(console, logProvider, initializationServices:initializationServices);
builder.ScriptEngine<RoslynScriptEngine>();
builder.Repl(false);
builder.LoadScriptPacks();
var services = builder.Build();
_services = services;
_fs = services.FileSystem;
_executor = services.Executor;
var resolver = services.AssemblyResolver;
var workingDirectory = _fs.CurrentDirectory;
services.ScriptLibraryComposer.Compose(workingDirectory);
var assemblies = resolver.GetAssemblyPaths(workingDirectory);
var scriptPacks = _services.ScriptPackResolver.GetPacks().ToArray();
GetScriptPackContexts(scriptPacks);
_executor.Initialize(assemblies, scriptPacks);
}
public object Execute(string script)
{
var result = _executor.ExecuteScript(script);
if (result.CompileExceptionInfo != null)
{
result.CompileExceptionInfo.Throw();
}
if (result.ExecuteExceptionInfo != null)
{
result.ExecuteExceptionInfo.Throw();
}
return result.ReturnValue;
}
private void GetScriptPackContexts(IEnumerable<IScriptPack> scriptPacks)
{
_contexts = new Dictionary<string, IScriptPackContext>();
foreach (var pack in scriptPacks)
{
var context = pack.GetContext();
_contexts[context.GetType().Name] = context;
}
}
public T Require<T>()
{
return (T) _contexts[typeof(T).Name];
}
public dynamic Require(string scriptPack) {
return _contexts[scriptPack];
}
private IEnumerable<string> PreparePackages(IPackageAssemblyResolver packageAssemblyResolver, IPackageInstaller packageInstaller, IEnumerable<IPackageReference> additionalNuGetReferences, IEnumerable<string> localDependencies, ILog logger)
{
var workingDirectory = _fs.CurrentDirectory;
var packages = packageAssemblyResolver.GetPackages(workingDirectory);
packages = packages.Concat(additionalNuGetReferences);
try
{
packageInstaller.InstallPackages(
packages, true);
}
catch (Exception e)
{
logger.ErrorFormat("Installation failed: {0}.", e.Message);
}
var assemblyNames = packageAssemblyResolver.GetAssemblyNames(workingDirectory);
assemblyNames = assemblyNames.Concat(localDependencies);
return assemblyNames;
}
}
var scriptcs = new ScriptCsHost();