forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteReplCommand.cs
More file actions
127 lines (109 loc) · 4.08 KB
/
ExecuteReplCommand.cs
File metadata and controls
127 lines (109 loc) · 4.08 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
using System;
using ScriptCs.Contracts;
namespace ScriptCs.Command
{
internal class ExecuteReplCommand : IExecuteReplCommand
{
private readonly string _scriptName;
private readonly string[] _scriptArgs;
private readonly IFileSystem _fileSystem;
private readonly IScriptPackResolver _scriptPackResolver;
private readonly IRepl _repl;
private readonly ILog _logger;
private readonly IConsole _console;
private readonly IAssemblyResolver _assemblyResolver;
private readonly IFileSystemMigrator _fileSystemMigrator;
private readonly IScriptLibraryComposer _composer;
public ExecuteReplCommand(
string scriptName,
string[] scriptArgs,
IFileSystem fileSystem,
IScriptPackResolver scriptPackResolver,
IRepl repl,
ILogProvider logProvider,
IConsole console,
IAssemblyResolver assemblyResolver,
IFileSystemMigrator fileSystemMigrator,
IScriptLibraryComposer composer)
{
Guard.AgainstNullArgument("fileSystem", fileSystem);
Guard.AgainstNullArgument("scriptPackResolver", scriptPackResolver);
Guard.AgainstNullArgument("repl", repl);
Guard.AgainstNullArgument("logProvider", logProvider);
Guard.AgainstNullArgument("console", console);
Guard.AgainstNullArgument("assemblyResolver", assemblyResolver);
Guard.AgainstNullArgument("fileSystemMigrator", fileSystemMigrator);
Guard.AgainstNullArgument("composer", composer);
_scriptName = scriptName;
_scriptArgs = scriptArgs;
_fileSystem = fileSystem;
_scriptPackResolver = scriptPackResolver;
_repl = repl;
_logger = logProvider.ForCurrentType();
_console = console;
_assemblyResolver = assemblyResolver;
_fileSystemMigrator = fileSystemMigrator;
_composer = composer;
}
public string[] ScriptArgs
{
get { return _scriptArgs; }
}
public CommandResult Execute()
{
_fileSystemMigrator.Migrate();
_console.WriteLine("scriptcs (ctrl-c to exit or :help for help)" + Environment.NewLine);
var workingDirectory = _fileSystem.CurrentDirectory;
var assemblies = _assemblyResolver.GetAssemblyPaths(workingDirectory);
var scriptPacks = _scriptPackResolver.GetPacks();
_composer.Compose(workingDirectory);
_repl.Initialize(assemblies, scriptPacks, ScriptArgs);
if (!string.IsNullOrWhiteSpace(_scriptName))
{
_logger.InfoFormat("Executing script '{0}'", _scriptName);
try
{
_repl.Execute(string.Format("#load {0}", _scriptName));
}
catch (Exception ex)
{
_logger.ErrorException("Error executing script '{0}'", ex, _scriptName);
return CommandResult.Error;
}
}
try
{
while (ExecuteLine(_repl))
{
}
_console.WriteLine();
}
catch (Exception ex)
{
_logger.ErrorException("Error executing REPL", ex);
return CommandResult.Error;
}
_repl.Terminate();
return CommandResult.Success;
}
private bool ExecuteLine(IRepl repl)
{
var prompt = string.IsNullOrWhiteSpace (repl.Buffer) ? "> " : "* ";
try
{
var line = _console.ReadLine(prompt);
if (line == null)
return false;
if (!string.IsNullOrWhiteSpace(line))
{
repl.Execute(line);
}
return true;
}
catch
{
return false;
}
}
}
}