11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
4+ using System . Linq ;
35using System . Runtime . ExceptionServices ;
46using System . Text . RegularExpressions ;
57using Common . Logging ;
@@ -20,17 +22,20 @@ public Repl(
2022 IObjectSerializer serializer ,
2123 ILog logger ,
2224 IConsole console ,
23- IFilePreProcessor filePreProcessor ) : base ( fileSystem , filePreProcessor , scriptEngine , logger )
25+ IFilePreProcessor filePreProcessor , IEnumerable < IReplCommand > replCommands ) : base ( fileSystem , filePreProcessor , scriptEngine , logger )
2426 {
2527 _scriptArgs = scriptArgs ;
2628 _serializer = serializer ;
2729 Console = console ;
30+ Commands = replCommands . ToList ( ) ;
2831 }
2932
3033 public string Buffer { get ; set ; }
3134
3235 public IConsole Console { get ; private set ; }
3336
37+ public List < IReplCommand > Commands { get ; private set ; }
38+
3439 public override void Terminate ( )
3540 {
3641 base . Terminate ( ) ;
@@ -44,38 +49,42 @@ public override ScriptResult Execute(string script, params string[] scriptArgs)
4449
4550 try
4651 {
47- if ( script . StartsWith ( "#clear" , StringComparison . OrdinalIgnoreCase ) )
52+ if ( script . StartsWith ( ":" ) )
4853 {
49- Console . Clear ( ) ;
50- return ScriptResult . Empty ;
51- }
52-
53- if ( script . StartsWith ( "#reset" ) )
54- {
55- Reset ( ) ;
56- return ScriptResult . Empty ;
57- }
58-
59- if ( script . StartsWith ( ":cd" , StringComparison . OrdinalIgnoreCase ) )
60- {
61- var m = Regex . Match ( script , @":cd\s+(.*)" ) ;
62-
63- var relativePath = m . Groups [ 1 ] . Value ;
64-
65- FileSystem . CurrentDirectory = Path . Combine ( FileSystem . CurrentDirectory , relativePath ) ;
66-
67- return ScriptResult . Empty ;
68- }
69-
70- if ( script . StartsWith ( ":cwd" , StringComparison . OrdinalIgnoreCase ) )
71- {
72- var dir = FileSystem . CurrentDirectory ;
73-
74- Console . ForegroundColor = ConsoleColor . Yellow ;
75-
76- Console . WriteLine ( dir ) ;
77-
78- return ScriptResult . Empty ;
54+ var arguments = script . Split ( ' ' ) ;
55+ var command = Commands . FirstOrDefault ( x => x . CommandName == arguments [ 0 ] . Substring ( 1 ) ) ;
56+
57+ if ( command != null )
58+ {
59+ var argsToPass = new List < object > ( ) ;
60+ foreach ( var argument in arguments . Skip ( 1 ) )
61+ {
62+ try
63+ {
64+ var argumentResult = ScriptEngine . Execute ( argument , _scriptArgs , References ,
65+ DefaultNamespaces , ScriptPackSession ) ;
66+ //if Roslyn can evaluate the argument, use its value, otherwise assume the string
67+ argsToPass . Add ( argumentResult . ReturnValue ?? argument ) ;
68+ }
69+ catch ( Exception )
70+ {
71+ argsToPass . Add ( argument ) ;
72+ }
73+ }
74+ var commandResult = command . Execute ( this , argsToPass . ToArray ( ) ) ;
75+ if ( commandResult != null )
76+ {
77+ //if command has a result, print it
78+ Console . WriteLine ( _serializer . Serialize ( commandResult ) ) ;
79+ }
80+
81+ Buffer = null ;
82+
83+ return new ScriptResult
84+ {
85+ ReturnValue = commandResult
86+ } ;
87+ }
7988 }
8089
8190 var preProcessResult = FilePreProcessor . ProcessScript ( script ) ;
0 commit comments