Skip to content

Commit

Permalink
style: reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmet-cetinkaya committed Feb 7, 2024
1 parent 03c0b27 commit 979ab8b
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"csharpier": {
"version": "0.23.0",
"version": "0.27.2",
"commands": [
"dotnet-csharpier"
]
Expand Down
30 changes: 12 additions & 18 deletions src/corePackages/Core.CodeGen/Code/CSharp/CSharpCodeInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class CSharpCodeInjector
{
public static async Task AddCodeLinesToMethodAsync(string filePath, string methodName, string[] codeLines)
{
List<string> fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
var fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
string methodStartRegex =
@"((public|protected|internal|protected internal|private protected|private)\s+)?(static\s+)?(void|[a-zA-Z]+(<.*>)?)\s+\b"
+ methodName
Expand Down Expand Up @@ -71,13 +71,10 @@ public static async Task AddCodeLinesToMethodAsync(string filePath, string metho
throw new Exception($"{methodName} not found in \"{filePath}\".");

ICollection<string> methodContent = fileContent.Skip(methodStartIndex + 1).Take(methodEndIndex - 1 - methodStartIndex).ToArray();
int minimumSpaceCountInMethod;
if (methodContent.Count < 2)
minimumSpaceCountInMethod = fileContent[methodStartIndex].TakeWhile(char.IsWhiteSpace).Count() * 2;
else
minimumSpaceCountInMethod = methodContent
.Where(line => !string.IsNullOrEmpty(line))
.Min(line => line.TakeWhile(char.IsWhiteSpace).Count());
int minimumSpaceCountInMethod =
methodContent.Count < 2
? fileContent[methodStartIndex].TakeWhile(char.IsWhiteSpace).Count() * 2
: methodContent.Where(line => !string.IsNullOrEmpty(line)).Min(line => line.TakeWhile(char.IsWhiteSpace).Count());

fileContent.InsertRange(
methodEndIndex,
Expand Down Expand Up @@ -141,7 +138,7 @@ public static async Task AddCodeLinesAsPropertyAsync(string filePath, string[] c

public static async Task AddCodeLinesToRegionAsync(string filePath, IEnumerable<string> linesToAdd, string regionName)
{
List<string> fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
var fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
string regionStartRegex = @$"^\s*#region\s*{regionName}\s*";
const string regionEndRegex = @"^\s*#endregion\s*.*";

Expand Down Expand Up @@ -185,7 +182,7 @@ public static async Task AddCodeLinesToRegionAsync(string filePath, IEnumerable<

public static async Task AddUsingToFile(string filePath, IEnumerable<string> usingLines)
{
List<string> fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
var fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();

IEnumerable<string> usingLinesToAdd = usingLines.Where(usingLine => !fileContent.Contains(usingLine));

Expand All @@ -206,7 +203,7 @@ public static async Task AddUsingToFile(string filePath, IEnumerable<string> usi

public static async Task AddMethodToClass(string filePath, string className, string[] codeLines)
{
List<string> fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
var fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
Regex classStartRegex =
new(@$"((public|protected|internal|protected internal|private protected|private)\s+)?(static\s+)?\s+\b{className}");
Regex scopeBlockStartRegex = new(@"\{");
Expand Down Expand Up @@ -252,13 +249,10 @@ public static async Task AddMethodToClass(string filePath, string className, str

ICollection<string> classContent = fileContent.Skip(classStartIndex + 1).Take(classEndIndex - 1 - classStartIndex).ToArray();

int minimumSpaceCountInClass;
if (classContent.Count < 2)
minimumSpaceCountInClass = fileContent[classStartIndex].TakeWhile(char.IsWhiteSpace).Count() * 2;
else
minimumSpaceCountInClass = classContent
.Where(line => !string.IsNullOrEmpty(line))
.Min(line => line.TakeWhile(char.IsWhiteSpace).Count());
int minimumSpaceCountInClass =
classContent.Count < 2
? fileContent[classStartIndex].TakeWhile(char.IsWhiteSpace).Count() * 2
: classContent.Where(line => !string.IsNullOrEmpty(line)).Min(line => line.TakeWhile(char.IsWhiteSpace).Count());

fileContent.InsertRange(classEndIndex, collection: codeLines.Select(line => new string(' ', minimumSpaceCountInClass) + line));
await System.IO.File.WriteAllLinesAsync(filePath, contents: fileContent.ToArray());
Expand Down
5 changes: 2 additions & 3 deletions src/corePackages/Core.CodeGen/Code/CSharp/CSharpCodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static async Task<ICollection<PropertyInfo>> ReadClassPropertiesAsync(str
);

MatchCollection matches = propertyRegex.Matches(fileContent);
List<PropertyInfo> result = new();
List<PropertyInfo> result = [];
foreach (Match match in matches)
{
string accessModifier = match.Groups[1].Value.Trim();
Expand All @@ -80,8 +80,7 @@ public static async Task<ICollection<PropertyInfo>> ReadClassPropertiesAsync(str
values: potentialPropertyTypeFilePath
.Replace(projectPath, string.Empty)
.Replace(oldChar: '\\', newChar: '.')
.Replace(oldValue: $".{typeName}.cs", string.Empty)
.Substring(1)
.Replace(oldValue: $".{typeName}.cs", string.Empty)[1..]
.Split('.')
.Select(part => char.ToUpper(part[0], CultureInfo.GetCultureInfo("en-EN")) + part[1..])
);
Expand Down
8 changes: 4 additions & 4 deletions src/corePackages/Core.CodeGen/File/DirectoryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Core.CrossCuttingConcerns.Helpers;
using System.Reflection;
using System.Reflection;
using Core.CrossCuttingConcerns.Helpers;

namespace Core.CodeGen.File;

Expand All @@ -18,7 +18,7 @@ public static string AssemblyDirectory

public static ICollection<string> GetFilesInDirectoryTree(string root, string searchPattern)
{
List<string> files = new();
List<string> files = [];

Stack<string> stack = new();
stack.Push(root);
Expand All @@ -36,7 +36,7 @@ public static ICollection<string> GetFilesInDirectoryTree(string root, string se

public static ICollection<string> GetDirsInDirectoryTree(string root, string searchPattern)
{
List<string> dirs = new();
List<string> dirs = [];

Stack<string> stack = new();
stack.Push(root);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Scriban.Functions;
using System.Globalization;
using Scriban.Functions;
using Scriban.Runtime;
using System.Globalization;

namespace Core.CodeGen.TemplateEngine.Scriban;

Expand All @@ -13,7 +13,7 @@ public ScribanBuiltinFunctionsExtensions()

private void addStringFunctionsExtensions()
{
ScriptObject stringFunctions = new();
ScriptObject stringFunctions = [];
stringFunctions.Import(
obj: typeof(ScribanStringFunctionsExtensions),
renamer: member => member.Name.ToLower(CultureInfo.GetCultureInfo("en-EN"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ScribanTemplateRenderer : ITemplateRenderer

public ScribanTemplateRenderer()
{
_builtinFunctionsExtensions = new ScribanBuiltinFunctionsExtensions();
_builtinFunctionsExtensions = [];
}

public string TemplateExtension => "sbn";
Expand All @@ -18,7 +18,7 @@ public async Task<string> RenderAsync(string template, ITemplateData data)
{
TemplateContext templateContext = new();
templateContext.PushGlobal(_builtinFunctionsExtensions);
ScriptObject dataScriptObject = new();
ScriptObject dataScriptObject = [];
dataScriptObject.Import(data);
templateContext.PushGlobal(dataScriptObject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<ICollection<string>> RenderFileAsync(
ITemplateData templateData
)
{
List<string> newRenderedFilePaths = new();
List<string> newRenderedFilePaths = [];
foreach (string templateFilePath in templateFilePaths)
{
string newRenderedFilePath = await RenderFileAsync(templateFilePath, templateDir, replacePathVariable, outputDir, templateData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ public TypeRegistrar(IServiceCollection builder)

public ITypeResolver Build() => new TypeResolver(provider: _builder.BuildServiceProvider());

public void Register(Type service, Type implementation)
{
_builder.AddSingleton(service, implementation);
}
public void Register(Type service, Type implementation) => _builder.AddSingleton(service, implementation);

public void RegisterInstance(Type service, object implementation)
{
_builder.AddSingleton(service, implementation);
}
public void RegisterInstance(Type service, object implementation) => _builder.AddSingleton(service, implementation);

public void RegisterLazy(Type service, Func<object> func)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ public class BusinessException : Exception
{
public BusinessException(string message)
: base(message) { }

public BusinessException()
: base() { }

public BusinessException(string? message, Exception? innerException)
: base(message, innerException) { }
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
using System;
namespace Core.CrossCuttingConcerns.Helpers;

namespace Core.CrossCuttingConcerns.Helpers
public static class PlatformHelper
{
public static class PlatformHelper
public static string SecuredPathJoin(params string[] pathItems)
{
public static string SecuredPathJoin(params string[] pathItems)
string path;
_ = Environment.OSVersion.Platform switch
{
string path;
_ = Environment.OSVersion.Platform switch
{
PlatformID.Unix => path = String.Join("/", pathItems),
PlatformID.MacOSX => path = String.Join("/", pathItems),
_ => path = String.Join("\\", pathItems),
};
PlatformID.Unix => path = string.Join("/", pathItems),
PlatformID.MacOSX => path = string.Join("/", pathItems),
_ => path = string.Join("\\", pathItems),
};

return path;
}
return path;
}

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

return file;
}
return file;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Core.CodeGen.Code;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using Core.CodeGen.Code;
using Core.CodeGen.CommandLine.Git;
using Core.CodeGen.File;
using MediatR;
using System.IO.Compression;
using System.Runtime.CompilerServices;

namespace Application.Features.Create.Commands.New;

Expand Down Expand Up @@ -31,7 +31,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken
)
{
CreatedNewProjectResponse response = new();
List<string> newFilePaths = new();
List<string> newFilePaths = [];

response.CurrentStatusMessage = "Cloning starter project and core packages...";
yield return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ await _businessRules.FileShouldNotBeExists(
);

GeneratedCommandResponse response = new();
List<string> newFilePaths = new();
List<string> updatedFilePaths = new();
List<string> newFilePaths = [];
List<string> updatedFilePaths = [];

response.CurrentStatusMessage = "Generating Application layer codes...";
yield return response;
Expand Down Expand Up @@ -105,8 +105,8 @@ CommandTemplateData commandTemplateData
)
);
string[] commandOperationClaimPropertyCodeLines = await Task.WhenAll(
commandOperationClaimPropertyTemplateCodeLines.Select(
async line => await _templateEngine.RenderAsync(line, commandTemplateData)
commandOperationClaimPropertyTemplateCodeLines.Select(async line =>
await _templateEngine.RenderAsync(line, commandTemplateData)
)
);
await CSharpCodeInjector.AddCodeLinesAsPropertyAsync(featureOperationClaimFilePath, commandOperationClaimPropertyCodeLines);
Expand All @@ -130,8 +130,8 @@ CommandTemplateData commandTemplateData
)
);
string[] commandOperationClaimSeedCodeLines = await Task.WhenAll(
commandOperationClaimSeedTemplateCodeLines.Select(
async line => await _templateEngine.RenderAsync(line, commandTemplateData)
commandOperationClaimSeedTemplateCodeLines.Select(async line =>
await _templateEngine.RenderAsync(line, commandTemplateData)
)
);
await CSharpCodeInjector.AddCodeLinesToRegionAsync(
Expand All @@ -149,7 +149,7 @@ private async Task<ICollection<string>> generateFolderCodes(
CommandTemplateData commandTemplateData
)
{
List<string> templateFilePaths = DirectoryHelper
var templateFilePaths = DirectoryHelper
.GetFilesInDirectoryTree(templateDir, searchPattern: $"*.{_templateEngine.TemplateExtension}")
.ToList();
Dictionary<string, string> replacePathVariable =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Application.Features.Generate.Rules;
using System.Runtime.CompilerServices;
using Application.Features.Generate.Rules;
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;
using System.Runtime.CompilerServices;

namespace Application.Features.Generate.Commands.Crud;

public class GenerateCrudCommand : IStreamRequest<GeneratedCrudResponse>
{
public string ProjectPath { get; set; }
public CrudTemplateData CrudTemplateData { get; set; }
public string DbContextName { get; set; }
public required string ProjectPath { get; set; }
public required CrudTemplateData CrudTemplateData { get; set; }
public required string DbContextName { get; set; }

public class GenerateCrudCommandHandler : IStreamRequestHandler<GenerateCrudCommand, GeneratedCrudResponse>
{
Expand All @@ -35,8 +35,8 @@ [EnumeratorCancellation] CancellationToken cancellationToken
await _businessRules.EntityClassShouldBeInhreitEntityBaseClass(request.ProjectPath, request.CrudTemplateData.Entity.Name);

GeneratedCrudResponse response = new();
List<string> newFilePaths = new();
List<string> updatedFilePaths = new();
List<string> newFilePaths = [];
List<string> updatedFilePaths = [];

response.CurrentStatusMessage = $"Adding {request.CrudTemplateData.Entity.Name} entity to BaseContext.";
yield return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ await _businessRules.FileShouldNotBeExists(
);

GeneratedQueryResponse response = new();
List<string> newFilePaths = new();
List<string> updatedFilePaths = new();
List<string> newFilePaths = [];
List<string> updatedFilePaths = [];

response.CurrentStatusMessage = "Generating Application layer codes...";
yield return response;
Expand Down Expand Up @@ -106,8 +106,8 @@ QueryTemplateData QueryTemplateData
)
);
string[] queryOperationClaimPropertyCodeLines = await Task.WhenAll(
queryOperationClaimPropertyTemplateCodeLines.Select(
async line => await _templateEngine.RenderAsync(line, QueryTemplateData)
queryOperationClaimPropertyTemplateCodeLines.Select(async line =>
await _templateEngine.RenderAsync(line, QueryTemplateData)
)
);
await CSharpCodeInjector.AddCodeLinesAsPropertyAsync(featureOperationClaimFilePath, queryOperationClaimPropertyCodeLines);
Expand Down Expand Up @@ -148,7 +148,7 @@ private async Task<ICollection<string>> generateFolderCodes(
QueryTemplateData QueryTemplateData
)
{
List<string> templateFilePaths = DirectoryHelper
var templateFilePaths = DirectoryHelper
.GetFilesInDirectoryTree(templateDir, searchPattern: $"*.{_templateEngine.TemplateExtension}")
.ToList();
Dictionary<string, string> replacePathVariable =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void CheckProjectName()

public void CheckMechanismOptions()
{
List<string> mechanismsToPrompt = new();
List<string> mechanismsToPrompt = [];

if (IsCachingUsed)
AnsiConsole.MarkupLine("[green]Caching[/] is used.");
Expand Down
Loading

0 comments on commit 979ab8b

Please sign in to comment.