forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepl.cs
More file actions
90 lines (77 loc) · 3.32 KB
/
Repl.cs
File metadata and controls
90 lines (77 loc) · 3.32 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Common.Logging;
using ScriptCs.Contracts;
using ServiceStack.Text;
namespace ScriptCs
{
public class Repl
{
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" };
public IFileSystem FileSystem { get; private set; }
public IScriptEngine ScriptEngine { get; private set; }
public IFilePreProcessor FilePreProcessor { get; private set; }
public ILog Logger { get; private set; }
public IConsole Console { get; private set; }
public ScriptPackSession ScriptPackSession { get; private set; }
public IEnumerable<string> References { get; private set; }
public Repl(IFileSystem fileSystem, IScriptEngine scriptEngine, ILog logger, IConsole console, IFilePreProcessor filePreProcessor)
{
FileSystem = fileSystem;
ScriptEngine = scriptEngine;
FilePreProcessor = filePreProcessor;
Logger = logger;
Console = console;
}
public void Initialize(IEnumerable<string> paths, IEnumerable<IScriptPack> scriptPacks)
{
References = DefaultReferences.Union(paths);
var bin = Path.Combine(FileSystem.CurrentDirectory, "bin");
ScriptEngine.BaseDirectory = bin;
Logger.Debug("Initializing script packs");
var scriptPackSession = new ScriptPackSession(scriptPacks);
scriptPackSession.InitializePacks();
ScriptPackSession = scriptPackSession;
}
public void Terminate()
{
Logger.Debug("Terminating packs");
ScriptPackSession.TerminatePacks();
}
public void Execute(string script)
{
var foregroundColor = Console.ForegroundColor;
try
{
if (PreProcessorUtil.IsLoadLine(script))
{
var filepath = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, script);
script = FilePreProcessor.ProcessFile(filepath);
}
else if (PreProcessorUtil.IsRLine(script))
{
var assemblyPath = PreProcessorUtil.GetPath(PreProcessorUtil.RString, script);
References = References.Union(new[] { assemblyPath });
return;
}
Console.ForegroundColor = ConsoleColor.Cyan;
var result = ScriptEngine.Execute(script, References, DefaultNamespaces, ScriptPackSession);
if (result != null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(result.ToJsv()
);
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\r\n" + ex + "\r\n");
}
Console.ForegroundColor = foregroundColor;
}
}
}