CodeGeneratorForWing.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using FlutterCodeGenerator.Map;
  2. using FlutterCodeGenerator.Map.Interface;
  3. using FlutterCodeGenerator.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. namespace FlutterCodeGenerator.Helper
  11. {
  12. public class CodeGeneratorForWing
  13. {
  14. private const string WingDllName = "WingInterfaceLibrary.dll";
  15. private const string FlyinsonoFolder = "fis_lib_jsonrpc";
  16. private readonly string _generatedFolderPathForFlyinsono;
  17. private readonly string _dllPath;
  18. private readonly string _rpcTextPath;
  19. private readonly string _serviceFolderPath;
  20. private readonly Dictionary<string, IServiceMap> _serviceMapDictionary;
  21. private readonly List<string> _generatedServiceFileNameList;
  22. private readonly List<string> _generatedServiceModelFileNameList;
  23. public CodeGeneratorForWing(string dllPath, string filePath)
  24. {
  25. GenerateDataCache.Instance.CurrentGenerator = EnumCodeGenerator.Wing.ToString();
  26. _generatedFolderPathForFlyinsono = Path.Combine(filePath, FlyinsonoFolder);
  27. _dllPath = dllPath;
  28. _serviceFolderPath = Path.Combine(_generatedFolderPathForFlyinsono, "services");
  29. _rpcTextPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rpc.txt");
  30. _serviceMapDictionary = new Dictionary<string, IServiceMap>();
  31. _generatedServiceFileNameList = new List<string>() { "platform.dart" };
  32. _generatedServiceModelFileNameList = new List<string>() { "platform.m.dart" };
  33. LoadDll();
  34. GenerateDataCache.Instance.IsLoadFinished = true;
  35. }
  36. private void LoadDll()
  37. {
  38. var dll = Directory.GetFiles(_dllPath, WingDllName).FirstOrDefault();
  39. if (dll == null)
  40. {
  41. throw new DllNotFoundException($"The {WingDllName} is not found!");
  42. }
  43. var assemblybytes = File.ReadAllBytes(dll);
  44. var assembly = Assembly.Load(assemblybytes);
  45. var allTypes = assembly.GetTypes();
  46. var restTypes = allTypes.Where(x => !x.FullName.Contains("DB") && !x.Name.EndsWith("ManagementService") && !x.Name.EndsWith("NotificationService") && !x.FullName.Contains(".Internal.") && !x.FullName.Contains("<>")).ToList();//当类的某个属性只有Get,没有set属性,该类会有<>c的Type
  47. var interfaceTypes = restTypes.Where(x => x.FullName.EndsWith("Service") && x.IsInterface).ToList();
  48. restTypes = restTypes.Except(interfaceTypes).ToList();
  49. var inotificationDTOType = restTypes.FirstOrDefault(x => x.Name.Contains("INotificationDTO"));
  50. var notificationEnumType = inotificationDTOType.GetProperties().First().PropertyType;
  51. if (notificationEnumType == null)
  52. {
  53. throw new Exception("Find NotificationEnumType Failed");
  54. }
  55. var notificationTypes = restTypes.Where(x => x.GetInterfaces().Any(x => x.Name.Contains("INotificationDTO"))).ToList();
  56. restTypes = restTypes.Except(notificationTypes).ToList();
  57. var restNeedGeneratedTypes = restTypes.Where(x => !x.GetProperties().Any(y => y.PropertyType == typeof(object)) && !x.GetProperties().Any(y => string.IsNullOrWhiteSpace(y.PropertyType.FullName))).ToList();
  58. var notificationServiceMap = new NotificationServiceMap(notificationEnumType, notificationTypes);
  59. _serviceMapDictionary.Add("notificationService", notificationServiceMap);
  60. foreach (var interfaceType in interfaceTypes)
  61. {
  62. var sericeName = interfaceType.Name[1..];
  63. var serviceMap = new ServiceMap(interfaceType, null);
  64. _serviceMapDictionary.Add(LetterConverterHelper.FirstCharToLower(sericeName), serviceMap);
  65. }
  66. var otherServiceMap = new OtherServiceMap(restNeedGeneratedTypes);
  67. _serviceMapDictionary.Add("otherService", otherServiceMap);
  68. }
  69. private void CheckDirectory()
  70. {
  71. if (Directory.Exists(_generatedFolderPathForFlyinsono))
  72. {
  73. Directory.Delete(_generatedFolderPathForFlyinsono, true);
  74. }
  75. Directory.CreateDirectory(_generatedFolderPathForFlyinsono);
  76. Directory.CreateDirectory(_serviceFolderPath);
  77. }
  78. public void GeneratedCode()
  79. {
  80. CheckDirectory();
  81. foreach (var serviceMap in _serviceMapDictionary)
  82. {
  83. GenerateDataCache.Instance.CurrentService = serviceMap.Value.ServiceName;
  84. var serviceDartFileName = serviceMap.Key[0..^7] + ".dart";
  85. var serviceModelDartFileName = serviceMap.Key[0..^7] + ".m.dart";
  86. var serviceDartPath = Path.Combine(_serviceFolderPath, serviceDartFileName);
  87. var serviceModelDartPath = Path.Combine(_serviceFolderPath, serviceModelDartFileName);
  88. var serviceModelDartString = serviceMap.Value.GetServiceModelDartString();
  89. var serviceString = serviceMap.Value.GetServiceDartString();
  90. if (serviceMap.Value is ServiceMap)
  91. {
  92. var serviceDartString = new StringBuilder();
  93. serviceDartString.AppendLine(CommonParameters.StringDartCore);
  94. serviceDartString.AppendLine();
  95. serviceDartString.AppendLine(CommonParameters.StringClientBase);
  96. if (serviceString.Contains("FJsonConvert"))
  97. {
  98. serviceDartString.AppendLine(CommonParameters.StringJsonConvert);
  99. }
  100. serviceDartString.AppendLine();
  101. if (serviceModelDartString != null)
  102. {
  103. File.WriteAllText(serviceModelDartPath, serviceModelDartString);
  104. serviceDartString.AppendLine($"import '{serviceModelDartFileName}';");
  105. _generatedServiceModelFileNameList.Add(serviceModelDartFileName);
  106. }
  107. serviceDartString.AppendLine();
  108. serviceDartString.AppendLine(serviceString);
  109. File.WriteAllText(serviceDartPath, serviceDartString.ToString());
  110. _generatedServiceFileNameList.Add(serviceDartFileName);
  111. }
  112. else if (serviceMap.Value is NotificationServiceMap notificationServiceMap)
  113. {
  114. if (serviceModelDartString != null)
  115. {
  116. File.WriteAllText(serviceModelDartPath, serviceModelDartString);
  117. _generatedServiceModelFileNameList.Add(serviceModelDartFileName);
  118. }
  119. var decodeDartString = notificationServiceMap.GetDecodeDartString();
  120. if (decodeDartString != null)
  121. {
  122. var decodeDartFileName = "notificationdecoder.dart";
  123. var decodeDartPath = Path.Combine(_serviceFolderPath, decodeDartFileName);
  124. File.WriteAllText(decodeDartPath, decodeDartString);
  125. _generatedServiceFileNameList.Add(decodeDartFileName);
  126. }
  127. }
  128. else if (serviceMap.Value is OtherServiceMap)
  129. {
  130. if (serviceModelDartString != null)
  131. {
  132. File.WriteAllText(serviceModelDartPath, serviceModelDartString);
  133. _generatedServiceModelFileNameList.Add(serviceModelDartFileName);
  134. }
  135. }
  136. }
  137. GenerateIndexDart();
  138. GenerateRpcDart();
  139. GenerateDataCache.Instance.IsGenerateFinished = true;
  140. }
  141. private void GenerateRpcDart()
  142. {
  143. var dartString = new StringBuilder();
  144. var rpcFilePath = Path.Combine(_generatedFolderPathForFlyinsono, CommonParameters.RpcDart);
  145. var rpcString = File.ReadAllText(_rpcTextPath);
  146. var tempStringArray = rpcString.Split("*******Separator******");
  147. if (tempStringArray.Length != 2)
  148. {
  149. throw new Exception("The rpc text is Wrong!");
  150. }
  151. dartString.AppendLine(tempStringArray[0]);
  152. foreach (var serviceMap in _serviceMapDictionary)
  153. {
  154. GenerateDataCache.Instance.CurrentService = serviceMap.Value.ServiceName;
  155. if (serviceMap.Value is ServiceMap)
  156. {
  157. var serviceName = LetterConverterHelper.FirstCharToUpper_2(serviceMap.Key);
  158. var actionName = serviceMap.Key[0..^7];
  159. dartString.AppendLine($"\t{serviceName} get {actionName} =>");
  160. dartString.AppendLine($"\tfindService(() => new {serviceName}(currentHostAddress));");
  161. dartString.AppendLine();
  162. }
  163. }
  164. dartString.AppendLine(tempStringArray[1]);
  165. File.WriteAllText(rpcFilePath, dartString.ToString());
  166. }
  167. private void GenerateIndexDart()
  168. {
  169. var dartString = new StringBuilder();
  170. var indexFilePath = Path.Combine(_serviceFolderPath, CommonParameters.IndexDart);
  171. foreach (var fileName in _generatedServiceFileNameList.OrderBy(x => x))
  172. {
  173. dartString.AppendLine($"export '{fileName}';");
  174. }
  175. foreach (var fileName in _generatedServiceModelFileNameList.OrderBy(x => x))
  176. {
  177. dartString.AppendLine($"export '{fileName}';");
  178. }
  179. File.WriteAllText(indexFilePath, dartString.ToString());
  180. }
  181. }
  182. }