-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGen.cs
More file actions
196 lines (177 loc) · 6.38 KB
/
CodeGen.cs
File metadata and controls
196 lines (177 loc) · 6.38 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Path: Services/CodeGen.cs
using System.Text.Json;
using System.Text.RegularExpressions;
using DevMiniOS.Models;
namespace DevMiniOS.Services;
/// <summary>
/// Provides code generation services based on static templates.
/// </summary>
public static class CodeGen
{
/// <summary>
/// Generates code based on the provided command and language.
/// </summary>
/// <param name="command">The command to generate code for.</param>
/// <param name="language">The target programming language (e.g., "csharp", "cpp", "javascript").</param>
/// <returns>A list of generated files, each with a path and content.</returns>
/// <example>
/// <code>
/// var command = new Command { Op = "make_controller", Name = "Foo", Parameters = "{}" };
/// var generatedFiles = CodeGen.GenerateCode(command, "csharp");
/// // generatedFiles will contain a list of GeneratedFile objects,
/// // one for the controller file (e.g., "FooController.cs").
/// </code>
/// </example>
public static List<GeneratedFile> GenerateCode(Command command, string language)
{
return command.Op switch
{
"make_controller" => GenerateController(command, language),
"make_model" => GenerateModel(command, language),
"wire_di" => GenerateDiRegistration(command, language),
"add_tests" => GenerateTests(command, language),
_ => new List<GeneratedFile>() // Return empty list for unknown operations.
};
}
private static List<GeneratedFile> GenerateController(Command command, string language)
{
string controllerName = command.Name;
string modelName = controllerName.Replace("Controller", ""); //Naively remove controller from name
if (language == "csharp")
{
string controllerContent = $@"
using Microsoft.AspNetCore.Mvc;
namespace MyProject.Controllers
{{
[ApiController]
[Route(""[controller]"")]
public class {controllerName} : ControllerBase
{{
[HttpGet]
public IActionResult Get()
{{
return Ok(""Hello from {controllerName}"");
}}
}}
}}";
return new List<GeneratedFile> { new GeneratedFile { Path = $"Controllers/{controllerName}.cs", Contents = controllerContent } };
}
else
{
return new List<GeneratedFile>(); // Unsupported language.
}
}
private static List<GeneratedFile> GenerateModel(Command command, string language)
{
if (language == "csharp")
{
string modelName = "ExampleModel"; // Default name
string fields = "";
if (command.Parameters != null)
{
try
{
var parameters = JsonSerializer.Deserialize<Dictionary<string, string>>(command.Parameters);
if (parameters != null && parameters.ContainsKey("slots"))
{
fields = parameters["slots"];
string[] fieldArray = fields.Split(',');
fields = "";
foreach (string field in fieldArray)
{
string cleanedField = field.Trim();
string[] parts = cleanedField.Split(':');
if (parts.Length == 2)
{
string name = parts[0].Trim();
string type = parts[1].Trim();
fields += $" public {type} {name} {{ get; set; }}\n";
}
}
modelName = "GeneratedModel";
}
}
catch (JsonException)
{
// Handle JSON parsing errors
}
}
string modelContent = $@"
namespace MyProject.Models
{{
public class {modelName}
{{
{fields}
}}
}}";
return new List<GeneratedFile> { new GeneratedFile { Path = $"Models/{modelName}.cs", Contents = modelContent } };
}
else
{
return new List<GeneratedFile>(); // Unsupported language.
}
}
private static List<GeneratedFile> GenerateDiRegistration(Command command, string language)
{
if (language == "csharp")
{
string className = "Foo"; // Replace with parsing logic if needed from command.Name or Parameters
string registrationContent = $@"
using Microsoft.Extensions.DependencyInjection;
namespace MyProject.Configuration
{{
public static class DependencyInjectionConfig
{{
public static void RegisterDependencies(this IServiceCollection services)
{{
services.AddScoped<IFoo, {className}>(); // Replace IFoo with interface if available.
}}
}}
}}";
return new List<GeneratedFile> { new GeneratedFile { Path = $"Configuration/DependencyInjectionConfig.cs", Contents = registrationContent } };
}
else
{
return new List<GeneratedFile>(); // Unsupported language.
}
}
private static List<GeneratedFile> GenerateTests(Command command, string language)
{
if (language == "csharp")
{
string testClassName = "ExampleTests"; //Replace with parsing logic if needed.
string testContent = $@"
using Xunit;
namespace MyProject.Tests
{{
public class {testClassName}
{{
[Fact]
public void Test1()
{{
Assert.True(true);
}}
}}
}}";
return new List<GeneratedFile> { new GeneratedFile { Path = $"Tests/{testClassName}.cs", Contents = testContent } };
}
else
{
return new List<GeneratedFile>(); // Unsupported language.
}
}
}
/// <summary>
/// Represents a generated file with its path and contents.
/// </summary>
public class GeneratedFile
{
/// <summary>
/// The path of the generated file.
/// </summary>
public string Path { get; set; } = "";
/// <summary>
/// The contents of the generated file.
/// </summary>
public string Contents { get; set; } = "";
}