This commit is contained in:
mdnapo 2024-07-23 22:04:51 +02:00
parent 5ccb40bb44
commit 91431fd996
12 changed files with 97 additions and 29 deletions

View File

@ -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()

View File

@ -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));

View File

@ -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)
;

View File

@ -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)

View File

@ -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);
}

View File

@ -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('/', '.');

View File

@ -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());

View File

@ -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\")"

View File

@ -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;

View File

@ -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<RootCommand>
{
private readonly Argument<string> NameArgument =
new(name: "name", description: "The name of the person to greet");
private readonly Option<bool> 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!"
);
}
}

View File

@ -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<ISubCommandOf<RootCommand>, HelloWorldCommand>();
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Content Include=".template.config\template.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MycroForge.Core" Version="1.0.0" />
</ItemGroup>
</Project>