123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using FlutterCodeGenerator.Map;
- using FlutterCodeGenerator.Map.Interface;
- using FlutterCodeGenerator.Model;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace FlutterCodeGenerator.Helper
- {
- public class CodeGeneratorForWing
- {
- private const string WingDllName = "WingInterfaceLibrary.dll";
- private const string PetCareDllName = "PetCareInterface.dll";
- private const string FlyinsonoFolder = "fis_lib_jsonrpc";
- private readonly string _generatedFolderPathForFlyinsono;
- private readonly string _dllPath;
- private readonly string _rpcTextPath;
- private readonly string _serviceFolderPath;
- private readonly Dictionary<string, IServiceMap> _serviceMapDictionary;
- private readonly List<string> _generatedServiceFileNameList;
- private readonly List<string> _generatedServiceModelFileNameList;
- public CodeGeneratorForWing(string dllPath, string filePath)
- {
- GenerateDataCache.Instance.CurrentGenerator = EnumCodeGenerator.Wing.ToString();
- _generatedFolderPathForFlyinsono = Path.Combine(filePath, FlyinsonoFolder);
- _dllPath = dllPath;
- _serviceFolderPath = Path.Combine(_generatedFolderPathForFlyinsono, "services");
- _rpcTextPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rpc.txt");
- _serviceMapDictionary = new Dictionary<string, IServiceMap>();
- _generatedServiceFileNameList = new List<string>() { "platform.dart" };
- _generatedServiceModelFileNameList = new List<string>() { "platform.m.dart" };
- LoadDll();
- GenerateDataCache.Instance.IsLoadFinished = true;
- }
- private void LoadDll()
- {
- var dll = Directory.GetFiles(_dllPath, WingDllName).FirstOrDefault();
- if (dll == null)
- {
- throw new DllNotFoundException($"The {WingDllName} is not found!");
- }
- var assemblybytes = File.ReadAllBytes(dll);
- var assembly = Assembly.Load(assemblybytes);
- var allTypes = assembly.GetTypes().ToList() ;
- dll = Directory.GetFiles(_dllPath, PetCareDllName).FirstOrDefault();
- if (dll == null)
- {
- throw new DllNotFoundException($"The {PetCareDllName} is not found!");
- }
- assembly = Assembly.LoadFrom(dll);
- var petCareTypes = assembly.GetTypes();
- allTypes.AddRange(petCareTypes);
- var restTypes = allTypes.Where(x => !x.FullName.Contains("DB") && !x.Name.EndsWith("ManagementService") && !x.Name.EndsWith("NotificationService") && !x.FullName.Contains(".Internal.") && !x.FullName.Contains("<>")).ToList();//当类的某个属性只有Get,没有set属性,该类会有<>c的Type
- 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, false);
- if (serviceMap.HasMethod)
- {
- _serviceMapDictionary.Add(LetterConverterHelper.FirstCharToLower(sericeName), serviceMap);
- }
- }
- var otherServiceMap = new OtherServiceMap(restNeedGeneratedTypes);
- _serviceMapDictionary.Add("otherService", otherServiceMap);
- }
- private void CheckDirectory()
- {
- if (Directory.Exists(_generatedFolderPathForFlyinsono))
- {
- Directory.Delete(_generatedFolderPathForFlyinsono, true);
- }
- Directory.CreateDirectory(_generatedFolderPathForFlyinsono);
- Directory.CreateDirectory(_serviceFolderPath);
- }
- public void GeneratedCode()
- {
- CheckDirectory();
- foreach (var serviceMap in _serviceMapDictionary)
- {
- GenerateDataCache.Instance.CurrentService = serviceMap.Value.ServiceName;
- 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(CommonParameters.StringDartCore);
- serviceDartString.AppendLine();
- serviceDartString.AppendLine(CommonParameters.StringClientBase);
- if (serviceString.Contains("FJsonConvert"))
- {
- serviceDartString.AppendLine(CommonParameters.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();
- GenerateDataCache.Instance.IsGenerateFinished = true;
- }
- private void GenerateRpcDart()
- {
- var dartString = new StringBuilder();
- var rpcFilePath = Path.Combine(_generatedFolderPathForFlyinsono, CommonParameters.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)
- {
- GenerateDataCache.Instance.CurrentService = serviceMap.Value.ServiceName;
- 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, CommonParameters.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());
- }
- }
- }
|