Integrated Provider to ProgrartExecutor.
This commit is contained in:
@@ -3,6 +3,7 @@ using Jint.Native;
|
||||
using Jint.Native.Function;
|
||||
using Jint.Native.Json;
|
||||
using Progrart.Core.Graphics;
|
||||
using Progrart.Core.Storage;
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
|
||||
@@ -12,10 +13,12 @@ namespace Progrart.Core.JSExecution
|
||||
{
|
||||
public ExecutionEngine engine;
|
||||
public Dictionary<string, object> ObjectPool = new();
|
||||
public ProgrartExecutor()
|
||||
public IStorageProvider StorageProvider;
|
||||
public ProgrartExecutor(IStorageProvider storageProvider)
|
||||
{
|
||||
engine = new ExecutionEngine();
|
||||
SetupCalls();
|
||||
StorageProvider = storageProvider;
|
||||
}
|
||||
public void SetupCalls()
|
||||
{
|
||||
|
||||
@@ -1,42 +1,47 @@
|
||||
using Newtonsoft.Json;
|
||||
using Progrart.Core.JSExecution;
|
||||
using Progrart.Core.Storage;
|
||||
|
||||
namespace Progrart.Core.ProjectSystem
|
||||
{
|
||||
public class Builder
|
||||
{
|
||||
public Project project;
|
||||
public string basePath;
|
||||
public IStorageProvider provider;
|
||||
public Action<int, int>? OnProgressUpdate;
|
||||
public Action? OnCompleted;
|
||||
public Builder(StreamReader reader, string basePath)
|
||||
public Builder(StreamReader reader, IStorageProvider provider)
|
||||
{
|
||||
var project = JsonConvert.DeserializeObject<Project>(reader.ReadToEnd());
|
||||
if (project is null) throw new JsonSerializationException();
|
||||
this.project = project;
|
||||
this.basePath = basePath;
|
||||
this.provider = provider;
|
||||
}
|
||||
public Builder(Project project, string basePath)
|
||||
public Builder(Project project, IStorageProvider provider)
|
||||
{
|
||||
this.project = project;
|
||||
this.basePath = basePath;
|
||||
this.provider = provider;
|
||||
}
|
||||
public void Execute(BuildConfiguration config, BuildItem item)
|
||||
public async Task Execute(BuildConfiguration config, BuildItem item)
|
||||
{
|
||||
FileInfo src = new FileInfo(Path.Combine(basePath, item.Source));
|
||||
FileInfo tgt = new FileInfo(Path.Combine(basePath, project.OutputDir, item.Target ?? item.Source + ".png"));
|
||||
//FileInfo src = new FileInfo(Path.Combine(basePath, item.Source));
|
||||
//FileInfo tgt = new FileInfo(Path.Combine(basePath, project.OutputDir, item.Target ?? item.Source + ".png"));
|
||||
var args = project.Arguments.Clone();
|
||||
args.MergeFrom(config.Arguments);
|
||||
args.MergeFrom(item.Arguments);
|
||||
ProgrartExecutor executor = new ProgrartExecutor();
|
||||
using var stream = src.OpenRead();
|
||||
ProgrartExecutor executor = new ProgrartExecutor(provider);
|
||||
using var stream = await provider.TryOpenRead(item.Source);
|
||||
if (stream is null)
|
||||
return;
|
||||
using var reader = new StreamReader(stream);
|
||||
var img = executor.RenderImage(item.Size, reader.ReadToEnd(), args);
|
||||
using var img_stream = tgt.OpenWrite();
|
||||
using var img_stream = await provider.TryOpenWrite(Path.Combine(project.OutputDir, item.Target ?? item.Source + ".png"));
|
||||
if (img_stream is null)
|
||||
return;
|
||||
img.DrawingCore.ToData().SaveTo(img_stream);
|
||||
img_stream.Flush();
|
||||
}
|
||||
public void Build(string targetConfig)
|
||||
public async Task Build(string targetConfig)
|
||||
{
|
||||
foreach (var config in project.Configurations)
|
||||
{
|
||||
@@ -45,7 +50,7 @@ namespace Progrart.Core.ProjectSystem
|
||||
int index = 0;
|
||||
foreach (var item in config.Items)
|
||||
{
|
||||
Execute(config, item);
|
||||
await Execute(config, item);
|
||||
index++;
|
||||
OnProgressUpdate?.Invoke(config.Items.Count, index);
|
||||
}
|
||||
|
||||
@@ -7,12 +7,14 @@ using Avalonia.Markup.Xaml;
|
||||
using Progrart.Views;
|
||||
using Progrart.Icons;
|
||||
using Progrart.Pages;
|
||||
using Avalonia.Platform.Storage;
|
||||
|
||||
namespace Progrart;
|
||||
|
||||
public partial class App : Application
|
||||
{
|
||||
public static bool isDesktop = false;
|
||||
public static IStorageFolder? CurrentOpenFolder = null;
|
||||
public override void Initialize()
|
||||
{
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
|
||||
@@ -9,15 +9,16 @@ namespace Progrart.Core.Storage
|
||||
{
|
||||
public class AvaloniaStorageProvider : IStorageProvider
|
||||
{
|
||||
IStorageFolder baseFolder;
|
||||
IStorageFolder? baseFolder;
|
||||
|
||||
public AvaloniaStorageProvider(IStorageFolder baseFolder)
|
||||
public AvaloniaStorageProvider(IStorageFolder? baseFolder)
|
||||
{
|
||||
this.baseFolder = baseFolder;
|
||||
}
|
||||
|
||||
public async Task<Stream?> TryOpenRead(string path)
|
||||
{
|
||||
if (baseFolder is null) return null;
|
||||
var file = await baseFolder.GetFileAsync(path);
|
||||
if (file != null)
|
||||
{
|
||||
@@ -28,6 +29,7 @@ namespace Progrart.Core.Storage
|
||||
|
||||
public async Task<Stream?> TryOpenWrite(string path)
|
||||
{
|
||||
if (baseFolder is null) return null;
|
||||
var file = await baseFolder.GetFileAsync(path);
|
||||
if (file != null)
|
||||
{
|
||||
|
||||
@@ -3,6 +3,7 @@ using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Platform.Storage;
|
||||
using Jint;
|
||||
using Progrart.Commands;
|
||||
using Progrart.Controls.TabSystem;
|
||||
using Progrart.Core;
|
||||
using System;
|
||||
@@ -21,7 +22,7 @@ public partial class Console : UserControl, ITabPage, IEditorPage
|
||||
{
|
||||
Output.Text += $"{obj}\n";
|
||||
}));
|
||||
engine.SetValue("getWDName", new Func<string>(() =>
|
||||
engine.SetValue("getWD", new Func<string>(() =>
|
||||
{
|
||||
return workdirectory?.Name ?? "<null>";
|
||||
}));
|
||||
@@ -30,6 +31,7 @@ public partial class Console : UserControl, ITabPage, IEditorPage
|
||||
if (workdirectory != null)
|
||||
{
|
||||
Task.Run(async () => workdirectory = await workdirectory.GetFolderAsync(str));
|
||||
Output.Text += $"Executed\n";
|
||||
}
|
||||
}));
|
||||
engine.SetValue("mkdir", new Action<string>((str) =>
|
||||
@@ -37,14 +39,28 @@ public partial class Console : UserControl, ITabPage, IEditorPage
|
||||
if (workdirectory != null)
|
||||
{
|
||||
Task.Run(async () => await workdirectory.CreateFolderAsync(str));
|
||||
Output.Text += $"Executed\n";
|
||||
}
|
||||
}));
|
||||
CommandBox.KeyBindings.Add(new Avalonia.Input.KeyBinding()
|
||||
{
|
||||
Gesture = new Avalonia.Input.KeyGesture(Avalonia.Input.Key.Enter),
|
||||
Command = new GenericCommand()
|
||||
{
|
||||
Checker = (_) => true,
|
||||
onExecute = (_) => executeCmd()
|
||||
}
|
||||
});
|
||||
ExecuteBtn.Click += (_, _) =>
|
||||
{
|
||||
engine.Execute(CommandBox.Text ?? "");
|
||||
executeCmd();
|
||||
};
|
||||
}
|
||||
void executeCmd()
|
||||
{
|
||||
Output.Text += $"{engine.Evaluate(CommandBox.Text ?? "")}\n";
|
||||
|
||||
}
|
||||
public void BindButton(TabButton button)
|
||||
{
|
||||
button.Title = "Console";
|
||||
|
||||
@@ -7,6 +7,7 @@ using Avalonia.Threading;
|
||||
using Progrart.Controls.TabSystem;
|
||||
using Progrart.Core;
|
||||
using Progrart.Core.JSExecution;
|
||||
using Progrart.Core.Storage;
|
||||
using Progrart.Pages;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
@@ -40,7 +41,8 @@ public partial class ProgrartEditorPage : UserControl, ITabPage, IEditorPage
|
||||
|
||||
public void Execute(ExecuteArguments? args = null)
|
||||
{
|
||||
using ProgrartExecutor executor = new ProgrartExecutor();
|
||||
AvaloniaStorageProvider provider = new AvaloniaStorageProvider(App.CurrentOpenFolder);
|
||||
using ProgrartExecutor executor = new ProgrartExecutor(provider);
|
||||
try
|
||||
{
|
||||
int Scale = 1024;
|
||||
@@ -82,7 +84,6 @@ public partial class ProgrartEditorPage : UserControl, ITabPage, IEditorPage
|
||||
{
|
||||
return (this.file?.Path == file.Path);
|
||||
}
|
||||
|
||||
public void LoadDocument(IStorageFile file)
|
||||
{
|
||||
this.file = file;
|
||||
|
||||
@@ -99,8 +99,8 @@ public partial class MainView : UserControl
|
||||
if (folders.Count >= 1)
|
||||
{
|
||||
FileContainer.Children.Clear();
|
||||
var folderPath = folders[0].TryGetLocalPath();
|
||||
var folder = folders[0];
|
||||
App.CurrentOpenFolder = folder;
|
||||
FileContainer.Children.Add(new FileItem(folder));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user