← All source files

V11 / ca6f28eff240

ProjectOperations.cs

Lilith/Core/Projects/ProjectOperations.cs

Changed in this version
CS
namespace Lilith.Core.Projects;

internal static class ProjectOperations
{
    internal static readonly string[] Names = ["project_check_architecture_operation", "project_prepare_operation", "project_store_operation", "project_assemble_operation", "project_inspect_operation", "project_validate_operation", "project_review_operation", "project_publish_operation"];
    internal static async Task<string> ExecuteAsync(ProjectGeneration projects, string name, IReadOnlyDictionary<string, string> inputs, CancellationToken ct)
    {
        string Get(string key) => inputs.GetValueOrDefault(key, "");
        ct.ThrowIfCancellationRequested();
        if (name == "project_check_architecture_operation")
        {
            try { return projects.Prepare(Get("name"), Get("kind"), Get("prd"), Get("architecture"), validateOnly: true); }
            catch (Exception ex) when (ex is ArgumentException or InvalidDataException or System.Text.Json.JsonException)
            { return ProjectJson.Serialize(new { valid = false, diagnostics = ex.Message }); }
        }
        if (name == "project_validate_operation")
        {
            try { return await projects.ValidateAsync(Get("generation_id"), Get("bundle"), ct); }
            catch (Exception ex) when (ex is InvalidDataException or System.Text.Json.JsonException or System.Xml.XmlException)
            {
                projects.RecordDiagnostic(ex.Message);
                return ProjectJson.Serialize(new { passed = false, report = new { errors = new[] { ex.Message }, checks = Array.Empty<string>(), buildOutput = "" } });
            }
        }
        return name switch
        {
            "project_prepare_operation" => projects.Prepare(Get("name"), Get("kind"), Get("prd"), Get("architecture")),
            "project_store_operation" => projects.Store(Get("generation_id"), Get("task_id"), Get("content")),
            "project_assemble_operation" => projects.Assemble(Get("generation_id")),
            "project_inspect_operation" => projects.Inspect(Get("generation_id")),
            "project_review_operation" => projects.Review(Get("generation_id"), Get("report")),
            "project_publish_operation" => ProjectJson.Serialize(await projects.PublishAsync(Get("generation_id"), ct)),
            _ => throw new ArgumentException("Unknown project operation.")
        };
    }
}