namespace Qingyun.QingBaiFang.V2.APITest.Web { using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.Text ...
namespace Qingyun.QingBaiFang.V2.APITest.Web { using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.Text; using System.IO; using System; /// <summary> /// 構建 /// </summary> [TestClass] public class BuildCodeTest { private string _basePath; /// <summary> /// 構造 /// </summary> public BuildCodeTest() { _basePath = Directory.GetParent(AppContext.BaseDirectory).Parent.Parent.Parent.Parent.FullName; } /// <summary> /// 構建 /// </summary> [TestMethod] public void ApprovalMeetingSubmit() { string controllerName = "BuliuderCodeController"; string controllerRemark = "BuliuderCodeController"; string actionName = "Demo"; string actionRemark = "Demo"; string requstClass = "WebDemoInput"; string responeseClass = "WebDemoOutput"; string inheritName = "IDenpendency"; string managerClassName = "BuliuderCodeManager"; CreateController(controllerName, controllerRemark, inheritName); CreateControllerAction(controllerName, actionName, actionRemark, requstClass, responeseClass); CreateInputModel(requstClass, actionRemark); CreateOutputModel(requstClass, actionRemark); CreateManagerClass(managerClassName, controllerRemark); CreateManagerAction(managerClassName, actionName, actionRemark, requstClass, responeseClass); } /// <summary> /// 模板 /// </summary> public void TempCode() { /* * 控制器類或方法 * 管理類或方法 * 服務介面類或方法 * 服務實現類或方法 * 入參類 * 出參類 * 枚舉類 * 數據類 */ } /// <summary> /// 創建控制器方法 /// </summary> /// <param name="controllerName">控制器名</param> /// <param name="actionName">方法名</param> /// <param name="actionRemark">方法備註</param> /// <param name="requstClass">入參的類名</param> /// <param name="responeseClass">出參的類名</param> public void CreateControllerAction(string controllerName, string actionName, string actionRemark, string requstClass, string responeseClass) { List<string> attributeNames = new List<string>(); string codePath = Path.Combine(_basePath, "API", "Controllers", $"{controllerName}.cs"); var controllerCodes = File.ReadAllLines(codePath); var code = ActionBuilder(actionName, actionRemark, requstClass, responeseClass, attributeNames); StringBuilder app = new StringBuilder(); for (var i = 0; i < controllerCodes.Length - 2; i++) { app.AppendLine(controllerCodes[i]); } app.AppendLine(code); app.AppendLine($" }}"); app.AppendLine($"}}"); File.WriteAllText(codePath, app.ToString()); } /// <summary> /// 創建控制器 /// </summary> /// <param name="className">控制器名</param> /// <param name="classRemark">方法名</param> /// <param name="inheritName">方法備註</param> public void CreateController(string className, string classRemark, string inheritName) { string nameSpace = "BuliuderCodeController"; List<string> usingNames = new List<string>(); string codePath = Path.Combine(_basePath, "API", "Controllers", $"{className}.cs"); if (!Directory.Exists(codePath)) { string controllerCode = ClassBuilder(className, classRemark, inheritName, nameSpace, usingNames); File.WriteAllText(codePath, controllerCode); } } /// <summary> /// 創建入參類 /// </summary> /// <param name="className">類名</param> /// <param name="classRemark">備註</param> public void CreateInputModel(string className, string classRemark) { string nameSpace = "Qingyun.QingBaiFang.V2.Common.DTOWeb.Input"; string codePath = Path.Combine(_basePath, "Common", "DTOWeb", "Input", $"{className}.cs"); string inheritName = string.Empty; List<string> usingNames = new List<string>(); if (!Directory.Exists(codePath)) { string classCode = ClassBuilder(className, classRemark, inheritName, nameSpace, usingNames); File.WriteAllText(codePath, classCode); } } /// <summary> /// 創建出參類 /// </summary> /// <param name="className">類名</param> /// <param name="classRemark">備註</param> public void CreateOutputModel(string className, string classRemark) { string nameSpace = "Qingyun.QingBaiFang.V2.Common.DTOWeb.Output"; string codePath = Path.Combine(_basePath, "Common", "DTOWeb", "Output", $"{className}.cs"); string inheritName = string.Empty; List<string> usingNames = new List<string>(); if (!Directory.Exists(codePath)) { string classCode = ClassBuilder(className, classRemark, inheritName, nameSpace, usingNames); File.WriteAllText(codePath, classCode); } } /// <summary> /// 創建管理類 /// </summary> /// <param name="className">類名</param> /// <param name="classRemark">備註</param> public void CreateManagerClass(string className, string classRemark) { string nameSpace = "Qingyun.QingBaiFang.V2.Service.Manager.Demo"; string codePath = Path.Combine(_basePath, "Service", "Manager", "Demo", $"{className}.cs"); string inheritName = "IDenpendency"; List<string> usingNames = new List<string>(); if (!Directory.Exists(codePath)) { string classCode = ClassBuilder(className, classRemark, inheritName, nameSpace, usingNames); File.WriteAllText(codePath, classCode); } } /// <summary> /// 創建管理類方法 /// </summary> /// <param name="className">類名</param> /// <param name="actionName">方法名</param> /// <param name="actionRemark">方法備註</param> /// <param name="requstClass">入參的類名</param> /// <param name="responeseClass">出參的類名</param> public void CreateManagerAction(string className, string actionName, string actionRemark, string requstClass, string responeseClass) { List<string> attributeNames = new List<string>(); string codePath = Path.Combine(_basePath, "Service", "Manager", "Demo", $"{className}.cs"); var controllerCodes = File.ReadAllLines(codePath); var code = ActionBuilder(actionName, actionRemark, requstClass, responeseClass, attributeNames); StringBuilder app = new StringBuilder(); for (var i = 0; i < controllerCodes.Length - 2; i++) { app.AppendLine(controllerCodes[i]); } app.AppendLine(code); app.AppendLine($" }}"); app.AppendLine($"}}"); File.WriteAllText(codePath, app.ToString()); } /// <summary> /// 生成Code /// </summary> /// <param name="actionName">方法名</param> /// <param name="actionRemark">方法備註</param> /// <param name="requstClass">入參的類名</param> /// <param name="responeseClass">出參的類名</param> /// <param name="attributeNames">特性([HttpGet(\"GetDetailSearch\")])</param> /// <returns>生成的代碼</returns> public string ActionBuilder(string actionName, string actionRemark, string requstClass, string responeseClass, List<string> attributeNames) { // 控制器類或方法 StringBuilder app = new StringBuilder(); app.AppendLine($""); app.AppendLine($" /// <summary>"); app.AppendLine($" /// {actionRemark}"); app.AppendLine($" /// </summary>"); app.AppendLine($" /// <param name=\"input\">入參</param>"); app.AppendLine($" /// <returns>結果</returns>"); foreach (var item in attributeNames) { app.AppendLine($" {item}"); } app.AppendLine($" public async Task<{responeseClass}> {actionName}({requstClass} input)"); app.AppendLine($" {{"); app.AppendLine($" return await _meetingManager.{actionName}(input);"); app.AppendLine($" }}"); return app.ToString().Trim(); } /// <summary> /// ClassBuilder /// </summary> /// <param name="className">類名</param> /// <param name="classRemark">備註</param> /// <param name="inheritName">繼承類(ITask)</param> /// <param name="nameSpace">命名空間(Qingyun.QingBaiFang.V2.Service.Service.Tasks)</param> /// <param name="usingNames">引用類</param> /// <returns>生成的Code</returns> public string ClassBuilder(string className, string classRemark, string inheritName, string nameSpace, List<string> usingNames) { StringBuilder app = new StringBuilder(); app.AppendLine($"namespace {nameSpace}"); app.AppendLine($"{{"); foreach (var item in usingNames) { app.AppendLine($"{item.Trim(';')};"); } if (usingNames.Count > 0) { app.AppendLine($""); } app.AppendLine($" /// <summary>"); app.AppendLine($" /// {classRemark}"); app.AppendLine($" /// </summary>"); if (string.IsNullOrEmpty(inheritName)) { app.AppendLine($" public class {className}"); } else { app.AppendLine($" public class {className} : {inheritName}"); } app.AppendLine($" {{"); app.AppendLine($" }}"); app.AppendLine($"}}"); return app.ToString().Trim(); } } }