Skip to content

Commit 50ba56d

Browse files
filipwadamralph
authored andcommitted
added infra for Replcommands
1 parent bc6ebaf commit 50ba56d

6 files changed

Lines changed: 61 additions & 37 deletions

File tree

src/ScriptCs.Core/Repl.cs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
4+
using System.Linq;
35
using System.Runtime.ExceptionServices;
46
using System.Text.RegularExpressions;
57
using Common.Logging;
@@ -20,17 +22,20 @@ public Repl(
2022
IObjectSerializer serializer,
2123
ILog logger,
2224
IConsole console,
23-
IFilePreProcessor filePreProcessor) : base(fileSystem, filePreProcessor, scriptEngine, logger)
25+
IFilePreProcessor filePreProcessor, IEnumerable<IReplCommand> replCommands) : base(fileSystem, filePreProcessor, scriptEngine, logger)
2426
{
2527
_scriptArgs = scriptArgs;
2628
_serializer = serializer;
2729
Console = console;
30+
Commands = replCommands.ToList();
2831
}
2932

3033
public string Buffer { get; set; }
3134

3235
public IConsole Console { get; private set; }
3336

37+
public List<IReplCommand> Commands { get; private set; }
38+
3439
public override void Terminate()
3540
{
3641
base.Terminate();
@@ -44,38 +49,42 @@ public override ScriptResult Execute(string script, params string[] scriptArgs)
4449

4550
try
4651
{
47-
if (script.StartsWith("#clear", StringComparison.OrdinalIgnoreCase))
52+
if (script.StartsWith(":"))
4853
{
49-
Console.Clear();
50-
return ScriptResult.Empty;
51-
}
52-
53-
if (script.StartsWith("#reset"))
54-
{
55-
Reset();
56-
return ScriptResult.Empty;
57-
}
58-
59-
if (script.StartsWith(":cd", StringComparison.OrdinalIgnoreCase))
60-
{
61-
var m = Regex.Match(script, @":cd\s+(.*)");
62-
63-
var relativePath = m.Groups[1].Value;
64-
65-
FileSystem.CurrentDirectory = Path.Combine(FileSystem.CurrentDirectory, relativePath);
66-
67-
return ScriptResult.Empty;
68-
}
69-
70-
if (script.StartsWith(":cwd", StringComparison.OrdinalIgnoreCase))
71-
{
72-
var dir = FileSystem.CurrentDirectory;
73-
74-
Console.ForegroundColor = ConsoleColor.Yellow;
75-
76-
Console.WriteLine(dir);
77-
78-
return ScriptResult.Empty;
54+
var arguments = script.Split(' ');
55+
var command = Commands.FirstOrDefault(x => x.CommandName == arguments[0].Substring(1));
56+
57+
if (command != null)
58+
{
59+
var argsToPass = new List<object>();
60+
foreach (var argument in arguments.Skip(1))
61+
{
62+
try
63+
{
64+
var argumentResult = ScriptEngine.Execute(argument, _scriptArgs, References,
65+
DefaultNamespaces, ScriptPackSession);
66+
//if Roslyn can evaluate the argument, use its value, otherwise assume the string
67+
argsToPass.Add(argumentResult.ReturnValue ?? argument);
68+
}
69+
catch (Exception)
70+
{
71+
argsToPass.Add(argument);
72+
}
73+
}
74+
var commandResult = command.Execute(this, argsToPass.ToArray());
75+
if (commandResult != null)
76+
{
77+
//if command has a result, print it
78+
Console.WriteLine(_serializer.Serialize(commandResult));
79+
}
80+
81+
Buffer = null;
82+
83+
return new ScriptResult
84+
{
85+
ReturnValue = commandResult
86+
};
87+
}
7988
}
8089

8190
var preProcessResult = FilePreProcessor.ProcessScript(script);

src/ScriptCs.Core/ScriptServices.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Common.Logging;
1+
using System.Collections.Generic;
2+
using Common.Logging;
23
using ScriptCs.Contracts;
34

45
namespace ScriptCs
@@ -16,6 +17,7 @@ public ScriptServices(
1617
IObjectSerializer objectSerializer,
1718
ILog logger,
1819
IAssemblyResolver assemblyResolver,
20+
IEnumerable<IReplCommand> replCommands,
1921
IConsole console = null,
2022
IInstallationProvider installationProvider = null)
2123
{
@@ -31,6 +33,7 @@ public ScriptServices(
3133
Console = console;
3234
AssemblyResolver = assemblyResolver;
3335
InstallationProvider = installationProvider;
36+
ReplCommands = replCommands;
3437
}
3538

3639
public IFileSystem FileSystem { get; private set; }
@@ -45,5 +48,6 @@ public ScriptServices(
4548
public IConsole Console { get; private set; }
4649
public IAssemblyResolver AssemblyResolver { get; private set; }
4750
public IInstallationProvider InstallationProvider { get; private set; }
51+
public IEnumerable<IReplCommand> ReplCommands { get; private set; }
4852
}
4953
}

src/ScriptCs.Hosting/RuntimeServices.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected override IContainer CreateContainer()
4242
builder.RegisterType<ScriptServices>().SingleInstance();
4343

4444
RegisterLineProcessors(builder);
45+
RegisterReplCommands(builder);
4546

4647
RegisterOverrideOrDefault<IFileSystem>(builder, b => b.RegisterType<FileSystem>().As<IFileSystem>().SingleInstance());
4748
RegisterOverrideOrDefault<IAssemblyUtility>(builder, b => b.RegisterType<AssemblyUtility>().As<IAssemblyUtility>().SingleInstance());
@@ -129,6 +130,12 @@ private void RegisterLineProcessors(ContainerBuilder builder)
129130
builder.RegisterTypes(processorArray).As<ILineProcessor>();
130131
}
131132

133+
private void RegisterReplCommands(ContainerBuilder builder)
134+
{
135+
var replCommands = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).Where(x => typeof(IReplCommand).IsAssignableFrom(x) && x.IsClass);
136+
builder.RegisterTypes(replCommands.ToArray()).As<IReplCommand>();
137+
}
138+
132139
public ScriptServices GetScriptServices()
133140
{
134141
this.Logger.Debug("Resolving ScriptServices");

src/ScriptCs/Command/CommandFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)
5656
scriptServices.ObjectSerializer,
5757
scriptServices.Logger,
5858
scriptServices.Console,
59-
scriptServices.AssemblyResolver);
59+
scriptServices.AssemblyResolver,
60+
scriptServices.ReplCommands);
6061

6162
return replCommand;
6263
}

src/ScriptCs/Command/ExecuteReplCommand.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Reflection;
34
using Common.Logging;
45
using ScriptCs.Contracts;
@@ -9,6 +10,7 @@ internal class ExecuteReplCommand : IScriptCommand
910
{
1011
private readonly IScriptPackResolver _scriptPackResolver;
1112
private readonly IAssemblyResolver _assemblyResolver;
13+
private readonly IEnumerable<IReplCommand> _replCommands;
1214
private readonly IFilePreProcessor _filePreProcessor;
1315
private readonly IObjectSerializer _serializer;
1416
private readonly IScriptEngine _scriptEngine;
@@ -28,7 +30,7 @@ public ExecuteReplCommand(
2830
IObjectSerializer serializer,
2931
ILog logger,
3032
IConsole console,
31-
IAssemblyResolver assemblyResolver)
33+
IAssemblyResolver assemblyResolver, IEnumerable<IReplCommand> replCommands)
3234
{
3335
_scriptName = scriptName;
3436
_scriptArgs = scriptArgs;
@@ -40,6 +42,7 @@ public ExecuteReplCommand(
4042
_logger = logger;
4143
_console = console;
4244
_assemblyResolver = assemblyResolver;
45+
_replCommands = replCommands;
4346
}
4447

4548
public string[] ScriptArgs
@@ -50,7 +53,7 @@ public string[] ScriptArgs
5053
public CommandResult Execute()
5154
{
5255
_console.WriteLine("scriptcs (ctrl-c to exit)" + Environment.NewLine);
53-
var repl = new Repl(_scriptArgs, _fileSystem, _scriptEngine, _serializer, _logger, _console, _filePreProcessor);
56+
var repl = new Repl(_scriptArgs, _fileSystem, _scriptEngine, _serializer, _logger, _console, _filePreProcessor, _replCommands);
5457

5558
var workingDirectory = _fileSystem.CurrentDirectory;
5659
var assemblies = _assemblyResolver.GetAssemblyPaths(workingDirectory);

test/ScriptCs.Core.Tests/ReplTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public Mocks()
4444

4545
public static Repl GetRepl(Mocks mocks)
4646
{
47-
return new Repl(new string[0], mocks.FileSystem.Object, mocks.ScriptEngine.Object, mocks.ObjectSerializer.Object, mocks.Logger.Object, mocks.Console.Object, mocks.FilePreProcessor.Object);
47+
return new Repl(new string[0], mocks.FileSystem.Object, mocks.ScriptEngine.Object, mocks.ObjectSerializer.Object, mocks.Logger.Object, mocks.Console.Object, mocks.FilePreProcessor.Object, null);
4848
}
4949

5050
public class TheConstructor

0 commit comments

Comments
 (0)