CodeGeneratorForWing.cs 10 KB

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