代码生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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();
        }
    }
}

  

posted @   个人天使  阅读(230)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示