From bb058fb3dbb8ff46303cd2ac0906a4816dfe6044 Mon Sep 17 00:00:00 2001 From: Zohar Peled Date: Wed, 2 Jul 2025 09:40:22 +0300 Subject: [PATCH 1/6] Updated CodeGenerator project to language version 13.0, updated all namespaces in project to file-based, minor refactor on constructors --- .../AutoEntityGenerator.CodeGenerator.csproj | 1 + .../CodeFileGenerator.cs | 49 +++++++++---------- .../CodeGeneratorBase.cs | 41 ++++++++-------- .../EntityGenerator.cs | 49 +++++++++---------- .../GeneratedCodeFile.cs | 18 +++---- .../MappingsClassGenerator.cs | 47 +++++++++--------- .../ServicesExtensions.cs | 17 +++---- 7 files changed, 107 insertions(+), 115 deletions(-) diff --git a/AutoEntityGenerator.CodeGenerator/AutoEntityGenerator.CodeGenerator.csproj b/AutoEntityGenerator.CodeGenerator/AutoEntityGenerator.CodeGenerator.csproj index 77923db..be16227 100644 --- a/AutoEntityGenerator.CodeGenerator/AutoEntityGenerator.CodeGenerator.csproj +++ b/AutoEntityGenerator.CodeGenerator/AutoEntityGenerator.CodeGenerator.csproj @@ -2,6 +2,7 @@ netstandard2.0 + 13.0 diff --git a/AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs b/AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs index a715a9d..61fa6b4 100644 --- a/AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs +++ b/AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs @@ -5,39 +5,38 @@ [assembly: InternalsVisibleTo("AutoEntityGenerator.CodeGenerator.Tests")] -namespace AutoEntityGenerator.CodeGenerator +namespace AutoEntityGenerator.CodeGenerator; + +internal class CodeFileGenerator : ICodeFileGenerator { - internal class CodeFileGenerator : ICodeFileGenerator + private readonly IEntityGenerator _entityGenerator; + private readonly IMappingsClassGenerator _mappingsClassGenerator; + + public CodeFileGenerator(IEntityGenerator entityGenerator, IMappingsClassGenerator mappingsClassGenerator) { - private readonly IEntityGenerator _entityGenerator; - private readonly IMappingsClassGenerator _mappingsClassGenerator; + _entityGenerator = entityGenerator; + _mappingsClassGenerator = mappingsClassGenerator; + } - public CodeFileGenerator(IEntityGenerator entityGenerator, IMappingsClassGenerator mappingsClassGenerator) + public IGeneratedCodeFile GenerateEntityCodeFile(Entity entityInfo) + { + if (entityInfo is null) { - _entityGenerator = entityGenerator; - _mappingsClassGenerator = mappingsClassGenerator; + return null; } - public IGeneratedCodeFile GenerateEntityCodeFile(Entity entityInfo) - { - if (entityInfo is null) - { - return null; - } - - var code = _entityGenerator.GenerateEntityCode(entityInfo); - return new GeneratedCodeFile(code, Path.GetFileName(entityInfo.SourceFilePath)); - } + var code = _entityGenerator.GenerateEntityCode(entityInfo); + return new GeneratedCodeFile(code, Path.GetFileName(entityInfo.SourceFilePath)); + } - public IGeneratedCodeFile GenerateMappingCodeFile(Entity from, Entity to) + public IGeneratedCodeFile GenerateMappingCodeFile(Entity from, Entity to) + { + if (from is null || to is null) { - if (from is null || to is null) - { - return null; - } - - var code = _mappingsClassGenerator.GenerateMappingClassCode(from, to); - return new GeneratedCodeFile(code, Path.GetFileNameWithoutExtension(from.SourceFilePath) + "MappingExtensions.cs"); + return null; } + + var code = _mappingsClassGenerator.GenerateMappingClassCode(from, to); + return new GeneratedCodeFile(code, Path.GetFileNameWithoutExtension(from.SourceFilePath) + "MappingExtensions.cs"); } } diff --git a/AutoEntityGenerator.CodeGenerator/CodeGeneratorBase.cs b/AutoEntityGenerator.CodeGenerator/CodeGeneratorBase.cs index 35bec98..3b8f4df 100644 --- a/AutoEntityGenerator.CodeGenerator/CodeGeneratorBase.cs +++ b/AutoEntityGenerator.CodeGenerator/CodeGeneratorBase.cs @@ -3,35 +3,34 @@ using System.Collections.Generic; using System.Text; -namespace AutoEntityGenerator.CodeGenerator +namespace AutoEntityGenerator.CodeGenerator; + +internal abstract class CodeGeneratorBase { - internal abstract class CodeGeneratorBase - { - public string Comments => - $@"/* + public string Comments => +$@"/* Generated by {nameof(AutoEntityGenerator)} on {DateTime.Now} For more information about {nameof(AutoEntityGenerator)}, Visit https://github.com/Peled-Zohar/AutoEntityGenerator */ "; - protected string GenerateProperties(IEnumerable properties, Func propertyFormat) + protected string GenerateProperties(IEnumerable properties, Func propertyFormat) + { + var propertiesBuilder = new StringBuilder(); + foreach (var property in properties) { - var propertiesBuilder = new StringBuilder(); - foreach (var property in properties) - { - propertiesBuilder.AppendLine(propertyFormat(property)); - } - return propertiesBuilder.ToString(0, propertiesBuilder.Length - Environment.NewLine.Length); + propertiesBuilder.AppendLine(propertyFormat(property)); } + return propertiesBuilder.ToString(0, propertiesBuilder.Length - Environment.NewLine.Length); + } - protected string GenerateTypeParameters(Entity entity) - => entity.TypeParameters.Count > 0 - ? $"<{string.Join(", ", entity.TypeParameters)}>" - : ""; + protected string GenerateTypeParameters(Entity entity) + => entity.TypeParameters.Count > 0 + ? $"<{string.Join(", ", entity.TypeParameters)}>" + : ""; - protected string GenerateGenericConstraints(Entity entity) - => entity.GenericConstraints.Count > 0 - ? " " + string.Join(" ", entity.GenericConstraints) - : ""; + protected string GenerateGenericConstraints(Entity entity) + => entity.GenericConstraints.Count > 0 + ? " " + string.Join(" ", entity.GenericConstraints) + : ""; - } } diff --git a/AutoEntityGenerator.CodeGenerator/EntityGenerator.cs b/AutoEntityGenerator.CodeGenerator/EntityGenerator.cs index d9c8fed..4ef7c80 100644 --- a/AutoEntityGenerator.CodeGenerator/EntityGenerator.cs +++ b/AutoEntityGenerator.CodeGenerator/EntityGenerator.cs @@ -1,44 +1,44 @@ using AutoEntityGenerator.Common.CodeInfo; -namespace AutoEntityGenerator.CodeGenerator +namespace AutoEntityGenerator.CodeGenerator; + +public interface IEntityGenerator { - public interface IEntityGenerator - { - string GenerateEntityCode(Entity entity); - } + string GenerateEntityCode(Entity entity); +} - internal class EntityGenerator : CodeGeneratorBase, IEntityGenerator +internal class EntityGenerator : CodeGeneratorBase, IEntityGenerator +{ + public string GenerateEntityCode(Entity entity) { - public string GenerateEntityCode(Entity entity) - { - var entityType = entity.Project.CSharpVersion == CSharpVersion.Default - || (int)entity.Project.CSharpVersion >= (int)CSharpVersion.CSharp9 - ? "record" - : "class"; + var entityType = entity.Project.CSharpVersion == CSharpVersion.Default + || (int)entity.Project.CSharpVersion >= (int)CSharpVersion.CSharp9 + ? "record" + : "class"; - var indentationLevel = entity.Namespace.IsFileScoped ? 1 : 2; - var indentation = new string('\t', indentationLevel); - var properties = GenerateProperties(entity.Properties, p => $"{indentation}public {p.Type} {p.Name} {{get;set;}}"); + var indentationLevel = entity.Namespace.IsFileScoped ? 1 : 2; + var indentation = new string('\t', indentationLevel); + var properties = GenerateProperties(entity.Properties, p => $"{indentation}public {p.Type} {p.Name} {{get;set;}}"); - return GenerateCode(entity, entityType, properties); - } + return GenerateCode(entity, entityType, properties); + } - private string GenerateCode(Entity entity, string typeName, string properties) - { - var typeParameters = GenerateTypeParameters(entity); - var genericConstraints = GenerateGenericConstraints(entity); + private string GenerateCode(Entity entity, string typeName, string properties) + { + var typeParameters = GenerateTypeParameters(entity); + var genericConstraints = GenerateGenericConstraints(entity); - return entity.Namespace.IsFileScoped - ? + return entity.Namespace.IsFileScoped + ? $@"{Comments}namespace {entity.Namespace.Name}; public partial {typeName} {entity.Name}{typeParameters}{genericConstraints} {{ {properties} }}" - : + : $@"{Comments}namespace {entity.Namespace.Name} {{ public partial {typeName} {entity.Name}{typeParameters}{genericConstraints} @@ -46,6 +46,5 @@ public partial {typeName} {entity.Name}{typeParameters}{genericConstraints} {properties} }} }}"; - } } } diff --git a/AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs b/AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs index ba8973f..bb9b4a8 100644 --- a/AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs +++ b/AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs @@ -1,18 +1,14 @@ using AutoEntityGenerator.Common.Interfaces; -namespace AutoEntityGenerator.CodeGenerator +namespace AutoEntityGenerator.CodeGenerator; + +internal class GeneratedCodeFile : IGeneratedCodeFile { - internal class GeneratedCodeFile : IGeneratedCodeFile - { - public GeneratedCodeFile(string content, string fileName) - { - Content = content; - FileName = fileName; - } + public GeneratedCodeFile(string content, string fileName) + => (Content, FileName) = (Content, fileName); - public string FileName { get; } + public string FileName { get; } - public string Content { get; } + public string Content { get; } - } } diff --git a/AutoEntityGenerator.CodeGenerator/MappingsClassGenerator.cs b/AutoEntityGenerator.CodeGenerator/MappingsClassGenerator.cs index 99fa3a0..bec76f3 100644 --- a/AutoEntityGenerator.CodeGenerator/MappingsClassGenerator.cs +++ b/AutoEntityGenerator.CodeGenerator/MappingsClassGenerator.cs @@ -1,35 +1,35 @@ using AutoEntityGenerator.Common.CodeInfo; -namespace AutoEntityGenerator.CodeGenerator +namespace AutoEntityGenerator.CodeGenerator; + +public interface IMappingsClassGenerator { - public interface IMappingsClassGenerator - { - string GenerateMappingClassCode(Entity from, Entity to); - } + string GenerateMappingClassCode(Entity from, Entity to); +} - internal class MappingsClassGenerator : CodeGeneratorBase, IMappingsClassGenerator +internal class MappingsClassGenerator : CodeGeneratorBase, IMappingsClassGenerator +{ + public string GenerateMappingClassCode(Entity from, Entity to) { - public string GenerateMappingClassCode(Entity from, Entity to) - { - var indentationLevel = from.Namespace.IsFileScoped ? 3 : 4; - var indentation = new string('\t', indentationLevel); - var properties = GenerateProperties(from.Properties, p => $"{indentation}{p.Name} = source.{p.Name},"); + var indentationLevel = from.Namespace.IsFileScoped ? 3 : 4; + var indentation = new string('\t', indentationLevel); + var properties = GenerateProperties(from.Properties, p => $"{indentation}{p.Name} = source.{p.Name},"); - return GenerateCode(from, to, properties); - } + return GenerateCode(from, to, properties); + } - private string GenerateCode(Entity from, Entity to, string properties) - { + private string GenerateCode(Entity from, Entity to, string properties) + { - var typeParameters = GenerateTypeParameters(to); - var genericConstraints = GenerateGenericConstraints(to); + var typeParameters = GenerateTypeParameters(to); + var genericConstraints = GenerateGenericConstraints(to); - var toFullName = string.IsNullOrEmpty(to.Namespace.Name) - ? to.Name - : to.Namespace.Name + "." + to.Name; + var toFullName = string.IsNullOrEmpty(to.Namespace.Name) + ? to.Name + : to.Namespace.Name + "." + to.Name; - return from.Namespace.IsFileScoped - ? + return from.Namespace.IsFileScoped + ? $@"{Comments}namespace {from.Namespace.Name}; public static partial class {from.Name}MappingExtensions @@ -42,7 +42,7 @@ public static partial class {from.Name}MappingExtensions }}; }} }}" - : + : $@"{Comments}namespace {from.Namespace.Name} {{ public static partial class {from.Name}MappingExtensions @@ -56,6 +56,5 @@ public static partial class {from.Name}MappingExtensions }} }} }}"; - } } } diff --git a/AutoEntityGenerator.CodeGenerator/ServicesExtensions.cs b/AutoEntityGenerator.CodeGenerator/ServicesExtensions.cs index 0d666eb..e21962c 100644 --- a/AutoEntityGenerator.CodeGenerator/ServicesExtensions.cs +++ b/AutoEntityGenerator.CodeGenerator/ServicesExtensions.cs @@ -1,15 +1,14 @@ using AutoEntityGenerator.Common.Interfaces; -namespace AutoEntityGenerator.CodeGenerator +namespace AutoEntityGenerator.CodeGenerator; + +public static class ServicesExtensions { - public static class ServicesExtensions + public static IServices AddCodeGenerator(this IServices services) { - public static IServices AddCodeGenerator(this IServices services) - { - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - return services; - } + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + return services; } } From 3287e31c8d7d155f29b0f7fae3aa365ba337907c Mon Sep 17 00:00:00 2001 From: Zohar Peled Date: Wed, 2 Jul 2025 09:40:59 +0300 Subject: [PATCH 2/6] Minor refactoring of constructor --- AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs b/AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs index 61fa6b4..d2a7cf4 100644 --- a/AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs +++ b/AutoEntityGenerator.CodeGenerator/CodeFileGenerator.cs @@ -13,10 +13,7 @@ internal class CodeFileGenerator : ICodeFileGenerator private readonly IMappingsClassGenerator _mappingsClassGenerator; public CodeFileGenerator(IEntityGenerator entityGenerator, IMappingsClassGenerator mappingsClassGenerator) - { - _entityGenerator = entityGenerator; - _mappingsClassGenerator = mappingsClassGenerator; - } + => (_entityGenerator, _mappingsClassGenerator) = (entityGenerator, mappingsClassGenerator); public IGeneratedCodeFile GenerateEntityCodeFile(Entity entityInfo) { From bc23477daa01bc260f7ca2897ee0e59032b9db90 Mon Sep 17 00:00:00 2001 From: Zohar Peled Date: Wed, 2 Jul 2025 09:43:10 +0300 Subject: [PATCH 3/6] Updated language reference to 13.0, Refactor Construcor's Constructor --- .../AutoEntityGenerator.Common.csproj | 2 +- AutoEntityGenerator.Common/CodeInfo/Constructor.cs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/AutoEntityGenerator.Common/AutoEntityGenerator.Common.csproj b/AutoEntityGenerator.Common/AutoEntityGenerator.Common.csproj index 74aa702..dc75854 100644 --- a/AutoEntityGenerator.Common/AutoEntityGenerator.Common.csproj +++ b/AutoEntityGenerator.Common/AutoEntityGenerator.Common.csproj @@ -2,7 +2,7 @@ netstandard2.0 - latest + 13.0 diff --git a/AutoEntityGenerator.Common/CodeInfo/Constructor.cs b/AutoEntityGenerator.Common/CodeInfo/Constructor.cs index 90d2236..24001b5 100644 --- a/AutoEntityGenerator.Common/CodeInfo/Constructor.cs +++ b/AutoEntityGenerator.Common/CodeInfo/Constructor.cs @@ -6,10 +6,8 @@ namespace AutoEntityGenerator.Common.CodeInfo; [ExcludeFromCodeCoverage] // There's no logic to test here... public sealed class Constructor { - public Constructor(IEnumerable parameters) - { - Parameters = new List(parameters); - } + public Constructor(IEnumerable parameters) + => Parameters = [.. parameters]; public List Parameters { get; } } From 7154d301dc26d70fe8c500df145498a53eff8086 Mon Sep 17 00:00:00 2001 From: Zohar Peled Date: Wed, 2 Jul 2025 09:51:26 +0300 Subject: [PATCH 4/6] Updated language version to 13.0, small refactoring --- AutoEntityGenerator/AutoEntityGenerator.csproj | 4 ++-- .../AutoEntityGeneratorCodeRefactoringProvider.cs | 2 +- AutoEntityGenerator/EntityGeneratorCodeAction.cs | 6 ++---- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/AutoEntityGenerator/AutoEntityGenerator.csproj b/AutoEntityGenerator/AutoEntityGenerator.csproj index fa44eb9..f137a43 100644 --- a/AutoEntityGenerator/AutoEntityGenerator.csproj +++ b/AutoEntityGenerator/AutoEntityGenerator.csproj @@ -1,9 +1,9 @@ - + netstandard2.0 true - latest + 13.0 diff --git a/AutoEntityGenerator/AutoEntityGeneratorCodeRefactoringProvider.cs b/AutoEntityGenerator/AutoEntityGeneratorCodeRefactoringProvider.cs index c3cbabd..ccad96a 100644 --- a/AutoEntityGenerator/AutoEntityGeneratorCodeRefactoringProvider.cs +++ b/AutoEntityGenerator/AutoEntityGeneratorCodeRefactoringProvider.cs @@ -31,7 +31,7 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var node = root.FindNode(context.Span); - if (!(node is ClassDeclarationSyntax || node is RecordDeclarationSyntax)) + if (node is not (ClassDeclarationSyntax or RecordDeclarationSyntax)) { return; } diff --git a/AutoEntityGenerator/EntityGeneratorCodeAction.cs b/AutoEntityGenerator/EntityGeneratorCodeAction.cs index a76e07c..8968cee 100644 --- a/AutoEntityGenerator/EntityGeneratorCodeAction.cs +++ b/AutoEntityGenerator/EntityGeneratorCodeAction.cs @@ -43,8 +43,6 @@ protected override Task> ComputeOperationsAsync ]); } - protected override Task> ComputePreviewOperationsAsync(CancellationToken cancellationToken) - { - return Task.FromResult(Enumerable.Empty()); - } + protected override Task> ComputePreviewOperationsAsync(CancellationToken cancellationToken) + => Task.FromResult(Enumerable.Empty()); } From 18be589b8de22442adbd60de4990d709ff09c43d Mon Sep 17 00:00:00 2001 From: Zohar Peled Date: Wed, 2 Jul 2025 09:59:46 +0300 Subject: [PATCH 5/6] fixed refactoring bug --- AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs b/AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs index bb9b4a8..13d4596 100644 --- a/AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs +++ b/AutoEntityGenerator.CodeGenerator/GeneratedCodeFile.cs @@ -5,7 +5,7 @@ namespace AutoEntityGenerator.CodeGenerator; internal class GeneratedCodeFile : IGeneratedCodeFile { public GeneratedCodeFile(string content, string fileName) - => (Content, FileName) = (Content, fileName); + => (Content, FileName) = (content, fileName); public string FileName { get; } From fd17d4a2ba4060f10934936b8ef78db0b4ab6950 Mon Sep 17 00:00:00 2001 From: Zohar Peled Date: Wed, 2 Jul 2025 12:48:58 +0300 Subject: [PATCH 6/6] Disable nullable reference types in test projects (it's simply not needed in these projects) --- .../AutoEntityGenerator.CodeGenerator.Tests.csproj | 2 +- .../AutoEntityGenerator.UI.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AutoEntityGenerator.CodeGenerator.Tests/AutoEntityGenerator.CodeGenerator.Tests.csproj b/AutoEntityGenerator.CodeGenerator.Tests/AutoEntityGenerator.CodeGenerator.Tests.csproj index 21432e4..f24ba39 100644 --- a/AutoEntityGenerator.CodeGenerator.Tests/AutoEntityGenerator.CodeGenerator.Tests.csproj +++ b/AutoEntityGenerator.CodeGenerator.Tests/AutoEntityGenerator.CodeGenerator.Tests.csproj @@ -3,7 +3,7 @@ net8.0 enable - enable + disable false true diff --git a/AutoEntityGenerator.UI.Tests/AutoEntityGenerator.UI.Tests.csproj b/AutoEntityGenerator.UI.Tests/AutoEntityGenerator.UI.Tests.csproj index 5dd9a91..da13dec 100644 --- a/AutoEntityGenerator.UI.Tests/AutoEntityGenerator.UI.Tests.csproj +++ b/AutoEntityGenerator.UI.Tests/AutoEntityGenerator.UI.Tests.csproj @@ -3,7 +3,7 @@ net8.0 enable - enable + disable false true