Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow REPL command aliasing #769

Merged
merged 4 commits into from Dec 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ScriptCs.Core/Repl.cs
Expand Up @@ -28,14 +28,14 @@ public class Repl : ScriptExecutor
_scriptArgs = scriptArgs;
_serializer = serializer;
Console = console;
Commands = replCommands != null ? replCommands.ToList() : new List<IReplCommand>();
Commands = replCommands != null ? replCommands.Where(x => x.CommandName != null).ToDictionary(x => x.CommandName, x => x) : new Dictionary<string, IReplCommand>();
}

public string Buffer { get; set; }

public IConsole Console { get; private set; }

public IEnumerable<IReplCommand> Commands { get; private set; }
public Dictionary<string, IReplCommand> Commands { get; private set; }

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

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

var commandResult = command.Execute(this, argsToPass.ToArray());
var commandResult = command.Value.Execute(this, argsToPass.ToArray());
return ProcessCommandResult(commandResult);
}
}
Expand Down
52 changes: 52 additions & 0 deletions src/ScriptCs.Core/ReplCommands/AliasCommand.cs
@@ -0,0 +1,52 @@
using System.IO;
using System.Linq;
using ScriptCs.Contracts;

namespace ScriptCs.ReplCommands
{
public class AliasCommand : IReplCommand
{
private readonly IConsole _console;

public AliasCommand(IConsole console)
{
_console = console;
}

public string CommandName
{
get { return "alias"; }
}

public object Execute(IScriptExecutor repl, object[] args)
{
Guard.AgainstNullArgument("repl", repl);

if (args == null || args.Length != 2)
{
return null;
}

var replInstance = repl as Repl;

if (replInstance == null)
{
return null;
}

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

if (replInstance.Commands.Any(x => x.Key.ToLowerInvariant() == aliasName.ToLowerInvariant()))
{
return null;
}

var oldReplCommand = replInstance.Commands[originalCommandName];
replInstance.Commands[aliasName] = oldReplCommand;
_console.WriteLine(string.Format("Aliased {0} as {1}", originalCommandName, aliasName));

return null;
}
}
}
1 change: 1 addition & 0 deletions src/ScriptCs.Core/ScriptCs.Core.csproj
Expand Up @@ -49,6 +49,7 @@
<ItemGroup>
<Compile Include="AssemblyResolver.cs" />
<Compile Include="AssemblyUtility.cs" />
<Compile Include="ReplCommands\AliasCommand.cs" />
<Compile Include="ReplCommands\CdCommand.cs" />
<Compile Include="ReplCommands\ClearCommand.cs" />
<Compile Include="ReplCommands\CwdCommand.cs" />
Expand Down
55 changes: 55 additions & 0 deletions test/ScriptCs.Core.Tests/ReplCommands/AliasCommandTests.cs
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Moq;
using ScriptCs.Contracts;
using ScriptCs.ReplCommands;
using Should;
using Xunit;

namespace ScriptCs.Tests.ReplCommands
{
public class AliasCommandTests
{
public class CommandNameProperty
{
[Fact]
public void ShouldReturnAlias()
{
// act
var cmd = new AliasCommand(new Mock<IConsole>().Object);

// assert
cmd.CommandName.ShouldEqual("alias");
}
}

public class ExecuteMethod
{
[Fact]
public void ShouldAliasCommandWithNewName()
{
// arrange
var currentDir = @"C:\";
var dummyCommand = new Mock<IReplCommand>();
dummyCommand.Setup(x => x.CommandName).Returns("foo");

var fs = new Mock<IFileSystem>();
fs.Setup(x => x.BinFolder).Returns(Path.Combine(currentDir, "bin"));
fs.Setup(x => x.DllCacheFolder).Returns(Path.Combine(currentDir, "cache"));

var console = new Mock<IConsole>();
var executor = new Repl(null, fs.Object, null, null, null, null, null, new List<IReplCommand> {dummyCommand.Object});

var cmd = new AliasCommand(console.Object);

// act
cmd.Execute(executor, new []{"foo", "bar"});

// assert
executor.Commands.Count.ShouldEqual(2);
executor.Commands["bar"].ShouldBeSameAs(executor.Commands["foo"]);
}
}
}
}
1 change: 1 addition & 0 deletions test/ScriptCs.Core.Tests/ScriptCs.Core.Tests.csproj
Expand Up @@ -55,6 +55,7 @@
<Compile Include="FileProcessorTests.cs" />
<Compile Include="PackageAssemblyResolverTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReplCommands\AliasCommandTests.cs" />
<Compile Include="ReplCommands\CdCommandTests.cs" />
<Compile Include="ReplCommands\ClearCommandTests.cs" />
<Compile Include="ReplCommands\CwdCommandTests.cs" />
Expand Down