-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFunctionHelper.cs
More file actions
279 lines (247 loc) · 11.7 KB
/
FunctionHelper.cs
File metadata and controls
279 lines (247 loc) · 11.7 KB
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
272
273
274
275
276
277
278
279
using Senparc.CO2NET.Extensions;
using Senparc.CO2NET.Helpers;
using Senparc.CO2NET.Trace;
using Senparc.Ncf.Core.AppServices;
using Senparc.Ncf.Core.Exceptions;
using Senparc.Ncf.XncfBase.FunctionRenders;
using Senparc.Ncf.XncfBase.Functions.Parameters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Senparc.Ncf.XncfBase.Functions
{
/// <summary>
/// Function 帮助类
/// </summary>
public class FunctionHelper
{
/// <summary>
/// 记录日志
/// </summary>
/// <param name="sb"></param>
/// <param name="msg"></param>
public static void RecordLog(StringBuilder sb, string msg)
{
sb.AppendLine($"[{SystemTime.Now.ToString()}]\t{msg}");
}
/// <summary>
/// 获取所有参数的信息列表
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="functionRenderBag">FunctionRenderBag</param>
/// <param name="tryLoadData">是否尝试载入数据(参数必须实现 IFunctionParameterLoadDataBase 接口)</param>
/// <returns></returns>
public static async Task<List<FunctionParameterInfo>> GetFunctionParameterInfoAsync(IServiceProvider serviceProvider, FunctionRenderBag functionRenderBag, bool tryLoadData = true)
{
var functionParameterType = functionRenderBag.FunctionParameterType;
IAppRequest paraObj = null;
if (ReflectionHelper.HasParameterlessConstructor(functionParameterType))
{
//包含不带参数的构造函数
paraObj = functionParameterType.Assembly.CreateInstance(functionParameterType.FullName) as IAppRequest;//TODO:通过 DI 生成
if (tryLoadData && paraObj is FunctionAppRequestBase functionRequestPara)
{
try
{
await functionRequestPara.LoadData(serviceProvider);//载入原有信息
}
catch (Exception ex)
{
SenparcTrace.BaseExceptionLog(ex);
throw;
}
}
}
var props = functionParameterType.GetProperties();
ParameterType parameterType = ParameterType.Text;
List<FunctionParameterInfo> result = new List<FunctionParameterInfo>();
foreach (var prop in props)
{
if (ShouldIgnoreProperty(prop))
{
continue;
}
SelectionList selectionList = null;
var filterable = false;
var allowCreate = false;
parameterType = ParameterType.Text;//默认为文本内容
var functionParameterUi = prop.GetCustomAttribute<FunctionParameterUiAttribute>();
if (functionParameterUi != null)
{
parameterType = functionParameterUi.ParameterType;
filterable = functionParameterUi.Filterable;
allowCreate = functionParameterUi.AllowCreate;
if (!functionParameterUi.SelectionListPropertyName.IsNullOrEmpty() && paraObj != null)
{
var selectionProp = functionParameterType.GetProperty(functionParameterUi.SelectionListPropertyName);
if (selectionProp?.PropertyType == typeof(SelectionList))
{
selectionList = selectionProp.GetValue(paraObj, null) as SelectionList;
}
}
}
else if (prop.PropertyType == typeof(SelectionList))
{
var selections = prop.GetValue(paraObj, null) as SelectionList;
switch (selections.SelectionType)
{
case SelectionType.DropDownList:
parameterType = ParameterType.DropDownList;
break;
case SelectionType.CheckBoxList:
parameterType = ParameterType.CheckBoxList;
break;
default:
//TODO: throw
break;
}
selectionList = selections;
}
else if (prop.Name.Contains("SECRET", StringComparison.OrdinalIgnoreCase) || prop.GetCustomAttribute<PasswordAttribute>() != null)
{
parameterType = ParameterType.Password;
}
var name = prop.Name;
string title = null;
string description = null;
var isRequired = prop.GetCustomAttribute<RequiredAttribute>() != null;
var descriptionAttr = prop.GetCustomAttribute<DescriptionAttribute>();
var maxLengthAttr = prop.GetCustomAttribute<MaxLengthAttribute>();
var maxLength = 0;
if (descriptionAttr != null && descriptionAttr.Description != null)
{
//分割:名称||说明
var descriptionAttrArr = descriptionAttr.Description.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
title = descriptionAttrArr[0];
if (descriptionAttrArr.Length > 1)
{
description = descriptionAttrArr[1];
}
}
if (maxLengthAttr != null)
{
maxLength = maxLengthAttr.Length;
}
var systemType = prop.PropertyType.Name;
object value = null;
try
{
value = prop.GetValue(paraObj);
}
catch (Exception ex)
{
SenparcTrace.BaseExceptionLog(ex);
}
var functionParamInfo = new FunctionParameterInfo(name, title, description, isRequired, systemType, parameterType,
selectionList ?? new SelectionList(SelectionType.Unknown), value, maxLength,
filterable, allowCreate);
result.Add(functionParamInfo);
}
return result;
}
private static bool ShouldIgnoreProperty(PropertyInfo propertyInfo)
{
if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null)
{
return true;
}
return propertyInfo.CustomAttributes.Any(z => z.AttributeType.FullName == "Newtonsoft.Json.JsonIgnoreAttribute");
}
/// <summary>
/// 给 SelectionList 对象添加当前解决方案中的 XNCF 项目
/// (扫描当前解决方案包含的所有领域项目)
/// </summary>
/// <param name="mustHaveXncfModule">当前解决方案是否必须包含 XNCF 项目</param>
/// <param name="rootDir">查找 XNCF 模块根目录,如果留 null,则使用 <code>System.IO.Directory.GetCurrentDirectory()</code>获取,并且查找 .sln 所在文件夹</param>
/// <param name="additionalProjects">除了标准的 XNCF 以外额外的项目</param>
public static List<SelectionItem> LoadXncfProjects(bool mustHaveXncfModule = false, string rootDir = null, params string[] additionalProjects)
{
var selectList = new List<SelectionItem>();
var currentDir = rootDir ?? System.IO.Directory.GetCurrentDirectory();
while (currentDir != null)
{
var slnFile = Directory.GetFiles(currentDir, "*.sln");
if (slnFile.Length > 0)
{
break;
}
currentDir = Directory.GetParent(currentDir).FullName;
}
if (currentDir != null)
{
//找到了 SLN 文件,开始展开地毯式搜索
//第一步:查找 XNCF
var projectFolders = Directory.GetDirectories(currentDir, "*.XNCF.*", SearchOption.AllDirectories).ToList();
if (additionalProjects != null && additionalProjects.Length > 0)
{
foreach (var proj in additionalProjects)
{
var addProjectFolders = Directory.GetDirectories(currentDir, proj, SearchOption.AllDirectories);
if (addProjectFolders.Count() > 0)
{
projectFolders.AddRange(addProjectFolders);
}
}
}
foreach (var projectFolder in projectFolders)
{
//第二步:查看 Register 文件是否存在
var registerFilePath = Path.Combine(projectFolder, "Register.cs");
if (!File.Exists(registerFilePath))
{
continue;//不存在则跳过
}
//第三步:检查 Register 文件是否合格
var registerContent = File.ReadAllText(registerFilePath);
if (registerContent.Contains("[XncfRegister]") &&
registerContent.Contains("IXncfRegister") &&
registerContent.Contains("Uid"))
{
selectList.Add(
new SelectionItem(
projectFolder,
projectFolder, /*Path.GetFileName(projectFolder)*/
projectFolder/*Path.GetDirectoryName(projectFolder)*/));
}
}
}
if (mustHaveXncfModule && (currentDir == null || selectList.Count == 0))
{
selectList.Add(
new SelectionItem(
"N/A",
"没有发现任何可用的 XNCF 项目,请确保你正在一个标准的 NCF 开发环境中!"));
}
return selectList;
}
/// <summary>
/// 获取 xxxSenparcEntities.cs 数据库文件
/// </summary>
/// <param name="projectPath"></param>
/// <param name="dbType">数据库类型</param>
/// <returns></returns>
/// <exception cref="NcfExceptionBase"></exception>
public static string GetSenparcEntitiesFilePath(string projectPath, string dbType)
{
var databaseModelPath = Path.Combine(projectPath, "Domain", "Models", "DatabaseModel");
var files = Directory.GetFiles(databaseModelPath, "*SenparcEntities.cs");
if (files.Length == 0)
{
throw new NcfExceptionBase($"目录 {databaseModelPath} 下没有找到 SenparcEntities.cs 结尾的文件");
}
var databaseFile = Path.GetFileName(files[0]).Replace(".cs", "");
if (!dbType.IsNullOrEmpty())
{
databaseFile += "_" + dbType;
}
return databaseFile;
}
}
}