Support :help directive & code refactoring
This commit is contained in:
Родитель
eef32e0d7e
Коммит
7e879e1180
|
@ -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,23 +127,55 @@ namespace Microsoft.Spark.CSharp
|
||||||
ioHandler.Write(". ");
|
ioHandler.Write(". ");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cancelSubmission || scriptResult == null)
|
if (terminated) break;
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scriptResult.CompileExceptionInfo != null)
|
if (cancelSubmission || scriptResult == null) continue;
|
||||||
{
|
|
||||||
ioHandler.WriteException(scriptResult.CompileExceptionInfo.SourceException);
|
ProcessExecutionResult(scriptResult);
|
||||||
}
|
}
|
||||||
else if (scriptResult.ExecuteExceptionInfo != null)
|
}
|
||||||
{
|
|
||||||
ioHandler.WriteException(scriptResult.ExecuteExceptionInfo.SourceException);
|
internal bool IsDirective(string line)
|
||||||
}
|
{
|
||||||
else if (scriptResult.ReturnValue != null)
|
return Regex.Match(line.Trim(), ":\\S+").Success;
|
||||||
{
|
}
|
||||||
ioHandler.WriteLine(scriptResult.ReturnValue);
|
|
||||||
}
|
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)
|
||||||
|
{
|
||||||
|
ioHandler.WriteException(scriptResult.CompileExceptionInfo.SourceException);
|
||||||
|
}
|
||||||
|
else if (scriptResult.ExecuteExceptionInfo != null)
|
||||||
|
{
|
||||||
|
ioHandler.WriteException(scriptResult.ExecuteExceptionInfo.SourceException);
|
||||||
|
}
|
||||||
|
else if (scriptResult.ReturnValue != null)
|
||||||
|
{
|
||||||
|
ioHandler.WriteLine(scriptResult.ReturnValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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++]);
|
||||||
|
|
Загрузка…
Ссылка в новой задаче