From 15bf94e1952905ee12146bb4e83753876386d3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donn=C3=A9=20Napo?= Date: Fri, 17 May 2024 07:06:06 +0200 Subject: [PATCH] Fixed sevice generator and orm feature --- .../Commands/MycroForge.Generate.Service.cs | 35 +++++++++++++------ MycroForge.CLI/Features/Orm.cs | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/MycroForge.CLI/Commands/MycroForge.Generate.Service.cs b/MycroForge.CLI/Commands/MycroForge.Generate.Service.cs index 6088149..0d94938 100644 --- a/MycroForge.CLI/Commands/MycroForge.Generate.Service.cs +++ b/MycroForge.CLI/Commands/MycroForge.Generate.Service.cs @@ -13,7 +13,10 @@ public partial class MycroForge private static readonly Argument NameArgument = new(name: "name", description: "The name of the service"); - private static readonly Option WithSession = + private static readonly Option PathOption = + new(name: "--path", description: "The folder path of the service") { IsRequired = true }; + + private static readonly Option 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); } } } diff --git a/MycroForge.CLI/Features/Orm.cs b/MycroForge.CLI/Features/Orm.cs index 62c4f88..f51136e 100644 --- a/MycroForge.CLI/Features/Orm.cs +++ b/MycroForge.CLI/Features/Orm.cs @@ -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)" ];