Skip to content

Commit

Permalink
Cross platform directory path support
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammet-klc committed Aug 17, 2023
1 parent b3cf0d5 commit aa5c646
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 79 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/corePackages/.DS_Store
Binary file not shown.
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.GetDirectorHeader() + Assembly.GetExecutingAssembly().Location;
UriBuilder uri = new(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path)!;
Expand Down
Binary file not shown.
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 GetDirectorHeader()
{
string file;
_ = Environment.OSVersion.Platform switch
{
PlatformID.Unix => file = "file://",
PlatformID.MacOSX => file = "file://",
_ => file = "",
};

return file;
}
}
}

Binary file added src/nArchGen/.DS_Store
Binary file not shown.
Binary file added src/nArchGen/Application/.DS_Store
Binary file not shown.
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
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 @@ -37,8 +38,8 @@ public async IAsyncEnumerable<GeneratedQueryResponse> Handle(
[EnumeratorCancellation] CancellationToken cancellationToken
)
{
await _businessRules.FileShouldNotBeExists(
@$"{request.ProjectPath}\Application\features\{request.FeatureName.ToPascalCase()}\Queries\{request.QueryName}\{request.QueryName}Query.cs"
await _businessRules.FileShouldNotBeExists(
PlatformHelper.SecuredPathJoin(request.ProjectPath, "Application", "features", request.FeatureName.ToPascalCase(), "Queries", request.QueryName, $"{request.QueryName}Query.cs")
);

GeneratedQueryResponse response = new();
Expand Down Expand Up @@ -82,11 +83,10 @@ private async Task<ICollection<string>> generateApplicationCodes(
QueryTemplateData QueryTemplateData
)
{
string templateDir =
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Query}\Folders\Application";
string templateDir = PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Query, "Folders", "Application");
return await generateFolderCodes(
templateDir,
outputDir: $@"{projectPath}\Application",
outputDir: PlatformHelper.SecuredPathJoin(projectPath, "Application"),
QueryTemplateData
);
}
Expand All @@ -97,10 +97,10 @@ private async Task<ICollection<string>> injectOperationClaims(
QueryTemplateData QueryTemplateData
)
{
string featureOperationClaimFilePath =
@$"{projectPath}\Application\Features\{featureName}\Constants\{featureName}OperationClaims.cs";
string featureOperationClaimFilePath = PlatformHelper.SecuredPathJoin(projectPath, "Application", "Features", featureName, "Constants", $"{featureName}OperationClaims.cs");

string[] queryOperationClaimPropertyTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Query}\Lines\QueryOperationClaimProperty.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Query, "Lines", "QueryOperationClaimProperty.cs.sbn")
);
string[] queryOperationClaimPropertyCodeLines = await Task.WhenAll(
queryOperationClaimPropertyTemplateCodeLines.Select(
Expand All @@ -112,13 +112,13 @@ await CSharpCodeInjector.AddCodeLinesAsPropertyAsync(
queryOperationClaimPropertyCodeLines
);

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[] queryOperationClaimSeedTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Query}\Lines\QueryOperationClaimSeed.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Query, "Lines", "QueryOperationClaimSeed.cs.sbn")
);
string[] queryOperationClaimSeedCodeLines = await Task.WhenAll(
queryOperationClaimSeedTemplateCodeLines.Select(
Expand Down Expand Up @@ -173,9 +173,9 @@ QueryTemplateData QueryTemplateData
)
{
string controllerFilePath =
@$"{projectPath}\WebAPI\Controllers\{featureName}Controller.cs";
PlatformHelper.SecuredPathJoin(projectPath, "WebAPI", "Controllers", $"{featureName}Controller.cs");
string[] controllerEndPointMethodTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Query}\Lines\ControllerEndPointMethod.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Query, "Lines", "ControllerEndPointMethod.cs.sbn")
);
string[] controllerEndPointMethodRenderedCodeLines = await Task.WhenAll(
controllerEndPointMethodTemplateCodeLines.Select(
Expand All @@ -190,7 +190,7 @@ await CSharpCodeInjector.AddMethodToClass(
);

string[] queryUsingNameSpaceTemplateCodeLines = await File.ReadAllLinesAsync(
@$"{DirectoryHelper.AssemblyDirectory}\{Templates.Paths.Query}\Lines\QueryUsingNameSpaces.cs.sbn"
PlatformHelper.SecuredPathJoin(DirectoryHelper.AssemblyDirectory, Templates.Paths.Query, "Lines", "QueryUsingNameSpaces.cs.sbn")
);
string[] queryUsingNameSpaceCodeLines = await Task.WhenAll(
queryUsingNameSpaceTemplateCodeLines.Select(
Expand Down
Loading

0 comments on commit aa5c646

Please sign in to comment.