Skip to content

Commit

Permalink
Merge pull request #794 from ironmansoftware/758
Browse files Browse the repository at this point in the history
Implements #758
  • Loading branch information
adamdriscoll authored May 19, 2019
2 parents 618f08c + a275557 commit 3842185
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 41 deletions.
10 changes: 10 additions & 0 deletions src/UniversalDashboard/Execution/ExecutionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Management.Automation.Runspaces;
using System.Text;
using UniversalDashboard.Interfaces;
using System.Reflection;

namespace UniversalDashboard.Execution
{
Expand Down Expand Up @@ -53,6 +54,9 @@ public ExecutionService(IDashboardService dashboardService, IHubContext<Dashboar
_stateRequestService = stateRequestService;
}

private static readonly Type _runspaceBase = typeof(Runspace).Assembly.GetType("System.Management.Automation.Runspaces.RunspaceBase");
private static readonly PropertyInfo _host = _runspaceBase.GetProperty("Host", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance);

public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint)
{
var script = endpoint.ScriptBlock.ToString();
Expand Down Expand Up @@ -93,6 +97,9 @@ public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint)
Runspace.DefaultRunspace = runspaceRef.Runspace;
ps.Runspace = runspaceRef.Runspace;

var host = (UDHost)_host.GetValue(ps.Runspace);
var ui = (UDHostUserInterface)host.UI;

if (endpoint.Variables != null)
{
Log.Debug("Scope variables--------------");
Expand All @@ -113,6 +120,9 @@ public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint)
SetVariable(ps, "ArgumentList", context.Endpoint.ArgumentList?.ToList());
SetVariable(ps, Constants.UDPage, context.Endpoint.Page);

ui.HubContext = _hubContext;
ui.ConnectionId = context.ConnectionId;

if (context.User != null)
{
SetVariable(ps, "ClaimsPrinciple", context.User);
Expand Down
11 changes: 11 additions & 0 deletions src/UniversalDashboard/Models/Enums/MessageType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace UniversalDashboard.Models.Enums
{
public enum MessageType
{
Informational,
Debug,
Warning,
Error,
Verbose
}
}
10 changes: 9 additions & 1 deletion src/UniversalDashboard/Server/DashboardHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
using UniversalDashboard.Models;
using UniversalDashboard.Models.Basics;
using UniversalDashboard.Services;
using System.Text;
using System.Text;
using UniversalDashboard.Models.Enums;

namespace UniversalDashboard
{
Expand Down Expand Up @@ -97,6 +98,13 @@ public static async Task SyncElement(this IHubContext<DashboardHub> hub, string
{
await hub.Clients.All.SendAsync("syncElement", componentId);
}

// PS Host

public static async Task Write(this IHubContext<DashboardHub> hub, string clientId, string message, MessageType messageType)
{
await hub.Clients.Client(clientId).SendAsync("write", message, messageType);
}
}

public class DashboardHub : Hub {
Expand Down
149 changes: 149 additions & 0 deletions src/UniversalDashboard/Services/UDHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using Microsoft.AspNetCore.SignalR;
using NLog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using System.Security;
using UniversalDashboard.Models.Enums;

namespace UniversalDashboard.Services
{
public class UDHost : PSHost
{
private readonly UDHostUserInterface _udHostUserInterface;

public UDHost()
{
_udHostUserInterface = new UDHostUserInterface();
}

public override string Name => "UDHost";

public override Version Version => new Version(1, 0);

public override Guid InstanceId => Guid.NewGuid();

public override PSHostUserInterface UI => _udHostUserInterface;

public override CultureInfo CurrentCulture => CultureInfo.CurrentCulture;

public override CultureInfo CurrentUICulture => CultureInfo.CurrentUICulture;

public override void EnterNestedPrompt()
{

}

public override void ExitNestedPrompt()
{

}

public override void NotifyBeginApplication()
{

}

public override void NotifyEndApplication()
{

}

public override void SetShouldExit(int exitCode)
{

}
}

public class UDHostUserInterface : PSHostUserInterface
{
private static readonly Logger Log = LogManager.GetLogger(nameof(UDHostUserInterface));

public override PSHostRawUserInterface RawUI => null;

public IHubContext<DashboardHub> HubContext { get; set; }
public string ConnectionId { get; set; }

public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
throw new NotImplementedException();
}

public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
throw new NotImplementedException();
}

public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
throw new NotImplementedException();
}

public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
throw new NotImplementedException();
}

public override string ReadLine()
{
throw new NotImplementedException();
}

public override SecureString ReadLineAsSecureString()
{
throw new NotImplementedException();
}

public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
Log.Trace(value);
//HubContext.Write(ConnectionId, value, MessageType.Informational).ConfigureAwait(false);
}

public override void Write(string value)
{
Log.Trace(value);
//HubContext.Write(ConnectionId, value, MessageType.Informational).ConfigureAwait(false);
}

public override void WriteDebugLine(string message)
{
Log.Trace("[DEBUG]:" + message);
//HubContext.Write(ConnectionId, message + Environment.NewLine, MessageType.Debug).ConfigureAwait(false);
}

public override void WriteErrorLine(string value)
{
Log.Trace("[ERROR]:" + value);
//HubContext.Write(ConnectionId, value + Environment.NewLine, MessageType.Error).ConfigureAwait(false);
}

public override void WriteLine(string value)
{
Log.Trace("[DEBUG]:" + value);
//HubContext.Write(ConnectionId, value + Environment.NewLine, MessageType.Informational).ConfigureAwait(false);
}

public override void WriteProgress(long sourceId, ProgressRecord record)
{

}

public override void WriteVerboseLine(string message)
{
Log.Trace("[VERBOSE]:" + message);
//HubContext.Write(ConnectionId, message + Environment.NewLine, MessageType.Verbose).ConfigureAwait(false);
}

public override void WriteWarningLine(string message)
{
Log.Trace("[WARNING]:" + message);
//HubContext.Write(ConnectionId, message + Environment.NewLine, MessageType.Warning).ConfigureAwait(false);
}
}
}
40 changes: 0 additions & 40 deletions src/UniversalDashboard/Services/UDRunspaceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,44 +106,4 @@ public void Dispose()
}
#endregion
}

public class UDHost : PSHost
{
public override string Name => "UDHost";

public override Version Version => new Version(1, 0);

public override Guid InstanceId => Guid.NewGuid();

public override PSHostUserInterface UI => null;

public override CultureInfo CurrentCulture => CultureInfo.CurrentCulture;

public override CultureInfo CurrentUICulture => CultureInfo.CurrentUICulture;

public override void EnterNestedPrompt()
{

}

public override void ExitNestedPrompt()
{

}

public override void NotifyBeginApplication()
{

}

public override void NotifyEndApplication()
{

}

public override void SetShouldExit(int exitCode)
{

}
}
}
5 changes: 5 additions & 0 deletions src/client/src/app/ud-dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export default class UdDashboard extends React.Component {
}
});

connection.on('write', (message) => {
console.log(message);
PubSub.publish("write", message);
});

PubSub.subscribe('element-event', function(e, data) {
if (data.type === "requestStateResponse") {
connection.invoke("requestStateResponse", data.requestId, data.state)
Expand Down

0 comments on commit 3842185

Please sign in to comment.