CodeGenerator.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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, ServiceMap> _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 static Dictionary<ComplexModelType, string> AlreadyGeneratedList;
  26. public static Dictionary<string, Dictionary<ModelType, int>> ConflictModelTypeList;
  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, ServiceMap>();
  34. _generatedServiceFileNameList = new List<string>() { "platform.dart" };
  35. _generatedServiceModelFileNameList = new List<string>() { "platform.m.dart" };
  36. AlreadyGeneratedList = new Dictionary<ComplexModelType, string>();
  37. ConflictModelTypeList = new Dictionary<string, Dictionary<ModelType, int>>();
  38. LoadDll();
  39. }
  40. private void LoadDll()
  41. {
  42. var dll = Directory.GetFiles(_dllPath, _dllName).FirstOrDefault();
  43. if (dll == null)
  44. {
  45. throw new Exception($"The {_dllName} is not found!");
  46. }
  47. var assemblybytes = File.ReadAllBytes(dll);
  48. var assembly = Assembly.Load(assemblybytes);
  49. var allTypes= assembly.GetTypes();
  50. var liveConsultationService = allTypes.FirstOrDefault(x => x.FullName.Contains(".LiveConsultation") && x.Name.EndsWith("Service"));
  51. if (liveConsultationService == null)
  52. {
  53. throw new Exception("Live Consultation Service Load Failed.");
  54. }
  55. var interfaceTypes = allTypes.Where(x => x.FullName.Contains(".Interface.") && x.Name.EndsWith("Service") && !x.Name.EndsWith("DBService") && !x.Name.EndsWith("ManagementService") && !x.Name.EndsWith("NotificationService")).ToList();
  56. interfaceTypes.Add(liveConsultationService);
  57. foreach (var interfaceType in interfaceTypes)
  58. {
  59. var sericeName = interfaceType.Name[1..];
  60. var serviceMap = new ServiceMap(interfaceType);
  61. _serviceMapDictionary.Add(LetterConverterHelper.FirstCharToLower(sericeName), serviceMap);
  62. }
  63. }
  64. private void CheckDirectory()
  65. {
  66. if (Directory.Exists(_generatedFolderPath))
  67. {
  68. Directory.Delete(_generatedFolderPath, true);
  69. }
  70. Directory.CreateDirectory(_generatedFolderPath);
  71. Directory.CreateDirectory(_serviceFolderPath);
  72. }
  73. public void GeneratedCode()
  74. {
  75. CheckDirectory();
  76. foreach (var serviceMap in _serviceMapDictionary)
  77. {
  78. var serviceDartFileName = serviceMap.Key[0..^7] + ".dart";
  79. var serviceModelDartFileName = serviceMap.Key[0..^7] + ".m.dart";
  80. var serviceDartPath = Path.Combine(_serviceFolderPath, serviceDartFileName);
  81. var serviceModelDartPath = Path.Combine(_serviceFolderPath, serviceModelDartFileName);
  82. var serviceModelDartString = serviceMap.Value.GetServiceModelDartString();
  83. var serviceString = serviceMap.Value.GetServiceDartString();
  84. var serviceDartString = new StringBuilder();
  85. serviceDartString.AppendLine(StringDartCore);
  86. serviceDartString.AppendLine();
  87. serviceDartString.AppendLine(StringClientBase);
  88. if (serviceString.Contains("FJsonConvert"))
  89. {
  90. serviceDartString.AppendLine(StringJsonConvert);
  91. }
  92. serviceDartString.AppendLine();
  93. if (serviceModelDartString != null)
  94. {
  95. File.WriteAllText(serviceModelDartPath, serviceModelDartString);
  96. serviceDartString.AppendLine($"import '{serviceModelDartFileName}';");
  97. _generatedServiceModelFileNameList.Add(serviceModelDartFileName);
  98. }
  99. serviceDartString.AppendLine();
  100. serviceDartString.AppendLine(serviceString);
  101. File.WriteAllText(serviceDartPath, serviceDartString.ToString());
  102. _generatedServiceFileNameList.Add(serviceDartFileName);
  103. }
  104. GenerateIndexDart();
  105. GenerateRpcDart();
  106. }
  107. private void GenerateRpcDart()
  108. {
  109. var dartString = new StringBuilder();
  110. var rpcFilePath = Path.Combine(_generatedFolderPath, _rpcDart);
  111. var rpcString = File.ReadAllText(_rpcTextPath);
  112. var tempStringArray = rpcString.Split("*******Separator******");
  113. if (tempStringArray.Length != 2)
  114. {
  115. throw new Exception("The rpc text is Wrong!");
  116. }
  117. dartString.AppendLine(tempStringArray[0]);
  118. foreach (var serviceMap in _serviceMapDictionary)
  119. {
  120. var serviceName = LetterConverterHelper.FirstCharToUpper_2(serviceMap.Key);
  121. var actionName = serviceMap.Key[0..^7];
  122. dartString.AppendLine($"\t{serviceName} get {actionName} =>");
  123. dartString.AppendLine($"\tfindService(() => new {serviceName}(currentHostAddress));");
  124. dartString.AppendLine();
  125. }
  126. dartString.AppendLine(tempStringArray[1]);
  127. File.WriteAllText(rpcFilePath, dartString.ToString());
  128. }
  129. private void GenerateIndexDart()
  130. {
  131. var dartString = new StringBuilder();
  132. var indexFilePath = Path.Combine(_serviceFolderPath, _indexDart);
  133. foreach (var fileName in _generatedServiceFileNameList.OrderBy(x => x))
  134. {
  135. dartString.AppendLine($"export '{fileName}';");
  136. }
  137. foreach (var fileName in _generatedServiceModelFileNameList.OrderBy(x => x))
  138. {
  139. dartString.AppendLine($"export '{fileName}';");
  140. }
  141. File.WriteAllText(indexFilePath, dartString.ToString());
  142. }
  143. }
  144. }