public static string GenerateJs(JSBuilderModel model, ClassGeneratorOptions options) { var sbOut = new StringBuilder(); foreach (var cModel in model.FileClasses) { var sb = new StringBuilder(); if (options.IncludeHeaders) { BuildClassHeaders(sb); } BuildClassConstructor(cModel.ClassName, sb); foreach (var fileProperty in cModel.FileProperties) { if (fileProperty.IsArray) { BuildArrayProperty(sb, fileProperty, cModel.ClassName); } else { BuildPrimitiveProperty(sb, fileProperty); } } if (options.IncludeIsLoadingVar) { BuildLoadingVar(sb, options.ConversionType); } sb.AppendLine(" }"); // first close constructor parenthesis if (options.IncludeUnmapFunctions) { BuildUnmapFunction(sb, cModel.FileProperties, options.ConversionType); } BuildClassClosure(cModel.ClassName, sb); sbOut.AppendLine(sb.ToString()); sbOut.AppendLine(); } return(sbOut.ToString()); }
public static JSBuilderModel GetBuilderModel(List <FileLinesOverviewModel> listOfProperties) { JSBuilderModel retModel = new JSBuilderModel { FileClasses = new List <FileClassModel>() }; // Get all unique class names var uniqeClassNamesList = listOfProperties .DistinctBy(s => s.ClassName) .Select(s => s.ClassName) .ToList(); foreach (var cName in uniqeClassNamesList) { // Insert first class name var tempFileClassModel = new FileClassModel { ClassName = cName, FileProperties = new List <FilePropertyModel>() }; // Find their properties var listOfClassProperties = listOfProperties .Where(s => s.ClassName.ToLower().Trim().Contains(cName.ToLower().Trim())).ToList(); // Then insert their properties foreach (var property in listOfClassProperties) { FilePropertyModel tempFilePropertyModel = new FilePropertyModel { PropertyType = property.LineType.PropertyType, IsArray = property.LineType.IsArray, PropertyName = property.PropertyName, PropertyTypeName = property.LineType.PropertyTypeName }; tempFileClassModel.FileProperties.Add(tempFilePropertyModel); } retModel.FileClasses.Add(tempFileClassModel); } return(retModel); }
private void btnGenerate_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(filePath)) { ShowErrorMessage(); return; } List <string> listOfClassNames = new List <string>(); List <FileLinesOverviewModel> listOfProperties = new List <FileLinesOverviewModel>(); List <string> listOfVarTypes = new List <string> { "Int", "Int16", "Int32", "Int64", "UInt", "Short", "Bool", "Boolean", "Byte", "SByte", "Char", // "Date", // TODO: Check what to do with Date type vars, cause it is interfeering with DateTime vars "DateTime", "Decimal", "Double", "Float", "String", "Object", "Long" }; List <string> listOfLowerVarTypes = listOfVarTypes.Select(d => d.ToLower()).ToList(); listOfLowerVarTypes.AddRange(listOfVarTypes.Select(d => d.ToLower() + "?").ToList()); string line; // Read the file and display it line by line. StreamReader file = new StreamReader(filePath); while ((line = file.ReadLine()) != null) { // Add all class names (Classes are imidiately stripped) if (ClassNamesUtility.IsClass(line)) { listOfClassNames.Add(ClassNamesUtility.StripClassName(line)); } // And their properties (Properties are stripped down below) else if (PropertyNamesUtility.IsProperty(line)) { listOfProperties.Add(new FileLinesOverviewModel { ClassName = listOfClassNames.Last(), OriginalPropertyLine = line, LineType = new LineType() }); } } file.Close(); // First determine property type foreach (var pair in listOfProperties) { pair.LineType = PropertyNamesUtility.GetPropertyType(pair.OriginalPropertyLine, listOfClassNames, listOfLowerVarTypes); } // Then get property name foreach (var pair in listOfProperties) { pair.PropertyName = PropertyNamesUtility.StripPropertyName(pair.OriginalPropertyLine.Trim(), pair.LineType); } // Map it into proper model, so data is purposely there JSBuilderModel model = ModelsMapper.GetBuilderModel(listOfProperties); // Get options model var options = GetOptionsModel(); // Finally generate string result = null; switch (options.ConversionType) { case EGenerateOptions.Javascript: result = JavascriptGenerator.GenerateJs(model, options); break; case EGenerateOptions.Ecma6: result = Ecma6Generator.GenerateJs(model, options); break; case EGenerateOptions.KnockoutEcma6: result = Ecma6WithKnockoutGenerator.GenerateJs(model, options); break; } // result = JavascriptClassGenerator.GenerateJs(model, options); codeTextEditor.Text = result; }