Skip to content

Commit

Permalink
Experimental passing PSInvocationState to WebView #5
Browse files Browse the repository at this point in the history
  • Loading branch information
megastary committed Aug 25, 2021
1 parent e61f78a commit 85f93c7
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HoubyStudio.LazyAdmin.DesktopApp
{
public class CSharpMessage
{
private Guid _uid;

public Guid Uid
{
set => _uid = value;
get => _uid;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HoubyStudio.LazyAdmin.DesktopApp
{
public class CSharpRunspaceStatusMessage : CSharpMessage
{
public string Type { set; get; }
public string Status { set; get; }
public string Result { set; get; }

public CSharpRunspaceStatusMessage(Guid uid, string status, string result)
{
Type = "runespacestatus";
Uid = uid;
Status = status;
Result = result;
}

public CSharpRunspaceStatusMessage(Guid uid, string status)
{
Type = "runespacestatus";
Uid = uid;
Status = status;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HoubyStudio.LazyAdmin.DesktopApp
{
public class ExecutePowerShellMessage : WebViewMessage
{
// TODO: Create command class containing command, parameters, common parameters etc.
// TODO: Create list of command classes here to be iritated and added to ps command stack
private string _command;

public string Command
{
set => _command = value;
get => _command;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Cake.Powershell" Version="1.0.1" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.1.4" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.902.49" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Management.Automation" Version="7.1.4" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;
using Newtonsoft.Json;

namespace HoubyStudio.LazyAdmin.DesktopApp
{
Expand All @@ -24,7 +25,7 @@ public class LazyAdminPowerShell

public void ReceiveMessage(object sender, CoreWebView2WebMessageReceivedEventArgs args)
{
MockPowerShell.Text = "Staaaaaaarting";
MockPowerShell.Text = "Execution started";
LazyAdminWebView.ShowMessage("Execution started");
////try
////{
Expand Down Expand Up @@ -93,11 +94,16 @@ public void ReceiveMessage(object sender, CoreWebView2WebMessageReceivedEventArg

using (PowerShell ps = PowerShell.Create())
{
var receivedJson = JsonConvert.DeserializeObject<ExecutePowerShellMessage>(args.WebMessageAsJson);
var uid = receivedJson.Uid;
var command = receivedJson.Command;
// Set the PowerShell object to use the JEA runspace
// TODO: On creation pass runspace id or instanceId to webview, so it knows what to kill
// OR LazyAdminPowerShell could hold reference to each runspace associated to uid from WebView and communicate with identified runspace that way
ps.Runspace = runspace;

// Now you can add and invoke commands
ps.AddCommand("Get-Process");
_ = ps.AddCommand(command);

// Simple handle PowerShell async
//IAsyncResult gpcAsyncResult = ps.BeginInvoke();
Expand All @@ -116,19 +122,27 @@ public void ReceiveMessage(object sender, CoreWebView2WebMessageReceivedEventArg
// Add the event handlers. If we did not care about hooking the DataAdded
// event, we would let BeginInvoke create the output stream for us.
PSDataCollection<PSObject> output = new();
// TODO: Extend DataAddedEventArgs and PSInvocationStateChangedEventArgs to pass uid and command
output.DataAdded += new EventHandler<DataAddedEventArgs>(Output_DataAdded);
ps.InvocationStateChanged += new EventHandler<PSInvocationStateChangedEventArgs>(Powershell_InvocationStateChanged);

// Invoke the pipeline asynchronously.
IAsyncResult asyncResult = ps.BeginInvoke<PSObject, PSObject>(null, output);

PSDataCollection<PSObject> gpcOutput = ps.EndInvoke(asyncResult);
try
{
PSDataCollection<PSObject> gpcOutput = ps.EndInvoke(asyncResult);
}
catch
{

}

// This is how different thread may access function from main thread
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
MainWindow.ShowMessageFromThread();
}));
//_ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
// {
// MainWindow.ShowMessageFromThread(uid);
// }));
// This throws error
//LazyAdminWebView.ShowMessage("Endded invoke");

Expand Down Expand Up @@ -169,6 +183,11 @@ private void Output_DataAdded(object sender, DataAddedEventArgs e)
Collection<PSObject> results = myp.ReadAll();
foreach (PSObject result in results)
{
_ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
//LazyAdminWebView.ShowMessage("Results obtained");
MainWindow.ShowMessageFromThread(Guid.NewGuid(), "Running");
}));
//LazyAdminWebView.ShowMessage("Result mate");
//dispatcher.Invoke((Action)(() => MockPowerShell.Text = result + "`r`n"));
}
Expand All @@ -188,6 +207,11 @@ private void Powershell_InvocationStateChanged(object sender, PSInvocationStateC
{
//dispatcher.Invoke((Action)(() => MockPowerShell.Text = "Processing completed, press a key to exit!"));
//MockPowerShell.Text = "Processing completed, press a key to exit!";
_ = System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
LazyAdminWebView.ShowMessage("Finished");
MainWindow.ShowMessageFromThread(Guid.NewGuid(), "Completed");
}));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
using Microsoft.Web.WebView2.Wpf;
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace HoubyStudio.LazyAdmin.DesktopApp
{
Expand Down Expand Up @@ -53,6 +52,28 @@ public static void ShowMessage(string Message)
_webView.CoreWebView2.ExecuteScriptAsync($"alert('{Message}')");
}

/// <summary>
/// Change Runspace status
/// </summary>
public static void PostRunspaceStatus(Guid Uid, string Status, string Result)
{
CSharpRunspaceStatusMessage jsonMessage = new(Uid, Status, Result);

string jsonString = JsonConvert.SerializeObject(jsonMessage);
_webView.CoreWebView2.PostWebMessageAsJson(jsonString);
}

/// <summary>
/// Change Runspace status
/// </summary>
public static void PostRunspaceStatus(Guid Uid, string Status)
{
CSharpRunspaceStatusMessage jsonMessage = new(Uid, Status);

string jsonString = JsonConvert.SerializeObject(jsonMessage);
_webView.CoreWebView2.PostWebMessageAsJson(jsonString);
}

/// <summary>
/// Posts WebMessage in the JSON form to the WebView2 control.
/// </summary>
Expand All @@ -61,7 +82,7 @@ public static void PostWebMessageAsJSON(string Message)
{
PowerShellData jsonMessage = new(Message);

string jsonString = JsonSerializer.Serialize(jsonMessage);
string jsonString = JsonConvert.SerializeObject(jsonMessage);
_webView.CoreWebView2.PostWebMessageAsJson(jsonString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ private void Execute_Click(object sender, RoutedEventArgs e)
LazyAdminWebView.PostWebMessageAsJSON(PowerShell.Text);
}

public static void ShowMessageFromThread()
public static void ShowMessageFromThread(Guid uid, string status, string message)
{
LazyAdminWebView.ShowMessage("Ended");
LazyAdminWebView.PostRunspaceStatus(uid, status, message);
}

public static void ShowMessageFromThread(Guid uid, string status)
{
LazyAdminWebView.PostRunspaceStatus(uid, status);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HoubyStudio.LazyAdmin.DesktopApp
{
public class WebViewMessage
{
private Guid _uid;
private Guid _token;

public Guid Uid
{
set => _uid = value;
get => _uid;
}

public Guid Token
{
set => _token = value;
get => _token;
}
}
}

0 comments on commit 85f93c7

Please sign in to comment.