|
@@ -0,0 +1,177 @@
|
|
|
+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 CodeGeneratorForFISLib
|
|
|
+ {
|
|
|
+ private const string FisLibDllName = "FISLib.dll";
|
|
|
+ private const string USMachineFolder = "fis_lib_usMachine_jsonrpc";
|
|
|
+ private const string ServicePrefix = "fis";
|
|
|
+
|
|
|
+ private readonly string _generatedFolderPathForUS;
|
|
|
+ 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 CodeGeneratorForFISLib(string dllPath, string filePath)
|
|
|
+ {
|
|
|
+ GenerateDataCache.Instance.CurrentGenerator = EnumCodeGenerator.FISLib.ToString();
|
|
|
+ _generatedFolderPathForUS = Path.Combine(filePath, USMachineFolder);
|
|
|
+ _dllPath = dllPath;
|
|
|
+ _serviceFolderPath = Path.Combine(_generatedFolderPathForUS, "services");
|
|
|
+ _rpcTextPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "rpc.txt");
|
|
|
+ _serviceMapDictionary = new Dictionary<string, IServiceMap>();
|
|
|
+ _generatedServiceFileNameList = new List<string>();
|
|
|
+ _generatedServiceModelFileNameList = new List<string>();
|
|
|
+ LoadDll();
|
|
|
+ GenerateDataCache.Instance.IsLoadFinished = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void LoadDll()
|
|
|
+ {
|
|
|
+ var dll = Directory.GetFiles(_dllPath, FisLibDllName).FirstOrDefault();
|
|
|
+ if (dll == null)
|
|
|
+ {
|
|
|
+ throw new DllNotFoundException($"The {FisLibDllName} is not found!");
|
|
|
+ }
|
|
|
+ var assemblybytes = File.ReadAllBytes(dll);
|
|
|
+ var assembly = Assembly.Load(assemblybytes);
|
|
|
+ var allTypes = assembly.GetTypes();
|
|
|
+ var interfaceTypes = allTypes.Where(x => x.IsInterface && x.FullName.EndsWith("Service") && x.Name.StartsWith("I") && x.Name != "IService" && !x.Name.Contains("Notification")).ToList();
|
|
|
+ foreach (var interfaceType in interfaceTypes)
|
|
|
+ {
|
|
|
+ var sericeName = ServicePrefix + interfaceType.Name[1..];
|
|
|
+ var serviceMap = new ServiceMap(interfaceType, ServicePrefix);
|
|
|
+ _serviceMapDictionary.Add(LetterConverterHelper.FirstCharToLower(sericeName), serviceMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CheckDirectory()
|
|
|
+ {
|
|
|
+ if (Directory.Exists(_generatedFolderPathForUS))
|
|
|
+ {
|
|
|
+ Directory.Delete(_generatedFolderPathForUS, true);
|
|
|
+ }
|
|
|
+ Directory.CreateDirectory(_generatedFolderPathForUS);
|
|
|
+ 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(_generatedFolderPathForUS, 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|