Files
Progrart/Progrart.Core/ProjectSystem/Builder.cs
T

84 lines
2.5 KiB
C#
Raw Normal View History

2026-01-02 03:24:33 +11:00
using Newtonsoft.Json;
using Progrart.Core.JSExecution;
2026-01-04 21:14:17 +11:00
using Progrart.Core.Storage;
2026-01-02 03:24:33 +11:00
namespace Progrart.Core.ProjectSystem
{
public class Builder
{
public Project project;
2026-01-04 21:14:17 +11:00
public IStorageProvider provider;
2026-01-02 03:24:33 +11:00
public Action<int, int>? OnProgressUpdate;
public Action? OnCompleted;
2026-01-04 21:14:17 +11:00
public Builder(StreamReader reader, IStorageProvider provider)
2026-01-02 03:24:33 +11:00
{
var project = JsonConvert.DeserializeObject<Project>(reader.ReadToEnd());
if (project is null) throw new JsonSerializationException();
this.project = project;
2026-01-04 21:14:17 +11:00
this.provider = provider;
2026-01-02 03:24:33 +11:00
}
2026-01-04 21:14:17 +11:00
public Builder(Project project, IStorageProvider provider)
2026-01-02 03:24:33 +11:00
{
this.project = project;
2026-01-04 21:14:17 +11:00
this.provider = provider;
2026-01-02 03:24:33 +11:00
}
2026-01-04 21:14:17 +11:00
public async Task Execute(BuildConfiguration config, BuildItem item)
2026-01-02 03:24:33 +11:00
{
var args = project.Arguments.Clone();
args.MergeFrom(config.Arguments);
args.MergeFrom(item.Arguments);
2026-01-04 21:14:17 +11:00
ProgrartExecutor executor = new ProgrartExecutor(provider);
using var stream = await provider.TryOpenRead(item.Source);
if (stream is null)
return;
2026-01-02 03:24:33 +11:00
using var reader = new StreamReader(stream);
var img = executor.RenderImage(item.Size, reader.ReadToEnd(), args);
2026-01-16 00:36:38 +11:00
var outputFile= Path.Combine(project.OutputDir, item.Target ?? item.Source + ".png");
using var img_stream = await provider.TryOpenWrite(outputFile);
2026-01-04 21:14:17 +11:00
if (img_stream is null)
return;
2026-01-16 00:36:38 +11:00
img.DrawingCore.ToData(Path.GetExtension(outputFile)).SaveTo(img_stream);
2026-01-02 03:24:33 +11:00
img_stream.Flush();
}
public async Task Build(string targetConfig, int maxJobCount = 1)
2026-01-02 03:24:33 +11:00
{
int finalJobCount= Math.Max(maxJobCount < 0 ? Environment.ProcessorCount : maxJobCount, 1);
2026-01-02 03:24:33 +11:00
foreach (var config in project.Configurations)
{
if (config.Name == targetConfig)
{
int index = 0;
//List<Task> tasks = new List<Task>();
if (finalJobCount == 1)
2026-01-02 03:24:33 +11:00
{
foreach (var item in config.Items)
2026-01-13 03:33:49 +11:00
{
await Execute(config, item);
index++;
OnProgressUpdate?.Invoke(config.Items.Count, index);
}
}
else
{
var options = new ParallelOptions
2026-01-13 03:33:49 +11:00
{
MaxDegreeOfParallelism = finalJobCount
};
2026-01-13 03:33:49 +11:00
await Parallel.ForEachAsync(config.Items, options, async (item, token) =>
{
await Execute(config, item);
2026-01-13 03:33:49 +11:00
int currentCount = Interlocked.Increment(ref index);
OnProgressUpdate?.Invoke(config.Items.Count, currentCount);
});
2026-01-02 03:24:33 +11:00
}
OnCompleted?.Invoke();
return;
}
}
2026-01-13 03:33:49 +11:00
throw new Exception($"Configuration \"{targetConfig}\" not found!");
2026-01-02 03:24:33 +11:00
}
}
}