public void GenerateModel(JObject modelData) { var modelName = modelData["rdfs:label"]?.ToObject <string>(); if (String.IsNullOrEmpty(modelName) || TypeBlacklist.Contains(modelName.ToLower())) { return; } modelName = CaptalizeFirstLetter(modelName); Console.Out.WriteLine($"Creating models for {modelName}"); var schemaSourceUrl = modelData["@id"]?.ToObject <string>(); var modelDescription = CleanDescription(modelData?["rdfs:comment"]?.ToObject <string>()); var parentName = GetNameFromId(DataModel.GetItemParentFor(modelData)); if (String.IsNullOrWhiteSpace(parentName)) { parentName = "SchemaModelBase"; } else if (IsGeneratedEnum(parentName)) { parentName = $"TypeEquivalent<{parentName}>"; } var interfaceProperties = DataModel.GetModelPropertiesFor(modelData); Console.Out.WriteLine($" found {interfaceProperties.Count()} properties"); Console.Out.WriteLine($" creating class"); using (var classFile = new StreamWriter(File.Create($"{ModelDirectory.FullName}\\{modelName}.cs"), Encoding.UTF8)) { classFile.WriteLine($"namespace {Namespace}.Models"); classFile.WriteLine("{"); classFile.WriteLine($" using System;"); classFile.WriteLine($" using Newtonsoft.Json;"); classFile.WriteLine($" using {Namespace}.BaseTypes;"); classFile.WriteLine($" using {Namespace}.Enumerations;"); classFile.WriteLine(); classFile.WriteLine($" /// <summary>{modelDescription}</summary>"); classFile.WriteLine($" [SchemaSource(\"{schemaSourceUrl}\")]"); classFile.WriteLine($" public class {modelName} : {parentName}"); classFile.WriteLine(" {"); foreach (var item in interfaceProperties) { var propertyDescription = CleanDescription(item?["rdfs:comment"]?.ToObject <string>()); var propertyName = item?["rdfs:label"]?.ToObject <string>(); string originalName = null; if (PropertyNameBlacklist.Contains(propertyName)) { originalName = propertyName; propertyName = "@" + propertyName; } var propertyType = GetPropertyDataType(item); classFile.WriteLine(); classFile.WriteLine($" /// <summary>{propertyDescription}</summary>"); if (!String.IsNullOrWhiteSpace(originalName)) { classFile.WriteLine($" [JsonProperty(\"{originalName}\")]"); } classFile.WriteLine($" public {propertyType} {propertyName} {{ get; set; }}"); } classFile.WriteLine(); classFile.WriteLine(" }"); classFile.WriteLine("}"); } }
/// <summary> /// /// </summary> /// <param name="enumData"></param> public void GenerateEnumeration(JObject enumData) { var enumName = enumData["rdfs:label"]?.ToObject <string>(); if (String.IsNullOrEmpty(enumName) || TypeBlacklist.Contains(enumName.ToLower())) { return; } enumName = CaptalizeFirstLetter(enumName); Console.Out.WriteLine($"Creating enumeration for {enumName}"); var schemaSourceUrl = enumData["@id"]?.ToObject <string>(); var enumDescription = CleanDescription(enumData["rdfs:comment"]?.ToObject <string>()); var parentName = GetNameFromId(DataModel.GetItemParentFor(enumData)); if (parentName != "Enumeration") { parentName = $"TypeEquivalent<{parentName}>"; } var members = DataModel.GetEnumerationMembersFor(enumData); Console.Out.WriteLine($" found {members.Count()} members"); using (var enumFile = new StreamWriter(File.Create($"{EnumerationDirectory.FullName}\\{enumName}.cs"), Encoding.UTF8)) { enumFile.WriteLine($"namespace {Namespace}.Enumerations"); enumFile.WriteLine("{"); enumFile.WriteLine(" /// <summary>"); enumFile.WriteLine($" /// {enumDescription}"); enumFile.WriteLine(" /// </summary>"); enumFile.WriteLine($" [SchemaSource(\"{schemaSourceUrl}\")]"); if (parentName == "Enumeration") { enumFile.WriteLine($" public enum {enumName}"); } else { enumFile.WriteLine($" public class {enumName} : {parentName}"); } enumFile.WriteLine(" {"); foreach (var item in members) { var memberDescription = CleanDescription(item?["rdfs:comment"]?.ToObject <string>()); var memberName = item?["rdfs:label"]?.ToObject <string>(); string originalName = null; if (PropertyNameBlacklist.Contains(memberName)) { originalName = memberName; memberName = "@" + memberName; } enumFile.WriteLine(); enumFile.WriteLine($" /// <summary>{memberDescription}</summary>"); //if (!String.IsNullOrWhiteSpace(originalName)) //{ // enumFile.WriteLine($" [JsonProperty(\"{originalName}\")]"); //} enumFile.WriteLine($" {memberName},"); } enumFile.WriteLine(); enumFile.WriteLine(" }"); enumFile.WriteLine("}"); } }