Skip to content

Commit cde7015

Browse files
Tune: Add async getting of dte2
Bugfix: Figure out exception failure Tune: Add mechanism for main to return false and cancel output in visual studio Tune: VsScriptExceptionHandler - make class public vsDev: Remove async, as it can eat exceptions
1 parent ad0320d commit cde7015

5 files changed

Lines changed: 22 additions & 14 deletions

File tree

ScriptEngine/ScriptEnginePackage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics.CodeAnalysis;
44
using System.Runtime.InteropServices;
55
using System.Threading;
6+
using EnvDTE80;
67
using Microsoft.VisualStudio.Shell;
78
using Microsoft.VisualStudio.Shell.Interop;
89
using Task = System.Threading.Tasks.Task;
@@ -24,6 +25,7 @@ public sealed class ScriptEnginePackage : AsyncPackage
2425
public const int CommandId = 0x0100;
2526

2627
public dynamic vsModule;
28+
public DTE2 dte;
2729

2830
/// <summary>
2931
/// Initialization of the package; this method is called right after the package is sited, so this is the place
@@ -36,6 +38,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
3638
{
3739
// Switch to main thread
3840
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
41+
dte = await this.GetServiceAsync(typeof(EnvDTE.DTE)) as DTE2;
3942

4043
// Register menu handlers
4144
OleMenuCommandService commandService = await GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService;

ScriptEngineStarter/CsScript.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class CsScript
3535
/// Use CleanupScScriptTempDir() on application startup to wipe out compilation folder.
3636
///
3737
/// </summary>
38-
static public void RunScript( String scriptPath, Object mainArg)
38+
/// <returns>Returns object from main method call, null if not called</returns>
39+
static public object RunScript( String scriptPath, Object mainArg)
3940
{
4041
String tempDir = GetScriptTempDir();
4142

@@ -168,23 +169,23 @@ static public void RunScript( String scriptPath, Object mainArg)
168169
// ----------------------------------------------------------------
169170
try
170171
{
171-
entry.Invoke(null, new object[] { mainArg });
172+
object oRet = entry.Invoke(null, new object[] { mainArg });
172173
Directory.SetCurrentDirectory(oldDir);
174+
175+
return oRet;
173176
}
174177
catch (Exception ex)
175-
{
178+
{
176179
Directory.SetCurrentDirectory(oldDir);
177180

178181
String errors = "";
179182

180183
try
181184
{
182185
StackFrame[] stack = new StackTrace(ex.InnerException, true).GetFrames();
183-
StackFrame lastCall = stack[0];
184-
185-
errors = String.Format("{0}({1},{2}): error: {3}\r\n", path,
186-
lastCall.GetFileLineNumber(), lastCall.GetFileColumnNumber(), ex.InnerException.Message);
186+
StackFrame lastCall = stack.Last();
187187

188+
errors = String.Format("{0}({1},{2}): error: {3}\r\n", path, lastCall.GetFileLineNumber(), lastCall.GetFileColumnNumber(), ex.InnerException.Message);
188189
}
189190
catch (Exception ex3)
190191
{
@@ -203,6 +204,8 @@ static public void RunScript( String scriptPath, Object mainArg)
203204
// Works only when there is no debugger attached.
204205
//try { File.Delete(pdbPath); } catch { }
205206
}
207+
208+
return null;
206209
}
207210

208211
static String GetGlobalScriptTempDir()

ScriptEngineStarter/ScriptHost.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,10 @@ static void FileReloadUIThread( String file )
483483
// helper.Invoke("*.Main");
484484
//}
485485

486-
CsScript.RunScript(file, mainArg);
487-
exceptionHandler.ReportScriptResult(file, null);
486+
object oRet = CsScript.RunScript(file, mainArg);
487+
488+
if(!(oRet is bool) || (bool)oRet != false)
489+
exceptionHandler.ReportScriptResult(file, null);
488490
break;
489491
}
490492
catch (IOException ex)

ScriptEngineStarter/VsScriptExceptionHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/// <summary>
1313
/// Handles errors coming from C# script compilation.
1414
/// </summary>
15-
class VsScriptExceptionHandler : ScriptExceptionHandler
15+
public class VsScriptExceptionHandler : ScriptExceptionHandler
1616
{
1717
public override async void ReportScriptResult(String file, Exception ex)
1818
{

Tools/vsDev/vsDev.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
public class vsDev
2727
{
28-
29-
public static async void Main( object arg )
28+
public static void Main( object arg )
3029
{
3130
ScriptEnginePackage sepkg = (ScriptEnginePackage)arg;
32-
DTE2 dte = await sepkg.GetServiceAsync(typeof(DTE)) as DTE2;
33-
IServiceProvider serviceProvider = sepkg as IServiceProvider;
3431

3532
Debug.WriteLine("New compilation: " + Assembly.GetExecutingAssembly().FullName);
33+
var dte = sepkg.dte;
34+
35+
//File.Delete(Assembly.GetExecutingAssembly().Location);
3636
}
3737

3838
}

0 commit comments

Comments
 (0)