Example #1
0
        private static void UpdateLocalisedString(JournalClass journalClass, List <string> errors)
        {
            var localisedProperties =
                journalClass.Properties.Where(p => p.Name.EndsWith("_Localised", StringComparison.OrdinalIgnoreCase));

            foreach (var localisedProperty in localisedProperties)
            {
                var name = localisedProperty.Name.Substring(
                    0,
                    localisedProperty.Name.IndexOf("_", StringComparison.Ordinal));
                var match = journalClass.Properties.SingleOrDefault(p => p.Name == name);
                if (match == null)
                {
                    errors.Add($"{journalClass.Name} has orphan localised property {localisedProperty.Name}");
                    continue;
                }

                match.Name            += "Id";
                localisedProperty.Name = name;
            }
        }
Example #2
0
        private static JournalClass GetOrCreateJournalClass(
            Dictionary <string, JournalClass> generatedClasses,
            string className,
            string type      = null,
            string eventName = null,
            bool timestamp   = false)
        {
            if (!generatedClasses.ContainsKey(className))
            {
                var classFilePath = Path.Combine(GetClassFolderPath(), $"{className}.cs");
                var journalClass  = new JournalClass
                {
                    FilePath  = classFilePath,
                    Name      = className,
                    Type      = type,
                    Event     = eventName,
                    Timestamp = timestamp
                };
                generatedClasses.Add(className, journalClass);
            }

            return(generatedClasses[className]);
        }
Example #3
0
        private static void UpdateProperties(
            Dictionary <string, JournalClass> generatedClasses,
            List <PropertyEnumConnection> propertyEnumConnections,
            JournalClass journalClass,
            string eventName,
            List <ManualParameter> manualParameters,
            JObject source)
        {
            foreach (var part in source)
            {
                var jsonName = part.Key;
                var value    = part.Value;

                if (jsonName == "event" || jsonName == "timestamp")
                {
                    continue;
                }

                var             name = jsonName.ToPascalCase();
                string          type;
                JournalProperty enumAccessor = null;
                if (value.Type == JTokenType.Array)
                {
                    var array = (JArray)value;
                    if (array.First == null)
                    {
                        continue;
                    }

                    if (array.First.Type == JTokenType.Object)
                    {
                        var arrayType = $"{eventName}{jsonName.TrimEnd('s')}";
                        GeneratePartTypeClass(
                            generatedClasses,
                            propertyEnumConnections,
                            arrayType,
                            eventName,
                            manualParameters,
                            array);
                        type = $"List<{arrayType}>";
                    }
                    else
                    {
                        var arrayType = GetPartTypeName(array.First.Type);
                        type = $"List<{arrayType}>";
                    }

                    name = name + "List";
                }
                else
                {
                    type = GetPartTypeName(value.Type);

                    var propertyEnumConnection =
                        propertyEnumConnections.FirstOrDefault(p => p.EventName == eventName && p.PropertyName == name);
                    if (propertyEnumConnection != null)
                    {
                        enumAccessor =
                            new JournalProperty
                        {
                            Type      = propertyEnumConnection.EnumName,
                            Name      = name,
                            Accessors = $@"=> {name}Raw.GetEnumValue<{
                                            propertyEnumConnection.EnumName
                                        }>();"
                        };
                        name += "Raw";
                        propertyEnumConnection.IsUsed = true;
                    }
                }

                var existingProperty = journalClass.Properties.FirstOrDefault(p => p.Name == name);
                if (existingProperty != null)
                {
                    if (existingProperty.Type == type)
                    {
                        continue;
                    }

                    existingProperty.JsonName = jsonName;
                    existingProperty.Type     = type;
                    continue;
                }

                var manualParameter = manualParameters.FirstOrDefault(
                    p => p.Name.Equals(jsonName, StringComparison.OrdinalIgnoreCase));
                var description = manualParameter?.Description?.Replace("\"", "\\\"").Replace("\r", string.Empty)
                                  .Replace("\n", " - ").Trim();

                var journalProperty = new JournalProperty
                {
                    JsonName    = jsonName,
                    Description = description,
                    Type        = type,
                    Name        = name,
                    Accessors   = "{ get; internal set; }"
                };
                journalClass.Properties.Add(journalProperty);

                if (enumAccessor != null)
                {
                    enumAccessor.Description = description;
                    journalClass.Properties.Add(enumAccessor);
                }
            }
        }
Example #4
0
        private static void OutputClass(List <string> enumClassNames, JournalClass journalClass)
        {
            var typeText = journalClass.Type != null ? $" : {journalClass.Type}" : string.Empty;

            var classBuilder = new StringBuilder();

            classBuilder.Append(GetHeader(journalClass.Name));
            classBuilder.AppendLine("namespace EdNetApi.Journal.JournalEntries");
            classBuilder.AppendLine("{");

            var systemAdded = false;

            if (journalClass.Timestamp || journalClass.Properties.Any(p => p.Type.Contains("DateTime")))
            {
                classBuilder.AppendLine("    using System;");
                systemAdded = true;
            }

            if (journalClass.Properties.Any(p => p.Type.Contains("List<")))
            {
                classBuilder.AppendLine("    using System.Collections.Generic;");
                systemAdded = true;
            }

            classBuilder.AppendLine("    using System.ComponentModel;");

            var enumAdded = false;

            if (journalClass.Properties.Any(p => enumClassNames.Contains(p.Type)))
            {
                if (systemAdded)
                {
                    classBuilder.AppendLine();
                }

                classBuilder.AppendLine("    using EdNetApi.Common;");
                classBuilder.AppendLine("    using EdNetApi.Journal.Enums;");
                enumAdded = true;
            }

            if (systemAdded || enumAdded)
            {
                classBuilder.AppendLine();
            }

            classBuilder.AppendLine("    using Newtonsoft.Json;");
            classBuilder.AppendLine();
            classBuilder.AppendLine($"    public class {journalClass.Name}{typeText}");
            classBuilder.AppendLine("    {");

            if (journalClass.Event != null)
            {
                classBuilder.AppendLine(
                    $"        public const JournalEventType EventConst = JournalEventType.{journalClass.Event};");
                classBuilder.AppendLine();
            }

            classBuilder.AppendLine($"        internal {journalClass.Name}()");
            classBuilder.AppendLine("        {");
            classBuilder.AppendLine("        }");

            if (journalClass.Event != null)
            {
                classBuilder.AppendLine();
                classBuilder.AppendLine("        [JsonIgnore]");
                classBuilder.AppendLine("        public override JournalEventType Event => EventConst;");
            }

            if (journalClass.Timestamp)
            {
                classBuilder.AppendLine();
                classBuilder.AppendLine(@"        [JsonProperty(""timestamp"")]");
                classBuilder.AppendLine("        public override DateTime Timestamp { get; internal set; }");
            }

            foreach (var journalClassProperty in journalClass.Properties)
            {
                classBuilder.AppendLine();
                if (journalClassProperty.JsonName != null)
                {
                    classBuilder.AppendLine($@"        [JsonProperty(""{journalClassProperty.JsonName}"")]");
                }
                else
                {
                    classBuilder.AppendLine("        [JsonIgnore]");
                }

                classBuilder.AppendLine($@"        [Description(""{journalClassProperty.Description}"")]");
                classBuilder.AppendLine(
                    $"        public {journalClassProperty.Type} {journalClassProperty.Name} {journalClassProperty.Accessors}");
            }

            classBuilder.AppendLine("    }");
            classBuilder.Append("}");

            File.WriteAllText(journalClass.FilePath, classBuilder.ToString());
        }