浏览代码

调整生成逻辑

lichunlei 5 月之前
父节点
当前提交
454a9e8db2
共有 3 个文件被更改,包括 155 次插入108 次删除
  1. 5 5
      Task/MakeHelper.cs
  2. 1 1
      Template/Sql/db.sql
  3. 149 102
      Util/Maker.cs

+ 5 - 5
Task/MakeHelper.cs

@@ -35,7 +35,7 @@ namespace Tasks
             }
         }
 
-        public void DoQueue(string content)
+        public async void DoQueue(string content)
         {
             JsonData jsonObj = JsonMapper.ToObject(content);
             //流水号
@@ -45,14 +45,14 @@ namespace Tasks
             //附加数据,原样返回
             string attach = jsonObj["attach"].ToString();
             //读取模板文件内容
-            // string templateString = Function.ReadInstance("Template/" + modePath);
+            string templateString = Function.ReadInstance("Template/" + modePath + ".json");
             var templateService = App.GetRequiredService<IMakeTemplateService>();
             var template = templateService.GetFirst(m => m.templateName == modePath);
             if(template == null) return;
-            string templateString = template.templateContent;
-            templateString = HttpUtility.UrlDecode(templateString);
+            // string templateString = template.templateContent;
+            // templateString = HttpUtility.UrlDecode(templateString);
             //解析模板内容
-            string resultString = Util.Maker.StartMake(jsonObj["data"], templateString);
+            string resultString = await Util.Maker.StartMake(jsonObj["data"], templateString);
             //生成文件
             string path = template.makePath.Substring(0, template.makePath.LastIndexOf("/") + 1);
             string fileName = template.makePath.Substring(template.makePath.LastIndexOf("/") + 1);

+ 1 - 1
Template/Sql/db.sql

@@ -11,6 +11,6 @@ CREATE table <<tableName>>(
 <<ym-loop:fields>>
 <<fieldName>> <<fieldType>><<ym-if:!isEmpty("<<fieldLength>>")>>(<<fieldLength>>)<</ym-if>><<ym-if:<<notNull>>>> not null<</ym-if>><<ym-if:<<autoIncrement>>>> AUTO_INCREMENT<</ym-if>><<ym-if:!isEmpty("<<fieldDefaultValue>>")>> default <<fieldDefaultValue>><</ym-if>> COMMENT <<fieldTitle>><<fieldDetail>>,
 <</ym-loop:fields>>
-PRIMARY KEY(<<ym-loop:fields removeEnd=",">><<fieldName>>,<</ym-loop:fields>>)
+PRIMARY KEY(<<ym-loop:fields removeEnd=",">><<ym-if:<<primaryKey>>>><<fieldName>>,<</ym-if>><</ym-loop:fields>>)
 ) COMMENT '<<tableTitle>>' ENGINE=InnoDB DEFAULT charset=utf8mb4 COLLATE utf8mb4_general_ci;
 <</ym-loop:databaseTable>>

+ 149 - 102
Util/Maker.cs

@@ -10,135 +10,179 @@ namespace Util
     {
         
         #region 读取模版数据,替换对应的标签内容
-        public static string StartMake(JsonData jsonObj, string templateString)
+        public static async Task<string> StartMake(JsonData jsonObj, string templateString)
         {
             foreach (string key in jsonObj.Keys)
             {
                 JsonData obj = jsonObj[key];
-                if (obj.IsArray) //如果是数组
+                if(obj != null)
                 {
-                    //匹配循环带loop循环标记的内容
-                    MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
-                    foreach (Match m in mc)
+                    if (obj.IsArray) //如果是数组
                     {
-                        string listString = "";
-                        string matchValue = m.Value;
-                        Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*>>");
-                        if (head.Success)
-                        {
-                            string headValue = head.Value;
-                            char[] removeString = new char[] { }; //两头要过滤的符号
-                            char[] removeStartString = new char[] { }; //头部要过滤的符号
-                            char[] removeEndString = new char[] { }; //尾部要过滤的符号
-                                                                     //匹配两头要过滤的符号
-                            Match remove = Regex.Match(headValue, "remove=\".*?\"");
-                            if (remove.Success)
-                            {
-                                removeString = remove.Value.Replace("remove=\"", "").Replace("\"", "").ToCharArray();
-                            }
-                            //匹配头部要过滤的符号
-                            Match removeStart = Regex.Match(headValue, "removeStart=\".*?\"");
-                            if (removeStart.Success)
-                            {
-                                removeStartString = removeStart.Value.Replace("removeStart=\"", "").Replace("\"", "").ToCharArray();
-                            }
-                            //匹配尾头要过滤的符号
-                            Match removeEnd = Regex.Match(headValue, "removeEnd=\".*?\"");
-                            if (removeEnd.Success)
-                            {
-                                removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray();
-                            }
-                            //匹配要循环的模板内容
-                            string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
-                            //循环读取数组数据
-                            foreach (JsonData item in obj)
-                            {
-                                string itemString = itemTemplate;
-                                IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
-                                //循环读取每个字段的数据
-                                foreach (string itemKey in itemData.Keys)
-                                {
-                                    JsonData itemObj = itemData[itemKey];
-                                    if (itemObj.IsArray || itemObj.IsObject)
-                                    {
-                                        itemString = StartMake(itemObj, itemTemplate);
-                                    }
-                                    else
-                                    {
-                                        itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
-                                    }
-                                }
-                                listString += itemString;
-                            }
-                            //过滤两头的符号
-                            if (removeString.Length > 0)
-                            {
-                                listString.TrimStart(removeString);
-                            }
-                            //过滤头部的符号
-                            if (removeStartString.Length > 0)
-                            {
-                                listString.TrimStart(removeStartString);
-                            }
-                            //过滤尾部的符号
-                            if (removeEndString.Length > 0)
-                            {
-                                listString.TrimEnd(removeEndString);
-                            }
-                        }
-                        templateString.Replace(matchValue, listString);
+                        //匹配循环带loop循环标记的内容
+                        templateString = await makeArrayAsync(obj, templateString, key);
                     }
+                    else if (obj.IsObject) //如果是单个对象
+                    {
+                        //匹配循环带item单个对象标记的内容
+                        templateString = await makeObjectAsync(obj, templateString, key);
+                    }
+                    else //否则当数值处理
+                    {
+                        templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
+                    }
+                }
+                else
+                {
+                    templateString = templateString.Replace("<<ym:" + key + ">>", "");
                 }
-                else if (obj.IsObject) //如果是单个对象
+            }
+            templateString = await PublicMakeAsync(templateString);
+            return templateString;
+        }
+        
+        public static async Task<string> makeArrayAsync(JsonData jsonObj, string templateString, string key)
+        { 
+            MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
+            foreach (Match m in mc)
+            {
+                string listString = "";
+                string matchValue = m.Value;
+                Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*?>>");
+                if (head.Success)
                 {
-                    //匹配循环带item单个对象标记的内容
-                    MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
-                    foreach (Match m in mc)
+                    string headValue = head.Value;
+                    char[] removeString = new char[] { }; //两头要过滤的符号
+                    char[] removeStartString = new char[] { }; //头部要过滤的符号
+                    char[] removeEndString = new char[] { }; //尾部要过滤的符号
+                                                                //匹配两头要过滤的符号
+                    Match remove = Regex.Match(headValue, "remove=\".*?\"");
+                    if (remove.Success)
+                    {
+                        removeString = remove.Value.Replace("remove=\"", "").Replace("\"", "").ToCharArray();
+                    }
+                    //匹配头部要过滤的符号
+                    Match removeStart = Regex.Match(headValue, "removeStart=\".*?\"");
+                    if (removeStart.Success)
+                    {
+                        removeStartString = removeStart.Value.Replace("removeStart=\"", "").Replace("\"", "").ToCharArray();
+                    }
+                    //匹配尾头要过滤的符号
+                    Match removeEnd = Regex.Match(headValue, "removeEnd=\".*?\"");
+                    if (removeEnd.Success)
                     {
-                        string listString = "";
-                        string matchValue = m.Value;
-                        Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*>>");
-                        if (head.Success)
+                        removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray();
+                    }
+                    //匹配要循环的模板内容
+                    string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
+                    //循环读取数组数据
+                    foreach (JsonData item in jsonObj)
+                    {
+                        string itemString = itemTemplate;
+                        // IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
+                        //循环读取每个字段的数据
+                        foreach (string itemKey in item.Keys)
                         {
-                            string headValue = head.Value;
-                            string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
-                            string itemString = itemTemplate;
-                            IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
-                            //循环读取每个字段的数据
-                            foreach (string itemKey in itemData.Keys)
+                            JsonData itemObj = item[itemKey];
+                            Console.WriteLine(itemKey + " => " + itemObj);
+                            if(itemObj != null)
                             {
-                                JsonData itemObj = itemData[itemKey];
-                                if (itemObj.IsArray || itemObj.IsObject)
+                                if (itemObj.IsArray)
                                 {
-                                    itemString = StartMake(itemObj, itemTemplate);
+                                    itemString = await makeArrayAsync(itemObj, itemString, itemKey);
+                                }
+                                else if (itemObj.IsObject)
+                                {
+                                    itemString = await makeObjectAsync(itemObj, itemString, itemKey);
                                 }
                                 else
                                 {
-                                    itemString = itemString.Replace("<<" + itemKey + ">>", itemObj[itemKey].ToString());
+                                    itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
                                 }
                             }
-                            listString += itemString;
+                            else
+                            {
+                                itemString = itemString.Replace("<<" + itemKey + ">>", "");
+                            }
                         }
-                        templateString.Replace(matchValue, listString);
+                        listString += itemString;
+                    }
+                    listString = await PublicMakeAsync(listString);
+                    //过滤两头的符号
+                    if (removeString.Length > 0)
+                    {
+                        listString = listString.Trim(removeString);
+                    }
+                    //过滤头部的符号
+                    if (removeStartString.Length > 0)
+                    {
+                        listString = listString.TrimStart(removeStartString);
+                    }
+                    //过滤尾部的符号
+                    if (removeEndString.Length > 0)
+                    {
+                        listString = listString.TrimEnd(removeEndString);
                     }
-                    mc = Regex.Matches(templateString, "<<ym:" + key + ".*?>>");
-                    foreach (Match m in mc)
+                }
+                templateString = templateString.Replace(matchValue, listString);
+            }
+            return templateString;
+        }
+
+        public static async Task<string> makeObjectAsync(JsonData jsonObj, string templateString, string key)
+        {
+            MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
+            foreach (Match m in mc)
+            {
+                string listString = "";
+                string matchValue = m.Value;
+                Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*?>>");
+                if (head.Success)
+                {
+                    string headValue = head.Value;
+                    string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
+                    string itemString = itemTemplate;
+                    // IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
+                    //循环读取每个字段的数据
+                    foreach (string itemKey in jsonObj.Keys)
                     {
-                        string matchValue = m.Value;
-                        string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
-                        if(data.Length == 1)
+                        JsonData itemObj = jsonObj[itemKey];
+                        if(itemObj != null)
                         {
-                            templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
+                            if (itemObj.IsArray)
+                            {
+                                itemString = await makeArrayAsync(itemObj, itemString, itemKey);
+                            }
+                            else if (itemObj.IsObject)
+                            {
+                                itemString = await makeObjectAsync(itemObj, itemString, itemKey);
+                            }
+                            else
+                            {
+                                itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
+                            }
                         }
-                        else if(data.Length == 2)
+                        else
                         {
-                            templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
+                            itemString = itemString.Replace("<<" + itemKey + ">>", "");
                         }
                     }
+                    listString += itemString;
+                }
+                templateString = templateString.Replace(matchValue, listString);
+            }
+            mc = Regex.Matches(templateString, "<<ym:" + key + ".*?>>");
+            foreach (Match m in mc)
+            {
+                string matchValue = m.Value;
+                string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
+                if(data.Length == 1)
+                {
+                    templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
                 }
-                else //否则当数值处理
+                else if(data.Length == 2)
                 {
-                    templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
+                    templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
                 }
             }
             return templateString;
@@ -152,7 +196,7 @@ namespace Util
             foreach (Match m in mc)
             {
                 string matchValue = m.Value;
-                Match head = Regex.Match(matchValue.Replace("<</ym-if>>", ""), "<<ym-if:.*>>");
+                Match head = Regex.Match(matchValue.Replace("<</ym-if>>", ""), "<<ym-if:.*?>>");
                 if (head.Success)
                 {
                     string headValue = head.Value;
@@ -166,6 +210,9 @@ namespace Util
                     }
                     string condition = headValue.Substring(8, headValue.Length - 10);
                     condition = condition.Replace("isEmpty", "string.IsNullOrEmpty");
+                    condition = condition.Replace("True", "true");
+                    condition = condition.Replace("False", "false");
+                    Console.WriteLine(condition);
                     bool result = await CSharpScript.EvaluateAsync<bool>(condition);
                     content = content.Replace(matchValue, result ? resultValue : elseValue);
                 }