namespace Qingyun.QingBaiFang.V2.APITest.Web { using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.Text; using System.IO; using System; ////// 构建 /// [TestClass] public class BuildCodeTest { private string _basePath; ////// 构造 /// public BuildCodeTest() { _basePath = Directory.GetParent(AppContext.BaseDirectory).Parent.Parent.Parent.Parent.FullName; } ////// 构建 /// [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); } ////// 模板 /// public void TempCode() { /* * 控制器类或方法 * 管理类或方法 * 服务接口类或方法 * 服务实现类或方法 * 入参类 * 出参类 * 枚举类 * 数据类 */ } ////// 创建控制器方法 /// /// 控制器名 /// 方法名 /// 方法备注 /// 入参的类名 /// 出参的类名 public void CreateControllerAction(string controllerName, string actionName, string actionRemark, string requstClass, string responeseClass) { ListattributeNames = new List (); 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()); } /// /// 创建控制器 /// /// 控制器名 /// 方法名 /// 方法备注 public void CreateController(string className, string classRemark, string inheritName) { string nameSpace = "BuliuderCodeController"; ListusingNames = new List (); 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); } } /// /// 创建入参类 /// /// 类名 /// 备注 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; ListusingNames = new List (); if (!Directory.Exists(codePath)) { string classCode = ClassBuilder(className, classRemark, inheritName, nameSpace, usingNames); File.WriteAllText(codePath, classCode); } } /// /// 创建出参类 /// /// 类名 /// 备注 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; ListusingNames = new List (); if (!Directory.Exists(codePath)) { string classCode = ClassBuilder(className, classRemark, inheritName, nameSpace, usingNames); File.WriteAllText(codePath, classCode); } } /// /// 创建管理类 /// /// 类名 /// 备注 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"; ListusingNames = new List (); if (!Directory.Exists(codePath)) { string classCode = ClassBuilder(className, classRemark, inheritName, nameSpace, usingNames); File.WriteAllText(codePath, classCode); } } /// /// 创建管理类方法 /// /// 类名 /// 方法名 /// 方法备注 /// 入参的类名 /// 出参的类名 public void CreateManagerAction(string className, string actionName, string actionRemark, string requstClass, string responeseClass) { ListattributeNames = new List (); 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()); } /// /// 生成Code /// /// 方法名 /// 方法备注 /// 入参的类名 /// 出参的类名 /// 特性([HttpGet(\"GetDetailSearch\")]) ///生成的代码 public string ActionBuilder(string actionName, string actionRemark, string requstClass, string responeseClass, ListattributeNames) { // 控制器类或方法 StringBuilder app = new StringBuilder(); app.AppendLine($""); app.AppendLine($" /// "); app.AppendLine($" /// {actionRemark}"); app.AppendLine($" /// "); app.AppendLine($" /// 入参"); app.AppendLine($" ///结果 "); 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(); } ////// ClassBuilder /// /// 类名 /// 备注 /// 继承类(ITask) /// 命名空间(Qingyun.QingBaiFang.V2.Service.Service.Tasks) /// 引用类 ///生成的Code public string ClassBuilder(string className, string classRemark, string inheritName, string nameSpace, ListusingNames) { 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($" /// "); app.AppendLine($" /// {classRemark}"); app.AppendLine($" /// "); 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(); } } }