void AddToTemplate(string outpath, string templatePath) { string templateContent = File.ReadAllText(templatePath); string content = File.ReadAllText(outpath); content = string.Format(templateContent, content, OutFolder); FileUtilityEditor.Write(outpath, content, false); }
/// <summary> /// 将内容枚举内容写入生成文件中 /// </summary> /// <param name="outFolder">生成文件存放路径</param> /// <param name="name">写入内容的xml文件名字</param> /// <param name="id">写入内容的xml所以功能ID</param> /// <param name="content">写入的枚举内容</param> /// <param name="fileContent">文本内容</param> /// <param name="IsWrite">是否直接写入文件,当批量增加xml的时候,反复的写入会造成大量的开销</param> private void ParseToESocketEventEnum(string outFolder, string name, int id, string content, ref string fileContent, bool IsWrite = true) { string outFile = outFolder + "ESocketEvent.cs"; if (string.IsNullOrEmpty(fileContent)) { fileContent = FileUtilityEditor.ReadToEnd(outFile); } if (string.IsNullOrEmpty(fileContent)) { string defaultContext = "/// <summary>\r\n/// 网络事件\r\n/// </summary>\r\npublic enum ESocketEvent\r\n{\r\n}"; FileUtilityEditor.Write(outFile, defaultContext, false); fileContent = defaultContext; } string startFlag = GetServerProtoEnumStart(name, id); string endFlag = GetServerProtoEnumEnd(name, id); pattern = startFlag + "([\\s\\S]*)" + endFlag; content = startFlag + content + endFlag; matchs = Regex.Matches(fileContent, pattern); if (matchs != null && matchs.Count > 0) { string enumContent = matchs[0].Groups[0].Value; fileContent = fileContent.Replace(enumContent, content); } else//沒有找到之前的生成内容,说明这个xml是新加入的,那么插入到最下面 { pattern = "public enum ESocketEvent\r\n{([^}]+)}"; matchs = Regex.Matches(fileContent, pattern); //拿到之前的ESocketEvent枚举内所有内容 if (matchs != null && matchs.Count > 0) { string enumContent = matchs[0].Groups[0].Value; content = enumContent.Insert(enumContent.Length - 1, "\r\n" + content); fileContent = fileContent.Replace(enumContent, content); } } if (IsWrite) { FileUtilityEditor.Write(outFile, fileContent, false); } }