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
213 lines (174 loc) · 7.96 KB
/
Repl.cs
File metadata and controls
213 lines (174 loc) · 7.96 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
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using ScriptCs.Contracts;
namespace ScriptCs
{
public class Repl : ScriptExecutor, IRepl
{
private readonly string[] _scriptArgs;
private readonly IObjectSerializer _serializer;
private readonly Printers _printers;
private readonly ILog _log;
public Repl(
string[] scriptArgs,
IFileSystem fileSystem,
IScriptEngine scriptEngine,
IObjectSerializer serializer,
ILogProvider logProvider,
IScriptLibraryComposer composer,
IConsole console,
IFilePreProcessor filePreProcessor,
IEnumerable<IReplCommand> replCommands,
Printers printers,
IScriptInfo scriptInfo)
: base(fileSystem, filePreProcessor, scriptEngine, logProvider, composer, scriptInfo)
{
Guard.AgainstNullArgument("serializer", serializer);
Guard.AgainstNullArgument("logProvider", logProvider);
Guard.AgainstNullArgument("console", console);
_scriptArgs = scriptArgs;
_serializer = serializer;
_printers = printers;
_log = logProvider.ForCurrentType();
Console = console;
Commands = replCommands != null ? replCommands.Where(x => x.CommandName != null).ToDictionary(x => x.CommandName, x => x) : new Dictionary<string, IReplCommand>();
}
public string Buffer { get; set; }
public IConsole Console { get; private set; }
public Dictionary<string, IReplCommand> Commands { get; private set; }
public override void Terminate()
{
base.Terminate();
_log.Debug("Exiting console");
Console.Exit();
}
public override ScriptResult Execute(string script, params string[] scriptArgs)
{
Guard.AgainstNullArgument("script", script);
try
{
if (script.StartsWith(":"))
{
var tokens = script.Split(' ');
if (tokens[0].Length > 1)
{
var command = Commands.FirstOrDefault(x => x.Key == tokens[0].Substring(1));
if (command.Value != null)
{
var argsToPass = new List<object>();
foreach (var argument in tokens.Skip(1))
{
var argumentResult = ScriptEngine.Execute(
argument, _scriptArgs, References, Namespaces, ScriptPackSession);
if (argumentResult.CompileExceptionInfo != null)
{
throw new Exception(
GetInvalidCommandArgumentMessage(argument),
argumentResult.CompileExceptionInfo.SourceException);
}
if (argumentResult.ExecuteExceptionInfo != null)
{
throw new Exception(
GetInvalidCommandArgumentMessage(argument),
argumentResult.ExecuteExceptionInfo.SourceException);
}
if (!argumentResult.IsCompleteSubmission)
{
throw new Exception(GetInvalidCommandArgumentMessage(argument));
}
argsToPass.Add(argumentResult.ReturnValue);
}
var commandResult = command.Value.Execute(this, argsToPass.ToArray());
return ProcessCommandResult(commandResult);
}
}
}
var preProcessResult = FilePreProcessor.ProcessScript(script);
ImportNamespaces(preProcessResult.Namespaces.ToArray());
foreach (var reference in preProcessResult.References)
{
var referencePath = FileSystem.GetFullPath(Path.Combine(FileSystem.BinFolder, reference));
AddReferences(FileSystem.FileExists(referencePath) ? referencePath : reference);
}
Console.ForegroundColor = ConsoleColor.Cyan;
InjectScriptLibraries(FileSystem.CurrentDirectory, preProcessResult, ScriptPackSession.State);
Buffer = (Buffer == null)
? preProcessResult.Code
: Buffer + Environment.NewLine + preProcessResult.Code;
var namespaces = Namespaces.Union(preProcessResult.Namespaces);
var references = References.Union(preProcessResult.References);
var result = ScriptEngine.Execute(Buffer, _scriptArgs, references, namespaces, ScriptPackSession);
if (result == null) return ScriptResult.Empty;
if (result.CompileExceptionInfo != null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(result.CompileExceptionInfo.SourceException.Message);
}
if (result.ExecuteExceptionInfo != null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(result.ExecuteExceptionInfo.SourceException.Message);
}
if (result.InvalidNamespaces.Any())
{
RemoveNamespaces(result.InvalidNamespaces.ToArray());
}
if (!result.IsCompleteSubmission)
{
return result;
}
if (result.ReturnValue != null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(_printers.GetStringFor(result.ReturnValue));
}
Buffer = null;
return result;
}
catch (FileNotFoundException fileEx)
{
RemoveReferences(fileEx.FileName);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Environment.NewLine + fileEx + Environment.NewLine);
return new ScriptResult(compilationException: fileEx);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Environment.NewLine + ex + Environment.NewLine);
return new ScriptResult(executionException: ex);
}
finally
{
Console.ResetColor();
}
}
private static string GetInvalidCommandArgumentMessage(string argument)
{
return string.Format(CultureInfo.InvariantCulture, "Argument is not a valid expression: {0}", argument);
}
private ScriptResult ProcessCommandResult(object commandResult)
{
Buffer = null;
if (commandResult != null)
{
if (commandResult is ScriptResult)
{
var scriptCommandResult = commandResult as ScriptResult;
if (scriptCommandResult.ReturnValue != null)
{
Console.WriteLine(_serializer.Serialize(scriptCommandResult.ReturnValue));
}
return scriptCommandResult;
}
//if command has a result, print it
Console.WriteLine(_serializer.Serialize(commandResult));
return new ScriptResult(returnValue: commandResult);
}
return ScriptResult.Empty;
}
}
}