From adeeb6ceb9919b5ef43d8dc51ed92b423ddeabf4 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Tue, 26 Apr 2011 15:19:34 +0100 Subject: [PATCH] Show line being executed in the console window when a PowerShell script has a runtime error. --- .../Project/PackageManagement.Cmdlets.csproj | 1 + .../Project/Src/PackageManagementCmdlet.cs | 8 +++- .../Src/RuntimeExceptionErrorMessage.cs | 46 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/RuntimeExceptionErrorMessage.cs diff --git a/src/AddIns/Misc/PackageManagement/Cmdlets/Project/PackageManagement.Cmdlets.csproj b/src/AddIns/Misc/PackageManagement/Cmdlets/Project/PackageManagement.Cmdlets.csproj index 953386d35e..3d7650ffd2 100644 --- a/src/AddIns/Misc/PackageManagement/Cmdlets/Project/PackageManagement.Cmdlets.csproj +++ b/src/AddIns/Misc/PackageManagement/Cmdlets/Project/PackageManagement.Cmdlets.csproj @@ -49,6 +49,7 @@ + diff --git a/src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/PackageManagementCmdlet.cs b/src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/PackageManagementCmdlet.cs index f0f3154a60..0939bc2337 100644 --- a/src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/PackageManagementCmdlet.cs +++ b/src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/PackageManagementCmdlet.cs @@ -90,7 +90,13 @@ namespace ICSharpCode.PackageManagement.Cmdlets public void InvokeScript(string script) { - InvokeCommand.InvokeScript(script); + try { + InvokeCommand.InvokeScript(script); + } catch (RuntimeException ex) { + var errorMessage = new RuntimeExceptionErrorMessage(ex); + WriteWarning(errorMessage.ToString()); + throw; + } } } } diff --git a/src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/RuntimeExceptionErrorMessage.cs b/src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/RuntimeExceptionErrorMessage.cs new file mode 100644 index 0000000000..13240f2ba5 --- /dev/null +++ b/src/AddIns/Misc/PackageManagement/Cmdlets/Project/Src/RuntimeExceptionErrorMessage.cs @@ -0,0 +1,46 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using System.Management.Automation; +using System.Text; + +namespace ICSharpCode.PackageManagement.Cmdlets +{ + public class RuntimeExceptionErrorMessage + { + StringBuilder message = new StringBuilder(); + + public RuntimeExceptionErrorMessage(RuntimeException ex) + { + CreateErrorMessage(ex); + } + + void CreateErrorMessage(RuntimeException ex) + { + message.AppendLine(ex.Message); + AppendErrorRecord(ex.ErrorRecord); + } + + void AppendErrorRecord(ErrorRecord errorRecord) + { + if (errorRecord != null) { + AppendInvocationInfo(errorRecord.InvocationInfo); + } + } + + void AppendInvocationInfo(InvocationInfo invocationInfo) + { + if (invocationInfo != null) { + message.AppendLine(invocationInfo.PositionMessage); + message.AppendLine("Script Line: " + invocationInfo.ScriptLineNumber); + message.AppendLine("Script Name: " + invocationInfo.ScriptName); + } + } + + public override string ToString() + { + return message.ToString(); + } + } +}