@@ -25,15 +25,55 @@ public enum CodeType
2525} ;
2626
2727
28+ /// <summary>
29+ /// Depending on application itself exception from compilation and execution errors can be handled differently (log, print, display in output panel, etc)
30+ /// Here is specified base class with initial implementation for console application, override it if necessary
31+ /// </summary>
32+ public class ScriptExceptionHandler
33+ {
34+ /// <summary>
35+ /// Reports either successful script compilation & execution or reports an exception happening in either compilation or execution of script
36+ /// </summary>
37+ /// <param name="path">Path to .cs script</param>
38+ /// <param name="ex">Exception occurred, null if everything was ok</param>
39+ virtual public async void ReportScriptResult ( String path , Exception ex )
40+ {
41+ if ( ex == null )
42+ return ;
43+
44+ Console . WriteLine ( ex . Message ) ;
45+ Debug . WriteLine ( ex . Message ) ;
46+ }
47+ }
48+
2849
2950public class ScriptHost
30- {
51+ {
52+ public static ScriptExceptionHandler exceptionHandler = new ScriptExceptionHandler ( ) ;
3153 static String serverSwitch = "/rootsuffix" ;
3254
3355 /// <summary>
3456 /// User-defined object just to use a global variable storage between scripts runs
3557 /// </summary>
36- static public List < Object > userObj = new List < object > ( ) ;
58+ static public List < Object > userObj = new List < object > ( ) ;
59+
60+ /// <summary>
61+ /// Gets user defined objects from script host.
62+ /// </summary>
63+ /// <typeparam name="T">Type of specific object</typeparam>
64+ /// <param name="args">Additional parameters to constructor in case if object will be created.</param>
65+ /// <returns></returns>
66+ public static T GetUserObject < T > ( params object [ ] args ) where T : class
67+ {
68+ T t = userObj . Where ( x => x is T ) . FirstOrDefault ( ) as T ;
69+ if ( t == null )
70+ {
71+ t = Activator . CreateInstance ( typeof ( T ) , args ) as T ;
72+ userObj . Add ( t ) ;
73+ }
74+
75+ return t ;
76+ }
3777
3878
3979 // Must be here when using EnvDTE
@@ -44,16 +84,10 @@ static void Main(string[] args)
4484
4585 String hostExePath = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe" ;
4686 String cmdArgs = "Exp" ; // "/rootsuffix Exp" - launch experimental version of visual studio.
47- String csScript = null ;
87+ List < String > csScripts = new List < string > ( ) ;
4888
4989 for ( int i = 0 ; i < args . Length ; i ++ )
5090 {
51- if ( csScript == null )
52- {
53- csScript = args [ i ] ;
54- continue ;
55- }
56-
5791 if ( args [ i ] == "/exe" )
5892 {
5993 hostExePath = args [ ++ i ] ;
@@ -63,10 +97,10 @@ static void Main(string[] args)
6397 continue ;
6498 }
6599
66- cmdArgs += " " + args [ i ] ;
100+ csScripts . Add ( args [ i ] ) ;
67101 }
68102
69- ScriptServer_ConnectDebugger ( null , CodeType . Managed , csScript , hostExePath , cmdArgs ) ;
103+ ScriptServer_ConnectDebugger ( null , CodeType . Managed , csScripts , hostExePath , cmdArgs ) ;
70104 }
71105
72106 /// <summary>
@@ -82,9 +116,10 @@ public static void ConnectDebugger(
82116 {
83117 string [ ] args = Environment . GetCommandLineArgs ( ) ;
84118 if ( args . Contains ( serverSwitch ) )
85- return ;
86-
87- ScriptServer_ConnectDebugger ( null , codetype , csScript , hostExePath , additionCommandLineArguments ) ;
119+ return ;
120+
121+ String [ ] scripts = new string [ ] { csScript } ;
122+ ScriptServer_ConnectDebugger ( null , codetype , scripts , hostExePath , additionCommandLineArguments ) ;
88123 }
89124
90125 public static object mainArg = null ;
@@ -95,7 +130,7 @@ public static void ConnectDebugger(
95130 /// </summary>
96131 /// <param name="csScript">C# script</param>
97132 public static void ScriptServer_ConnectDebugger (
98- Object _mainArg = null , CodeType codetype = CodeType . Managed , String csScript = null ,
133+ Object _mainArg = null , CodeType codetype = CodeType . Managed , IEnumerable < String > csScripts = null ,
99134 String hostExePath = null , String additionCommandLineArguments = "" )
100135 {
101136 mainArg = _mainArg ;
@@ -198,9 +233,12 @@ public static void ScriptServer_ConnectDebugger(
198233
199234 // No need to attach if debugging multiple processes
200235 if ( dte != null && dte . Debugger . DebuggedProcesses . Count <= 1 )
201- process . Attach2 ( engines ) ;
202-
203- new IpcChannel ( process . ProcessID ) . Send ( csScript ) ;
236+ process . Attach2 ( engines ) ;
237+
238+ foreach ( String csScript in csScripts )
239+ {
240+ new IpcChannel ( process . ProcessID ) . Send ( csScript ) ;
241+ }
204242 bAttached = true ;
205243 break ;
206244 }
@@ -446,6 +484,7 @@ static void FileReloadUIThread( String file )
446484 //}
447485
448486 CsScript . RunScript ( file , mainArg ) ;
487+ exceptionHandler . ReportScriptResult ( file , null ) ;
449488 break ;
450489 }
451490 catch ( IOException ex )
@@ -467,8 +506,16 @@ static void FileReloadUIThread( String file )
467506 }
468507
469508 if ( lastException != null )
470- Console . WriteLine ( lastException . Message ) ;
471-
509+ {
510+ try
511+ {
512+ exceptionHandler . ReportScriptResult ( file , lastException ) ;
513+ }
514+ catch ( Exception ex )
515+ {
516+ Debug . WriteLine ( "Exception handler throwed exception by itself: " + ex . Message ) ;
517+ }
518+ }
472519 }
473520
474521 [ DllImport ( "ole32.dll" ) ]
0 commit comments