2023-07-19 22:09:25 +00:00
|
|
|
|
using System;
|
2023-07-22 00:20:46 +00:00
|
|
|
|
using System.Collections.Generic;
|
2023-07-19 22:09:25 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-07-22 00:20:46 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
2023-07-21 20:11:56 +00:00
|
|
|
|
using Cauldron.Core;
|
2023-07-19 22:09:25 +00:00
|
|
|
|
|
2023-07-21 20:11:56 +00:00
|
|
|
|
namespace Cauldron.Macos;
|
2023-07-19 22:09:25 +00:00
|
|
|
|
|
|
|
|
|
public static class ScriptRunner
|
|
|
|
|
{
|
|
|
|
|
public static void RunScript(MainWindow window)
|
|
|
|
|
{
|
|
|
|
|
window.SetScriptRunState(true);
|
2023-07-22 00:20:46 +00:00
|
|
|
|
|
|
|
|
|
TagBuilder body = new("body");
|
|
|
|
|
|
|
|
|
|
window.ScriptOutputWebView.SetOutputPanelContent(body);
|
2023-07-19 22:09:25 +00:00
|
|
|
|
TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();
|
|
|
|
|
|
2023-07-21 20:11:56 +00:00
|
|
|
|
CauldronWriter writer = new(obj =>
|
2023-07-19 22:09:25 +00:00
|
|
|
|
{
|
2023-07-22 00:20:46 +00:00
|
|
|
|
window.BeginInvokeOnMainThread(() =>
|
2023-07-19 22:09:25 +00:00
|
|
|
|
{
|
2023-07-22 00:20:46 +00:00
|
|
|
|
body.InnerHtml.AppendHtml(GenerateValueOutput(obj));
|
|
|
|
|
window.ScriptOutputWebView.SetOutputPanelContent(body);
|
|
|
|
|
});
|
2023-07-19 22:09:25 +00:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.ScriptCancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
string script = window.ScriptText;
|
|
|
|
|
|
|
|
|
|
Task task = Task
|
|
|
|
|
.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await RoslynHost.RunScript(script, Array.Empty<string>(),
|
|
|
|
|
new RoslynHostGlobals(writer),
|
|
|
|
|
window.ScriptCancellationTokenSource.Token);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
window.BeginInvokeOnMainThread(() =>
|
2023-07-22 00:20:46 +00:00
|
|
|
|
{
|
|
|
|
|
TagBuilder exTag = new("pre");
|
|
|
|
|
exTag.InnerHtml.Append(ex.ToString());
|
|
|
|
|
body.InnerHtml.AppendHtml(exTag);
|
|
|
|
|
|
|
|
|
|
window.ScriptOutputWebView.SetOutputPanelContent(body);
|
|
|
|
|
});
|
2023-07-19 22:09:25 +00:00
|
|
|
|
}
|
|
|
|
|
}, window.ScriptCancellationTokenSource.Token)
|
|
|
|
|
.ContinueWith((t) => window.SetScriptRunState(false),
|
|
|
|
|
uiThread);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-22 00:20:46 +00:00
|
|
|
|
private static TagBuilder GenerateValueOutput(object value)
|
|
|
|
|
{
|
|
|
|
|
if (value is string str)
|
|
|
|
|
{
|
|
|
|
|
TagBuilder tag = new("p");
|
|
|
|
|
tag.InnerHtml.Append(str);
|
|
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
if (value.GetType().IsPrimitive)
|
|
|
|
|
{
|
|
|
|
|
TagBuilder tag = new("p");
|
|
|
|
|
tag.InnerHtml.Append(value.ToString());
|
|
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
if (value is IEnumerable<object> enumberable)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SetOutputPanelContent(this WebKit.WKWebView webView,
|
|
|
|
|
TagBuilder body)
|
|
|
|
|
{
|
|
|
|
|
TagBuilder head = new("head");
|
|
|
|
|
|
|
|
|
|
string contents = "<!DOCTYPE html>"
|
|
|
|
|
+ head.RenderAsString()
|
|
|
|
|
+ body.RenderAsString();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Contents: " + contents);
|
|
|
|
|
|
|
|
|
|
webView.LoadHtmlString(new Foundation.NSString(contents), null);
|
|
|
|
|
}
|
|
|
|
|
}
|