代码生成
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(); } } }