56 lines
2.0 KiB
C#
56 lines
2.0 KiB
C#
using System.CommandLine;
|
|
using Humanizer;
|
|
using MicroForge.CLI.CodeGen;
|
|
using MicroForge.CLI.Commands.Interfaces;
|
|
|
|
namespace MicroForge.CLI.Commands;
|
|
|
|
public partial class MicroForge
|
|
{
|
|
public partial class Generate
|
|
{
|
|
public class Entity : Command, ISubCommandOf<Generate>
|
|
{
|
|
private static readonly string[] Template =
|
|
[
|
|
"from sqlalchemy import INTEGER, Column, String",
|
|
"from orm.entities.entity_base import EntityBase",
|
|
"",
|
|
"class %class_name%(EntityBase):",
|
|
"\t__tablename__ = \"%table_name%\"",
|
|
"\tid = Column(INTEGER, primary_key=True)",
|
|
"",
|
|
"\tdef __repr__(self) -> str:",
|
|
"\t\treturn f\"%class_name%(id={self.id!r})\""
|
|
];
|
|
|
|
private static readonly Argument<string> NameArgument =
|
|
new(name: "name", description: "The name of the orm entity");
|
|
|
|
private readonly ProjectContext _context;
|
|
|
|
public Entity(ProjectContext context) : base("entity", "Generate and orm entity")
|
|
{
|
|
_context = context;
|
|
AddAlias("e");
|
|
AddArgument(NameArgument);
|
|
this.SetHandler(ExecuteAsync, NameArgument);
|
|
}
|
|
|
|
private async Task ExecuteAsync(string name)
|
|
{
|
|
var className = name.Underscore().Pascalize();
|
|
var moduleName = name.Underscore();
|
|
var code = string.Join('\n', Template);
|
|
|
|
code = code.Replace("%class_name%", className);
|
|
code = code.Replace("%table_name%", name.ToLower().Underscore());
|
|
await _context.CreateFile($"orm/entities/{moduleName}.py", code);
|
|
|
|
var env = await _context.ReadFile("orm/env.py");
|
|
env = new OrmEnvUpdater(env, moduleName, className).Rewrite();
|
|
await _context.WriteFile("orm/env.py", env);
|
|
}
|
|
}
|
|
}
|
|
} |