Skip to content

Commit bd73584

Browse files
committed
refactor: introduce IRepl, move IReplEngine to own file, add guards, remove redundant code, fix code formatting
1 parent 148e0f6 commit bd73584

31 files changed

Lines changed: 114 additions & 122 deletions

src/ScriptCs.Contracts/IRepl.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ScriptCs.Contracts
2+
{
3+
using System.Collections.Generic;
4+
5+
public interface IRepl : IScriptExecutor
6+
{
7+
Dictionary<string, IReplCommand> Commands { get; }
8+
}
9+
}

src/ScriptCs.Contracts/IReplCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public interface IReplCommand
66

77
string CommandName { get; }
88

9-
object Execute(IScriptExecutor repl, object[] args);
9+
object Execute(IRepl repl, object[] args);
1010
}
1111
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
3+
namespace ScriptCs.Contracts
4+
{
5+
public interface IReplEngine : IScriptEngine
6+
{
7+
ICollection<string> GetLocalVariables(ScriptPackSession scriptPackSession);
8+
}
9+
}

src/ScriptCs.Contracts/IScriptEngine.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ public interface IScriptEngine
77
string BaseDirectory { get; set; }
88

99
string CacheDirectory { get; set; }
10-
10+
1111
string FileName { get; set; }
12-
13-
ScriptResult Execute(string code, string[] scriptArgs, AssemblyReferences references, IEnumerable<string> namespaces, ScriptPackSession scriptPackSession);
14-
}
1512

16-
public interface IReplEngine : IScriptEngine
17-
{
18-
ICollection<string> GetLocalVariables(ScriptPackSession scriptPackSession);
13+
ScriptResult Execute(
14+
string code,
15+
string[] scriptArgs,
16+
AssemblyReferences references,
17+
IEnumerable<string> namespaces,
18+
ScriptPackSession scriptPackSession);
1919
}
2020
}

src/ScriptCs.Contracts/IScriptExecutor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Collections.ObjectModel;
32
using System.Reflection;
43

54
namespace ScriptCs.Contracts

src/ScriptCs.Contracts/ScriptCs.Contracts.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@
7474
<Compile Include="IPackageObject.cs" />
7575
<Compile Include="IPackageReference.cs" />
7676
<Compile Include="IReplCommand.cs" />
77+
<Compile Include="IReplEngine.cs" />
7778
<Compile Include="IScriptEngine.cs" />
79+
<Compile Include="IRepl.cs" />
7880
<Compile Include="IScriptExecutor.cs" />
7981
<Compile Include="IScriptHost.cs" />
8082
<Compile Include="IScriptHostFactory.cs" />

src/ScriptCs.Core/Repl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace ScriptCs
1010
{
11-
public class Repl : ScriptExecutor
11+
public class Repl : ScriptExecutor, IRepl
1212
{
1313
private readonly string[] _scriptArgs;
1414

src/ScriptCs.Core/ReplCommands/AliasCommand.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.IO;
2-
using System.Linq;
1+
using System.Linq;
32
using ScriptCs.Contracts;
43

54
namespace ScriptCs.ReplCommands
@@ -23,32 +22,26 @@ public string CommandName
2322
get { return "alias"; }
2423
}
2524

26-
public object Execute(IScriptExecutor repl, object[] args)
25+
public object Execute(IRepl repl, object[] args)
2726
{
2827
Guard.AgainstNullArgument("repl", repl);
2928

3029
if (args == null || args.Length != 2)
3130
{
32-
return null;
33-
}
34-
35-
var replInstance = repl as Repl;
36-
37-
if (replInstance == null)
38-
{
31+
_console.WriteLine("You must specifiy the command name and alias, e.g. :alias \"clear\" \"cls\"");
3932
return null;
4033
}
4134

4235
var originalCommandName = args[0].ToString();
4336
var aliasName = args[1].ToString();
4437

45-
if (replInstance.Commands.Any(x => x.Key.ToLowerInvariant() == aliasName.ToLowerInvariant()))
38+
if (repl.Commands.Any(x => x.Key.ToLowerInvariant() == aliasName.ToLowerInvariant()))
4639
{
4740
return null;
4841
}
4942

50-
var oldReplCommand = replInstance.Commands[originalCommandName];
51-
replInstance.Commands[aliasName] = oldReplCommand;
43+
var oldReplCommand = repl.Commands[originalCommandName];
44+
repl.Commands[aliasName] = oldReplCommand;
5245
_console.WriteLine(string.Format("Aliased {0} as {1}", originalCommandName, aliasName));
5346

5447
return null;

src/ScriptCs.Core/ReplCommands/CdCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public string CommandName
1515
get { return "cd"; }
1616
}
1717

18-
public object Execute(IScriptExecutor repl, object[] args)
18+
public object Execute(IRepl repl, object[] args)
1919
{
2020
Guard.AgainstNullArgument("repl", repl);
2121

src/ScriptCs.Core/ReplCommands/ClearCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public string CommandName
2323
get { return "clear"; }
2424
}
2525

26-
public object Execute(IScriptExecutor repl, object[] args)
26+
public object Execute(IRepl repl, object[] args)
2727
{
2828
_console.Clear();
2929
return null;

0 commit comments

Comments
 (0)