From 91431fd996d6d40176b5f700776c5b45052b62db Mon Sep 17 00:00:00 2001 From: mdnapo Date: Tue, 23 Jul 2024 22:04:51 +0200 Subject: [PATCH] Clean up --- MycroForge.CLI/CodeGen/CrudRouterGenerator.cs | 6 ++-- MycroForge.CLI/CodeGen/EntityLinker.cs | 4 +-- .../CodeGen/RequestClassGenerator.cs | 4 +-- .../Attributes/RequiresPluginAttribute.cs | 12 +++++-- MycroForge.CLI/Commands/FullyQualifiedName.cs | 10 +++--- .../MycroForge.Api.Generate.Router.cs | 12 +++---- .../Commands/MycroForge.Db.Generate.Entity.cs | 4 +-- .../MycroForge.Db.Generate.Migration.cs | 2 -- .../Commands/MycroForge.Generate.Service.cs | 4 +-- MycroForge.CLI/My.Plugin/HelloWorldCommand.cs | 36 +++++++++++++++++++ .../My.Plugin/HelloWorldCommandPlugin.cs | 15 ++++++++ MycroForge.CLI/My.Plugin/My.Plugin.csproj | 17 +++++++++ 12 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 MycroForge.CLI/My.Plugin/HelloWorldCommand.cs create mode 100644 MycroForge.CLI/My.Plugin/HelloWorldCommandPlugin.cs create mode 100644 MycroForge.CLI/My.Plugin/My.Plugin.csproj diff --git a/MycroForge.CLI/CodeGen/CrudRouterGenerator.cs b/MycroForge.CLI/CodeGen/CrudRouterGenerator.cs index 975a431..4333182 100644 --- a/MycroForge.CLI/CodeGen/CrudRouterGenerator.cs +++ b/MycroForge.CLI/CodeGen/CrudRouterGenerator.cs @@ -97,14 +97,14 @@ public class CrudRouterGenerator var entityRoutePrefix = fqn.PascalizedName.Kebaberize().Pluralize().ToLower(); var serviceFilePath = Path.Join( - Features.Api.FeatureName, "services", fqn.FolderPath, $"{fqn.SnakeCasedName}_service" + Features.Api.FeatureName, "services", fqn.Namespace, $"{fqn.SnakeCasedName}_service" ); var serviceImportPath = serviceFilePath.SlashesToDots(); - var routerFolderPath = Path.Join(Features.Api.FeatureName, "routers", fqn.FolderPath); + var routerFolderPath = Path.Join(Features.Api.FeatureName, "routers", fqn.Namespace); var routerFilePath = Path.Join(routerFolderPath, $"{fqn.SnakeCasedName}"); var routerImportPath = routerFolderPath.SlashesToDots(); - var requestsFolderPath = Path.Join(Features.Api.FeatureName, "requests", fqn.FolderPath); + var requestsFolderPath = Path.Join(Features.Api.FeatureName, "requests", fqn.Namespace); var createRequestImportPath = Path.Join(requestsFolderPath, $"Create{fqn.PascalizedName}Request") .SlashesToDots() diff --git a/MycroForge.CLI/CodeGen/EntityLinker.cs b/MycroForge.CLI/CodeGen/EntityLinker.cs index 3b2f505..a23d28b 100644 --- a/MycroForge.CLI/CodeGen/EntityLinker.cs +++ b/MycroForge.CLI/CodeGen/EntityLinker.cs @@ -147,8 +147,8 @@ public partial class EntityLinker var fqn = new FullyQualifiedName(name); var path = $"{Features.Db.FeatureName}/entities"; - if (fqn.HasPath) - path = Path.Combine(path, fqn.FolderPath); + if (fqn.HasNamespace) + path = Path.Combine(path, fqn.Namespace); path = Path.Combine(path, $"{fqn.SnakeCasedName}.py"); var entity = new EntityModel(fqn.PascalizedName, path, await _context.ReadFile(path)); diff --git a/MycroForge.CLI/CodeGen/RequestClassGenerator.cs b/MycroForge.CLI/CodeGen/RequestClassGenerator.cs index 41e1bc7..2abb40f 100644 --- a/MycroForge.CLI/CodeGen/RequestClassGenerator.cs +++ b/MycroForge.CLI/CodeGen/RequestClassGenerator.cs @@ -51,8 +51,7 @@ public class RequestClassGenerator var requestFilePath = Path.Join( Features.Api.FeatureName, "requests", - fqn.FolderPath, - // requestsFolderPath, + fqn.Namespace, $"{type.ToString().ToLower()}_{fqn.SnakeCasedName}_request.py" ); @@ -60,7 +59,6 @@ public class RequestClassGenerator .Replace("%imports%", GetImportString(entitySource, fieldInfo, type)) .Replace("%request_type%", type.ToString().Pascalize()) .Replace("%entity_class_name%", fqn.PascalizedName) - // .Replace("%entity_class_name%", entityClassName) .Replace("%fields%", fields) ; diff --git a/MycroForge.CLI/Commands/Attributes/RequiresPluginAttribute.cs b/MycroForge.CLI/Commands/Attributes/RequiresPluginAttribute.cs index 3efa19f..eaf4e1d 100644 --- a/MycroForge.CLI/Commands/Attributes/RequiresPluginAttribute.cs +++ b/MycroForge.CLI/Commands/Attributes/RequiresPluginAttribute.cs @@ -7,15 +7,21 @@ public class RequiresPluginAttribute : Attribute { public void RequirePluginProject(string command) { + var currentDirectoryInfo = new DirectoryInfo(Environment.CurrentDirectory); var matcher = new Matcher() .AddInclude("*.csproj") - .Execute(new DirectoryInfoWrapper(new DirectoryInfo(Environment.CurrentDirectory))); + .Execute(new DirectoryInfoWrapper(currentDirectoryInfo)); if (!matcher.HasMatches) throw new($"Command '{command}' must be run in a command plugin project."); - var csprojFileName = $"{Path.GetDirectoryName(Environment.CurrentDirectory)}.csproj"; - bool IsCsprojFile(FilePatternMatch file) => Path.GetFileNameWithoutExtension(file.Path).Equals(csprojFileName); + var csprojFileName = $"{new DirectoryInfo(Environment.CurrentDirectory).Name}.csproj"; + + bool IsCsprojFile(FilePatternMatch file) + { + return Path.GetFileName(file.Path) == csprojFileName; + } + var hasCsprojFile = matcher.Files.Any(IsCsprojFile); if (!hasCsprojFile) diff --git a/MycroForge.CLI/Commands/FullyQualifiedName.cs b/MycroForge.CLI/Commands/FullyQualifiedName.cs index 528ea4b..da4eab0 100644 --- a/MycroForge.CLI/Commands/FullyQualifiedName.cs +++ b/MycroForge.CLI/Commands/FullyQualifiedName.cs @@ -5,16 +5,16 @@ namespace MycroForge.CLI.Commands; public class FullyQualifiedName { - public string FolderPath { get; } + public string Namespace { get; } public string PascalizedName { get; } public string SnakeCasedName { get; } public string FilePath => - string.IsNullOrEmpty(FolderPath.Trim()) + string.IsNullOrEmpty(Namespace.Trim()) ? SnakeCasedName - : Path.Join(FolderPath, SnakeCasedName); + : Path.Join(Namespace, SnakeCasedName); - public bool HasPath => FolderPath.Length > 0; + public bool HasNamespace => Namespace.Length > 0; public FullyQualifiedName(string name) @@ -27,7 +27,7 @@ public class FullyQualifiedName name = fullName[1]; } - FolderPath = path; + Namespace = path; PascalizedName = name.Pascalize(); SnakeCasedName = SnakeCase(name); } diff --git a/MycroForge.CLI/Commands/MycroForge.Api.Generate.Router.cs b/MycroForge.CLI/Commands/MycroForge.Api.Generate.Router.cs index 14a4db1..659038f 100644 --- a/MycroForge.CLI/Commands/MycroForge.Api.Generate.Router.cs +++ b/MycroForge.CLI/Commands/MycroForge.Api.Generate.Router.cs @@ -43,19 +43,17 @@ public partial class MycroForge private async Task ExecuteAsync(string name) { var fqn = new FullyQualifiedName(name); - var folderPath = $"{Features.Api.FeatureName}/routers"; + var routersFolderPath = $"{Features.Api.FeatureName}/routers"; - _context.AssertDirectoryExists(folderPath); - - if (fqn.HasPath) - folderPath = Path.Combine(folderPath, fqn.FolderPath); + if (fqn.HasNamespace) + routersFolderPath = Path.Combine(routersFolderPath, fqn.Namespace); var fileName = $"{fqn.SnakeCasedName}.py"; - var filePath = Path.Combine(folderPath, fileName); + var filePath = Path.Combine(routersFolderPath, fileName); await _context.CreateFile(filePath, Template); - var moduleImportPath = folderPath + var moduleImportPath = routersFolderPath .Replace('\\', '.') .Replace('/', '.'); diff --git a/MycroForge.CLI/Commands/MycroForge.Db.Generate.Entity.cs b/MycroForge.CLI/Commands/MycroForge.Db.Generate.Entity.cs index 9fffbf7..fa08287 100644 --- a/MycroForge.CLI/Commands/MycroForge.Db.Generate.Entity.cs +++ b/MycroForge.CLI/Commands/MycroForge.Db.Generate.Entity.cs @@ -73,8 +73,8 @@ public partial class MycroForge _context.AssertDirectoryExists(Features.Db.FeatureName); - if (fqn.HasPath) - folderPath = Path.Combine(folderPath, fqn.FolderPath); + if (fqn.HasNamespace) + folderPath = Path.Combine(folderPath, fqn.Namespace); var _columns = GetColumnDefinitions(columns.ToArray()); var typeImports = string.Join(", ", _columns.Select(c => c.OrmType.Split('(').First()).Distinct()); diff --git a/MycroForge.CLI/Commands/MycroForge.Db.Generate.Migration.cs b/MycroForge.CLI/Commands/MycroForge.Db.Generate.Migration.cs index a559912..5ac790f 100644 --- a/MycroForge.CLI/Commands/MycroForge.Db.Generate.Migration.cs +++ b/MycroForge.CLI/Commands/MycroForge.Db.Generate.Migration.cs @@ -27,8 +27,6 @@ public partial class MycroForge private async Task ExecuteAsync(string name) { - _context.AssertDirectoryExists($"{Features.Db.FeatureName}/versions"); - await _context.Bash( "source .venv/bin/activate", $"alembic revision --autogenerate -m \"{name}\" --rev-id $(date -u +\"%Y%m%d%H%M%S\")" diff --git a/MycroForge.CLI/Commands/MycroForge.Generate.Service.cs b/MycroForge.CLI/Commands/MycroForge.Generate.Service.cs index a8b1a5d..0d4277b 100644 --- a/MycroForge.CLI/Commands/MycroForge.Generate.Service.cs +++ b/MycroForge.CLI/Commands/MycroForge.Generate.Service.cs @@ -58,8 +58,8 @@ public partial class MycroForge var fqn = new FullyQualifiedName(name); var folderPath = string.Empty; - if (fqn.HasPath) - folderPath = Path.Combine(folderPath, fqn.FolderPath); + if (fqn.HasNamespace) + folderPath = Path.Combine(folderPath, fqn.Namespace); var filePath = Path.Combine(folderPath, $"{fqn.SnakeCasedName}.py"); var template = withSession ? WithSessionTemplate : DefaultTemplate; diff --git a/MycroForge.CLI/My.Plugin/HelloWorldCommand.cs b/MycroForge.CLI/My.Plugin/HelloWorldCommand.cs new file mode 100644 index 0000000..281b5dc --- /dev/null +++ b/MycroForge.CLI/My.Plugin/HelloWorldCommand.cs @@ -0,0 +1,36 @@ +using System.CommandLine; +using MycroForge.Core; +using MycroForge.Core.Contract; +using RootCommand = MycroForge.Core.RootCommand; + +namespace My.Plugin; + +public class HelloWorldCommand : Command, ISubCommandOf +{ + private readonly Argument NameArgument = + new(name: "name", description: "The name of the person to greet"); + + private readonly Option AllCapsOption = + new(aliases: ["-a", "--all-caps"], description: "Print the name in all caps"); + + private readonly ProjectContext _context; + + public HelloWorldCommand(ProjectContext context) : + base("hello", "An example command generated by dotnet new using the m4gp template") + { + _context = context; + AddArgument(NameArgument); + AddOption(AllCapsOption); + this.SetHandler(ExecuteAsync, NameArgument, AllCapsOption); + } + + private async Task ExecuteAsync(string name, bool allCaps) + { + name = allCaps ? name.ToUpper() : name; + + await _context.CreateFile("hello_world.txt", + $"Hello {name}!", + "This file was generated by your custom command!" + ); + } +} \ No newline at end of file diff --git a/MycroForge.CLI/My.Plugin/HelloWorldCommandPlugin.cs b/MycroForge.CLI/My.Plugin/HelloWorldCommandPlugin.cs new file mode 100644 index 0000000..adbecfa --- /dev/null +++ b/MycroForge.CLI/My.Plugin/HelloWorldCommandPlugin.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.DependencyInjection; +using MycroForge.Core.Contract; +using RootCommand = MycroForge.Core.RootCommand; + +namespace My.Plugin; + +public class HelloWorldCommandPlugin : ICommandPlugin +{ + public string Name => "My.Plugin"; + + public void RegisterServices(IServiceCollection services) + { + services.AddScoped, HelloWorldCommand>(); + } +} \ No newline at end of file diff --git a/MycroForge.CLI/My.Plugin/My.Plugin.csproj b/MycroForge.CLI/My.Plugin/My.Plugin.csproj new file mode 100644 index 0000000..4943c9c --- /dev/null +++ b/MycroForge.CLI/My.Plugin/My.Plugin.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + +