-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputService.cs
More file actions
83 lines (76 loc) · 2.81 KB
/
OutputService.cs
File metadata and controls
83 lines (76 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Path: Services/OutputService.cs
using System.Text.Json;
using DevMiniOS.Models;
namespace DevMiniOS.Services;
/// <summary>
/// Provides a service to generate a human-readable DSL summary and the generated files from a Plan.
/// </summary>
public class OutputService
{
/// <summary>
/// Generates a human-readable DSL summary and the generated files from a Plan.
/// </summary>
/// <param name="plan">The Plan to process.</param>
/// <param name="language">The target programming language.</param>
/// <returns>An OutputResult object containing the DSL summary and generated files.</returns>
/// <example>
/// <code>
/// var plan = new Plan { Commands = new List<Command> { new Command { Op = "make_controller", Name = "FooController", Parameters = "{}" } } };
/// var outputResult = outputService.GenerateOutput(plan, "csharp");
/// // outputResult.DslSummary will contain a human-readable summary of the Plan.
/// // outputResult.GeneratedFiles will contain the generated files.
/// </code>
/// </example>
public OutputResult GenerateOutput(Plan plan, string language)
{
string dslSummary = GenerateDslSummary(plan);
List<GeneratedFile> generatedFiles = GenerateFiles(plan, language);
return new OutputResult { DslSummary = dslSummary, GeneratedFiles = generatedFiles };
}
private string GenerateDslSummary(Plan plan)
{
string summary = "Plan:\n";
if (plan.Commands != null)
{
foreach (var command in plan.Commands)
{
summary += $" Op: {command.Op}\n";
summary += $" Name: {command.Name}\n";
summary += $" Parameters: {command.Parameters}\n";
summary += "---\n";
}
}
else
{
summary += "No commands in plan.\n";
}
return summary;
}
private List<GeneratedFile> GenerateFiles(Plan plan, string language)
{
List<GeneratedFile> allGeneratedFiles = new();
if (plan.Commands != null)
{
foreach (var command in plan.Commands)
{
List<GeneratedFile> generatedFiles = CodeGen.GenerateCode(command, language);
allGeneratedFiles.AddRange(generatedFiles);
}
}
return allGeneratedFiles;
}
}
/// <summary>
/// Represents the output result, containing the DSL summary and generated files.
/// </summary>
public class OutputResult
{
/// <summary>
/// The human-readable DSL summary.
/// </summary>
public string DslSummary { get; set; } = "";
/// <summary>
/// The list of generated files.
/// </summary>
public List<GeneratedFile> GeneratedFiles { get; set; } = new();
}