Skip to content

Commit

Permalink
Make debugging easier.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdriscoll committed Oct 3, 2019
1 parent c33eb93 commit e8d2255
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/UniversalDashboard/Execution/ExecutionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public ExecutionService(IDashboardService dashboardService, IHubContext<Dashboar

public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint)
{
var script = endpoint.ScriptBlock.ToString();
var scriptBuilder = new StringBuilder();
var scriptBlockAst = endpoint.ScriptBlock.Ast as ScriptBlockAst;

string header = string.Empty;
if (scriptBlockAst.ParamBlock == null && context.Parameters.Any())
{
if (Log.IsDebugEnabled)
Expand Down Expand Up @@ -92,8 +93,24 @@ public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint)
paramBlockBuilder.Remove(paramBlockBuilder.Length - 1, 1);
paramBlockBuilder.Append(")");
paramBlockBuilder.AppendLine();
header = paramBlockBuilder.ToString();

script = paramBlockBuilder.ToString() + scriptBlockAst.EndBlock.ToString();
}
else if (scriptBlockAst.ParamBlock != null)
{
header = scriptBlockAst.ParamBlock.ToString();
}

scriptBuilder.AppendLine(header);

if (_dashboardService.Debugger.ShouldBreak(endpoint.Name))
{
scriptBuilder.AppendLine("Wait-Debugger");
}

foreach(var statement in scriptBlockAst.EndBlock.Statements)
{
scriptBuilder.AppendLine(statement.ToString());
}

Collection<PSObject> output;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/UniversalDashboard/Interfaces/IDashboardService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using UniversalDashboard.Models;
using UniversalDashboard.Services;

namespace UniversalDashboard.Interfaces
{
Expand All @@ -15,5 +16,6 @@ public interface IDashboardService : IDisposable
string ReloadToken { get; set; }
DateTime StartTime { get; }
IEndpointService EndpointService { get; }
Debugger Debugger { get; }
}
}
2 changes: 2 additions & 0 deletions src/UniversalDashboard/Services/DashboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public DashboardService(DashboardOptions dashboardOptions, string reloadToken) {
StartTime = DateTime.UtcNow;
Properties = new Dictionary<string, object>();
DashboardOptions = dashboardOptions;
Debugger = new Debugger();
}

public Dashboard Dashboard { get; private set; }
Expand All @@ -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
{
Expand Down
25 changes: 25 additions & 0 deletions src/UniversalDashboard/Services/Debugger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

using System.Collections.Generic;

namespace UniversalDashboard.Services
{
public class Debugger
{
internal List<string> BreakOnEndpoints { get; private set; } = new List<string>();

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());
}
}
}
8 changes: 3 additions & 5 deletions src/UniversalDashboard/Services/UDRunspaceFactory.cs
Original file line number Diff line number Diff line change
@@ -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;}
Expand All @@ -33,6 +30,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Runspace.Name = _runspaceName;
_factory.ReturnRunspace(Runspace);
}

Expand Down

0 comments on commit e8d2255

Please sign in to comment.