private static string GenerateMessages(File file) { if (!file.Messages.Any()) { return(null); } var globalNs = new CodeNamespace(); globalNs.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); var unit = new CodeCompileUnit(); unit.Namespaces.Add(globalNs); var ns = new CodeNamespace($"{file.Namespace}.Messages"); if (file.Entities.Any()) { ns.Imports.Add(new CodeNamespaceImport($"{file.Namespace}.Entities")); } unit.Namespaces.Add(ns); foreach (var message in file.Messages) { ns.Types.Add(GenerateMessage(message)); } return(GenerateCode(unit)); }
public static OutputFiles GenerateSource(this File file) { return(new OutputFiles { Markers = GenerateMarkers(file), Messages = GenerateMessages(file), StateDefinitions = GenerateStateHandlers(file), Enumerations = GenerateEnumerations(file), Entities = GenerateEntities(file) }); }
private static string GenerateMarkers(File file) { if (!file.MarkerInterfaces.Any()) { return(null); } var globalNs = new CodeNamespace(); globalNs.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); var unit = new CodeCompileUnit(); unit.Namespaces.Add(globalNs); var ns = new CodeNamespace($"{file.Namespace}.Messages"); ns.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); unit.Namespaces.Add(ns); foreach (var marker in file.MarkerInterfaces) { var type = new CodeTypeDeclaration(marker.Name) { TypeAttributes = TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.Interface, Attributes = MemberAttributes.Public }; foreach (var property in marker.Properties) { type.Members.Add(new CodeMemberProperty { Name = property.Name.PascalCased(), HasGet = true, HasSet = false, Type = new CodeTypeReference(property.Type.SafeTypeName()) }); } ns.Types.Add(type); } return(GenerateCode(unit)); }
private static string GenerateEntities(File file) { if (!file.Entities.Any()) return null; var unit = new CodeCompileUnit(); var ns = new CodeNamespace($"{file.Namespace}.Entities"); var globalNs = new CodeNamespace(); globalNs.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); unit.Namespaces.Add(globalNs); unit.Namespaces.Add(ns); foreach (var entity in file.Entities) { ns.Types.Add(GenerateEntity(entity)); } return GenerateCode(unit); }
private static string GenerateEnumerations(File file) { if (!file.Enums.Any()) { return(null); } var globalNs = new CodeNamespace(); globalNs.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); var unit = new CodeCompileUnit(); var ns = new CodeNamespace($"{file.Namespace}.Messages"); unit.Namespaces.Add(globalNs); unit.Namespaces.Add(ns); foreach (var enumeration in file.Enums) { var type = new CodeTypeDeclaration(enumeration.Name) { IsEnum = true }; var fields = enumeration.Values.Select(x => new CodeMemberField(enumeration.Name, x.Name) { InitExpression = new CodePrimitiveExpression(x.Value) }).ToArray(); type.Members.AddRange(fields); ns.Types.Add(type); } return(GenerateCode(unit)); }
private static string GenerateMarkers(File file) { if (!file.MarkerInterfaces.Any()) return null; var globalNs = new CodeNamespace(); globalNs.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); var unit = new CodeCompileUnit(); unit.Namespaces.Add(globalNs); var ns = new CodeNamespace($"{file.Namespace}.Messages"); ns.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); unit.Namespaces.Add(ns); foreach (var marker in file.MarkerInterfaces) { var type = new CodeTypeDeclaration(marker.Name) { TypeAttributes = TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.Interface, Attributes = MemberAttributes.Public }; foreach (var property in marker.Properties) { type.Members.Add(new CodeMemberProperty { Name = property.Name.PascalCased(), HasGet = true, HasSet = false, Type = new CodeTypeReference(property.Type.SafeTypeName()) }); } ns.Types.Add(type); } return GenerateCode(unit); }
private static string GenerateEnumerations(File file) { if (!file.Enums.Any()) return null; var globalNs = new CodeNamespace(); globalNs.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); var unit = new CodeCompileUnit(); var ns = new CodeNamespace($"{file.Namespace}.Messages"); unit.Namespaces.Add(globalNs); unit.Namespaces.Add(ns); foreach (var enumeration in file.Enums) { var type = new CodeTypeDeclaration(enumeration.Name) { IsEnum = true }; var fields = enumeration.Values.Select(x => new CodeMemberField(enumeration.Name, x.Name) { InitExpression = new CodePrimitiveExpression(x.Value) }).ToArray(); type.Members.AddRange(fields); ns.Types.Add(type); } return GenerateCode(unit); }
private static string GenerateStateHandlers(File file) { if (!file.StateDefinitions.Any()) return null; var unit = new CodeCompileUnit(); var ns = new CodeNamespace($"{file.Namespace}.State"); var globalNs = new CodeNamespace(); globalNs.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); unit.Namespaces.Add(globalNs); ns.Imports.Add(new CodeNamespaceImport($"{file.Namespace}.Messages")); unit.Namespaces.Add(ns); foreach (var definition in file.StateDefinitions) { var type = new CodeTypeDeclaration($"{definition.Name}Subscription") { TypeAttributes = TypeAttributes.Abstract | TypeAttributes.Public, IsClass = true, BaseTypes = {new CodeTypeReference("IStateHandler")} }; CodeTypeDeclaration iface = null; if (definition.GenerateInterface) { iface = new CodeTypeDeclaration(definition.Interface) { TypeAttributes = TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.Interface }; type.BaseTypes.Add(iface.Name); ns.Types.Add(iface); } var receive = new CodeMemberMethod { Name = "Receive", Attributes = MemberAttributes.Public, Parameters = { new CodeParameterDeclarationExpression(typeof (object), "message") }, ReturnType = new CodeTypeReference(typeof (bool)) }; var matcher = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("message"), "Match"); var invoker = definition.Receives.Aggregate(matcher, (current, messageType) => new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(current, "With", new CodeTypeReference(messageType)), new CodeMethodReferenceExpression { MethodName = "When" })); receive.Statements.Add( new CodeMethodReturnStatement(new CodePropertyReferenceExpression(invoker, "WasHandled"))); type.Members.Add(receive); var whens = definition.Receives.Select(messageType => new CodeMemberMethod { Name = "When", Attributes = iface != null ? MemberAttributes.Public | MemberAttributes.Abstract : MemberAttributes.Family | MemberAttributes.Abstract, Parameters = { new CodeParameterDeclarationExpression(messageType, "message") } }).ToArray(); iface?.Members.AddRange(whens); type.Members.AddRange(whens); ns.Types.Add(type); } return GenerateCode(unit); }
private static string GenerateStateHandlers(File file) { if (!file.StateDefinitions.Any()) { return(null); } var unit = new CodeCompileUnit(); var ns = new CodeNamespace($"{file.Namespace}.State"); var globalNs = new CodeNamespace(); globalNs.Imports.AddRange(file.Usings.OrderBy(x => x).Select(x => new CodeNamespaceImport(x)).ToArray()); unit.Namespaces.Add(globalNs); ns.Imports.Add(new CodeNamespaceImport($"{file.Namespace}.Messages")); unit.Namespaces.Add(ns); foreach (var definition in file.StateDefinitions) { var type = new CodeTypeDeclaration($"{definition.Name}Subscription") { TypeAttributes = TypeAttributes.Abstract | TypeAttributes.Public, IsClass = true, BaseTypes = { new CodeTypeReference("IStateHandler") } }; CodeTypeDeclaration iface = null; if (definition.GenerateInterface) { iface = new CodeTypeDeclaration(definition.Interface) { TypeAttributes = TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.Interface }; type.BaseTypes.Add(iface.Name); ns.Types.Add(iface); } var receive = new CodeMemberMethod { Name = "Receive", Attributes = MemberAttributes.Public, Parameters = { new CodeParameterDeclarationExpression(typeof(object), "message") }, ReturnType = new CodeTypeReference(typeof(bool)) }; var matcher = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("message"), "Match"); var invoker = definition.Receives.Aggregate(matcher, (current, messageType) => new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(current, "With", new CodeTypeReference(messageType)), new CodeMethodReferenceExpression { MethodName = "When" })); receive.Statements.Add( new CodeMethodReturnStatement(new CodePropertyReferenceExpression(invoker, "WasHandled"))); type.Members.Add(receive); var whens = definition.Receives.Select(messageType => new CodeMemberMethod { Name = "When", Attributes = iface != null ? MemberAttributes.Public | MemberAttributes.Abstract : MemberAttributes.Family | MemberAttributes.Abstract, Parameters = { new CodeParameterDeclarationExpression(messageType, "message") } }).ToArray(); iface?.Members.AddRange(whens); type.Members.AddRange(whens); ns.Types.Add(type); } return(GenerateCode(unit)); }