|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="UmlCoreJsonDtoSerializerGenerator.cs" company="Starion Group S.A."> |
| 3 | +// |
| 4 | +// Copyright 2022-2025 Starion Group S.A. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// </copyright> |
| 19 | +// ------------------------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +namespace SysML2.NET.CodeGenerator.Generators.UmlHandleBarsGenerators |
| 22 | +{ |
| 23 | + using System; |
| 24 | + using System.IO; |
| 25 | + using System.Linq; |
| 26 | + using System.Threading.Tasks; |
| 27 | + |
| 28 | + using uml4net.Extensions; |
| 29 | + using uml4net.HandleBars; |
| 30 | + using uml4net.StructuredClassifiers; |
| 31 | + using uml4net.xmi.Readers; |
| 32 | + |
| 33 | + using NamedElementHelper = SysML2.NET.CodeGenerator.HandleBarHelpers.NamedElementHelper; |
| 34 | + using PropertyHelper = SysML2.NET.CodeGenerator.HandleBarHelpers.PropertyHelper; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// A UML Handlebars based DTO Json Serializer code generator |
| 38 | + /// </summary> |
| 39 | + public class UmlCoreJsonDtoSerializerGenerator : UmlHandleBarsGenerator |
| 40 | + { |
| 41 | + /// <summary> |
| 42 | + /// Gets the name of the template used to generate DTO Json Serializaer |
| 43 | + /// </summary> |
| 44 | + private const string DtoSerializerTemplateName = "core-json-dto-serializer-uml-template"; |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets the name of the template used to generate Json Serializer provider |
| 48 | + /// </summary> |
| 49 | + private const string DtoSerializerProviderTemplateName = "core-json-dto-serialization-provider-uml-template"; |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Generates code specific to the concrete implementation |
| 53 | + /// </summary> |
| 54 | + /// <param name="xmiReaderResult"> |
| 55 | + /// the <see cref="XmiReaderResult" /> that contains the UML model to generate from |
| 56 | + /// </param> |
| 57 | + /// <param name="outputDirectory"> |
| 58 | + /// The target <see cref="DirectoryInfo" /> |
| 59 | + /// </param> |
| 60 | + /// <returns> |
| 61 | + /// an awaitable <see cref="Task" /> |
| 62 | + /// </returns> |
| 63 | + public override async Task GenerateAsync(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory) |
| 64 | + { |
| 65 | + await this.GenerateDtoJsonSerializer(xmiReaderResult, outputDirectory); |
| 66 | + await this.GenerateSerializationProvider(xmiReaderResult, outputDirectory); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Register the custom helpers |
| 71 | + /// </summary> |
| 72 | + protected override void RegisterHelpers() |
| 73 | + { |
| 74 | + this.Handlebars.RegisterStringHelper(); |
| 75 | + this.Handlebars.RegisterClassHelper(); |
| 76 | + this.Handlebars.RegisterPropertyHelper(); |
| 77 | + |
| 78 | + NamedElementHelper.RegisterNamedElementHelper(this.Handlebars); |
| 79 | + PropertyHelper.RegisterPropertyHelper(this.Handlebars); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Register the code templates |
| 84 | + /// </summary> |
| 85 | + protected override void RegisterTemplates() |
| 86 | + { |
| 87 | + this.RegisterTemplate(DtoSerializerTemplateName); |
| 88 | + this.RegisterTemplate(DtoSerializerProviderTemplateName); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Generates the Serialization Provider class |
| 93 | + /// </summary> |
| 94 | + /// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param> |
| 95 | + /// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param> |
| 96 | + /// <returns> |
| 97 | + /// an awaitable <see cref="Task" /> |
| 98 | + /// </returns> |
| 99 | + /// <exception cref="ArgumentNullException"> |
| 100 | + /// In case of null value for <paramref name="xmiReaderResult" /> or |
| 101 | + /// <paramref name="outputDirectory" /> |
| 102 | + /// </exception> |
| 103 | + private Task GenerateSerializationProvider(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory) |
| 104 | + { |
| 105 | + ArgumentNullException.ThrowIfNull(xmiReaderResult); |
| 106 | + ArgumentNullException.ThrowIfNull(outputDirectory); |
| 107 | + |
| 108 | + return this.GenerateSerializationProviderInternal(xmiReaderResult, outputDirectory); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Generates the Serialization Provider class |
| 113 | + /// </summary> |
| 114 | + /// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param> |
| 115 | + /// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param> |
| 116 | + /// <returns> |
| 117 | + /// an awaitable <see cref="Task" /> |
| 118 | + /// </returns> |
| 119 | + private async Task GenerateSerializationProviderInternal(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory) |
| 120 | + { |
| 121 | + var template = this.Templates[DtoSerializerProviderTemplateName]; |
| 122 | + |
| 123 | + var classes = xmiReaderResult.Root.QueryPackages() |
| 124 | + .SelectMany(x => x.PackagedElement.OfType<IClass>()) |
| 125 | + .Where(x => !x.IsAbstract) |
| 126 | + .OrderBy(x => x.Name) |
| 127 | + .ToList(); |
| 128 | + |
| 129 | + var generatedSerializationProvider = template(classes); |
| 130 | + |
| 131 | + generatedSerializationProvider = this.CodeCleanup(generatedSerializationProvider); |
| 132 | + |
| 133 | + const string fileName = "SerializationProvider.cs"; |
| 134 | + |
| 135 | + await Write(generatedSerializationProvider, outputDirectory, fileName); |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Generates DTO Json Serializer files |
| 140 | + /// </summary> |
| 141 | + /// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param> |
| 142 | + /// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param> |
| 143 | + /// <returns> |
| 144 | + /// an awaitable <see cref="Task" /> |
| 145 | + /// </returns> |
| 146 | + /// <exception cref="ArgumentNullException"> |
| 147 | + /// In case of null value for <paramref name="xmiReaderResult" /> or |
| 148 | + /// <paramref name="outputDirectory" /> |
| 149 | + /// </exception> |
| 150 | + private Task GenerateDtoJsonSerializer(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory) |
| 151 | + { |
| 152 | + ArgumentNullException.ThrowIfNull(xmiReaderResult); |
| 153 | + ArgumentNullException.ThrowIfNull(outputDirectory); |
| 154 | + |
| 155 | + return this.GenerateDtoJsonSerializerInternal(xmiReaderResult, outputDirectory); |
| 156 | + } |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Generates DTO Json Serializer files |
| 160 | + /// </summary> |
| 161 | + /// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param> |
| 162 | + /// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param> |
| 163 | + /// <returns> |
| 164 | + /// an awaitable <see cref="Task" /> |
| 165 | + /// </returns> |
| 166 | + private async Task GenerateDtoJsonSerializerInternal(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory) |
| 167 | + { |
| 168 | + var template = this.Templates[DtoSerializerTemplateName]; |
| 169 | + |
| 170 | + var classes = xmiReaderResult.Root.QueryPackages() |
| 171 | + .SelectMany(x => x.PackagedElement.OfType<IClass>()) |
| 172 | + .Where(x => !x.IsAbstract) |
| 173 | + .ToList(); |
| 174 | + |
| 175 | + foreach (var @class in classes) |
| 176 | + { |
| 177 | + var generatedInterface = template(@class); |
| 178 | + |
| 179 | + generatedInterface = this.CodeCleanup(generatedInterface); |
| 180 | + |
| 181 | + var fileName = $"{@class.Name.CapitalizeFirstLetter()}Serializer.cs"; |
| 182 | + |
| 183 | + await WriteAsync(generatedInterface, outputDirectory, fileName); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments