From e8d22553fa08db888e71bfd109fa57a24540fdd8 Mon Sep 17 00:00:00 2001 From: Adam Driscoll Date: Thu, 3 Oct 2019 08:55:22 -0600 Subject: [PATCH] Make debugging easier. --- .../Execution/ExecutionService.cs | 24 +++++++++++++++--- .../Interfaces/IDashboardService.cs | 2 ++ .../Services/DashboardService.cs | 2 ++ src/UniversalDashboard/Services/Debugger.cs | 25 +++++++++++++++++++ .../Services/UDRunspaceFactory.cs | 8 +++--- 5 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/UniversalDashboard/Services/Debugger.cs diff --git a/src/UniversalDashboard/Execution/ExecutionService.cs b/src/UniversalDashboard/Execution/ExecutionService.cs index 549af139..6d92a017 100644 --- a/src/UniversalDashboard/Execution/ExecutionService.cs +++ b/src/UniversalDashboard/Execution/ExecutionService.cs @@ -59,9 +59,10 @@ public ExecutionService(IDashboardService dashboardService, IHubContext output; @@ -104,6 +121,7 @@ public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint) { runspaceRef.Runspace.ResetRunspaceState(); Runspace.DefaultRunspace = runspaceRef.Runspace; + runspaceRef.Runspace.Name = endpoint.Name; ps.Runspace = runspaceRef.Runspace; var host = (UDHost)_host.GetValue(ps.Runspace); @@ -138,7 +156,7 @@ public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint) SetVariable(ps, "ClaimsPrincipal", context.User); } - ps.AddStatement().AddScript(script); + ps.AddStatement().AddScript(scriptBuilder.ToString()); foreach (var parameter in context.Parameters) { diff --git a/src/UniversalDashboard/Interfaces/IDashboardService.cs b/src/UniversalDashboard/Interfaces/IDashboardService.cs index 21f9916f..562983bd 100644 --- a/src/UniversalDashboard/Interfaces/IDashboardService.cs +++ b/src/UniversalDashboard/Interfaces/IDashboardService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using UniversalDashboard.Models; +using UniversalDashboard.Services; namespace UniversalDashboard.Interfaces { @@ -15,5 +16,6 @@ public interface IDashboardService : IDisposable string ReloadToken { get; set; } DateTime StartTime { get; } IEndpointService EndpointService { get; } + Debugger Debugger { get; } } } diff --git a/src/UniversalDashboard/Services/DashboardService.cs b/src/UniversalDashboard/Services/DashboardService.cs index 5d7b0d23..c147ca77 100644 --- a/src/UniversalDashboard/Services/DashboardService.cs +++ b/src/UniversalDashboard/Services/DashboardService.cs @@ -23,6 +23,7 @@ public DashboardService(DashboardOptions dashboardOptions, string reloadToken) { StartTime = DateTime.UtcNow; Properties = new Dictionary(); DashboardOptions = dashboardOptions; + Debugger = new Debugger(); } public Dashboard Dashboard { get; private set; } @@ -32,6 +33,7 @@ public DashboardService(DashboardOptions dashboardOptions, string reloadToken) { public string UpdateToken {get;set;} public string ReloadToken {get;set;} public DateTime StartTime { get; private set; } + public Debugger Debugger { get; private set; } public IEndpointService EndpointService { diff --git a/src/UniversalDashboard/Services/Debugger.cs b/src/UniversalDashboard/Services/Debugger.cs new file mode 100644 index 00000000..f2a78b8f --- /dev/null +++ b/src/UniversalDashboard/Services/Debugger.cs @@ -0,0 +1,25 @@ + +using System.Collections.Generic; + +namespace UniversalDashboard.Services +{ + public class Debugger + { + internal List BreakOnEndpoints { get; private set; } = new List(); + + public void BreakOnEndpoint(string id) + { + BreakOnEndpoints.Add(id.ToLower()); + } + + public void ClearEndpoint(string id) + { + BreakOnEndpoints.Remove(id.ToLower()); + } + + public bool ShouldBreak(string id) + { + return BreakOnEndpoints.Contains(id.ToLower()); + } + } +} \ No newline at end of file diff --git a/src/UniversalDashboard/Services/UDRunspaceFactory.cs b/src/UniversalDashboard/Services/UDRunspaceFactory.cs index 70838d95..4dbb3026 100644 --- a/src/UniversalDashboard/Services/UDRunspaceFactory.cs +++ b/src/UniversalDashboard/Services/UDRunspaceFactory.cs @@ -1,25 +1,22 @@ using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Linq; using System.Management.Automation; -using System.Management.Automation.Host; using System.Management.Automation.Runspaces; using System.Reflection; -using System.Threading; using NLog; using UniversalDashboard.Interfaces; -using UniversalDashboard.Models; namespace UniversalDashboard.Services { public class RunspaceReference : IRunspaceReference { private IUDRunspaceFactory _factory; + private readonly string _runspaceName; public RunspaceReference(IUDRunspaceFactory factory, Runspace runspace) { _factory = factory; Runspace = runspace; + _runspaceName = runspace.Name; } public Runspace Runspace {get;} @@ -33,6 +30,7 @@ protected virtual void Dispose(bool disposing) { if (disposing) { + Runspace.Name = _runspaceName; _factory.ReturnRunspace(Runspace); }