123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace FlutterCodeGenerator
- {
- 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<string, ServiceMap> _serviceMapDictionary;
- private List<string> _generatedServiceFileNameList;
- private List<string> _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<string, ServiceMap>();
- _generatedServiceFileNameList = new List<string>() { "platform.dart" };
- _generatedServiceModelFileNameList = new List<string>() { "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 liveConsultationService = allTypes.FirstOrDefault(x => x.FullName.Contains(".LiveConsultation") && x.Name.EndsWith("Service"));
- if (liveConsultationService == null)
- {
- throw new Exception("Live Consultation Service Load Failed.");
- }
- 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();
- interfaceTypes.Add(liveConsultationService);
- foreach (var interfaceType in interfaceTypes)
- {
- var sericeName = interfaceType.Name[1..];
- var serviceMap = new ServiceMap(interfaceType);
- _serviceMapDictionary.Add(LetterConverterHelper.FirstCharToLower(sericeName), serviceMap);
- }
- }
- 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();
- 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);
- }
- 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)
- {
- 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());
- }
- }
- }
|