Skip to content

Commit a12a281

Browse files
committed
Merge pull request scriptcs#769 from filipw/feature/alias-repl
Allow REPL command aliasing
2 parents 1ecf5b5 + 4bc80ce commit a12a281

5 files changed

Lines changed: 114 additions & 5 deletions

File tree

src/ScriptCs.Core/Repl.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public Repl(
2828
_scriptArgs = scriptArgs;
2929
_serializer = serializer;
3030
Console = console;
31-
Commands = replCommands != null ? replCommands.ToList() : new List<IReplCommand>();
31+
Commands = replCommands != null ? replCommands.Where(x => x.CommandName != null).ToDictionary(x => x.CommandName, x => x) : new Dictionary<string, IReplCommand>();
3232
}
3333

3434
public string Buffer { get; set; }
3535

3636
public IConsole Console { get; private set; }
3737

38-
public IEnumerable<IReplCommand> Commands { get; private set; }
38+
public Dictionary<string, IReplCommand> Commands { get; private set; }
3939

4040
public override void Terminate()
4141
{
@@ -55,9 +55,9 @@ public override ScriptResult Execute(string script, params string[] scriptArgs)
5555
var tokens = script.Split(' ');
5656
if (tokens[0].Length > 1)
5757
{
58-
var command = Commands.FirstOrDefault(x => x.CommandName == tokens[0].Substring(1));
58+
var command = Commands.FirstOrDefault(x => x.Key == tokens[0].Substring(1));
5959

60-
if (command != null)
60+
if (command.Value != null)
6161
{
6262
var argsToPass = new List<object>();
6363
foreach (var argument in tokens.Skip(1))
@@ -87,7 +87,7 @@ public override ScriptResult Execute(string script, params string[] scriptArgs)
8787
argsToPass.Add(argumentResult.ReturnValue);
8888
}
8989

90-
var commandResult = command.Execute(this, argsToPass.ToArray());
90+
var commandResult = command.Value.Execute(this, argsToPass.ToArray());
9191
return ProcessCommandResult(commandResult);
9292
}
9393
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.IO;
2+
using System.Linq;
3+
using ScriptCs.Contracts;
4+
5+
namespace ScriptCs.ReplCommands
6+
{
7+
public class AliasCommand : IReplCommand
8+
{
9+
private readonly IConsole _console;
10+
11+
public AliasCommand(IConsole console)
12+
{
13+
_console = console;
14+
}
15+
16+
public string CommandName
17+
{
18+
get { return "alias"; }
19+
}
20+
21+
public object Execute(IScriptExecutor repl, object[] args)
22+
{
23+
Guard.AgainstNullArgument("repl", repl);
24+
25+
if (args == null || args.Length != 2)
26+
{
27+
return null;
28+
}
29+
30+
var replInstance = repl as Repl;
31+
32+
if (replInstance == null)
33+
{
34+
return null;
35+
}
36+
37+
var originalCommandName = args[0].ToString();
38+
var aliasName = args[1].ToString();
39+
40+
if (replInstance.Commands.Any(x => x.Key.ToLowerInvariant() == aliasName.ToLowerInvariant()))
41+
{
42+
return null;
43+
}
44+
45+
var oldReplCommand = replInstance.Commands[originalCommandName];
46+
replInstance.Commands[aliasName] = oldReplCommand;
47+
_console.WriteLine(string.Format("Aliased {0} as {1}", originalCommandName, aliasName));
48+
49+
return null;
50+
}
51+
}
52+
}

src/ScriptCs.Core/ScriptCs.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Compile Include="AssemblyInfo.cs" />
5252
<Compile Include="AssemblyResolver.cs" />
5353
<Compile Include="AssemblyUtility.cs" />
54+
<Compile Include="ReplCommands\AliasCommand.cs" />
5455
<Compile Include="ReplCommands\CdCommand.cs" />
5556
<Compile Include="ReplCommands\ClearCommand.cs" />
5657
<Compile Include="ReplCommands\CwdCommand.cs" />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using Moq;
5+
using ScriptCs.Contracts;
6+
using ScriptCs.ReplCommands;
7+
using Should;
8+
using Xunit;
9+
10+
namespace ScriptCs.Tests.ReplCommands
11+
{
12+
public class AliasCommandTests
13+
{
14+
public class CommandNameProperty
15+
{
16+
[Fact]
17+
public void ShouldReturnAlias()
18+
{
19+
// act
20+
var cmd = new AliasCommand(new Mock<IConsole>().Object);
21+
22+
// assert
23+
cmd.CommandName.ShouldEqual("alias");
24+
}
25+
}
26+
27+
public class ExecuteMethod
28+
{
29+
[Fact]
30+
public void ShouldAliasCommandWithNewName()
31+
{
32+
// arrange
33+
var currentDir = @"C:\";
34+
var dummyCommand = new Mock<IReplCommand>();
35+
dummyCommand.Setup(x => x.CommandName).Returns("foo");
36+
37+
var fs = new Mock<IFileSystem>();
38+
fs.Setup(x => x.BinFolder).Returns(Path.Combine(currentDir, "bin"));
39+
fs.Setup(x => x.DllCacheFolder).Returns(Path.Combine(currentDir, "cache"));
40+
41+
var console = new Mock<IConsole>();
42+
var executor = new Repl(null, fs.Object, null, null, null, null, null, new List<IReplCommand> {dummyCommand.Object});
43+
44+
var cmd = new AliasCommand(console.Object);
45+
46+
// act
47+
cmd.Execute(executor, new []{"foo", "bar"});
48+
49+
// assert
50+
executor.Commands.Count.ShouldEqual(2);
51+
executor.Commands["bar"].ShouldBeSameAs(executor.Commands["foo"]);
52+
}
53+
}
54+
}
55+
}

test/ScriptCs.Core.Tests/ScriptCs.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="FileProcessorTests.cs" />
5757
<Compile Include="PackageAssemblyResolverTests.cs" />
5858
<Compile Include="Properties\AssemblyInfo.cs" />
59+
<Compile Include="ReplCommands\AliasCommandTests.cs" />
5960
<Compile Include="ReplCommands\CdCommandTests.cs" />
6061
<Compile Include="ReplCommands\ClearCommandTests.cs" />
6162
<Compile Include="ReplCommands\CwdCommandTests.cs" />

0 commit comments

Comments
 (0)