using FlutterCodeGenerator.Map; using FlutterCodeGenerator.Map.Interface; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; namespace FlutterCodeGenerator.Helper { public class CodeGenerator { private readonly string _generatedFolderPath; private readonly string _dllPath; private readonly string _rpcTextPath; private readonly string _serviceFolderPath; private const string _dllName = "WingInterfaceLibrary.dll"; private readonly string _indexDart = "index.dart"; private readonly string _rpcDart = "rpc.dart"; private Dictionary _serviceMapDictionary; private List _generatedServiceFileNameList; private List _generatedServiceModelFileNameList; public static readonly string StringDartCore = "import 'dart:core';"; public static readonly string StringClientBase = "import 'package:fis_jsonrpc/client_base.dart';"; public static readonly string StringJsonConvert = "import 'package:fis_common/json_convert.dart';"; public static readonly string StringUtils = "import 'package:fis_jsonrpc/utils.dart';"; public CodeGenerator(string dllPath, string filePath) { _generatedFolderPath = filePath; _dllPath = dllPath; _serviceFolderPath = Path.Combine(_generatedFolderPath, "services"); _rpcTextPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rpc.txt"); _serviceMapDictionary = new Dictionary(); _generatedServiceFileNameList = new List() { "platform.dart" }; _generatedServiceModelFileNameList = new List() { "platform.m.dart" }; LoadDll(); } private void LoadDll() { var dll = Directory.GetFiles(_dllPath, _dllName).FirstOrDefault(); if (dll == null) { throw new Exception($"The {_dllName} is not found!"); } var assemblybytes = File.ReadAllBytes(dll); var assembly = Assembly.Load(assemblybytes); var allTypes = assembly.GetTypes(); var restTypes = allTypes.Where(x => !x.FullName.Contains("DB") && !x.Name.EndsWith("ManagementService") && !x.Name.EndsWith("NotificationService") && !x.Name.Contains(".Internal.")).ToList(); var interfaceTypes = restTypes.Where(x => x.FullName.EndsWith("Service") && x.IsInterface).ToList(); restTypes = restTypes.Except(interfaceTypes).ToList(); var inotificationDTOType = restTypes.FirstOrDefault(x => x.Name.Contains("INotificationDTO")); var notificationEnumType = inotificationDTOType.GetProperties().First().PropertyType; if (notificationEnumType == null) { throw new Exception("Find NotificationEnumType Failed"); } var notificationTypes = restTypes.Where(x => x.GetInterfaces().Any(x => x.Name.Contains("INotificationDTO"))).ToList(); restTypes = restTypes.Except(notificationTypes).ToList(); var restNeedGeneratedTypes = restTypes.Where(x => !x.GetProperties().Any(y => y.PropertyType == typeof(object)) && !x.GetProperties().Any(y => string.IsNullOrWhiteSpace(y.PropertyType.FullName))).ToList(); var notificationServiceMap = new NotificationServiceMap(notificationEnumType, notificationTypes); _serviceMapDictionary.Add("notificationService", notificationServiceMap); foreach (var interfaceType in interfaceTypes) { var sericeName = interfaceType.Name[1..]; var serviceMap = new ServiceMap(interfaceType); _serviceMapDictionary.Add(LetterConverterHelper.FirstCharToLower(sericeName), serviceMap); } var otherServiceMap = new OtherServiceMap(restNeedGeneratedTypes); _serviceMapDictionary.Add("otherService", otherServiceMap); } private void CheckDirectory() { if (Directory.Exists(_generatedFolderPath)) { Directory.Delete(_generatedFolderPath, true); } Directory.CreateDirectory(_generatedFolderPath); Directory.CreateDirectory(_serviceFolderPath); } public void GeneratedCode() { CheckDirectory(); foreach (var serviceMap in _serviceMapDictionary) { var serviceDartFileName = serviceMap.Key[0..^7] + ".dart"; var serviceModelDartFileName = serviceMap.Key[0..^7] + ".m.dart"; var serviceDartPath = Path.Combine(_serviceFolderPath, serviceDartFileName); var serviceModelDartPath = Path.Combine(_serviceFolderPath, serviceModelDartFileName); var serviceModelDartString = serviceMap.Value.GetServiceModelDartString(); var serviceString = serviceMap.Value.GetServiceDartString(); if (serviceMap.Value is ServiceMap) { var serviceDartString = new StringBuilder(); serviceDartString.AppendLine(StringDartCore); serviceDartString.AppendLine(); serviceDartString.AppendLine(StringClientBase); if (serviceString.Contains("FJsonConvert")) { serviceDartString.AppendLine(StringJsonConvert); } serviceDartString.AppendLine(); if (serviceModelDartString != null) { File.WriteAllText(serviceModelDartPath, serviceModelDartString); serviceDartString.AppendLine($"import '{serviceModelDartFileName}';"); _generatedServiceModelFileNameList.Add(serviceModelDartFileName); } serviceDartString.AppendLine(); serviceDartString.AppendLine(serviceString); File.WriteAllText(serviceDartPath, serviceDartString.ToString()); _generatedServiceFileNameList.Add(serviceDartFileName); } else if (serviceMap.Value is NotificationServiceMap notificationServiceMap) { if (serviceModelDartString != null) { File.WriteAllText(serviceModelDartPath, serviceModelDartString); _generatedServiceModelFileNameList.Add(serviceModelDartFileName); } var decodeDartString = notificationServiceMap.GetDecodeDartString(); if (decodeDartString != null) { var decodeDartFileName = "notificationdecoder.dart"; var decodeDartPath = Path.Combine(_serviceFolderPath, decodeDartFileName); File.WriteAllText(decodeDartPath, decodeDartString); _generatedServiceFileNameList.Add(decodeDartFileName); } } else if (serviceMap.Value is OtherServiceMap) { if (serviceModelDartString != null) { File.WriteAllText(serviceModelDartPath, serviceModelDartString); _generatedServiceModelFileNameList.Add(serviceModelDartFileName); } } } GenerateIndexDart(); GenerateRpcDart(); } private void GenerateRpcDart() { var dartString = new StringBuilder(); var rpcFilePath = Path.Combine(_generatedFolderPath, _rpcDart); var rpcString = File.ReadAllText(_rpcTextPath); var tempStringArray = rpcString.Split("*******Separator******"); if (tempStringArray.Length != 2) { throw new Exception("The rpc text is Wrong!"); } dartString.AppendLine(tempStringArray[0]); foreach (var serviceMap in _serviceMapDictionary) { if (serviceMap.Value is ServiceMap) { var serviceName = LetterConverterHelper.FirstCharToUpper_2(serviceMap.Key); var actionName = serviceMap.Key[0..^7]; dartString.AppendLine($"\t{serviceName} get {actionName} =>"); dartString.AppendLine($"\tfindService(() => new {serviceName}(currentHostAddress));"); dartString.AppendLine(); } } dartString.AppendLine(tempStringArray[1]); File.WriteAllText(rpcFilePath, dartString.ToString()); } private void GenerateIndexDart() { var dartString = new StringBuilder(); var indexFilePath = Path.Combine(_serviceFolderPath, _indexDart); foreach (var fileName in _generatedServiceFileNameList.OrderBy(x => x)) { dartString.AppendLine($"export '{fileName}';"); } foreach (var fileName in _generatedServiceModelFileNameList.OrderBy(x => x)) { dartString.AppendLine($"export '{fileName}';"); } File.WriteAllText(indexFilePath, dartString.ToString()); } } }