Support :help directive & code refactoring

This commit is contained in:
tawan0109 2016-07-22 16:46:04 +08:00
Родитель eef32e0d7e
Коммит 7e879e1180
2 изменённых файлов: 62 добавлений и 27 удалений

Просмотреть файл

@ -87,36 +87,33 @@ namespace Microsoft.Spark.CSharp
ioHandler.WriteLine("Spark context available as sc."); ioHandler.WriteLine("Spark context available as sc.");
ioHandler.WriteLine("SQL context available as sqlContext."); ioHandler.WriteLine("SQL context available as sqlContext.");
ioHandler.WriteLine("Use :quit to exit."); ioHandler.WriteLine("Use :quit to exit.");
ioHandler.WriteLine("Type \":help\" for more information.");
} }
public void Run() public void Run()
{ {
var terminated = false;
while (true) while (true)
{ {
ioHandler.Write("> "); ioHandler.Write("> ");
var inputLines = new StringBuilder(); var inputLines = new StringBuilder();
bool cancelSubmission = false; var cancelSubmission = false;
ScriptResult scriptResult = null; ScriptResult scriptResult = null;
while (true) while (true)
{ {
var line = ioHandler.ReadLine(); var line = ioHandler.ReadLine();
if (string.IsNullOrWhiteSpace(line)) if (string.IsNullOrWhiteSpace(line))
{ {
cancelSubmission = true; cancelSubmission = true;
break; break;
} }
// quit if (IsDirective(line))
if (line.Trim().Equals(":quit", StringComparison.InvariantCultureIgnoreCase))
{ {
return; ProcessDirective(line, ref terminated);
}
// load DLL
if (line.Trim().StartsWith(":load", StringComparison.InvariantCultureIgnoreCase))
{
LoadAssebmly(line);
break; break;
} }
@ -130,11 +127,44 @@ namespace Microsoft.Spark.CSharp
ioHandler.Write(". "); ioHandler.Write(". ");
} }
if (cancelSubmission || scriptResult == null) if (terminated) break;
{
continue; if (cancelSubmission || scriptResult == null) continue;
ProcessExecutionResult(scriptResult);
}
} }
internal bool IsDirective(string line)
{
return Regex.Match(line.Trim(), ":\\S+").Success;
}
internal void ProcessDirective(string directive, ref bool terminated)
{
var verb = directive.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries)[0];
switch (verb)
{
case ":quit": // quit
terminated = true;
break;
case ":load": // load DLL
LoadAssebmly(directive);
break;
case ":help": // display help message
Help();
break;
default:
ioHandler.WriteException(new Exception("Invalid directive." + verb));
break;
}
}
internal void ProcessExecutionResult(ScriptResult scriptResult)
{
if (scriptResult.CompileExceptionInfo != null) if (scriptResult.CompileExceptionInfo != null)
{ {
ioHandler.WriteException(scriptResult.CompileExceptionInfo.SourceException); ioHandler.WriteException(scriptResult.CompileExceptionInfo.SourceException);
@ -148,7 +178,6 @@ namespace Microsoft.Spark.CSharp
ioHandler.WriteLine(scriptResult.ReturnValue); ioHandler.WriteLine(scriptResult.ReturnValue);
} }
} }
}
internal void LoadAssebmly(string directive) internal void LoadAssebmly(string directive)
{ {
@ -172,6 +201,12 @@ namespace Microsoft.Spark.CSharp
} }
} }
internal void Help()
{
const string helps = "Commands:\r\n :help\t\tDisplay help on available commands.\r\n :load\t\tLoad extra library to current execution context, e.g. :load \"myLib.dll\".\r\n :quit\t\tcd loExit REPL.";
ioHandler.WriteLine(helps);
}
static void Main(string[] args) static void Main(string[] args)
{ {
SparkConf sparkConf = new SparkConf(); SparkConf sparkConf = new SparkConf();

Просмотреть файл

@ -96,7 +96,7 @@ namespace ReplTest
thread.Join(); thread.Join();
scriptEngine.Cleanup(); scriptEngine.Cleanup();
// Console.WriteLine(string.Join("\r\n", ioHandler.output)); Console.WriteLine(string.Join("\r\n", ioHandler.output));
var seq = 0; var seq = 0;
Assert.AreEqual("> ", ioHandler.output[seq++]); Assert.AreEqual("> ", ioHandler.output[seq++]);
Assert.AreEqual(". ", ioHandler.output[seq++]); Assert.AreEqual(". ", ioHandler.output[seq++]);