Browse Source

出错信息优化

felix 1 year ago
parent
commit
9250141244

+ 5 - 0
Helper/CodeGenerator.cs

@@ -1,5 +1,6 @@
 using FlutterCodeGenerator.Map;
 using FlutterCodeGenerator.Map.Interface;
+using FlutterCodeGenerator.Model;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -37,6 +38,7 @@ namespace FlutterCodeGenerator.Helper
             _generatedServiceFileNameList = new List<string>() { "platform.dart" };
             _generatedServiceModelFileNameList = new List<string>() { "platform.m.dart" };
             LoadDll();
+            GenerateDataCache.Instance.IsLoadFinished = true;
         }
 
         private void LoadDll()
@@ -88,6 +90,7 @@ namespace FlutterCodeGenerator.Helper
             CheckDirectory();
             foreach (var serviceMap in _serviceMapDictionary)
             {
+                GenerateDataCache.Instance.CurrentService = serviceMap.Value.ServiceName;
                 var serviceDartFileName = serviceMap.Key[0..^7] + ".dart";
                 var serviceModelDartFileName = serviceMap.Key[0..^7] + ".m.dart";
                 var serviceDartPath = Path.Combine(_serviceFolderPath, serviceDartFileName);
@@ -143,6 +146,7 @@ namespace FlutterCodeGenerator.Helper
             }
             GenerateIndexDart();
             GenerateRpcDart();
+            GenerateDataCache.Instance.IsGenerateFinished = true;
         }
 
         private void GenerateRpcDart()
@@ -158,6 +162,7 @@ namespace FlutterCodeGenerator.Helper
             dartString.AppendLine(tempStringArray[0]);
             foreach (var serviceMap in _serviceMapDictionary)
             {
+                GenerateDataCache.Instance.CurrentService = serviceMap.Value.ServiceName;
                 if (serviceMap.Value is ServiceMap)
                 {
                     var serviceName = LetterConverterHelper.FirstCharToUpper_2(serviceMap.Key);

+ 1 - 0
Helper/ModelTypeGenerator.cs

@@ -16,6 +16,7 @@ namespace FlutterCodeGenerator.Helper
             {
                 _tempTypes.Clear();
             }
+            GenerateDataCache.Instance.CurrentParameter = argName;
             var usedComplexModelTypeList = GenerateDataCache.Instance.GetCurrentServiceMap().UsedComplexModelTypeList;
             var conflictModelTypeList = GenerateDataCache.Instance.ConflictModelTypeList;
             if ((_tempTypes.ContainsKey(type) && _tempTypes[type] < 20) || !_tempTypes.ContainsKey(type))

+ 2 - 0
Map/Interface/IServiceMap.cs

@@ -10,5 +10,7 @@ namespace FlutterCodeGenerator.Map.Interface
         string GetServiceDartString();
 
         List<ComplexModelType> UsedComplexModelTypeList { get; }
+
+        string ServiceName { get; }
     }
 }

+ 7 - 5
Map/MethodMap.cs

@@ -1,4 +1,5 @@
 using FlutterCodeGenerator.Helper;
+using FlutterCodeGenerator.Model;
 using FlutterCodeGenerator.ModelTypes;
 using System;
 using System.Collections.Generic;
@@ -13,12 +14,13 @@ namespace FlutterCodeGenerator.Map
     {
         public List<ModelType> ParameterModelTypes;
         public ModelType ReturnParameterModelType { get; set; }
-        private readonly string _methodName;
+        public string MethodName { get; private set; }
 
         public MethodMap(MethodInfo method)
         {
             ParameterModelTypes = new List<ModelType>();
-            _methodName = method.Name;
+            GenerateDataCache.Instance.CurrentMethod = method.Name;
+            MethodName = method.Name;
             Type returnParameterType;
             if (!method.ReturnType.IsGenericType)
             {
@@ -49,14 +51,14 @@ namespace FlutterCodeGenerator.Map
             {
                 argumentNames = "[" + argumentNames + "]";
             }
-            dartString.AppendLine($"\tFuture<{ReturnParameterModelType.GetFlutterTypeName()}> {LetterConverterHelper.FirstCharToLower(_methodName)}({parameterNames}) async {{");
+            dartString.AppendLine($"\tFuture<{ReturnParameterModelType.GetFlutterTypeName()}> {LetterConverterHelper.FirstCharToLower(MethodName)}({parameterNames}) async {{");
             if (ReturnParameterModelType is VoidModelType)
             {
-                dartString.AppendLine($"\t\tawait call(\"{_methodName}\", {argumentNames});");
+                dartString.AppendLine($"\t\tawait call(\"{MethodName}\", {argumentNames});");
             }
             else
             {
-                dartString.AppendLine($"\t\tvar rpcRst = await call(\"{_methodName}\", {argumentNames});");
+                dartString.AppendLine($"\t\tvar rpcRst = await call(\"{MethodName}\", {argumentNames});");
                 if (ReturnParameterModelType is SimpleModelType && !(ReturnParameterModelType is ListModelType) && !(ReturnParameterModelType is DictionaryModelType) && !(ReturnParameterModelType is ArrayModelType))
                 {
                     dartString.AppendLine("\t\treturn rpcRst;");

+ 5 - 5
Map/NotificationServiceMap.cs

@@ -13,11 +13,11 @@ namespace FlutterCodeGenerator.Map
     internal class NotificationServiceMap : IServiceMap
     {
         private List<Type> _typeList;
-        private string _serviceName;
 
         private EnumModelType _notificationEnumModelType;
         private List<ModelType> _notificationModelTypeList;
 
+        public string ServiceName { get; private set; }
         public List<ComplexModelType> UsedComplexModelTypeList { get; }
 
         public NotificationServiceMap(Type notificationEnumType, List<Type> types)
@@ -27,7 +27,7 @@ namespace FlutterCodeGenerator.Map
             GenerateDataCache.Instance.SetCurrentServiceMap(this);
             _notificationModelTypeList = new List<ModelType>();
             _notificationEnumModelType = (EnumModelType)ModelTypeGenerator.Create(notificationEnumType, notificationEnumType.Name, true);
-            _serviceName = "NotificationService";
+            ServiceName = "NotificationService";
             foreach (var type in _typeList)
             {
                 try
@@ -56,7 +56,7 @@ namespace FlutterCodeGenerator.Map
             {
                 if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
                 {
-                    alreadyGeneratedList.Add(modelType, _serviceName);
+                    alreadyGeneratedList.Add(modelType, ServiceName);
                     dartString.AppendLine(modelType.GetDartString());
                 }
                 else
@@ -66,7 +66,7 @@ namespace FlutterCodeGenerator.Map
                     {
                         throw new Exception("Import Service is null");
                     }
-                    if (importService != _serviceName && !importServiceList.Contains(importService))
+                    if (importService != ServiceName && !importServiceList.Contains(importService))
                     {
                         importServiceList.Add(importService);
                     }
@@ -152,7 +152,7 @@ namespace FlutterCodeGenerator.Map
                         importServiceList.Add(importService);
                     }
                 }
-                if(notificationModelType.ParameterType.IsAbstract)
+                if (notificationModelType.ParameterType.IsAbstract)
                 {
                     continue;
                 }

+ 7 - 5
Map/OtherServiceMap.cs

@@ -12,7 +12,9 @@ namespace FlutterCodeGenerator.Map
     internal class OtherServiceMap : IServiceMap
     {
         private List<Type> _typeList;
-        private string _serviceName;
+
+        public string ServiceName { get; private set; }
+
         public List<ComplexModelType> UsedComplexModelTypeList { get; }
 
         public OtherServiceMap(List<Type> types)
@@ -20,7 +22,7 @@ namespace FlutterCodeGenerator.Map
             _typeList = types;
             UsedComplexModelTypeList = new List<ComplexModelType>();
             GenerateDataCache.Instance.SetCurrentServiceMap(this);
-            _serviceName = "OtherService";
+            ServiceName = "OtherService";
             foreach (var type in _typeList)
             {
                 try
@@ -57,13 +59,13 @@ namespace FlutterCodeGenerator.Map
                         }
                         else
                         {
-                            alreadyGeneratedList.Add(modelType, _serviceName);
+                            alreadyGeneratedList.Add(modelType, ServiceName);
                             dartString.AppendLine(modelType.GetDartString());
                         }
                     }
                     else
                     {
-                        alreadyGeneratedList.Add(modelType, _serviceName);
+                        alreadyGeneratedList.Add(modelType, ServiceName);
                         dartString.AppendLine(modelType.GetDartString());
                     }
                 }
@@ -74,7 +76,7 @@ namespace FlutterCodeGenerator.Map
                     {
                         throw new Exception("Import Service is null");
                     }
-                    if (importService != _serviceName && !importServiceList.Contains(importService))
+                    if (importService != ServiceName && !importServiceList.Contains(importService))
                     {
                         importServiceList.Add(importService);
                     }

+ 18 - 15
Map/ServiceMap.cs

@@ -12,10 +12,10 @@ namespace FlutterCodeGenerator.Map
     internal class ServiceMap : IServiceMap
     {
         private List<string> _userDefinedComplexReturnTypeList;
-        private string _serviceName;
-
         private List<MethodMap> _methodMapList;
 
+        public string ServiceName { get; private set; }
+
         public List<ComplexModelType> UsedComplexModelTypeList { get; }
 
         public ServiceMap(Type type)
@@ -23,8 +23,8 @@ namespace FlutterCodeGenerator.Map
             UsedComplexModelTypeList = new List<ComplexModelType>();
             GenerateDataCache.Instance.SetCurrentServiceMap(this);
             _methodMapList = new List<MethodMap>();
-            var methodsList = type.GetMethods();
-            _serviceName = type.Name[1..];
+            var methodsList = type.GetMethods().Where(x => !x.IsSpecialName).ToList();
+            ServiceName = type.Name[1..];
             foreach (var method in methodsList)
             {
                 var methodMap = new MethodMap(method);
@@ -41,7 +41,7 @@ namespace FlutterCodeGenerator.Map
             {
                 if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
                 {
-                    alreadyGeneratedList.Add(modelType, _serviceName);
+                    alreadyGeneratedList.Add(modelType, ServiceName);
                     dartString.AppendLine(modelType.GetDartString());
                 }
                 else
@@ -51,7 +51,7 @@ namespace FlutterCodeGenerator.Map
                     {
                         throw new Exception("Import Service is null");
                     }
-                    if (importService != _serviceName && !importServiceList.Contains(importService))
+                    if (importService != ServiceName && !importServiceList.Contains(importService))
                     {
                         importServiceList.Add(importService);
                     }
@@ -94,6 +94,7 @@ namespace FlutterCodeGenerator.Map
             var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
             foreach (var methodMap in _methodMapList)
             {
+                GenerateDataCache.Instance.CurrentMethod = methodMap.MethodName;
                 string importService = null;
                 foreach (var modelType in methodMap.ParameterModelTypes)
                 {
@@ -104,7 +105,7 @@ namespace FlutterCodeGenerator.Map
                         {
                             throw new Exception("Import Service is null");
                         }
-                        if (importService != _serviceName && !importServiceList.Contains(importService))
+                        if (importService != ServiceName && !importServiceList.Contains(importService))
                         {
                             importServiceList.Add(importService);
                         }
@@ -118,7 +119,7 @@ namespace FlutterCodeGenerator.Map
                     {
                         throw new Exception("Import Service is null");
                     }
-                    if (importService != _serviceName && !importServiceList.Contains(importService))
+                    if (importService != ServiceName && !importServiceList.Contains(importService))
                     {
                         importServiceList.Add(importService);
                     }
@@ -133,7 +134,7 @@ namespace FlutterCodeGenerator.Map
                         {
                             throw new Exception("Import Service is null");
                         }
-                        if (importService != _serviceName && !importServiceList.Contains(importService))
+                        if (importService != ServiceName && !importServiceList.Contains(importService))
                         {
                             importServiceList.Add(importService);
                         }
@@ -149,7 +150,7 @@ namespace FlutterCodeGenerator.Map
                         {
                             throw new Exception("Import Service is null");
                         }
-                        if (importService != _serviceName && !importServiceList.Contains(importService))
+                        if (importService != ServiceName && !importServiceList.Contains(importService))
                         {
                             importServiceList.Add(importService);
                         }
@@ -166,7 +167,7 @@ namespace FlutterCodeGenerator.Map
                             {
                                 throw new Exception("Import Service is null");
                             }
-                            if (importService != _serviceName && !importServiceList.Contains(importService))
+                            if (importService != ServiceName && !importServiceList.Contains(importService))
                             {
                                 importServiceList.Add(importService);
                             }
@@ -192,7 +193,7 @@ namespace FlutterCodeGenerator.Map
                 {
                     throw new Exception("Import Service is null");
                 }
-                if (importService != _serviceName && !importServiceList.Contains(importService))
+                if (importService != ServiceName && !importServiceList.Contains(importService))
                 {
                     importServiceList.Add(importService);
                 }
@@ -208,10 +209,10 @@ namespace FlutterCodeGenerator.Map
             }
 
             serviceDartString.AppendLine();
-            serviceDartString.AppendLine($"class {_serviceName} extends JsonRpcClientBase {{");
-            serviceDartString.AppendLine($"\t{_serviceName}(");
+            serviceDartString.AppendLine($"class {ServiceName} extends JsonRpcClientBase {{");
+            serviceDartString.AppendLine($"\t{ServiceName}(");
             serviceDartString.AppendLine("\t\tString host, {");
-            serviceDartString.AppendLine($"\t\tString serviceName = \"I{_serviceName}\",");
+            serviceDartString.AppendLine($"\t\tString serviceName = \"I{ServiceName}\",");
             serviceDartString.AppendLine("\t\tMap<String, String>? headers,");
             serviceDartString.AppendLine("\t\tint? timeout,");
             serviceDartString.AppendLine("\t}) : super(");
@@ -237,6 +238,7 @@ namespace FlutterCodeGenerator.Map
             serviceDartString.AppendLine();
             foreach (var methodMap in _methodMapList)
             {
+                GenerateDataCache.Instance.CurrentMethod = methodMap.MethodName;
                 serviceDartString.AppendLine(methodMap.GetMethodDartString());
             }
 
@@ -250,6 +252,7 @@ namespace FlutterCodeGenerator.Map
             _userDefinedComplexReturnTypeList = new List<string>();
             foreach (var method in _methodMapList)
             {
+                GenerateDataCache.Instance.CurrentMethod = method.MethodName;
                 var veturnParameterModelType = method.ReturnParameterModelType;
                 AddUserDefinedComplexReturnType(veturnParameterModelType);
             }

+ 11 - 0
Model/GenerateDataCache.cs

@@ -16,6 +16,16 @@ namespace FlutterCodeGenerator.Model
 
         public Dictionary<string, Dictionary<ModelType, int>> ConflictModelTypeList { get; }
 
+        public string CurrentService { get; set; }
+
+        public string CurrentMethod { get; set; }
+
+        public string CurrentParameter { get; set; }
+
+        public bool IsLoadFinished { get; set; }
+
+        public bool IsGenerateFinished { get; set; }
+
         public GenerateDataCache()
         {
             AlreadyGeneratedList = new Dictionary<ComplexModelType, string>();
@@ -24,6 +34,7 @@ namespace FlutterCodeGenerator.Model
 
         public void SetCurrentServiceMap(IServiceMap serviceMap)
         {
+            CurrentService = serviceMap.ServiceName;
             _currentServiceMap = serviceMap;
         }
 

+ 2 - 1
Program.cs

@@ -1,4 +1,5 @@
 using FlutterCodeGenerator.Helper;
+using FlutterCodeGenerator.Model;
 using System;
 using System.IO;
 
@@ -27,7 +28,7 @@ namespace FlutterCodeGenerator
             }
             catch (Exception ex)
             {
-                Console.WriteLine($"An Error occured when generated!!! Error Message : {ex}");
+                Console.WriteLine($"An Error occured when generated!!! Service:{GenerateDataCache.Instance.CurrentService},Method:{GenerateDataCache.Instance.CurrentMethod},Parameter:{GenerateDataCache.Instance.CurrentParameter},IsLoadFinished:{GenerateDataCache.Instance.IsLoadFinished},IsGenerateFinished:{GenerateDataCache.Instance.IsGenerateFinished} Error Message : {ex}");
             }
             Console.ReadLine();
         }