CodeGenerator.cs 9.3 KB

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