Fixed sevice generator and orm feature

This commit is contained in:
Donné Napo 2024-05-17 07:06:06 +02:00
parent 6b946faf93
commit 15bf94e195
2 changed files with 25 additions and 12 deletions

View File

@ -13,7 +13,10 @@ public partial class MycroForge
private static readonly Argument<string> NameArgument =
new(name: "name", description: "The name of the service");
private static readonly Option<bool> WithSession =
private static readonly Option<string> PathOption =
new(name: "--path", description: "The folder path of the service") { IsRequired = true };
private static readonly Option<bool> WithSessionOption =
new(name: "--with-session", description: "Create a service that uses database sessions");
@ -28,14 +31,15 @@ public partial class MycroForge
private static readonly string[] WithSessionTemplate =
[
"from orm.engine.async_session import async_session",
"# from orm.entities.user import User",
"from sqlalchemy import select",
"# from orm.entities.some_entity import SomeEntity",
"",
"class %class_name%:",
"class %class_name%Service:",
"",
"\tasync def do_stuff(self, stuff: str) -> str:",
"\t\tasync with async_session() as session():",
"\t\t\t# stmt = select(User).where(User.firstname == \"John\")",
"\t\t\t# results = await session.scalars(stmt).all()",
"\t\tasync with async_session() as session:",
"\t\t\t# stmt = select(User).where(SomeEntity.value == \"some_value\")",
"\t\t\t# results = (await session.scalars(stmt)).all()",
"\t\t\t# print(len(results))",
"\t\t\tpass",
"\t\treturn f\"Hey, I'm doing stuff!\""
@ -47,19 +51,28 @@ public partial class MycroForge
{
_context = context;
AddAlias("s");
AddOption(WithSession);
this.SetHandler(ExecuteAsync, NameArgument, WithSession);
AddArgument(NameArgument);
AddOption(PathOption);
AddOption(WithSessionOption);
this.SetHandler(ExecuteAsync, NameArgument, PathOption, WithSessionOption);
}
private async Task ExecuteAsync(string name, bool withSession)
private async Task ExecuteAsync(string name, string? path, bool withSession)
{
_context.AssertDirectoryExists("services");
var folderPath = "services";
if (!string.IsNullOrEmpty(path) && !path.Equals("."))
{
folderPath = Path.Join(_context.RootDirectory, path);
Directory.CreateDirectory(folderPath);
}
var className = Path.GetFileName(name).Pascalize();
var code = string.Join('\n', withSession ? WithSessionTemplate : DefaultTemplate)
.Replace("%class_name%", className);
await _context.CreateFile($"services/{name.Underscore().ToLower()}.py", code);
var filePath = Path.Join(folderPath, $"{name.Underscore().ToLower()}_service.py");
await _context.CreateFile(filePath, code);
}
}
}

View File

@ -22,7 +22,7 @@ public sealed class Orm : IFeature
"",
"async_engine: AsyncEngine = create_async_engine(OrmSettings.get_connectionstring())",
"",
"def async_session():",
"def async_session() -> AsyncSession:",
"\treturn AsyncSession(async_engine, expire_on_commit=False)"
];