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 e467661 commit d55f846
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 551 deletions.
10 changes: 10 additions & 0 deletions .csharpierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 140,
"useTabs": false,
"tabWidth": 4,
"preprocessorSymbolSets": [
"",
"DEBUG",
"DEBUG,CODE_STYLE"
]
}
30 changes: 30 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: .NET

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- name: Restore tool dependencies
run: dotnet tool restore
- name: Check format
run: dotnet csharpier --check .
13 changes: 13 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: "Release"

on:
push:
tags:
- "v*.*.*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: GH Release
uses: softprops/action-gh-release@v0.1.15
60 changes: 14 additions & 46 deletions src/corePackages/Core.CodeGen/Code/CSharp/CSharpCodeInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ namespace Core.CodeGen.Code.CSharp;

public static class CSharpCodeInjector
{
public static async Task AddCodeLinesToMethodAsync(
string filePath,
string methodName,
string[] codeLines
)
public static async Task AddCodeLinesToMethodAsync(string filePath, string methodName, string[] codeLines)
{
List<string> fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
string methodStartRegex =
Expand Down Expand Up @@ -57,9 +53,7 @@ string[] codeLines
break;
if (
Regex.Match(input: fileContent[j], pattern: @"\s+return").Success
&& Regex
.Match(input: fileContent[j - 1], pattern: @"(if|else if|else)\s*\(")
.Success
&& Regex.Match(input: fileContent[j - 1], pattern: @"(if|else if|else)\s*\(").Success
)
break;

Expand All @@ -76,14 +70,10 @@ string[] codeLines
if (methodStartIndex == -1 || methodEndIndex == -1)
throw new Exception($"{methodName} not found in \"{filePath}\".");

ICollection<string> methodContent = fileContent
.Skip(methodStartIndex + 1)
.Take(methodEndIndex - 1 - methodStartIndex)
.ToArray();
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;
minimumSpaceCountInMethod = fileContent[methodStartIndex].TakeWhile(char.IsWhiteSpace).Count() * 2;
else
minimumSpaceCountInMethod = methodContent
.Where(line => !string.IsNullOrEmpty(line))
Expand Down Expand Up @@ -133,14 +123,11 @@ public static async Task AddCodeLinesAsPropertyAsync(string filePath, string[] c
}
}

propertySpaceCountInClass =
fileContent[indexToAdd].TakeWhile(char.IsWhiteSpace).Count() * 2;
propertySpaceCountInClass = fileContent[indexToAdd].TakeWhile(char.IsWhiteSpace).Count() * 2;
}
else
{
propertySpaceCountInClass = fileContent[indexToAdd]
.TakeWhile(char.IsWhiteSpace)
.Count();
propertySpaceCountInClass = fileContent[indexToAdd].TakeWhile(char.IsWhiteSpace).Count();
}

List<string> updatedFileContent = new(fileContent);
Expand All @@ -152,11 +139,7 @@ public static async Task AddCodeLinesAsPropertyAsync(string filePath, string[] c
await System.IO.File.WriteAllLinesAsync(filePath, contents: updatedFileContent.ToArray());
}

public static async Task AddCodeLinesToRegionAsync(
string filePath,
IEnumerable<string> linesToAdd,
string regionName
)
public static async Task AddCodeLinesToRegionAsync(string filePath, IEnumerable<string> linesToAdd, string regionName)
{
List<string> fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();
string regionStartRegex = @$"^\s*#region\s*{regionName}\s*";
Expand Down Expand Up @@ -189,15 +172,11 @@ string regionName
if (!string.IsNullOrEmpty(previousLine))
fileContent.Insert(index: indexToAdd - 1, string.Empty);

int minimumSpaceCountInRegion = fileContent[indexToAdd]
.TakeWhile(char.IsWhiteSpace)
.Count();
int minimumSpaceCountInRegion = fileContent[indexToAdd].TakeWhile(char.IsWhiteSpace).Count();

fileContent.InsertRange(
index: indexToAdd - 1,
collection: linesToAdd.Select(
line => new string(' ', minimumSpaceCountInRegion) + line
)
collection: linesToAdd.Select(line => new string(' ', minimumSpaceCountInRegion) + line)
);
await System.IO.File.WriteAllLinesAsync(filePath, fileContent);
break;
Expand All @@ -208,9 +187,7 @@ public static async Task AddUsingToFile(string filePath, IEnumerable<string> usi
{
List<string> fileContent = (await System.IO.File.ReadAllLinesAsync(filePath)).ToList();

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

Regex usingRegex = new(@"^using\s+.*;$");
int indexToAdd = 0;
Expand All @@ -231,9 +208,7 @@ public static async Task AddMethodToClass(string filePath, string className, str
{
List<string> 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}"
);
new(@$"((public|protected|internal|protected internal|private protected|private)\s+)?(static\s+)?\s+\b{className}");
Regex scopeBlockStartRegex = new(@"\{");
Regex scopeBlockEndRegex = new(@"\}");

Expand Down Expand Up @@ -275,24 +250,17 @@ public static async Task AddMethodToClass(string filePath, string className, str
if (classStartIndex == -1 || classEndIndex == -1)
throw new Exception($"{className} not found in \"{filePath}\".");

ICollection<string> classContent = fileContent
.Skip(classStartIndex + 1)
.Take(classEndIndex - 1 - classStartIndex)
.ToArray();
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;
minimumSpaceCountInClass = fileContent[classStartIndex].TakeWhile(char.IsWhiteSpace).Count() * 2;
else
minimumSpaceCountInClass = classContent
.Where(line => !string.IsNullOrEmpty(line))
.Min(line => line.TakeWhile(char.IsWhiteSpace).Count());

fileContent.InsertRange(
classEndIndex,
collection: codeLines.Select(line => new string(' ', minimumSpaceCountInClass) + line)
);
fileContent.InsertRange(classEndIndex, collection: codeLines.Select(line => new string(' ', minimumSpaceCountInClass) + line));
await System.IO.File.WriteAllLinesAsync(filePath, contents: fileContent.ToArray());
}
}
30 changes: 8 additions & 22 deletions src/corePackages/Core.CodeGen/Code/CSharp/CSharpCodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public static async Task<string> ReadBaseClassNameAsync(string filePath)
return match.Groups[1].Value;
}

public static async Task<ICollection<string>> ReadBaseClassGenericArgumentsAsync(
string filePath
)
public static async Task<ICollection<string>> ReadBaseClassGenericArgumentsAsync(string filePath)
{
string fileContent = await System.IO.File.ReadAllTextAsync(filePath);
const string pattern = @"class\s+\w+\s*:?\s*(\w+)\s*<([\w,\s]+)>";
Expand All @@ -46,10 +44,7 @@ string filePath
return genericArguments.Select(genericArgument => genericArgument.Trim()).ToArray();
}

public static async Task<ICollection<PropertyInfo>> ReadClassPropertiesAsync(
string filePath,
string projectPath
)
public static async Task<ICollection<PropertyInfo>> ReadClassPropertiesAsync(string filePath, string projectPath)
{
string fileContent = await System.IO.File.ReadAllTextAsync(filePath);
Regex propertyRegex =
Expand All @@ -73,14 +68,11 @@ string projectPath
string? nameSpace = null;
if (!builtInTypeRegex.IsMatch(typeName))
{
ICollection<string> potentialPropertyTypeFilePaths =
DirectoryHelper.GetFilesInDirectoryTree(
projectPath,
searchPattern: $"{typeName}.cs"
);
ICollection<string> usingNameSpacesInFile = await ReadUsingNameSpacesAsync(
filePath
ICollection<string> potentialPropertyTypeFilePaths = DirectoryHelper.GetFilesInDirectoryTree(
projectPath,
searchPattern: $"{typeName}.cs"
);
ICollection<string> usingNameSpacesInFile = await ReadUsingNameSpacesAsync(filePath);
foreach (string potentialPropertyTypeFilePath in potentialPropertyTypeFilePaths)
{
string potentialPropertyNameSpace = string.Join(
Expand All @@ -91,11 +83,7 @@ string projectPath
.Replace(oldValue: $".{typeName}.cs", string.Empty)
.Substring(1)
.Split('.')
.Select(
part =>
char.ToUpper(part[0], CultureInfo.GetCultureInfo("en-EN"))
+ part[1..]
)
.Select(part => char.ToUpper(part[0], CultureInfo.GetCultureInfo("en-EN")) + part[1..])
);
if (!usingNameSpacesInFile.Contains(potentialPropertyNameSpace))
continue;
Expand All @@ -107,9 +95,7 @@ string projectPath
PropertyInfo propertyInfo =
new()
{
AccessModifier = string.IsNullOrEmpty(accessModifier)
? "private"
: accessModifier,
AccessModifier = string.IsNullOrEmpty(accessModifier) ? "private" : accessModifier,
Type = type,
Name = name,
NameSpace = nameSpace
Expand Down
22 changes: 4 additions & 18 deletions src/corePackages/Core.CodeGen/Code/StringCaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public static string ToPascalCase(this string value)

return string.Join(
string.Empty,
values: words.Select(
word =>
char.ToUpper(word[index: 0], CultureInfo.GetCultureInfo("en-EN")) + word[1..]
)
values: words.Select(word => char.ToUpper(word[index: 0], CultureInfo.GetCultureInfo("en-EN")) + word[1..])
);
}

Expand All @@ -44,10 +41,7 @@ public static string ToSnakeCase(this string value)
if (words.Length == 1)
return value.ToLower(CultureInfo.GetCultureInfo("en-EN"));

return string.Join(
separator: "_",
values: words.Select(word => word.ToLower(CultureInfo.GetCultureInfo("en-EN")))
);
return string.Join(separator: "_", values: words.Select(word => word.ToLower(CultureInfo.GetCultureInfo("en-EN"))));
}

public static string ToKebabCase(this string value)
Expand All @@ -59,10 +53,7 @@ public static string ToKebabCase(this string value)
if (words.Length == 1)
return value.ToLower(CultureInfo.GetCultureInfo("en-EN"));

return string.Join(
separator: "-",
values: words.Select(word => word.ToLower(CultureInfo.GetCultureInfo("en-EN")))
);
return string.Join(separator: "-", values: words.Select(word => word.ToLower(CultureInfo.GetCultureInfo("en-EN"))));
}

public static string ToAbbreviation(this string value)
Expand All @@ -74,11 +65,6 @@ public static string ToAbbreviation(this string value)
if (words.Length == 1)
return char.ToLower(value[index: 0], CultureInfo.GetCultureInfo("en-EN")).ToString();

return string.Join(
string.Empty,
values: words.Select(
word => char.ToLower(word[index: 0], CultureInfo.GetCultureInfo("en-EN"))
)
);
return string.Join(string.Empty, values: words.Select(word => char.ToLower(word[index: 0], CultureInfo.GetCultureInfo("en-EN"))));
}
}
5 changes: 1 addition & 4 deletions src/corePackages/Core.CodeGen/Code/StringWordExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ namespace Core.CodeGen.Code;
public static class StringWordExtension
{
public static string[] GetWords(this string value) =>
Regex
.Split(value, pattern: @"(?<!^)(?=[A-Z0-9])|\s+|_|-")
.Where(word => !string.IsNullOrEmpty(word))
.ToArray();
Regex.Split(value, pattern: @"(?<!^)(?=[A-Z0-9])|\s+|_|-").Where(word => !string.IsNullOrEmpty(word)).ToArray();

public static string ToPlural(this string value) => PluralizationProvider.Pluralize(value);

Expand Down
3 changes: 1 addition & 2 deletions src/corePackages/Core.CodeGen/File/DirectoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public static string AssemblyDirectory
{
get
{
string codeBase =
PlatformHelper.GetDirectoryHeader() + 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
Expand Up @@ -19,6 +19,5 @@ public class ScribanStringFunctionsExtensions : StringFunctions

public static string Singular(string input) => input.ToSingular();

public static string Words(string input) =>
string.Join(separator: ' ', value: input.GetWords());
public static string Words(string input) => string.Join(separator: ' ', value: input.GetWords());
}
25 changes: 4 additions & 21 deletions src/corePackages/Core.CodeGen/TemplateEngine/TemplateEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,9 @@ ITemplateData templateData
{
string templateFileText = await System.IO.File.ReadAllTextAsync(templateFilePath);

string newRenderedFileText = await _templateRenderer.RenderAsync(
templateFileText,
templateData
);
string newRenderedFileText = await _templateRenderer.RenderAsync(templateFileText, templateData);
string newRenderedFilePath = await _templateRenderer.RenderAsync(
template: getOutputFilePath(
templateFilePath,
templateDir,
replacePathVariable,
outputDir
),
template: getOutputFilePath(templateFilePath, templateDir, replacePathVariable, outputDir),
templateData
);

Expand All @@ -55,13 +47,7 @@ ITemplateData templateData
List<string> newRenderedFilePaths = new();
foreach (string templateFilePath in templateFilePaths)
{
string newRenderedFilePath = await RenderFileAsync(
templateFilePath,
templateDir,
replacePathVariable,
outputDir,
templateData
);
string newRenderedFilePath = await RenderFileAsync(templateFilePath, templateDir, replacePathVariable, outputDir, templateData);
newRenderedFilePaths.Add(newRenderedFilePath);
}

Expand All @@ -77,10 +63,7 @@ string outputDir
{
string outputFilePath = templateFilePath;
foreach (KeyValuePair<string, string> replacePathVariableItem in replacePathVariable)
outputFilePath = outputFilePath.Replace(
replacePathVariableItem.Key,
replacePathVariableItem.Value
);
outputFilePath = outputFilePath.Replace(replacePathVariableItem.Key, replacePathVariableItem.Value);
outputFilePath = outputFilePath
.Replace(templateDir, outputDir)
.Replace(oldValue: $".{_templateRenderer.TemplateExtension}", string.Empty);
Expand Down
Loading

0 comments on commit d55f846

Please sign in to comment.