Browse Source

Add GenerateDataCache

felix 2 years ago
parent
commit
d455c849d7
4 changed files with 99 additions and 71 deletions
  1. 1 7
      CodeGenerator.cs
  2. 33 0
      GenerateDataCache.cs
  3. 45 43
      ModeTypeGenerator.cs
  4. 20 21
      ServiceMap.cs

+ 1 - 7
CodeGenerator.cs

@@ -25,10 +25,6 @@ namespace FlutterCodeGenerator
         public static readonly string StringJsonConvert = "import 'package:fis_common/json_convert.dart';";
         public static readonly string StringUtils = "import 'package:fis_jsonrpc/utils.dart';";
 
-        public static Dictionary<ComplexModelType, string> AlreadyGeneratedList;
-
-        public static Dictionary<string, Dictionary<ModelType, int>> ConflictModelTypeList;
-
         public CodeGenerator(string dllPath, string filePath)
         {
             _generatedFolderPath = filePath;
@@ -38,8 +34,6 @@ namespace FlutterCodeGenerator
             _serviceMapDictionary = new Dictionary<string, ServiceMap>();
             _generatedServiceFileNameList = new List<string>() { "platform.dart" };
             _generatedServiceModelFileNameList = new List<string>() { "platform.m.dart" };
-            AlreadyGeneratedList = new Dictionary<ComplexModelType, string>();
-            ConflictModelTypeList = new Dictionary<string, Dictionary<ModelType, int>>();
             LoadDll();
         }
 
@@ -52,7 +46,7 @@ namespace FlutterCodeGenerator
             }
             var assemblybytes = File.ReadAllBytes(dll);
             var assembly = Assembly.Load(assemblybytes);
-            var allTypes= assembly.GetTypes();
+            var allTypes = assembly.GetTypes();
             var liveConsultationService = allTypes.FirstOrDefault(x => x.FullName.Contains(".LiveConsultation") && x.Name.EndsWith("Service"));
             if (liveConsultationService == null)
             {

+ 33 - 0
GenerateDataCache.cs

@@ -0,0 +1,33 @@
+using System.Collections.Generic;
+
+namespace FlutterCodeGenerator
+{
+    internal class GenerateDataCache
+    {
+        private static GenerateDataCache _instance;
+
+        private ServiceMap _currentServiceMap;
+
+        public static GenerateDataCache Instance => _instance ?? (_instance = new GenerateDataCache());
+
+        public Dictionary<ComplexModelType, string> AlreadyGeneratedList { get; }
+
+        public Dictionary<string, Dictionary<ModelType, int>> ConflictModelTypeList { get; }
+
+        public GenerateDataCache()
+        {
+            AlreadyGeneratedList = new Dictionary<ComplexModelType, string>();
+            ConflictModelTypeList = new Dictionary<string, Dictionary<ModelType, int>>();
+        }
+
+        public void SetCurrentServiceMap(ServiceMap serviceMap)
+        {
+            _currentServiceMap = serviceMap;
+        }
+
+        public ServiceMap GetCurrentServiceMap()
+        {
+            return _currentServiceMap;
+        }
+    }
+}

+ 45 - 43
ModeTypeGenerator.cs

@@ -14,6 +14,8 @@ namespace FlutterCodeGenerator
             {
                 _tempTypes.Clear();
             }
+            var usedComplexModelTypeList = GenerateDataCache.Instance.GetCurrentServiceMap().UsedComplexModelTypeList;
+            var conflictModelTypeList = GenerateDataCache.Instance.ConflictModelTypeList;
             if ((_tempTypes.ContainsKey(type) && _tempTypes[type] < 2) || !_tempTypes.ContainsKey(type))
             {
                 var duplicatedTime = 0;
@@ -69,26 +71,26 @@ namespace FlutterCodeGenerator
                 else if (type.IsEnum)
                 {
                     var modelType = new EnumModeType(type, argName);
-                    if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
+                    if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
                     {
-                        CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
+                        conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
                     }
                     else
                     {
-                        if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
+                        if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
                         {
-                            var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
+                            var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
                             modelType.Index = index;
-                            CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
+                            conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
                         }
                         else
                         {
-                            modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
+                            modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
                         }
                     }
-                    if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
+                    if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
                     {
-                        ServiceMap.TemporaryList.Add(modelType);
+                        usedComplexModelTypeList.Add(modelType);
                     }
                     return modelType;
                 }
@@ -99,7 +101,7 @@ namespace FlutterCodeGenerator
                 }
                 else if (type.IsGenericType)
                 {
-                    if (type.Name == "List`1" || type.Name=="IList`1"|| type.Name == "IEnumerable`1")
+                    if (type.Name == "List`1" || type.Name == "IList`1" || type.Name == "IEnumerable`1")
                     {
                         var modelType = new ListModelType(type, argName);
                         return modelType;
@@ -112,26 +114,26 @@ namespace FlutterCodeGenerator
                     else if (type.Name == "Nullable`1")
                     {
                         var modelType = new EnumNullableModelType(type.GetGenericArguments()[0], argName);
-                        if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
+                        if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
                         {
-                            CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
+                            conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
                         }
                         else
                         {
-                            if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
+                            if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
                             {
-                                var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
+                                var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
                                 modelType.Index = index;
-                                CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
+                                conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
                             }
                             else
                             {
-                                modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
+                                modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
                             }
                         }
-                        if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
+                        if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
                         {
-                            ServiceMap.TemporaryList.Add(modelType);
+                            usedComplexModelTypeList.Add(modelType);
                         }
                         return modelType;
                     }
@@ -139,31 +141,31 @@ namespace FlutterCodeGenerator
                     {
                         _tempTypes[type] = duplicatedTime;
                         var modelType = new UserDefinedGenericModelType(type, argName);
-                        if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
+                        if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
                         {
-                            CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
+                            conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
                         }
                         else
                         {
-                            if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
+                            if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
                             {
-                                var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
+                                var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
                                 modelType.Index = index;
-                                CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
+                                conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
                             }
                             else
                             {
-                                modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
+                                modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
                             }
                         }
-                        if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
+                        if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
                         {
-                            ServiceMap.TemporaryList.Add(modelType);
+                            usedComplexModelTypeList.Add(modelType);
                         }
                         return modelType;
                     }
                 }
-                else if(type==typeof(object))
+                else if (type == typeof(object))
                 {
                     throw new Exception("Object is not supported, it should be specific type.");
                 }
@@ -171,26 +173,26 @@ namespace FlutterCodeGenerator
                 {
                     _tempTypes[type] = duplicatedTime;
                     var modelType = new UserDefinedDerivedModelType(type, argName);
-                    if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
+                    if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
                     {
-                        CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
+                        conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
                     }
                     else
                     {
-                        if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
+                        if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
                         {
-                            var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
+                            var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
                             modelType.Index = index;
-                            CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
+                            conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
                         }
                         else
                         {
-                            modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
+                            modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
                         }
                     }
-                    if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
+                    if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
                     {
-                        ServiceMap.TemporaryList.Add(modelType);
+                        usedComplexModelTypeList.Add(modelType);
                     }
                     return modelType;
                 }
@@ -198,26 +200,26 @@ namespace FlutterCodeGenerator
                 {
                     _tempTypes[type] = duplicatedTime;
                     var modelType = new UserDefinedModeType(type, argName);
-                    if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
+                    if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
                     {
-                        CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
+                        conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
                     }
                     else
                     {
-                        if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
+                        if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
                         {
-                            var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
+                            var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
                             modelType.Index = index;
-                            CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
+                            conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
                         }
                         else
                         {
-                            modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
+                            modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
                         }
                     }
-                    if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
+                    if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
                     {
-                        ServiceMap.TemporaryList.Add(modelType);
+                        usedComplexModelTypeList.Add(modelType);
                     }
                     return modelType;
                 }
@@ -229,4 +231,4 @@ namespace FlutterCodeGenerator
             }
         }
     }
-}
+}

+ 20 - 21
ServiceMap.cs

@@ -7,18 +7,17 @@ namespace FlutterCodeGenerator
 {
     internal class ServiceMap
     {
-        public List<MethodMap> MethodMapList;
-
-        public static List<ComplexModelType> TemporaryList { get; set; }
-        private List<ComplexModelType> _usedComplexModelTypeList;
-
         private List<string> _userDefinedComplexReturnTypeList;
         private string _serviceName;
 
+        public List<MethodMap> MethodMapList;
+
+        public List<ComplexModelType> UsedComplexModelTypeList;
+
         public ServiceMap(Type type)
         {
-            _usedComplexModelTypeList = new List<ComplexModelType>();
-            TemporaryList = new List<ComplexModelType>();
+            UsedComplexModelTypeList = new List<ComplexModelType>();
+            GenerateDataCache.Instance.SetCurrentServiceMap(this);
             MethodMapList = new List<MethodMap>();
             var methodsList = type.GetMethods();
             _serviceName = type.Name[1..];
@@ -27,24 +26,23 @@ namespace FlutterCodeGenerator
                 var methodMap = new MethodMap(method);
                 MethodMapList.Add(methodMap);
             }
-            _usedComplexModelTypeList.AddRange(TemporaryList);
-            TemporaryList.Clear();
         }
 
         public string GetServiceModelDartString()
         {
             var dartString = new StringBuilder();
             var importServiceList = new List<string>();
-            foreach (var modelType in _usedComplexModelTypeList)
+            var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
+            foreach (var modelType in UsedComplexModelTypeList)
             {
-                if (!CodeGenerator.AlreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
+                if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
                 {
-                    CodeGenerator.AlreadyGeneratedList.Add(modelType, _serviceName);
+                    alreadyGeneratedList.Add(modelType, _serviceName);
                     dartString.AppendLine(modelType.GetDartString());
                 }
                 else
                 {
-                    var importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
+                    var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
                     if (importService == null)
                     {
                         throw new Exception("Import Service is null");
@@ -89,6 +87,7 @@ namespace FlutterCodeGenerator
         {
             var serviceDartString = new StringBuilder();
             var importServiceList = new List<string>();
+            var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
             foreach (var methodMap in MethodMapList)
             {
                 string importService = null;
@@ -96,7 +95,7 @@ namespace FlutterCodeGenerator
                 {
                     if (modelType is ComplexModelType)
                     {
-                        importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
+                        importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
                         if (importService == null)
                         {
                             throw new Exception("Import Service is null");
@@ -110,7 +109,7 @@ namespace FlutterCodeGenerator
                 var returnModelType = methodMap.ReturnParameterModelType;
                 if (returnModelType is ComplexModelType)
                 {
-                    importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == returnModelType.ParameterType.Name && x.Key.ParameterType.Namespace == returnModelType.ParameterType.Namespace).Value;
+                    importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == returnModelType.ParameterType.Name && x.Key.ParameterType.Namespace == returnModelType.ParameterType.Namespace).Value;
                     if (importService == null)
                     {
                         throw new Exception("Import Service is null");
@@ -125,7 +124,7 @@ namespace FlutterCodeGenerator
                     var subReturnType = listModelType.GenericArgumentModelType;
                     if (subReturnType is ComplexModelType)
                     {
-                        importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
+                        importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
                         if (importService == null)
                         {
                             throw new Exception("Import Service is null");
@@ -141,7 +140,7 @@ namespace FlutterCodeGenerator
                     var subReturnType = arrayModelType.Child;
                     if (subReturnType is ComplexModelType)
                     {
-                        importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
+                        importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
                         if (importService == null)
                         {
                             throw new Exception("Import Service is null");
@@ -158,7 +157,7 @@ namespace FlutterCodeGenerator
                     {
                         if (subReturnType is ComplexModelType)
                         {
-                            importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
+                            importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
                             if (importService == null)
                             {
                                 throw new Exception("Import Service is null");
@@ -179,11 +178,11 @@ namespace FlutterCodeGenerator
                 if (complexReturnTypeName.Contains("<"))
                 {
                     var returnTypeName = complexReturnTypeName.Substring(0, complexReturnTypeName.IndexOf("<"));
-                    importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value;
+                    importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value;
                 }
                 else
                 {
-                    importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value;
+                    importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value;
                 }
                 if (importService == null)
                 {
@@ -292,4 +291,4 @@ namespace FlutterCodeGenerator
             }
         }
     }
-}
+}