Ejemplo n.º 1
0
 static void Main(string[] args)
 {
     try {
         Globals.debugLevel = DebugLevels.All;
         List <string>          backendRootFolders      = new List <string>();
         CommandLineApplication commandLineApplication  = new CommandLineApplication(throwOnUnexpectedArg: false);
         CommandOption          frontendTypeOption      = commandLineApplication.Option("-$|-t | --type <type>", "Frontend Type (qml, qmlv2, qmltypescript, angular, dotnet, flutter).", CommandOptionType.SingleValue);
         CommandOption          backendOption           = commandLineApplication.Option("-b |--backend <directory>", "Backend source directory", CommandOptionType.SingleValue);
         CommandOption          frontendOption          = commandLineApplication.Option("-f | --frontend <directory>", "Frontend app directory.", CommandOptionType.SingleValue);
         CommandOption          generateEntitiesOption  = commandLineApplication.Option("-g | --generate-entities <value>", "Set true to create all entities.", CommandOptionType.SingleValue);
         CommandOption          additionalEntityFolder  = commandLineApplication.Option("-a | --additional-entities-folder <path>", "Set this to read entities from a library", CommandOptionType.SingleValue);
         CommandOption          additionalEntityFolder2 = commandLineApplication.Option("-a | --additional-entities-folder-2 <path>", "Set this to read entities from a library", CommandOptionType.SingleValue);
         CommandOption          restInPeaceVersion      = commandLineApplication.Option("-r |--rest-in-peace-version <version>", "Rest in peace version", CommandOptionType.SingleValue);
         CommandOption          renameToMJSOption       = commandLineApplication.Option("--rename-to-mjs <directory>", "Rename to mjs", CommandOptionType.SingleValue);
         CommandOption          excludeMJSOption        = commandLineApplication.Option("--exclude <files>", "Do not rename these files", CommandOptionType.SingleValue);
         commandLineApplication.HelpOption("-? | -h | --help");
         commandLineApplication.OnExecute(() => {
             if (renameToMJSOption.HasValue())
             {
                 Helper.RenameJSToMJS(renameToMJSOption.Value(), excludeMJSOption.Value());
                 return(0);
             }
             if (!frontendTypeOption.HasValue())
             {
                 Logger.LogError("Missing --type");
                 return(0);
             }
             if (!backendOption.HasValue())
             {
                 Logger.LogError("Missing --backend");
                 return(0);
             }
             if (!frontendOption.HasValue())
             {
                 Logger.LogError("Missing --frontend");
                 return(0);
             }
             if (generateEntitiesOption.HasValue() && generateEntitiesOption.Value() != "true" && generateEntitiesOption.Value() != "false")
             {
                 Logger.LogError("--generate-entities must be true or false");
                 return(0);
             }
             string frontendType = frontendTypeOption.Value();
             Logger.LogInfo("Executing RestInPeace... Frontend type:" + frontendType);
             if (frontendType != "qml" && frontendType != "qmlv2" && frontendType != "angular" && frontendType != "dotnet" && frontendType != "flutter" && frontendType != "qmltypescript" && frontendType != "blazor")
             {
                 Logger.LogError("--type must be qml, qmltypescript, angular, dotnet, blazor or flutter");
                 return(0);
             }
             bool generateEntities = false;
             backendRootFolders.Add(backendOption.Value()); //we read all entities defined in the backend
             if (generateEntitiesOption.HasValue())
             {
                 generateEntities = generateEntitiesOption.Value() == "true";
             }
             if (generateEntities || frontendType == "qmltypescript" || frontendType == "flutter" || frontendType == "blazor" || frontendType == "dotnet")
             {
                 if (additionalEntityFolder.HasValue())
                 {
                     backendRootFolders.Add(additionalEntityFolder.Value());                                    //we read all entities defined in an optional library
                 }
                 if (additionalEntityFolder2.HasValue())
                 {
                     backendRootFolders.Add(additionalEntityFolder2.Value());                                     //we read all entities defined in an optional library
                 }
                 if (!CSharpEntityReader.AnalyseFiles(backendRootFolders))
                 {
                     Console.ReadKey();
                     return(0);
                 }
                 if (!CSharpEnumReader.AnalyseFiles(backendRootFolders))
                 {
                     Console.ReadKey();
                     return(0);
                 }
             }
             if (!CSharpFunctionReader.AnalyseFiles(backendRootFolders))
             {
                 Console.ReadKey();
                 return(0);
             }
             bool ok = WriteFrontendFiles(frontendType, generateEntitiesOption.HasValue(), frontendOption.Value(), restInPeaceVersion.Value());
             if (ok)
             {
                 Logger.LogInfo("=====================SUCCESS=====================");
             }
             //Console.ReadKey();
             return(0);
         });
         commandLineApplication.Execute(args);
     } catch (Exception e) {
         Logger.LogError(e);
     }
 }
Ejemplo n.º 2
0
        static private bool WriteTypescriptFile(string completeTypescriptFilename, EFunctionFile eFile)
        {
            try {
                //string typescriptClassName = "";
                //if (eFile.csharpFileName.StartsWith("S")) typescriptClassName = eFile.csharpFileName.Remove(0, 1);
                StringBuilder newFileContent = new StringBuilder();
                newFileContent.Append("//author Bruno Tezine\n");
                //let's insert all entity imports
                List <string> importList          = new List <string>();
                List <string> allFunctionArgTypes = GetFunctionArgumentTypeNames(eFile.functionList);
                List <string> allReturnTypes      = GetAllReturnTypeNames(eFile.functionList);
                newFileContent.Append("//region imports\n");
                foreach (string argType in allFunctionArgTypes)
                {
                    string completeImportLine = "";
                    if (CSharpEntityReader.IsEntity(argType))
                    {
                        completeImportLine = "import {" + argType + "} from \"./" + argType + "\";\n";
                    }
                    else if (CSharpEnumReader.IsEnum(argType))
                    {
                        completeImportLine = "import {" + argType + "} from \"./" + argType + "\";\n";
                    }
                    else if (!Helper.isBasicDotnetType(argType))
                    {
                        Logger.LogError($"Unknown arg type {argType} used in file {eFile.csharpFileName}!!!");
                        continue;
                    }
                    if (importList.Contains(completeImportLine))
                    {
                        continue;
                    }
                    importList.Add(completeImportLine);
                }
                foreach (string returnType in allReturnTypes)
                {
                    string completeImportLine = "";
                    if (CSharpEntityReader.IsEntity(returnType))
                    {
                        completeImportLine = "import {" + returnType + "} from \"./" + returnType + "\";\n";
                    }
                    else if (CSharpEnumReader.IsEnum(returnType))
                    {
                        completeImportLine = "import {" + returnType + "} from \"./" + returnType + "\";\n";
                    }
                    else if (!Helper.isBasicDotnetType(returnType))
                    {
                        Logger.LogError($"Unknown return type {returnType} used in file {eFile.csharpFileName}!!!");
                        continue;
                    }
                    if (importList.Contains(completeImportLine))
                    {
                        continue;
                    }
                    importList.Add(completeImportLine);
                }
                foreach (string importLine in importList)
                {
                    newFileContent.Append(importLine);
                }
//                newFileContent.Append("import {MySimpleEvent} from \"@QtTyped/Codes/MySimpleEvent\";\n");
                newFileContent.Append("import {MySimpleEvent} from \"./MySimpleEvent\";\n");
                newFileContent.Append("//endregion\n\n");
                //imports above

                foreach (EFunction eFunction in eFile.functionList)
                {
                    GetTypeScriptFuctionReturnType(eFunction, out string userTypeName);
                    if (string.IsNullOrEmpty(userTypeName))
                    {
                        continue;
                    }
                }

                newFileContent.Append("export declare class " + eFile.frontendClassName + " {\n");
                newFileContent.Append("\n");

                foreach (EFunction eFunction in eFile.functionList)
                {
                    Logger.LogInfoIfDebugLevel(DebugLevels.Functions | DebugLevels.All, "\tFunction " + eFunction.functionName);
                    string functionName = Char.ToLowerInvariant(eFunction.functionName[0]) + eFunction.functionName.Substring(1);
                    newFileContent.Append("\tstatic " + functionName + "(" + GetTypeScriptFuctionArguments(eFunction) + ")" + GetTypeScriptFuctionReturnType(eFunction, out string userTypeName));
                    newFileContent.Append("\n");
                }
                newFileContent.Append("}\n");
                string oldContent = "";
                if (File.Exists(completeTypescriptFilename))
                {
                    oldContent = File.ReadAllText(completeTypescriptFilename);
                }
                if (newFileContent.ToString() != oldContent)
                {
                    File.WriteAllText(completeTypescriptFilename, newFileContent.ToString());
                }
                return(true);
            } catch (Exception e) {
                Logger.LogError(e);
            }
            return(false);
        }
Ejemplo n.º 3
0
        static private bool WriteFlutterFile(EFunctionFile eFile)
        {
            try {
                StringBuilder newFileContent = new StringBuilder();
                newFileContent.Append("//region imports\n");
                newFileContent.Append("//author Bruno Tezine\n");
                newFileContent.Append("import \'dart:convert\';\n");
                newFileContent.Append("import \'package:http/http.dart\' as http;\n");
                newFileContent.Append("import \'package:flutter_base/codes/bool_helper.dart\';\n");
                newFileContent.Append($"import \'package:{Globals.flutterPackageName}/codes/defines.dart\';\n");
                //let's insert all entity imports
                List <string> importList          = new List <string>();
                List <string> allFunctionArgTypes = GetFunctionArgumentTypeNames(eFile.functionList);
                List <string> allReturnTypes      = GetAllReturnTypeNames(eFile.functionList);
                foreach (string argType in allFunctionArgTypes)
                {
                    string completeImportLine = "";
                    if (CSharpEntityReader.IsEntity(argType))
                    {
                        completeImportLine = "import 'package:" + Globals.flutterPackageName + "/entities/restinpeace/" + argType.ToLower() + ".dart';\n";
                    }
                    else if (CSharpEnumReader.IsEnum(argType))
                    {
                        completeImportLine = "import 'package:" + Globals.flutterPackageName + "/enums/" + argType.ToLower() + ".dart';\n";
                    }
                    else if (!Helper.isBasicDotnetType(argType))
                    {
                        Logger.LogError($"Unknown arg type {argType} used in file {eFile.csharpFileName}!!!");
                        continue;
                    }
                    if (importList.Contains(completeImportLine))
                    {
                        continue;
                    }
                    importList.Add(completeImportLine);
                }
                foreach (string returnType in allReturnTypes)
                {
                    string completeImportLine = "";
                    if (CSharpEntityReader.IsEntity(returnType))
                    {
                        completeImportLine = "import 'package:" + Globals.flutterPackageName + "/entities/restinpeace/" + returnType.ToLower() + ".dart';\n";
                    }
                    else if (CSharpEnumReader.IsEnum(returnType))
                    {
                        completeImportLine = "import 'package:" + Globals.flutterPackageName + "/enums/" + returnType.ToLower() + ".dart';\n";
                    }
                    else if (!Helper.isBasicDotnetType(returnType))
                    {
                        Logger.LogError($"Unknown return type {returnType} used in file {eFile.csharpFileName}!!!");
                        continue;
                    }
                    if (importList.Contains(completeImportLine))
                    {
                        continue;
                    }
                    importList.Add(completeImportLine);
                }
                foreach (string importLine in importList)
                {
                    newFileContent.Append(importLine);
                }
                newFileContent.Append("//endregion\n\n");
                //imports above

                newFileContent.Append($"class {eFile.frontendClassName}" + "{\n");
//                foreach (EFunction eFunction in eFile.functionList) {
//                    string flutterTypeName=FlutterWatcher.GetFlutterType(eFunction.returnTypeName, out bool isAnotherEntityImport);//antes era GetFlutterFuctionReturnType
//                    if (string.IsNullOrEmpty(flutterTypeName)) continue;
//                }
                newFileContent.Append("\n");
                foreach (EFunction eFunction in eFile.functionList)
                {
                    if (!WriteFunction(eFile, eFunction))
                    {
                        Logger.LogError("unable get function " + eFunction.functionName + " from file " + eFile.csharpFileName);
                        return(false);
                    }
                    newFileContent.Append(eFunction.frontendFunctionContent);
                    newFileContent.Append("\n\n");
                }
                newFileContent.Append("}\n");
                string oldContent = "";
                if (File.Exists(eFile.frontendFileName))
                {
                    oldContent = File.ReadAllText(eFile.frontendFileName);
                }
                if (newFileContent.ToString() != oldContent)
                {
                    File.WriteAllText(eFile.frontendFileName, newFileContent.ToString());
                }
                return(true);
            } catch (Exception e) {
                Logger.LogError(e);
            }
            return(false);
        }