Browse Source

实现相同的实体类不再重复生成

Felix 3 years ago
parent
commit
2e6a4b308d
3 changed files with 96 additions and 30 deletions
  1. 10 16
      CodeGenerator.cs
  2. 86 14
      ServiceMap.cs
  3. BIN
      WingInterfaceLibrary.dll

+ 10 - 16
CodeGenerator.cs

@@ -11,21 +11,20 @@ namespace FlutterCodeGenerator
     {
         private readonly string _generatedFolderPath;
         private readonly string _dllPath;
-
         private readonly string _rpcTextPath;
         private readonly string _serviceFolderPath;
         private const string _dllName = "WingInterfaceLibrary.dll";
-        public static readonly string StringDartCore = "import 'dart:core';";
-        public static readonly string StringClientBase = "import 'package:fis_jsonrpc/client_base.dart';";
-        public static readonly string StringJsonConvert = "import 'package:fis_common/json_convert.dart';";
-
-        public static readonly string StringUtils = "import 'package:fis_jsonrpc/utils.dart';";
-
         private readonly string _indexDart = "index.dart";
         private readonly string _rpcDart = "rpc.dart";
         private Dictionary<string, ServiceMap> _serviceMapDictionary;
         private List<string> _generatedServiceFileNameList;
 
+        public static readonly string StringDartCore = "import 'dart:core';";
+        public static readonly string StringClientBase = "import 'package:fis_jsonrpc/client_base.dart';";
+        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 CodeGenerator(string dllPath, string filePath)
         {
             _generatedFolderPath = filePath;
@@ -34,7 +33,9 @@ namespace FlutterCodeGenerator
             _rpcTextPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rpc.txt");
             _serviceMapDictionary = new Dictionary<string, ServiceMap>();
             _generatedServiceFileNameList = new List<string>() { "platform.dart" };
+            AlreadyGeneratedList = new Dictionary<ComplexModelType, string>();
             LoadDll();
+
         }
 
         private void LoadDll()
@@ -74,10 +75,9 @@ namespace FlutterCodeGenerator
                 var serviceModelDartFileName = serviceMap.Key[0..^7] + ".m.dart";
                 var serviceDartPath = Path.Combine(_serviceFolderPath, serviceDartFileName);
                 var serviceModelDartPath = Path.Combine(_serviceFolderPath, serviceModelDartFileName);
-                var serviceString = serviceMap.Value.GetServiceDartString();
                 var serviceModelDartString = serviceMap.Value.GetServiceModelDartString();
+                var serviceString = serviceMap.Value.GetServiceDartString();
                 var serviceDartString = new StringBuilder();
-                var modelDartString = new StringBuilder();
                 serviceDartString.AppendLine(StringDartCore);
                 serviceDartString.AppendLine();
                 serviceDartString.AppendLine(StringClientBase);
@@ -88,13 +88,7 @@ namespace FlutterCodeGenerator
                 serviceDartString.AppendLine();
                 if (serviceModelDartString != null)
                 {
-                    if (serviceModelDartString.Contains("DateTime"))
-                    {
-                        modelDartString.AppendLine(StringUtils);
-                        modelDartString.AppendLine();
-                    }
-                    modelDartString.AppendLine(serviceModelDartString);
-                    File.WriteAllText(serviceModelDartPath, modelDartString.ToString());
+                    File.WriteAllText(serviceModelDartPath, serviceModelDartString);
                     serviceDartString.AppendLine($"import '{serviceModelDartFileName}';");
                 }
                 serviceDartString.AppendLine();

+ 86 - 14
ServiceMap.cs

@@ -11,7 +11,7 @@ namespace FlutterCodeGenerator
 
         public static List<ComplexModelType> TemporaryList { get; set; }
         private List<ComplexModelType> _usedComplexModelTypeList;
-        private List<Type> _userDefinedGenericTypeList;
+
         private List<string> _userDefinedComplexReturnTypeList;
         private string _serviceName;
 
@@ -22,7 +22,6 @@ namespace FlutterCodeGenerator
             MethodMapList = new List<MethodMap>();
             var methodsList = type.GetMethods();
             _serviceName = type.Name[1..];
-            _userDefinedGenericTypeList = new List<Type>();
             foreach (var method in methodsList)
             {
                 var methodMap = new MethodMap(method);
@@ -35,40 +34,114 @@ namespace FlutterCodeGenerator
         public string GetServiceModelDartString()
         {
             var dartString = new StringBuilder();
+            var importServiceList = new List<string>();
             foreach (var modelType in _usedComplexModelTypeList)
             {
-                if (modelType is UserDefinedGenericModelType)
+                if (!CodeGenerator.AlreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name))
                 {
-                    var genericType = modelType.ParameterType.GetGenericTypeDefinition();
-                    if (!_userDefinedGenericTypeList.Contains(genericType))
-                    {
-                        dartString.AppendLine(modelType.GetDartString());
-                        _userDefinedGenericTypeList.Add(genericType);
-                    }
+                    CodeGenerator.AlreadyGeneratedList.Add(modelType, _serviceName);
+                    dartString.AppendLine(modelType.GetDartString());
                 }
                 else
                 {
-                    dartString.AppendLine(modelType.GetDartString());
+                    var importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name).Value;
+                    if (importService == null)
+                    {
+                        throw new Exception("Import Service is null");
+                    }
+                    if (importService != _serviceName && !importServiceList.Contains(importService))
+                    {
+                        importServiceList.Add(importService);
+                    }
                 }
-
             }
             if (string.IsNullOrWhiteSpace(dartString.ToString()))
             {
                 return null;
             }
             var serviceModelDartString = new StringBuilder();
-            if (dartString.ToString().Contains("FJsonConvert"))
+            foreach (var importService in importServiceList)
+            {
+                serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7]) }.m.dart';");
+            }
+            if (importServiceList.Count > 0)
+            {
+                serviceModelDartString.AppendLine();
+            }
+
+            var dartStringInfo = dartString.ToString();
+            if (dartStringInfo.Contains("JsonRpcUtils"))
+            {
+                serviceModelDartString.AppendLine(CodeGenerator.StringUtils);
+                serviceModelDartString.AppendLine();
+            }
+
+            if (dartStringInfo.Contains("FJsonConvert"))
             {
                 serviceModelDartString.AppendLine(CodeGenerator.StringJsonConvert);
                 serviceModelDartString.AppendLine();
             }
-            serviceModelDartString.AppendLine(dartString.ToString());
+            serviceModelDartString.AppendLine(dartStringInfo);
             return serviceModelDartString.ToString();
         }
 
         public string GetServiceDartString()
         {
             var serviceDartString = new StringBuilder();
+            var importServiceList = new List<string>();
+            foreach (var methodMap in MethodMapList)
+            {
+                string importService = null;
+                foreach (var modelType in methodMap.ParameterModeTypes)
+                {
+                    if (modelType is ComplexModelType)
+                    {
+                        importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name).Value;
+                        if (importService == null)
+                        {
+                            throw new Exception("Import Service is null");
+                        }
+                        if (importService != _serviceName && !importServiceList.Contains(importService))
+                        {
+                            importServiceList.Add(importService);
+                        }
+                    }
+                }
+            }
+
+            var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList();
+            foreach (var complexReturnTypeName in complexReturnTypeNameList)
+            {
+                string importService;
+                if (complexReturnTypeName.Contains("<"))
+                {
+                    var returnTypeName = complexReturnTypeName.Substring(0, complexReturnTypeName.IndexOf("<"));
+                    importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value;
+                }
+                else
+                {
+                    importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value;
+                }
+                if (importService == null)
+                {
+                    throw new Exception("Import Service is null");
+                }
+                if (importService != _serviceName && !importServiceList.Contains(importService))
+                {
+                    importServiceList.Add(importService);
+                }
+            }
+
+            foreach (var importService in importServiceList)
+            {
+                serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7]) }.m.dart';");
+            }
+            if (importServiceList.Count > 0)
+            {
+                serviceDartString.AppendLine();
+            }
+
+            serviceDartString.AppendLine();
             serviceDartString.AppendLine($"class {_serviceName} extends JsonRpcClientBase {{");
             serviceDartString.AppendLine($"\t{_serviceName}(");
             serviceDartString.AppendLine("\t\tString host, {");
@@ -81,7 +154,6 @@ namespace FlutterCodeGenerator
             serviceDartString.AppendLine("\t\t\t\t\t\theaders: headers,");
             serviceDartString.AppendLine("\t\t\t\t\t\ttimeout: timeout,");
 
-            var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList();
             if (complexReturnTypeNameList.Count() == 0)
             {
                 serviceDartString.AppendLine("\t\t\t\t);");

BIN
WingInterfaceLibrary.dll