CodeGenerator.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace FlutterCodeGenerator
  8. {
  9. public class CodeGenerator
  10. {
  11. private readonly string _generatedFolderPath;
  12. private readonly string _dllPath;
  13. private readonly string _rpcTextPath;
  14. private readonly string _serviceFolderPath;
  15. private const string _dllName = "WingInterfaceLibrary.dll";
  16. private readonly string _indexDart = "index.dart";
  17. private readonly string _rpcDart = "rpc.dart";
  18. private Dictionary<string, IServiceMap> _serviceMapDictionary;
  19. private List<string> _generatedServiceFileNameList;
  20. private List<string> _generatedServiceModelFileNameList;
  21. public static readonly string StringDartCore = "import 'dart:core';";
  22. public static readonly string StringClientBase = "import 'package:fis_jsonrpc/client_base.dart';";
  23. public static readonly string StringJsonConvert = "import 'package:fis_common/json_convert.dart';";
  24. public static readonly string StringUtils = "import 'package:fis_jsonrpc/utils.dart';";
  25. public CodeGenerator(string dllPath, string filePath)
  26. {
  27. _generatedFolderPath = filePath;
  28. _dllPath = dllPath;
  29. _serviceFolderPath = Path.Combine(_generatedFolderPath, "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. }
  36. private void LoadDll()
  37. {
  38. var dll = Directory.GetFiles(_dllPath, _dllName).FirstOrDefault();
  39. if (dll == null)
  40. {
  41. throw new Exception($"The {_dllName} 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")).ToList();
  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);
  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(_generatedFolderPath))
  72. {
  73. Directory.Delete(_generatedFolderPath, true);
  74. }
  75. Directory.CreateDirectory(_generatedFolderPath);
  76. Directory.CreateDirectory(_serviceFolderPath);
  77. }
  78. public void GeneratedCode()
  79. {
  80. CheckDirectory();
  81. foreach (var serviceMap in _serviceMapDictionary)
  82. {
  83. var serviceDartFileName = serviceMap.Key[0..^7] + ".dart";
  84. var serviceModelDartFileName = serviceMap.Key[0..^7] + ".m.dart";
  85. var serviceDartPath = Path.Combine(_serviceFolderPath, serviceDartFileName);
  86. var serviceModelDartPath = Path.Combine(_serviceFolderPath, serviceModelDartFileName);
  87. var serviceModelDartString = serviceMap.Value.GetServiceModelDartString();
  88. var serviceString = serviceMap.Value.GetServiceDartString();
  89. if (serviceMap.Value is ServiceMap)
  90. {
  91. var serviceDartString = new StringBuilder();
  92. serviceDartString.AppendLine(StringDartCore);
  93. serviceDartString.AppendLine();
  94. serviceDartString.AppendLine(StringClientBase);
  95. if (serviceString.Contains("FJsonConvert"))
  96. {
  97. serviceDartString.AppendLine(StringJsonConvert);
  98. }
  99. serviceDartString.AppendLine();
  100. if (serviceModelDartString != null)
  101. {
  102. File.WriteAllText(serviceModelDartPath, serviceModelDartString);
  103. serviceDartString.AppendLine($"import '{serviceModelDartFileName}';");
  104. _generatedServiceModelFileNameList.Add(serviceModelDartFileName);
  105. }
  106. serviceDartString.AppendLine();
  107. serviceDartString.AppendLine(serviceString);
  108. File.WriteAllText(serviceDartPath, serviceDartString.ToString());
  109. _generatedServiceFileNameList.Add(serviceDartFileName);
  110. }
  111. else if (serviceMap.Value is NotificationServiceMap notificationServiceMap)
  112. {
  113. if (serviceModelDartString != null)
  114. {
  115. File.WriteAllText(serviceModelDartPath, serviceModelDartString);
  116. _generatedServiceModelFileNameList.Add(serviceModelDartFileName);
  117. }
  118. var decodeDartString = notificationServiceMap.GetDecodeDartString();
  119. if (decodeDartString != null)
  120. {
  121. var decodeDartFileName = "notificationdecoder.dart";
  122. var decodeDartPath = Path.Combine(_serviceFolderPath, decodeDartFileName);
  123. File.WriteAllText(decodeDartPath, decodeDartString);
  124. _generatedServiceFileNameList.Add(decodeDartFileName);
  125. }
  126. }
  127. else if (serviceMap.Value is OtherServiceMap)
  128. {
  129. if (serviceModelDartString != null)
  130. {
  131. File.WriteAllText(serviceModelDartPath, serviceModelDartString);
  132. _generatedServiceModelFileNameList.Add(serviceModelDartFileName);
  133. }
  134. }
  135. }
  136. GenerateIndexDart();
  137. GenerateRpcDart();
  138. }
  139. private void GenerateRpcDart()
  140. {
  141. var dartString = new StringBuilder();
  142. var rpcFilePath = Path.Combine(_generatedFolderPath, _rpcDart);
  143. var rpcString = File.ReadAllText(_rpcTextPath);
  144. var tempStringArray = rpcString.Split("*******Separator******");
  145. if (tempStringArray.Length != 2)
  146. {
  147. throw new Exception("The rpc text is Wrong!");
  148. }
  149. dartString.AppendLine(tempStringArray[0]);
  150. foreach (var serviceMap in _serviceMapDictionary)
  151. {
  152. if (serviceMap.Value is ServiceMap)
  153. {
  154. var serviceName = LetterConverterHelper.FirstCharToUpper_2(serviceMap.Key);
  155. var actionName = serviceMap.Key[0..^7];
  156. dartString.AppendLine($"\t{serviceName} get {actionName} =>");
  157. dartString.AppendLine($"\tfindService(() => new {serviceName}(currentHostAddress));");
  158. dartString.AppendLine();
  159. }
  160. }
  161. dartString.AppendLine(tempStringArray[1]);
  162. File.WriteAllText(rpcFilePath, dartString.ToString());
  163. }
  164. private void GenerateIndexDart()
  165. {
  166. var dartString = new StringBuilder();
  167. var indexFilePath = Path.Combine(_serviceFolderPath, _indexDart);
  168. foreach (var fileName in _generatedServiceFileNameList.OrderBy(x => x))
  169. {
  170. dartString.AppendLine($"export '{fileName}';");
  171. }
  172. foreach (var fileName in _generatedServiceModelFileNameList.OrderBy(x => x))
  173. {
  174. dartString.AppendLine($"export '{fileName}';");
  175. }
  176. File.WriteAllText(indexFilePath, dartString.ToString());
  177. }
  178. }
  179. }