From dec144e24eff0e5468cc97f875b4e42bf218cd65 Mon Sep 17 00:00:00 2001 From: AlexNav73 Date: Sun, 25 Feb 2024 12:41:23 +0100 Subject: [PATCH] moved out code generation logic into the shared project --- build/Build.cs | 8 +- .../ApplicationModelGenerator.cs | 68 ++--------- .../CoreCraft.Generators.AssemblyInfo.cs | 4 - .../CoreCraft.Generators.csproj | 7 ++ .../CoreCraft.SourceGeneration.projitems | 22 ++++ .../CoreCraft.SourceGeneration.shproj | 13 +++ .../IndentedTextWriterExtensions.cs | 4 +- .../Generators}/EntitiesGenerator.cs | 28 ++--- .../Generators/GeneratorCommon.cs | 19 +++ .../Generators/ModelGenerator.cs | 36 ++++++ .../Generators}/ModelShardGenerator.cs | 110 +++++++++--------- .../ModelScheme.cs | 2 +- .../Serialization/DtoConverter.cs | 2 +- .../Serialization/ModelSchemeDto.cs | 2 +- .../Serialization/ModelSchemeReader.cs | 14 +++ .../CoreCraft.Storage.Json.AssemblyInfo.cs | 4 - .../CoreCraft.Storage.Json.csproj | 5 + .../CoreCraft.Storage.Sqlite.AssemblyInfo.cs | 4 - .../CoreCraft.Storage.Sqlite.csproj | 5 + src/CoreCraft.sln | 6 + src/CoreCraft/CoreCraft.AssemblyInfo.cs | 6 - src/CoreCraft/CoreCraft.csproj | 7 ++ 22 files changed, 224 insertions(+), 152 deletions(-) delete mode 100644 src/CoreCraft.Generators/CoreCraft.Generators.AssemblyInfo.cs create mode 100644 src/CoreCraft.SourceGeneration/CoreCraft.SourceGeneration.projitems create mode 100644 src/CoreCraft.SourceGeneration/CoreCraft.SourceGeneration.shproj rename src/{CoreCraft.Generators => CoreCraft.SourceGeneration/Extensions}/IndentedTextWriterExtensions.cs (95%) rename src/{CoreCraft.Generators => CoreCraft.SourceGeneration/Generators}/EntitiesGenerator.cs (76%) create mode 100644 src/CoreCraft.SourceGeneration/Generators/GeneratorCommon.cs create mode 100644 src/CoreCraft.SourceGeneration/Generators/ModelGenerator.cs rename src/{CoreCraft.Generators => CoreCraft.SourceGeneration/Generators}/ModelShardGenerator.cs (82%) rename src/{CoreCraft.Generators => CoreCraft.SourceGeneration}/ModelScheme.cs (97%) rename src/{CoreCraft.Generators => CoreCraft.SourceGeneration}/Serialization/DtoConverter.cs (98%) rename src/{CoreCraft.Generators => CoreCraft.SourceGeneration}/Serialization/ModelSchemeDto.cs (95%) create mode 100644 src/CoreCraft.SourceGeneration/Serialization/ModelSchemeReader.cs delete mode 100644 src/CoreCraft.Storage.Json/CoreCraft.Storage.Json.AssemblyInfo.cs delete mode 100644 src/CoreCraft.Storage.Sqlite/CoreCraft.Storage.Sqlite.AssemblyInfo.cs delete mode 100644 src/CoreCraft/CoreCraft.AssemblyInfo.cs diff --git a/build/Build.cs b/build/Build.cs index a617a1e2..c67842ed 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -133,28 +133,28 @@ internal partial class Build : NukeBuild DotNetPack(s => s .SetProject(Solution.CoreCraft) .Apply(PackSettingsBase) - .SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.5")) + .SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.6")) .SetDescription("A core library to build cross-platform and highly customizable domain models") .AddPackageTags("Model", "Domain")); DotNetPack(s => s .SetProject(Solution.CoreCraft_Generators) .Apply(PackSettingsBase) - .SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.5")) + .SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.6")) .SetDescription("Roslyn Source Generators for generating domain models using 'CoreCraft' library") .AddPackageTags("Model", "Domain", "SourceGenerator", "Generator")); DotNetPack(s => s .SetProject(Solution.CoreCraft_Storage_Sqlite) .Apply(PackSettingsBase) - .SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.5")) + .SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.6")) .SetDescription("SQLite storage implementation for 'CoreCraft' library") .AddPackageTags("Model", "Domain", "SQLite")); DotNetPack(s => s .SetProject(Solution.CoreCraft_Storage_Json) .Apply(PackSettingsBase) - .SetVersion(MakePreviewIfNeeded("0.3.0", "0.3.5")) + .SetVersion(MakePreviewIfNeeded("0.3.0", "0.3.6")) .SetDescription("Json storage implementation for 'CoreCraft' library") .AddPackageTags("Model", "Domain", "Json")); diff --git a/src/CoreCraft.Generators/ApplicationModelGenerator.cs b/src/CoreCraft.Generators/ApplicationModelGenerator.cs index 0680b491..0f41767c 100644 --- a/src/CoreCraft.Generators/ApplicationModelGenerator.cs +++ b/src/CoreCraft.Generators/ApplicationModelGenerator.cs @@ -1,12 +1,14 @@ using System.Collections.Immutable; -using CoreCraft.Generators.Serialization; +using CoreCraft.SourceGeneration; +using CoreCraft.SourceGeneration.Generators; +using CoreCraft.SourceGeneration.Serialization; using Microsoft.CodeAnalysis; using Newtonsoft.Json; namespace CoreCraft.Generators; [Generator(LanguageNames.CSharp)] -internal partial class ApplicationModelGenerator : GeneratorBase +internal sealed class ApplicationModelGenerator : GeneratorBase { protected override void ExecuteInternal( SourceProductionContext context, @@ -16,68 +18,18 @@ protected override void ExecuteInternal( var options = new JsonSerializerSettings(); var serializer = JsonSerializer.Create(options); - foreach (var file in files) + foreach (var (name, content) in files) { - ModelScheme modelScheme = null; - - using (var stringStream = new StringReader(file.content)) - using (var reader = new JsonTextReader(stringStream)) - { - modelScheme = DtoConverter.Convert(serializer.Deserialize(reader)); - } + ModelScheme modelScheme = ModelSchemeReader.Read(serializer, content); if (modelScheme == null) { - throw new InvalidOperationException($"Failed to deserialize model file [{file.name}]"); + throw new InvalidOperationException($"Failed to deserialize model file [{name}]"); } + + var code = ModelGenerator.Generate(assemblyName, name, modelScheme); - using (var writer = new StringWriter()) - using (var code = new IndentedTextWriter(writer, " ")) - { - code.Preamble(); - Generate(assemblyName, file.name, code, modelScheme); - - AddSourceFile(context, file.name, writer.ToString()); - } + AddSourceFile(context, name, code); } } - - private void Generate(string assemblyName, string modelName, IndentedTextWriter code, ModelScheme modelScheme) - { - var @namespace = $"{assemblyName}.{modelName}"; - - code.WriteLine($"namespace {@namespace}"); - code.Block(() => - { - code.WriteLine("using CoreCraft.Core;"); - code.WriteLine("using CoreCraft.ChangesTracking;"); - code.WriteLine("using CoreCraft.Persistence;"); - code.WriteLine($"using {@namespace}.Entities;"); - code.EmptyLine(); - - GenerateModelShards(code, modelScheme.Shards); - }); - code.EmptyLine(); - - code.WriteLine($"namespace {@namespace}.Entities"); - code.Block(() => - { - GenerateEntities(code, modelScheme.Shards); - }); - } - - private static string DefineProperty(string type, string name, string accessors = "get; private set;") - { - return string.Join(" ", type, name, "{", accessors, "}").Trim(); - } - - private static string ToCamelCase(string value) - { - if (value.Length > 1) - { - return $"{char.ToLower(value[0])}{value.Substring(1)}"; - } - - return value; - } } diff --git a/src/CoreCraft.Generators/CoreCraft.Generators.AssemblyInfo.cs b/src/CoreCraft.Generators/CoreCraft.Generators.AssemblyInfo.cs deleted file mode 100644 index 68070a5c..00000000 --- a/src/CoreCraft.Generators/CoreCraft.Generators.AssemblyInfo.cs +++ /dev/null @@ -1,4 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] -[assembly: InternalsVisibleTo("CoreCraft.Generators.Tests")] diff --git a/src/CoreCraft.Generators/CoreCraft.Generators.csproj b/src/CoreCraft.Generators/CoreCraft.Generators.csproj index 10e0cd0c..daf605aa 100644 --- a/src/CoreCraft.Generators/CoreCraft.Generators.csproj +++ b/src/CoreCraft.Generators/CoreCraft.Generators.csproj @@ -35,4 +35,11 @@ + + + + + + + diff --git a/src/CoreCraft.SourceGeneration/CoreCraft.SourceGeneration.projitems b/src/CoreCraft.SourceGeneration/CoreCraft.SourceGeneration.projitems new file mode 100644 index 00000000..642af6bb --- /dev/null +++ b/src/CoreCraft.SourceGeneration/CoreCraft.SourceGeneration.projitems @@ -0,0 +1,22 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + b11d3e00-59b5-4227-ab86-2f20e5fa330b + + + CoreCraft.SourceGeneration + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/CoreCraft.SourceGeneration/CoreCraft.SourceGeneration.shproj b/src/CoreCraft.SourceGeneration/CoreCraft.SourceGeneration.shproj new file mode 100644 index 00000000..28ecc415 --- /dev/null +++ b/src/CoreCraft.SourceGeneration/CoreCraft.SourceGeneration.shproj @@ -0,0 +1,13 @@ + + + + b11d3e00-59b5-4227-ab86-2f20e5fa330b + 14.0 + + + + + + + + diff --git a/src/CoreCraft.Generators/IndentedTextWriterExtensions.cs b/src/CoreCraft.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs similarity index 95% rename from src/CoreCraft.Generators/IndentedTextWriterExtensions.cs rename to src/CoreCraft.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs index ba9a5726..597919af 100644 --- a/src/CoreCraft.Generators/IndentedTextWriterExtensions.cs +++ b/src/CoreCraft.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs @@ -1,4 +1,4 @@ -namespace CoreCraft.Generators; +namespace CoreCraft.SourceGeneration.Extensions; internal static class IndentedTextWriterExtensions { @@ -111,7 +111,7 @@ public static void GeneratedInterfaceAttributes(this IndentedTextWriter code) public static void GeneratedCodeAttribute(this IndentedTextWriter code) { - code.WriteLine($"[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"C# Source Generator\", \"{typeof(GeneratorBase).Assembly.GetName().Version}\")]"); + code.WriteLine($"[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"C# Source Generator\", \"{typeof(IndentedTextWriterExtensions).Assembly.GetName().Version}\")]"); } public static void CompilerGeneratedCodeAttribute(this IndentedTextWriter code) diff --git a/src/CoreCraft.Generators/EntitiesGenerator.cs b/src/CoreCraft.SourceGeneration/Generators/EntitiesGenerator.cs similarity index 76% rename from src/CoreCraft.Generators/EntitiesGenerator.cs rename to src/CoreCraft.SourceGeneration/Generators/EntitiesGenerator.cs index e32e4578..3b53f8d1 100644 --- a/src/CoreCraft.Generators/EntitiesGenerator.cs +++ b/src/CoreCraft.SourceGeneration/Generators/EntitiesGenerator.cs @@ -1,31 +1,33 @@ -namespace CoreCraft.Generators; +using CoreCraft.SourceGeneration.Extensions; -internal partial class ApplicationModelGenerator +namespace CoreCraft.SourceGeneration.Generators; + +internal sealed class EntitiesGenerator(IndentedTextWriter code) : GeneratorCommon { - public void GenerateEntities(IndentedTextWriter code, IEnumerable shards) + public void Generate(IEnumerable shards) { code.WriteLine("using CoreCraft.Core;"); code.EmptyLine(); foreach (var modelShard in shards) { - DefineEntities(code, modelShard); + DefineEntities(modelShard); code.EmptyLine(); } } - private void DefineEntities(IndentedTextWriter code, ModelShard modelShard) + private void DefineEntities(ModelShard modelShard) { foreach (var entity in modelShard.Collections.Select(x => x.Entity)) { - DefineEntityType(code, entity); + DefineEntityType(entity); code.EmptyLine(); - DefineEntityPropertiesClass(code, entity); + DefineEntityPropertiesClass(entity); code.EmptyLine(); } } - private void DefineEntityType(IndentedTextWriter code, Entity entity) + private void DefineEntityType(Entity entity) { code.GeneratedClassAttributes(); code.WriteLine($"public sealed record {entity.Name}(global::System.Guid Id) : Entity(Id)"); @@ -38,13 +40,13 @@ private void DefineEntityType(IndentedTextWriter code, Entity entity) }); } - private void DefineEntityPropertiesClass(IndentedTextWriter code, Entity entity) + private void DefineEntityPropertiesClass(Entity entity) { code.GeneratedClassAttributes(); code.WriteLine($"public sealed partial record {entity.PropertiesType} : Properties"); code.Block(() => { - DefineCtor(code, entity); + DefineCtor(entity); code.EmptyLine(); foreach (var prop in entity.Properties) @@ -53,11 +55,11 @@ private void DefineEntityPropertiesClass(IndentedTextWriter code, Entity entity) } code.EmptyLine(); - ImplementEntityPropertiesMethods(code, entity); + ImplementEntityPropertiesMethods(entity); code.EmptyLine(); }); - void DefineCtor(IndentedTextWriter code, Entity entity) + void DefineCtor(Entity entity) { code.WriteLine($"public {entity.PropertiesType}()"); code.Block(() => @@ -69,7 +71,7 @@ void DefineCtor(IndentedTextWriter code, Entity entity) }); } - void ImplementEntityPropertiesMethods(IndentedTextWriter code, Entity entity) + void ImplementEntityPropertiesMethods(Entity entity) { code.NoIndent(c => c.WriteLine("#if NET5_0_OR_GREATER")); code.WriteLine($"public override {entity.PropertiesType} ReadFrom(IPropertiesBag bag)"); diff --git a/src/CoreCraft.SourceGeneration/Generators/GeneratorCommon.cs b/src/CoreCraft.SourceGeneration/Generators/GeneratorCommon.cs new file mode 100644 index 00000000..a638fbea --- /dev/null +++ b/src/CoreCraft.SourceGeneration/Generators/GeneratorCommon.cs @@ -0,0 +1,19 @@ +namespace CoreCraft.SourceGeneration.Generators; + +internal abstract class GeneratorCommon +{ + protected static string DefineProperty(string type, string name, string accessors = "get; private set;") + { + return string.Join(" ", type, name, "{", accessors, "}").Trim(); + } + + protected static string ToCamelCase(string value) + { + if (value.Length > 1) + { + return $"{char.ToLower(value[0])}{value.Substring(1)}"; + } + + return value; + } +} diff --git a/src/CoreCraft.SourceGeneration/Generators/ModelGenerator.cs b/src/CoreCraft.SourceGeneration/Generators/ModelGenerator.cs new file mode 100644 index 00000000..54994f00 --- /dev/null +++ b/src/CoreCraft.SourceGeneration/Generators/ModelGenerator.cs @@ -0,0 +1,36 @@ +using CoreCraft.SourceGeneration.Extensions; + +namespace CoreCraft.SourceGeneration.Generators; + +internal static class ModelGenerator +{ + public static string Generate(string assemblyName, string modelName, ModelScheme modelScheme) + { + var @namespace = $"{assemblyName}.{modelName}"; + using var writer = new StringWriter(); + using var code = new IndentedTextWriter(writer, " "); + + code.Preamble(); + + code.WriteLine($"namespace {@namespace}"); + code.Block(() => + { + code.WriteLine("using CoreCraft.Core;"); + code.WriteLine("using CoreCraft.ChangesTracking;"); + code.WriteLine("using CoreCraft.Persistence;"); + code.WriteLine($"using {@namespace}.Entities;"); + code.EmptyLine(); + + new ModelShardGenerator(code).Generate(modelScheme.Shards); + }); + code.EmptyLine(); + + code.WriteLine($"namespace {@namespace}.Entities"); + code.Block(() => + { + new EntitiesGenerator(code).Generate(modelScheme.Shards); + }); + + return writer.ToString(); + } +} diff --git a/src/CoreCraft.Generators/ModelShardGenerator.cs b/src/CoreCraft.SourceGeneration/Generators/ModelShardGenerator.cs similarity index 82% rename from src/CoreCraft.Generators/ModelShardGenerator.cs rename to src/CoreCraft.SourceGeneration/Generators/ModelShardGenerator.cs index ca167a44..d44f2171 100644 --- a/src/CoreCraft.Generators/ModelShardGenerator.cs +++ b/src/CoreCraft.SourceGeneration/Generators/ModelShardGenerator.cs @@ -1,33 +1,35 @@ -namespace CoreCraft.Generators; +using CoreCraft.SourceGeneration.Extensions; -internal partial class ApplicationModelGenerator +namespace CoreCraft.SourceGeneration.Generators; + +internal sealed class ModelShardGenerator(IndentedTextWriter code) : GeneratorCommon { - public void GenerateModelShards(IndentedTextWriter code, IEnumerable shards) + public void Generate(IEnumerable shards) { foreach (var modelShard in shards) { - DefineModelShardInterface(code, modelShard, false); + DefineModelShardInterface(modelShard, false); code.EmptyLine(); - DefineModelShardInterface(code, modelShard, true); + DefineModelShardInterface(modelShard, true); code.EmptyLine(); - DefineModelShardInfoClass(code, modelShard); + DefineModelShardInfoClass(modelShard); code.EmptyLine(); - DefineModelShardClass(code, modelShard); + DefineModelShardClass(modelShard); code.EmptyLine(); - DefineModelShardClassAsReadOnlyState(code, modelShard); + DefineModelShardClassAsReadOnlyState(modelShard); code.EmptyLine(); - DefineModelShardClassAsIFeatureContext(code, modelShard); + DefineModelShardClassAsIFeatureContext(modelShard); code.EmptyLine(); - DefineChangesFrameInterface(code, modelShard); + DefineChangesFrameInterface(modelShard); code.EmptyLine(); - DefineChangesFrameClass(code, modelShard); + DefineChangesFrameClass(modelShard); code.EmptyLine(); - DefineMutableModelShardClass(code, modelShard); + DefineMutableModelShardClass(modelShard); code.EmptyLine(); } } - private void DefineModelShardInterface(IndentedTextWriter code, ModelShard modelShard, bool isMutable) + private void DefineModelShardInterface(ModelShard modelShard, bool isMutable) { var mutability = isMutable ? "Mutable" : string.Empty; @@ -48,7 +50,7 @@ private void DefineModelShardInterface(IndentedTextWriter code, ModelShard model }); } - private void DefineModelShardInfoClass(IndentedTextWriter code, ModelShard modelShard) + private void DefineModelShardInfoClass(ModelShard modelShard) { var visibility = GetInternalTypeVisibility(modelShard); @@ -72,21 +74,21 @@ private void DefineModelShardInfoClass(IndentedTextWriter code, ModelShard model }); } - private void DefineModelShardClass(IndentedTextWriter code, ModelShard modelShard) + private void DefineModelShardClass(ModelShard modelShard) { code.GeneratedClassAttributes(); code.Class(modelShard.Visibility, "sealed partial", $"{modelShard.Name}ModelShard", [$"I{modelShard.Name}ModelShard"], () => { - DefineCtor(code, modelShard); + DefineCtor(modelShard); code.EmptyLine(); - DefineConversionCtor(code, modelShard); + DefineConversionCtor(modelShard); code.EmptyLine(); - ImplementModelShardInterface(code, modelShard); + ImplementModelShardInterface(modelShard); code.EmptyLine(); - ImplementSaveMethod(code, modelShard); + ImplementSaveMethod(modelShard); }); - void DefineCtor(IndentedTextWriter code, ModelShard modelShard) + void DefineCtor(ModelShard modelShard) { code.WriteLine($"public {modelShard.Name}ModelShard()"); code.Block(() => @@ -116,7 +118,7 @@ void DefineCtor(IndentedTextWriter code, ModelShard modelShard) }); } - void DefineConversionCtor(IndentedTextWriter code, ModelShard modelShard) + void DefineConversionCtor(ModelShard modelShard) { code.WriteLine($"internal {modelShard.Name}ModelShard(IMutable{modelShard.Name}ModelShard mutable)"); code.Block(() => @@ -134,7 +136,7 @@ void DefineConversionCtor(IndentedTextWriter code, ModelShard modelShard) }); } - void ImplementModelShardInterface(IndentedTextWriter code, ModelShard modelShard) + void ImplementModelShardInterface(ModelShard modelShard) { foreach (var collection in modelShard.Collections) { @@ -148,7 +150,7 @@ void ImplementModelShardInterface(IndentedTextWriter code, ModelShard modelShard } } - void ImplementSaveMethod(IndentedTextWriter code, ModelShard modelShard) + void ImplementSaveMethod(ModelShard modelShard) { code.WriteLine("public void Save(IRepository repository)"); code.Block(() => @@ -167,7 +169,7 @@ void ImplementSaveMethod(IndentedTextWriter code, ModelShard modelShard) } } - private void DefineModelShardClassAsReadOnlyState(IndentedTextWriter code, ModelShard modelShard) + private void DefineModelShardClassAsReadOnlyState(ModelShard modelShard) { code.Class(modelShard.Visibility, "sealed partial", $"{modelShard.Name}ModelShard", [ @@ -224,7 +226,7 @@ private void DefineModelShardClassAsReadOnlyState(IndentedTextWriter code, Model }); } - private void DefineModelShardClassAsIFeatureContext(IndentedTextWriter code, ModelShard modelShard) + private void DefineModelShardClassAsIFeatureContext(ModelShard modelShard) { code.Class(modelShard.Visibility, "sealed partial", $"{modelShard.Name}ModelShard", ["IFeatureContext"], () => @@ -237,7 +239,7 @@ private void DefineModelShardClassAsIFeatureContext(IndentedTextWriter code, Mod }); } - private void DefineChangesFrameInterface(IndentedTextWriter code, ModelShard modelShard) + private void DefineChangesFrameInterface(ModelShard modelShard) { code.GeneratedInterfaceAttributes(); code.Interface($"I{modelShard.Name}ChangesFrame", ["IChangesFrame"], () => @@ -256,7 +258,7 @@ private void DefineChangesFrameInterface(IndentedTextWriter code, ModelShard mod }); } - private void DefineChangesFrameClass(IndentedTextWriter code, ModelShard modelShard) + private void DefineChangesFrameClass(ModelShard modelShard) { var visibility = GetInternalTypeVisibility(modelShard); @@ -267,25 +269,25 @@ private void DefineChangesFrameClass(IndentedTextWriter code, ModelShard modelSh ], () => { - DefineCtor(code, modelShard); + DefineCtor(modelShard); code.EmptyLine(); - ImplementModelShardChangesFrameInterface(code, modelShard); + ImplementModelShardChangesFrameInterface(modelShard); code.EmptyLine(); - DefineGetMethod(code, modelShard); + DefineGetMethod(modelShard); code.EmptyLine(); - DefineInvertMethod(code, modelShard); + DefineInvertMethod(modelShard); code.EmptyLine(); - DefineApplyMethod(code, modelShard); + DefineApplyMethod(modelShard); code.EmptyLine(); - ImplementChangesFrameInterface(code, modelShard); + ImplementChangesFrameInterface(modelShard); code.EmptyLine(); - DefineMergeMethod(code, modelShard); + DefineMergeMethod(modelShard); code.EmptyLine(); - ImplementSaveMethod(code, modelShard); + ImplementSaveMethod(modelShard); code.EmptyLine(); }); - void DefineCtor(IndentedTextWriter code, ModelShard modelShard) + void DefineCtor(ModelShard modelShard) { code.WriteLine($"public {modelShard.Name}ChangesFrame()"); code.Block(() => @@ -303,7 +305,7 @@ void DefineCtor(IndentedTextWriter code, ModelShard modelShard) }); } - void ImplementModelShardChangesFrameInterface(IndentedTextWriter code, ModelShard modelShard) + void ImplementModelShardChangesFrameInterface(ModelShard modelShard) { foreach (var collection in modelShard.Collections) { @@ -317,7 +319,7 @@ void ImplementModelShardChangesFrameInterface(IndentedTextWriter code, ModelShar } } - void DefineGetMethod(IndentedTextWriter code, ModelShard modelShard) + void DefineGetMethod(ModelShard modelShard) { code.WriteLine("ICollectionChangeSet? IChangesFrame.Get(ICollection collection)"); code.Block(() => @@ -345,7 +347,7 @@ void DefineGetMethod(IndentedTextWriter code, ModelShard modelShard) }); } - void DefineInvertMethod(IndentedTextWriter code, ModelShard modelShard) + void DefineInvertMethod(ModelShard modelShard) { code.WriteLine($"IChangesFrame IChangesFrame.Invert()"); code.Block(() => @@ -367,7 +369,7 @@ void DefineInvertMethod(IndentedTextWriter code, ModelShard modelShard) }); } - void DefineApplyMethod(IndentedTextWriter code, ModelShard modelShard) + void DefineApplyMethod(ModelShard modelShard) { code.WriteLine($"public void Apply(IModel model)"); code.Block(() => @@ -385,7 +387,7 @@ void DefineApplyMethod(IndentedTextWriter code, ModelShard modelShard) }); } - void ImplementChangesFrameInterface(IndentedTextWriter code, ModelShard modelShard) + void ImplementChangesFrameInterface(ModelShard modelShard) { code.WriteLine($"public bool HasChanges()"); code.Block(() => @@ -397,7 +399,7 @@ void ImplementChangesFrameInterface(IndentedTextWriter code, ModelShard modelSha }); } - void DefineMergeMethod(IndentedTextWriter code, ModelShard modelShard) + void DefineMergeMethod(ModelShard modelShard) { code.WriteLine($"public IChangesFrame Merge(IChangesFrame frame)"); code.Block(() => @@ -422,7 +424,7 @@ void DefineMergeMethod(IndentedTextWriter code, ModelShard modelShard) }); } - void ImplementSaveMethod(IndentedTextWriter code, ModelShard modelShard) + void ImplementSaveMethod(ModelShard modelShard) { code.WriteLine($"public void Save(IRepository repository)"); code.Block(() => @@ -441,7 +443,7 @@ void ImplementSaveMethod(IndentedTextWriter code, ModelShard modelShard) } } - private void DefineMutableModelShardClass(IndentedTextWriter code, ModelShard modelShard) + private void DefineMutableModelShardClass(ModelShard modelShard) { var visibility = GetInternalTypeVisibility(modelShard); @@ -453,23 +455,23 @@ private void DefineMutableModelShardClass(IndentedTextWriter code, ModelShard mo ], () => { - DefineManualLoadRequiredProperty(code, modelShard); + DefineManualLoadRequiredProperty(modelShard); code.EmptyLine(); - ImplementModelShardInterface(code, modelShard); + ImplementModelShardInterface(modelShard); code.EmptyLine(); - ImplementMutableStateInterface(code, modelShard); + ImplementMutableStateInterface(modelShard); code.EmptyLine(); - ImplementLoadMethod(code, modelShard); + ImplementLoadMethod(modelShard); code.EmptyLine(); - ImplementSaveMethod(code, modelShard); + ImplementSaveMethod(modelShard); }); - void DefineManualLoadRequiredProperty(IndentedTextWriter code, ModelShard modelShard) + void DefineManualLoadRequiredProperty(ModelShard modelShard) { code.WriteLine($"public bool ManualLoadRequired => {modelShard.LoadManually.ToString().ToLowerInvariant()};"); } - void ImplementModelShardInterface(IndentedTextWriter code, ModelShard modelShard) + void ImplementModelShardInterface(ModelShard modelShard) { foreach (var collection in modelShard.Collections) { @@ -483,7 +485,7 @@ void ImplementModelShardInterface(IndentedTextWriter code, ModelShard modelShard } } - void ImplementMutableStateInterface(IndentedTextWriter code, ModelShard modelShard) + void ImplementMutableStateInterface(ModelShard modelShard) { code.WriteLine($"public I{modelShard.Name}ModelShard AsReadOnly()"); code.Block(() => @@ -492,7 +494,7 @@ void ImplementMutableStateInterface(IndentedTextWriter code, ModelShard modelSha }); } - void ImplementLoadMethod(IndentedTextWriter code, ModelShard modelShard) + void ImplementLoadMethod(ModelShard modelShard) { code.WriteLine("public void Load(IRepository repository, bool force = false)"); code.Block(() => @@ -524,7 +526,7 @@ void ImplementLoadMethod(IndentedTextWriter code, ModelShard modelShard) }); } - void ImplementSaveMethod(IndentedTextWriter code, ModelShard modelShard) + void ImplementSaveMethod(ModelShard modelShard) { code.WriteLine("public void Save(IRepository repository)"); code.Block(() => diff --git a/src/CoreCraft.Generators/ModelScheme.cs b/src/CoreCraft.SourceGeneration/ModelScheme.cs similarity index 97% rename from src/CoreCraft.Generators/ModelScheme.cs rename to src/CoreCraft.SourceGeneration/ModelScheme.cs index 5a4f9abc..6df38113 100644 --- a/src/CoreCraft.Generators/ModelScheme.cs +++ b/src/CoreCraft.SourceGeneration/ModelScheme.cs @@ -1,4 +1,4 @@ -namespace CoreCraft.Generators; +namespace CoreCraft.SourceGeneration; internal sealed record ModelScheme(IEnumerable Shards); diff --git a/src/CoreCraft.Generators/Serialization/DtoConverter.cs b/src/CoreCraft.SourceGeneration/Serialization/DtoConverter.cs similarity index 98% rename from src/CoreCraft.Generators/Serialization/DtoConverter.cs rename to src/CoreCraft.SourceGeneration/Serialization/DtoConverter.cs index e7cd54fa..b42ad717 100644 --- a/src/CoreCraft.Generators/Serialization/DtoConverter.cs +++ b/src/CoreCraft.SourceGeneration/Serialization/DtoConverter.cs @@ -1,4 +1,4 @@ -namespace CoreCraft.Generators.Serialization; +namespace CoreCraft.SourceGeneration.Serialization; internal class DtoConverter { diff --git a/src/CoreCraft.Generators/Serialization/ModelSchemeDto.cs b/src/CoreCraft.SourceGeneration/Serialization/ModelSchemeDto.cs similarity index 95% rename from src/CoreCraft.Generators/Serialization/ModelSchemeDto.cs rename to src/CoreCraft.SourceGeneration/Serialization/ModelSchemeDto.cs index b449855a..6274d31c 100644 --- a/src/CoreCraft.Generators/Serialization/ModelSchemeDto.cs +++ b/src/CoreCraft.SourceGeneration/Serialization/ModelSchemeDto.cs @@ -1,4 +1,4 @@ -namespace CoreCraft.Generators.Serialization; +namespace CoreCraft.SourceGeneration.Serialization; internal sealed record ModelSchemeDto(IEnumerable Shards); diff --git a/src/CoreCraft.SourceGeneration/Serialization/ModelSchemeReader.cs b/src/CoreCraft.SourceGeneration/Serialization/ModelSchemeReader.cs new file mode 100644 index 00000000..7c1bef8b --- /dev/null +++ b/src/CoreCraft.SourceGeneration/Serialization/ModelSchemeReader.cs @@ -0,0 +1,14 @@ +using Newtonsoft.Json; + +namespace CoreCraft.SourceGeneration.Serialization; + +internal static class ModelSchemeReader +{ + public static ModelScheme Read(JsonSerializer serializer, string content) + { + using var stringStream = new StringReader(content); + using var reader = new JsonTextReader(stringStream); + + return DtoConverter.Convert(serializer.Deserialize(reader)); + } +} diff --git a/src/CoreCraft.Storage.Json/CoreCraft.Storage.Json.AssemblyInfo.cs b/src/CoreCraft.Storage.Json/CoreCraft.Storage.Json.AssemblyInfo.cs deleted file mode 100644 index ead09e2b..00000000 --- a/src/CoreCraft.Storage.Json/CoreCraft.Storage.Json.AssemblyInfo.cs +++ /dev/null @@ -1,4 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] -[assembly: InternalsVisibleTo("CoreCraft.Storage.Json.Tests")] diff --git a/src/CoreCraft.Storage.Json/CoreCraft.Storage.Json.csproj b/src/CoreCraft.Storage.Json/CoreCraft.Storage.Json.csproj index c805285e..9d23a563 100644 --- a/src/CoreCraft.Storage.Json/CoreCraft.Storage.Json.csproj +++ b/src/CoreCraft.Storage.Json/CoreCraft.Storage.Json.csproj @@ -21,4 +21,9 @@ + + + + + diff --git a/src/CoreCraft.Storage.Sqlite/CoreCraft.Storage.Sqlite.AssemblyInfo.cs b/src/CoreCraft.Storage.Sqlite/CoreCraft.Storage.Sqlite.AssemblyInfo.cs deleted file mode 100644 index 30111186..00000000 --- a/src/CoreCraft.Storage.Sqlite/CoreCraft.Storage.Sqlite.AssemblyInfo.cs +++ /dev/null @@ -1,4 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] -[assembly: InternalsVisibleTo("CoreCraft.Storage.Sqlite.Tests")] diff --git a/src/CoreCraft.Storage.Sqlite/CoreCraft.Storage.Sqlite.csproj b/src/CoreCraft.Storage.Sqlite/CoreCraft.Storage.Sqlite.csproj index 10b50dcf..7954ad18 100644 --- a/src/CoreCraft.Storage.Sqlite/CoreCraft.Storage.Sqlite.csproj +++ b/src/CoreCraft.Storage.Sqlite/CoreCraft.Storage.Sqlite.csproj @@ -21,4 +21,9 @@ + + + + + diff --git a/src/CoreCraft.sln b/src/CoreCraft.sln index 1a7ffb74..31e61c64 100644 --- a/src/CoreCraft.sln +++ b/src/CoreCraft.sln @@ -40,6 +40,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreCraft.Storage.Json", "C EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreCraft.Storage.Json.Tests", "Tests\CoreCraft.Storage.Json.Tests\CoreCraft.Storage.Json.Tests.csproj", "{0A3247DC-5E51-4DE4-B25B-C9A016B5EE48}" EndProject +Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "CoreCraft.SourceGeneration", "CoreCraft.SourceGeneration\CoreCraft.SourceGeneration.shproj", "{B11D3E00-59B5-4227-AB86-2F20E5FA330B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -118,4 +120,8 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9E61BBAA-E73F-4733-86CA-24FCF8402964} EndGlobalSection + GlobalSection(SharedMSBuildProjectFiles) = preSolution + CoreCraft.SourceGeneration\CoreCraft.SourceGeneration.projitems*{b11d3e00-59b5-4227-ab86-2f20e5fa330b}*SharedItemsImports = 13 + CoreCraft.SourceGeneration\CoreCraft.SourceGeneration.projitems*{bf7c92e9-5688-4bca-961f-511b81d2eff1}*SharedItemsImports = 5 + EndGlobalSection EndGlobal diff --git a/src/CoreCraft/CoreCraft.AssemblyInfo.cs b/src/CoreCraft/CoreCraft.AssemblyInfo.cs deleted file mode 100644 index a10c751a..00000000 --- a/src/CoreCraft/CoreCraft.AssemblyInfo.cs +++ /dev/null @@ -1,6 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] -[assembly: InternalsVisibleTo("CoreCraft.Tests")] -[assembly: InternalsVisibleTo("CoreCraft.Storage.Sqlite.Tests")] -[assembly: InternalsVisibleTo("CoreCraft.Storage.Json.Tests")] diff --git a/src/CoreCraft/CoreCraft.csproj b/src/CoreCraft/CoreCraft.csproj index 5f05b97d..3b98d24c 100644 --- a/src/CoreCraft/CoreCraft.csproj +++ b/src/CoreCraft/CoreCraft.csproj @@ -18,4 +18,11 @@ + + + + + + +