static private string GetPropertyType(EEntityProperty eEntityProperty) { try { string ret = ""; switch (eEntityProperty.csharpTypeName) { case "string": return("string"); case "bool": return("boolean"); case "Int32": case "Int64": case "int": case "float": case "double": case "decimal": case "Decimal": return("number"); default: return("any"); } } catch (Exception e) { Logger.LogError(e); } return(""); }
static private bool GetTypescriptPropertyTypeName(ref EEntityProperty eEntityProperty) { try { //Logger.LogInfo(eEntityProperty.csharpTypeName); eEntityProperty.typescriptPropName = eEntityProperty.name; if (eEntityProperty.csharpTypeName.Contains("Int32") || eEntityProperty.csharpTypeName.Contains("Int64") || eEntityProperty.csharpTypeName.Contains("int") || eEntityProperty.csharpTypeName.Contains("Decimal") || eEntityProperty.csharpTypeName.Contains("decimal") || eEntityProperty.csharpTypeName.Contains("float") || eEntityProperty.csharpTypeName.Contains("double")) { eEntityProperty.typescriptTypeName = "number"; } else if (eEntityProperty.csharpTypeName.Contains("string")) { eEntityProperty.typescriptTypeName = "string"; } else if (eEntityProperty.csharpTypeName.Contains("DateTime")) { eEntityProperty.typescriptTypeName = "string"; } else if (eEntityProperty.csharpTypeName.Contains("TimeSpan")) { eEntityProperty.typescriptTypeName = "string"; } else if (eEntityProperty.csharpTypeName.Contains("bool")) { eEntityProperty.typescriptTypeName = "boolean"; } else { eEntityProperty.typescriptTypeName = eEntityProperty.csharpTypeName; } if (!string.IsNullOrEmpty(eEntityProperty.typescriptTypeName) && eEntityProperty.csharpTypeName.Contains('?')) { eEntityProperty.typescriptPropName += "?"; return(true); } /*foreach (EEntityFile eEntityFile in fileList) { * foreach(EEntityProperty e in eEntityFile.propertyList){ * * } * }*/ return(true); } catch (Exception e) { Logger.LogError(e); } return(false); }
static private bool WriteProperty(EEntityProperty eEntityProperty, out string propertyLine) { propertyLine = ""; try { /*if (eEntityProperty.csharpTypeName.Contains("?")) { * propertyLine = "\t" + eEntityProperty.name + "?: " + GetPropertyType(eEntityProperty)+";\n"; * return true; * } else */propertyLine = "\t" + FlutterWatcher.GetFlutterType(eEntityProperty.csharpTypeName, out bool isAnotherEntityImport) + $" {eEntityProperty.name};\n"; return(true); } catch (Exception e) { Logger.LogError(e); } return(false); }
static private bool WriteProperty(EEntityProperty eEntityProperty, out string propertyLine) { propertyLine = ""; try { if (eEntityProperty.csharpTypeName.Contains("?")) { propertyLine = "\t" + eEntityProperty.name + "?: " + GetPropertyType(eEntityProperty) + ";\n"; return(true); } else { propertyLine = "\t" + eEntityProperty.name + ": " + GetPropertyType(eEntityProperty) + ";\n"; } return(true); } catch (Exception e) { Logger.LogError(e); } return(false); }
static private bool SetTypescriptPropertyTypes() { try { foreach (EEntityFile eEntityFile in fileList) { for (int j = 0; j < eEntityFile.propertyList.Count; j++) { EEntityProperty eEntityProperty = eEntityFile.propertyList[j]; if (!GetTypescriptPropertyTypeName(ref eEntityProperty)) { Logger.LogError("unable to analyse property in file " + eEntityFile.className + " named " + eEntityProperty.csharpTypeName + " " + eEntityProperty.name); return(false); } } } return(true); } catch (Exception e) { Logger.LogError(e); } return(false); }
static private bool AnalyseEntity(ref EEntityFile eEntityFile, string fileContent) { try { var regex = new Regex("public\\s+(\\w+\\?*)\\s+(\\w+)\\s+{\\s*get", RegexOptions.None, TimeSpan.FromMilliseconds(1000)); Match match = regex.Match(fileContent); if (!match.Success) { return(true); } for (Int32 i = 0; match.Success; i++, match = match.NextMatch()) { EEntityProperty eEntityProperty = new EEntityProperty(); eEntityProperty.csharpTypeName = match.Groups[1].Value; eEntityProperty.name = match.Groups[2].Value; eEntityFile.propertyList.Add(eEntityProperty); } return(true); } catch (Exception e) { Logger.LogError(e); } return(false); }
static private bool GetPropertyLine(EEntityProperty eEntityProperty, out string propertyLine) { propertyLine = ""; try { if (eEntityProperty.csharpTypeName.Contains("?")) { propertyLine = "\n\tproperty var " + eEntityProperty.name; return(true); } if (eEntityProperty.csharpTypeName.Contains("Int32") || eEntityProperty.csharpTypeName.Contains("Int64") || eEntityProperty.csharpTypeName.Contains("int")) { propertyLine = "\n\tproperty int " + eEntityProperty.name; } else if (eEntityProperty.csharpTypeName.Contains("double") || eEntityProperty.csharpTypeName.Contains("Decimal") || eEntityProperty.csharpTypeName.Contains("decimal")) { propertyLine = "\n\tproperty double " + eEntityProperty.name; } else if (eEntityProperty.csharpTypeName.Contains("float")) { propertyLine = "\n\tproperty float " + eEntityProperty.name; } else if (eEntityProperty.csharpTypeName.Contains("bool")) { propertyLine = "\n\tproperty bool " + eEntityProperty.name; } else if (eEntityProperty.csharpTypeName.Contains("string")) { propertyLine = "\n\tproperty string " + eEntityProperty.name; } else { propertyLine = "\n\tproperty var " + eEntityProperty.name; } return(true); } catch (Exception e) { Logger.LogError(e); } return(false); }
static private bool WriteFlutterEntityFile(EEntityFile eEntityFile, string entityDirectory) { try { string flutterEntityFileName = eEntityFile.className.ToLower() + ".dart"; string completeFilePath = entityDirectory + "/" + flutterEntityFileName; Logger.LogInfoIfDebugLevel(DebugLevels.Files | DebugLevels.Functions | DebugLevels.All, "\t" + flutterEntityFileName); StringBuilder fileContent = new StringBuilder(); fileContent.Append("//region imports\n"); fileContent.Append("//author Bruno Tezine\n"); fileContent.Append("import \'package:json_annotation/json_annotation.dart\';\n"); //let's add all imports from other entities foreach (EEntityProperty eEntityProperty in eEntityFile.propertyList) { if (string.IsNullOrEmpty(eEntityProperty.name)) { continue; } FlutterWatcher.GetFlutterType(eEntityProperty.csharpTypeName, out bool isAnotherEntityImport); if (!isAnotherEntityImport) { continue; } fileContent.Append("import \'package:" + Globals.flutterPackageName + "/entities/restinpeace/" + eEntityProperty.csharpTypeName.ToLower() + ".dart\';\n"); } fileContent.Append("part '" + eEntityFile.className.ToLower() + ".g.dart\';\n"); fileContent.Append("//endregion\n\n"); fileContent.Append("@JsonSerializable(nullable: true)\n"); fileContent.Append($"class {eEntityFile.className}" + "{\n"); foreach (EEntityProperty eEntityProperty in eEntityFile.propertyList) { if (string.IsNullOrEmpty(eEntityProperty.name)) { continue; } if (!WriteProperty(eEntityProperty, out string propertyLine)) { Logger.LogError("unable to write property in file " + flutterEntityFileName); return(false); } fileContent.Append(propertyLine); } fileContent.Append("\n\t" + eEntityFile.className + "({\n"); for (int p = 0; p < eEntityFile.propertyList.Count; p++) { EEntityProperty eEntityProperty = eEntityFile.propertyList.ElementAt(p); fileContent.Append("\t\tthis." + eEntityProperty.name); if (p != (eEntityFile.propertyList.Count - 1)) { fileContent.Append(",\n"); } else { fileContent.Append("\n});\n"); } } fileContent.Append($"\n\tfactory {eEntityFile.className}.fromJson(Map<String, dynamic> json) => _${eEntityFile.className}FromJson(json);\n"); fileContent.Append($"\tMap<String, dynamic> toJson() => _${eEntityFile.className}ToJson(this);\n"); fileContent.Append("}"); //Logger.LogInfo(newFileContent.ToString()); string oldContent = ""; if (File.Exists(completeFilePath)) { oldContent = File.ReadAllText(completeFilePath); } if (fileContent.ToString() != oldContent) { File.WriteAllText(completeFilePath, fileContent.ToString()); } return(true); } catch (Exception e) { Logger.LogError(e); } return(false); }