Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmet-cetinkaya committed Jan 5, 2024
2 parents 9afe69c + d2f3b1a commit e6f434d
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,5 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ string[] codeLines
fileContent.InsertRange(
methodEndIndex,
collection: codeLines.Select(line => new string(' ', minimumSpaceCountInMethod) + line)
.Where(line => !fileContent.Contains(line))
);
await System.IO.File.WriteAllLinesAsync(filePath, contents: fileContent.ToArray());
}
Expand Down
3 changes: 3 additions & 0 deletions src/corePackages/Core.CodeGen/Core.CodeGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
<Folder Include="File\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core.CrossCuttingConcerns\Core.CrossCuttingConcerns.csproj" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions src/corePackages/Core.CodeGen/File/DirectoryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Core.CrossCuttingConcerns.Helpers;
using System.Reflection;

namespace Core.CodeGen.File;

Expand All @@ -8,7 +9,7 @@ public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().Location;
string codeBase = PlatformHelper.GetDirectoryHeader() + Assembly.GetExecutingAssembly().Location;
UriBuilder uri = new(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path)!;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
namespace Core.CrossCuttingConcerns.Helpers
{
public static class PlatformHelper
{
public static string SecuredPathJoin(params string[] pathItems)
{
string path;
_ = Environment.OSVersion.Platform switch
{
PlatformID.Unix => path = String.Join("/", pathItems),
PlatformID.MacOSX => path = String.Join("/", pathItems),
_ => path = String.Join("\\", pathItems),
};

return path;
}

public static string GetDirectoryHeader()
{
string file;
_ = Environment.OSVersion.Platform switch
{
PlatformID.Unix => file = "file://",
PlatformID.MacOSX => file = "file://",
_ => file = "",
};

return file;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Core.CodeGen.Code.CSharp;
using Core.CodeGen.File;
using Core.CodeGen.TemplateEngine;
using Core.CrossCuttingConcerns.Helpers;
using Domain.Constants;
using Domain.ValueObjects;
using MediatR;
Expand Down Expand Up @@ -38,7 +39,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken
)
{
await _businessRules.FileShouldNotBeExists(
@$"{request.ProjectPath}\Application\features\{request.FeatureName.ToPascalCase()}\Commands\{request.CommandName}\{request.CommandName}Command.cs"
PlatformHelper.SecuredPathJoin(request.ProjectPath, "Application", "features", request.FeatureName.ToPascalCase(), "Commands", request.CommandName, $"{request.CommandName}Command.cs")
);

GeneratedCommandResponse response = new();
Expand Down Expand Up @@ -82,11 +83,10 @@ private async Task<ICollection<string>> generateApplicationCodes(
CommandTemplateData commandTemplateData
)
{
string templateDir =
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Command}\Folders\Application";
string templateDir = PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Command, "Folders", "Application");
return await generateFolderCodes(
templateDir,
outputDir: $@"{projectPath}\Application",
outputDir: PlatformHelper.SecuredPathJoin(projectPath, "Application"),
commandTemplateData
);
}
Expand All @@ -97,10 +97,9 @@ private async Task<ICollection<string>> injectOperationClaims(
CommandTemplateData commandTemplateData
)
{
string featureOperationClaimFilePath =
@$"{projectPath}\Application\Features\{featureName}\Constants\{featureName}OperationClaims.cs";
string featureOperationClaimFilePath = PlatformHelper.SecuredPathJoin(projectPath, "Application", "Features", featureName, "Constants", $"{featureName}OperationClaims.cs");
string[] commandOperationClaimPropertyTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Command}\Lines\CommandOperationClaimProperty.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Command, "Lines", "CommandOperationClaimProperty.cs.sbn")
);
string[] commandOperationClaimPropertyCodeLines = await Task.WhenAll(
commandOperationClaimPropertyTemplateCodeLines.Select(
Expand All @@ -112,13 +111,13 @@ await CSharpCodeInjector.AddCodeLinesAsPropertyAsync(
commandOperationClaimPropertyCodeLines
);

string operationClaimsEntityConfigurationFilePath =
@$"{projectPath}\Persistence\EntityConfigurations\OperationClaimConfiguration.cs";
if(!File.Exists(operationClaimsEntityConfigurationFilePath))
string operationClaimsEntityConfigurationFilePath = PlatformHelper.SecuredPathJoin(projectPath, "Persistence", "EntityConfigurations", "OperationClaimConfiguration.cs");

if (!File.Exists(operationClaimsEntityConfigurationFilePath))
return new[] { featureOperationClaimFilePath };

string[] commandOperationClaimSeedTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Command}\Lines\CommandOperationClaimSeed.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Command, "Lines", "CommandOperationClaimSeed.cs.sbn")
);
string[] commandOperationClaimSeedCodeLines = await Task.WhenAll(
commandOperationClaimSeedTemplateCodeLines.Select(
Expand Down Expand Up @@ -172,10 +171,10 @@ private async Task<ICollection<string>> injectWebApiEndpoint(
CommandTemplateData commandTemplateData
)
{
string controllerFilePath =
@$"{projectPath}\WebAPI\Controllers\{featureName}Controller.cs";
string controllerFilePath = PlatformHelper.SecuredPathJoin(projectPath, "WebAPI", "Controllers", $"{featureName}Controller.cs");

string[] controllerEndPointMethodTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Command}\Lines\ControllerEndPointMethod.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Command, "Lines", "ControllerEndPointMethod.cs.sbn")
);
string[] controllerEndPointMethodRenderedCodeLines = await Task.WhenAll(
controllerEndPointMethodTemplateCodeLines.Select(
Expand All @@ -190,7 +189,7 @@ await CSharpCodeInjector.AddMethodToClass(
);

string[] commandUsingNameSpaceTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Command}\Lines\CommandUsingNameSpaces.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Command, "Lines", "CommandUsingNameSpaces.cs.sbn")
);
string[] commandUsingNameSpaceRenderedCodeLines = await Task.WhenAll(
commandUsingNameSpaceTemplateCodeLines.Select(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Core.CodeGen.Code.CSharp;
using Core.CodeGen.File;
using Core.CodeGen.TemplateEngine;
using Core.CrossCuttingConcerns.Helpers;
using Domain.Constants;
using Domain.ValueObjects;
using MediatR;
Expand Down Expand Up @@ -102,16 +103,15 @@ private async Task<string> injectEntityToContext(
CrudTemplateData crudTemplateData
)
{
string contextFilePath =
@$"{projectPath}\Persistence\Contexts\{crudTemplateData.DbContextName}.cs";
string contextFilePath = PlatformHelper.SecuredPathJoin(projectPath, "Persistence", "Contexts", $"{crudTemplateData.DbContextName}.cs");

string[] entityNameSpaceUsingTemplate = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Lines\EntityNameSpaceUsing.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Lines", "EntityNameSpaceUsing.cs.sbn")
);
await CSharpCodeInjector.AddUsingToFile(contextFilePath, entityNameSpaceUsingTemplate);

string dbSetPropertyTemplateCodeLine = await File.ReadAllTextAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Lines\EntityContextProperty.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Lines", "EntityContextProperty.cs.sbn")
);
string dbSetPropertyCodeLine = await _templateEngine.RenderAsync(
dbSetPropertyTemplateCodeLine,
Expand All @@ -134,11 +134,10 @@ private async Task<ICollection<string>> generatePersistenceCodes(
CrudTemplateData crudTemplateData
)
{
string templateDir =
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Folders\Persistence";
string templateDir = PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Folders", "Persistence");
return await generateFolderCodes(
templateDir,
outputDir: $@"{projectPath}\Persistence",
outputDir: PlatformHelper.SecuredPathJoin(projectPath, "Persistence"),
crudTemplateData
);
}
Expand All @@ -148,13 +147,13 @@ CrudTemplateData crudTemplateData
CrudTemplateData crudTemplateData
)
{
string operationClaimConfigurationFilePath =
@$"{projectPath}\Persistence\EntityConfigurations\OperationClaimConfiguration.cs";
string operationClaimConfigurationFilePath = PlatformHelper.SecuredPathJoin(projectPath, "Persistence", "EntityConfigurations", "OperationClaimConfiguration.cs");

if (!File.Exists(operationClaimConfigurationFilePath))
return null;

string[] seedTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Lines\EntityFeatureOperationClaimSeeds.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Lines", "EntityFeatureOperationClaimSeeds.cs.sbn")
);

List<string> seedCodeLines = new() { string.Empty };
Expand All @@ -181,11 +180,11 @@ private async Task<ICollection<string>> generateApplicationCodes(
CrudTemplateData crudTemplateData
)
{
string templateDir =
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Folders\Application";
string templateDir = PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Folders", "Application");

return await generateFolderCodes(
templateDir,
outputDir: $@"{projectPath}\Application",
outputDir: PlatformHelper.SecuredPathJoin(projectPath, "Application"),
crudTemplateData
);
}
Expand All @@ -195,11 +194,10 @@ private async Task<ICollection<string>> generateWebApiCodes(
CrudTemplateData crudTemplateData
)
{
string templateDir =
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Folders\WebAPI";
string templateDir = PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Folders", "WebAPI");
return await generateFolderCodes(
templateDir,
outputDir: $@"{projectPath}\WebAPI",
outputDir: PlatformHelper.SecuredPathJoin(projectPath, "WebAPI"),
crudTemplateData
);
}
Expand Down Expand Up @@ -238,10 +236,10 @@ CrudTemplateData crudTemplateData
)
{
#region Persistence
string persistenceServiceRegistrationFilePath =
@$"{projectPath}\Persistence\PersistenceServiceRegistration.cs";
string persistenceServiceRegistrationFilePath = PlatformHelper.SecuredPathJoin(projectPath, "Persistence", "PersistenceServiceRegistration.cs");

string persistenceServiceRegistrationTemplateCodeLine = await File.ReadAllTextAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Lines\EntityRepositoryServiceRegistration.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Lines", "EntityRepositoryServiceRegistration.cs.sbn")
);
string persistenceServiceRegistrationRenderedCodeLine =
await _templateEngine.RenderAsync(
Expand All @@ -264,11 +262,11 @@ await CSharpCodeInjector.AddCodeLinesToMethodAsync(
#endregion

#region Application
string applicationServiceRegistrationNameSpaceUsingFilePath =
@$"{projectPath}\Application\ApplicationServiceRegistration.cs";
string applicationServiceRegistrationNameSpaceUsingFilePath = PlatformHelper.SecuredPathJoin(projectPath, "Application", "ApplicationServiceRegistration.cs");

string applicationServiceRegistrationNameSpaceUsingTemplateCodeLine =
await File.ReadAllTextAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Lines\EntityServiceRegistrationNameSpaceUsing.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Lines", "EntityServiceRegistrationNameSpaceUsing.cs.sbn")
);
string applicationServiceRegistrationNameSpaceUsingRenderedCodeLine =
await _templateEngine.RenderAsync(
Expand All @@ -280,10 +278,10 @@ await CSharpCodeInjector.AddUsingToFile(
usingLines: new[] { applicationServiceRegistrationNameSpaceUsingRenderedCodeLine }
);

string applicationServiceRegistrationFilePath =
@$"{projectPath}\Application\ApplicationServiceRegistration.cs";
string applicationServiceRegistrationFilePath = PlatformHelper.SecuredPathJoin(projectPath, "Application", "ApplicationServiceRegistration.cs");

string applicationServiceRegistrationTemplateCodeLine = await File.ReadAllTextAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Crud}\Lines\EntityServiceRegistration.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Crud, "Lines", "EntityServiceRegistration.cs.sbn")
);
string applicationServiceRegistrationRenderedCodeLine =
await _templateEngine.RenderAsync(
Expand Down
Loading

0 comments on commit e6f434d

Please sign in to comment.