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
110 lines (96 loc) · 3.24 KB
/
ExecuteReplCommand.cs
File metadata and controls
110 lines (96 loc) · 3.24 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Logging;
using System.Reflection;
namespace ScriptCs.Command
{
internal class ExecuteReplCommand : IScriptCommand
{
private readonly IFileSystem _fileSystem;
private readonly IScriptPackResolver _scriptPackResolver;
private readonly IScriptEngine _scriptEngine;
private readonly IFilePreProcessor _filePreProcessor;
private readonly IAssemblyName _assemblyName;
private readonly ILog _logger;
private readonly IConsole _console;
public string[] ScriptArgs { get; private set; }
public ExecuteReplCommand(
IFileSystem fileSystem,
IScriptPackResolver scriptPackResolver,
IScriptEngine scriptEngine,
IFilePreProcessor filePreProcessor,
ILog logger,
IConsole console,
IAssemblyName assemblyName
)
{
_fileSystem = fileSystem;
_scriptPackResolver = scriptPackResolver;
_scriptEngine = scriptEngine;
_filePreProcessor = filePreProcessor;
_logger = logger;
_console = console;
_assemblyName = assemblyName;
}
public CommandResult Execute()
{
_console.WriteLine("scriptcs (ctrl-c or blank to exit)\r\n");
var repl = new Repl(_fileSystem, _scriptEngine, _logger, _console, _filePreProcessor);
repl.Initialize(GetAssemblyPaths(_fileSystem.CurrentDirectory), _scriptPackResolver.GetPacks());
try
{
while (ExecuteLine(repl))
{
}
}
catch (Exception ex)
{
_logger.Error(ex.Message);
return CommandResult.Error;
}
repl.Terminate();
return CommandResult.Success;
}
private bool ExecuteLine(Repl repl)
{
_console.Write("> ");
var line = _console.ReadLine();
if (line == "")
return false;
repl.Execute(line);
return true;
}
private IEnumerable<string> GetAssemblyPaths(string workingDirectory)
{
var binFolder = Path.Combine(workingDirectory, "bin");
if (!_fileSystem.DirectoryExists(binFolder))
_fileSystem.CreateDirectory(binFolder);
var assemblyPaths =
_fileSystem.EnumerateFiles(binFolder, "*.dll")
.Union(_fileSystem.EnumerateFiles(binFolder, "*.exe"))
.Where(IsManagedAssembly)
.ToList();
foreach (var path in assemblyPaths.Select(Path.GetFileName))
{
_logger.DebugFormat("Found assembly reference: {0}", path);
}
return assemblyPaths;
}
private bool IsManagedAssembly(string path)
{
try
{
_assemblyName.GetAssemblyName(path);
}
catch (BadImageFormatException)
{
return false;
}
return true;
}
}
}