static private bool WriteFunction(EFunctionFile eFile, EFunction eFunction) { try { StringBuilder result = new StringBuilder(); StringBuilder argsStringBuilder = new StringBuilder(); List <EArg> segmentsList, mandatoryList, optionalList, formArgList, allArgsList; EArg bodyArg; Logger.LogInfoIfDebugLevel(DebugLevels.Functions | DebugLevels.All, "\tFunction " + eFunction.functionName); string functionName = Char.ToLowerInvariant(eFunction.functionName[0]) + eFunction.functionName.Substring(1); eFunction.frontendReturnTypeName = FlutterWatcher.GetFlutterType(eFunction.returnTypeName, out bool isAnotherEntityImport); //antes era GetFlutterFuctionReturnType string operation = Helper.GetRESTOperation(eFile, eFunction); if (string.IsNullOrEmpty(operation)) { return(false); } if (!GetFunctionArgs(eFunction, ref argsStringBuilder, out allArgsList, out segmentsList, out mandatoryList, out optionalList, out formArgList, out bodyArg)) { Logger.LogError("unable to get funcion parameters to write file " + eFile.frontendFileName + ": " + eFunction.functionName); return(false); } result.Append("\tstatic Future<" + eFunction.frontendReturnTypeName + "> " + functionName + "(" + argsStringBuilder.ToString() + ") async {"); string functionBody = GetFunctionBody(eFile, eFunction, operation, segmentsList, mandatoryList, optionalList, formArgList, bodyArg); result.Append(functionBody); eFunction.frontendFunctionContent = result.ToString(); 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 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); }
static private bool GetFunctionArgs(EFunction eFunction, ref StringBuilder argsStringBuilder, out List <EArg> allArgsList, out List <EArg> segmentsList, out List <EArg> mandatoryList, out List <EArg> optionalList, out List <EArg> formArgList, out EArg bodyArg) { segmentsList = null; mandatoryList = null; optionalList = null; formArgList = null; allArgsList = new List <EArg>(); bodyArg = null; try { //orderby then to linq nao funciona para mesmo campo bodyArg = Helper.GetBodyArg(eFunction.argsList); segmentsList = Helper.GetSegmentsArgs(eFunction.argsList); mandatoryList = Helper.GetMandatoryArgs(eFunction.argsList); optionalList = Helper.GetOptionalArgs(eFunction.argsList); formArgList = Helper.GetFormArgs(eFunction.argsList); if (bodyArg != null) { allArgsList.Add(bodyArg); } if (segmentsList.Any()) { allArgsList.AddRange(segmentsList); } if (mandatoryList.Any()) { allArgsList.AddRange(mandatoryList); } if (optionalList.Any()) { allArgsList.AddRange(optionalList); } if (formArgList.Any()) { allArgsList.AddRange(formArgList); } bool optionalArgsStarted = false; for (int i = 0; i < allArgsList.Count; i++) { EArg eArg = allArgsList.ElementAt(i); bool hasDefaultValue = !string.IsNullOrEmpty(eArg.defaultValue); if (hasDefaultValue && (!optionalArgsStarted)) { argsStringBuilder.Append("{"); optionalArgsStarted = true; } string flutterTypeName = FlutterWatcher.GetFlutterType(eArg.typeName, out bool isAnotherEntityImport); argsStringBuilder.Append(flutterTypeName + " " + eArg.name); if (hasDefaultValue) { argsStringBuilder.Append("=" + eArg.defaultValue); } if (i < (allArgsList.Count - 1)) { argsStringBuilder.Append(", "); } } if (optionalArgsStarted) { argsStringBuilder.Append("}"); } return(true); } catch (Exception e) { Logger.LogError(e); } return(false); }