Skip to content

Commit 5a0c1ec

Browse files
committed
Refactoring Printers impl to the ScriptEnvironment. Also using DI as much as possible
1 parent f2506d5 commit 5a0c1ec

14 files changed

Lines changed: 124 additions & 66 deletions

File tree

src/ScriptCs.Contracts/IScriptEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ namespace ScriptCs.Contracts
99
public interface IScriptEnvironment
1010
{
1111
IReadOnlyList<string> ScriptArgs { get; }
12+
void AddCustomPrinter<T>(Func<T, string> printer);
1213
}
1314
}

src/ScriptCs.Contracts/Printers.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ScriptCs.Contracts
5+
{
6+
public class Printers : Dictionary<Type, Func<object, string>>
7+
{
8+
}
9+
}

src/ScriptCs.Contracts/ScriptCs.Contracts.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<Compile Include="LogLevel.cs" />
106106
<Compile Include="LogProviderExtensions.cs" />
107107
<Compile Include="ModuleAttribute.cs" />
108+
<Compile Include="Printers.cs" />
108109
<Compile Include="ProjectItem.cs" />
109110
<Compile Include="Properties\AssemblyInfo.cs" />
110111
<Compile Include="ScriptPack.cs" />

src/ScriptCs.Core/Repl.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Repl : ScriptExecutor, IRepl
1212
private readonly string[] _scriptArgs;
1313

1414
private readonly IObjectSerializer _serializer;
15+
private readonly Printers _printers;
1516
private readonly ILog _log;
1617

1718
[Obsolete("Support for Common.Logging types was deprecated in version 0.15.0 and will soon be removed.")]
@@ -24,7 +25,8 @@ public Repl(
2425
IScriptLibraryComposer composer,
2526
IConsole console,
2627
IFilePreProcessor filePreProcessor,
27-
IEnumerable<IReplCommand> replCommands)
28+
IEnumerable<IReplCommand> replCommands,
29+
Printers printers)
2830
: this(
2931
scriptArgs,
3032
fileSystem,
@@ -34,7 +36,8 @@ public Repl(
3436
composer,
3537
console,
3638
filePreProcessor,
37-
replCommands)
39+
replCommands,
40+
printers)
3841
{
3942
}
4043

@@ -47,7 +50,8 @@ public Repl(
4750
IScriptLibraryComposer composer,
4851
IConsole console,
4952
IFilePreProcessor filePreProcessor,
50-
IEnumerable<IReplCommand> replCommands)
53+
IEnumerable<IReplCommand> replCommands,
54+
Printers printers)
5155
: base(fileSystem, filePreProcessor, scriptEngine, logProvider, composer)
5256
{
5357
Guard.AgainstNullArgument("serializer", serializer);
@@ -56,6 +60,7 @@ public Repl(
5660

5761
_scriptArgs = scriptArgs;
5862
_serializer = serializer;
63+
_printers = printers;
5964
_log = logProvider.ForCurrentType();
6065
Console = console;
6166
Commands = replCommands != null ? replCommands.Where(x => x.CommandName != null).ToDictionary(x => x.CommandName, x => x) : new Dictionary<string, IReplCommand>();
@@ -172,9 +177,13 @@ public override ScriptResult Execute(string script, params string[] scriptArgs)
172177
{
173178
Console.ForegroundColor = ConsoleColor.Yellow;
174179

175-
var serializedResult = _serializer.Serialize(result.ReturnValue);
176-
177-
Console.WriteLine(serializedResult);
180+
Func<object, string> printer;
181+
if(_printers.TryGetValue(result.ReturnValue.GetType(), out printer)) {
182+
Console.WriteLine(printer(result.ReturnValue));
183+
} else {
184+
var serializedResult = _serializer.Serialize(result.ReturnValue);
185+
Console.WriteLine(serializedResult);
186+
}
178187
}
179188

180189
Buffer = null;
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using ScriptCs.Contracts;
34

45
namespace ScriptCs
56
{
67
public class ScriptEnvironment : IScriptEnvironment
78
{
8-
public ScriptEnvironment(string[] scriptArgs)
9+
private readonly IConsole _console;
10+
private readonly Printers _printers;
11+
12+
public ScriptEnvironment(string[] scriptArgs, IConsole console, Printers printers)
913
{
14+
_console = console;
15+
_printers = printers;
1016
ScriptArgs = scriptArgs;
1117
}
1218

1319
public IReadOnlyList<string> ScriptArgs { get; private set; }
20+
21+
public void AddCustomPrinter<T>(Func<T, string> printer)
22+
{
23+
_console.WriteLine("Adding custom printer");
24+
_printers[typeof(T)] = x => printer((T) x);
25+
}
1426
}
1527
}

src/ScriptCs.Core/ScriptHost.cs

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

34
namespace ScriptCs
45
{

src/ScriptCs.Core/ScriptHostFactory.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@ namespace ScriptCs
44
{
55
public class ScriptHostFactory : IScriptHostFactory
66
{
7+
private readonly IConsole _console;
8+
private readonly Printers _printers;
9+
10+
public ScriptHostFactory(IConsole console, Printers printers)
11+
{
12+
_console = console;
13+
_printers = printers;
14+
}
15+
716
public IScriptHost CreateScriptHost(IScriptPackManager scriptPackManager, string[] scriptArgs)
817
{
9-
return new ScriptHost(scriptPackManager, new ScriptEnvironment(scriptArgs));
18+
return new ScriptHost(scriptPackManager, new ScriptEnvironment(scriptArgs, _console, _printers));
1019
}
1120
}
1221
}

src/ScriptCs.Hosting/RuntimeServices.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ protected override IContainer CreateContainer()
8585
builder.RegisterType(_replType).As<IRepl>().SingleInstance();
8686
builder.RegisterType<ScriptServices>().SingleInstance();
8787
builder.RegisterType<Repl>().As<IRepl>().SingleInstance();
88+
builder.RegisterType<Printers>().SingleInstance();
8889

8990
RegisterLineProcessors(builder);
9091
RegisterReplCommands(builder);

test/ScriptCs.Core.Tests/ReplCommands/AliasCommandTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public void ShouldAliasCommandWithNewName(
5353
composer.Object,
5454
console.Object,
5555
filePreProcessor.Object,
56-
new List<IReplCommand> { dummyCommand.Object });
56+
new List<IReplCommand> { dummyCommand.Object },
57+
new Printers());
5758

5859
var cmd = new AliasCommand(console.Object);
5960

test/ScriptCs.Core.Tests/ReplTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public static Repl GetRepl(Mocks mocks)
6464
mocks.ScriptLibraryComposer.Object,
6565
mocks.Console.Object,
6666
mocks.FilePreProcessor.Object,
67-
mocks.ReplCommands.Select(x => x.Object));
67+
mocks.ReplCommands.Select(x => x.Object),
68+
new Printers());
6869
}
6970

7071
public class TheConstructor

0 commit comments

Comments
 (0)