CodeGenerator.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 interfaceTypes = assembly.GetTypes().Where(x => x.FullName.Contains(".Interface.") && x.Name.Contains("Service") && !x.Name.Contains("DBService") && !x.Name.Contains("Management") && !x.Name.Contains("Notification")).ToList();
  50. foreach (var interfaceType in interfaceTypes)
  51. {
  52. var sericeName = interfaceType.Name[1..];
  53. var serviceMap = new ServiceMap(interfaceType);
  54. _serviceMapDictionary.Add(LetterConverterHelper.FirstCharToLower(sericeName), serviceMap);
  55. }
  56. }
  57. private void CheckDirectory()
  58. {
  59. if (Directory.Exists(_generatedFolderPath))
  60. {
  61. Directory.Delete(_generatedFolderPath, true);
  62. }
  63. Directory.CreateDirectory(_generatedFolderPath);
  64. Directory.CreateDirectory(_serviceFolderPath);
  65. }
  66. public void GeneratedCode()
  67. {
  68. CheckDirectory();
  69. foreach (var serviceMap in _serviceMapDictionary)
  70. {
  71. var serviceDartFileName = serviceMap.Key[0..^7] + ".dart";
  72. var serviceModelDartFileName = serviceMap.Key[0..^7] + ".m.dart";
  73. var serviceDartPath = Path.Combine(_serviceFolderPath, serviceDartFileName);
  74. var serviceModelDartPath = Path.Combine(_serviceFolderPath, serviceModelDartFileName);
  75. var serviceModelDartString = serviceMap.Value.GetServiceModelDartString();
  76. var serviceString = serviceMap.Value.GetServiceDartString();
  77. var serviceDartString = new StringBuilder();
  78. serviceDartString.AppendLine(StringDartCore);
  79. serviceDartString.AppendLine();
  80. serviceDartString.AppendLine(StringClientBase);
  81. if (serviceString.Contains("FJsonConvert"))
  82. {
  83. serviceDartString.AppendLine(StringJsonConvert);
  84. }
  85. serviceDartString.AppendLine();
  86. if (serviceModelDartString != null)
  87. {
  88. File.WriteAllText(serviceModelDartPath, serviceModelDartString);
  89. serviceDartString.AppendLine($"import '{serviceModelDartFileName}';");
  90. _generatedServiceModelFileNameList.Add(serviceModelDartFileName);
  91. }
  92. serviceDartString.AppendLine();
  93. serviceDartString.AppendLine(serviceString);
  94. File.WriteAllText(serviceDartPath, serviceDartString.ToString());
  95. _generatedServiceFileNameList.Add(serviceDartFileName);
  96. }
  97. GenerateIndexDart();
  98. GenerateRpcDart();
  99. }
  100. private void GenerateRpcDart()
  101. {
  102. var dartString = new StringBuilder();
  103. var rpcFilePath = Path.Combine(_generatedFolderPath, _rpcDart);
  104. var rpcString = File.ReadAllText(_rpcTextPath);
  105. var tempStringArray = rpcString.Split("*******Separator******");
  106. if (tempStringArray.Length != 2)
  107. {
  108. throw new Exception("The rpc text is Wrong!");
  109. }
  110. dartString.AppendLine(tempStringArray[0]);
  111. foreach (var serviceMap in _serviceMapDictionary)
  112. {
  113. var serviceName = LetterConverterHelper.FirstCharToUpper_2(serviceMap.Key);
  114. var actionName = serviceMap.Key[0..^7];
  115. dartString.AppendLine($"\t{serviceName} get {actionName} =>");
  116. dartString.AppendLine($"\tfindService(() => new {serviceName}(currentHostAddress));");
  117. dartString.AppendLine();
  118. }
  119. dartString.AppendLine(tempStringArray[1]);
  120. File.WriteAllText(rpcFilePath, dartString.ToString());
  121. }
  122. private void GenerateIndexDart()
  123. {
  124. var dartString = new StringBuilder();
  125. var indexFilePath = Path.Combine(_serviceFolderPath, _indexDart);
  126. foreach (var fileName in _generatedServiceFileNameList.OrderBy(x => x))
  127. {
  128. dartString.AppendLine($"export '{fileName}';");
  129. }
  130. foreach (var fileName in _generatedServiceModelFileNameList.OrderBy(x => x))
  131. {
  132. dartString.AppendLine($"export '{fileName}';");
  133. }
  134. File.WriteAllText(indexFilePath, dartString.ToString());
  135. }
  136. }
  137. }