private static string GetCodeTemplateFileNameFor(SBFileType tarFileType) { string fileName = null; switch (tarFileType) { case SBFileType.Controller: fileName = "ControllerTemplate.txt"; break; case SBFileType.Service: fileName = "ServiceTemplate.txt"; break; case SBFileType.Mapper: fileName = "MapperTemplate.txt"; break; case SBFileType.ResourceMapper: fileName = "MapperResourceTemplate.txt"; break; default: throw new Exception("非支持类型:" + tarFileType); } return("JavaCodeTemplate/" + fileName); }
private static string GetMemberTypeFor(SBFileType tarFileType, string tarFileName) { int reqPathRegexIndex = (int)tarFileType; string memType = Regex.Replace(tarFileName, C_RegexFilePathToMatch[reqPathRegexIndex], C_RegexMemTypeToReplace[reqPathRegexIndex]); return(memType); }
private static string GetImportMemPath(SBFileType tarFileType, string packageInfo, string memTypeName) { int tarIndex = (int)tarFileType; string reqPath = Regex.Replace(packageInfo, C_RegexPackagePathToMatch[tarIndex], C_FormatRegexImportMemPathToReplace[tarIndex]); return(string.Format(reqPath, memTypeName)); }
private static string GetFileNameFor(string srcFileName, SBFileType srcFileType, SBFileType tarFileType) { int tarRegexIndex = (int)tarFileType; int srcRegexIndex = (int)srcFileType; string tarFileName = Regex.Replace(srcFileName, C_RegexFilePathToMatch[srcRegexIndex], C_RegexFilePathToReplace[tarRegexIndex]); return(tarFileName); }
/// <summary> /// 获得Controller的RequestMapping路径地址 /// </summary> /// <param name="tarFileType">文件类型(仅对Controller类型有效)</param> /// <param name="tarFileName">文件路径</param> /// <returns></returns> private static string GetRequestPathFor(SBFileType tarFileType, string tarFileName) { int reqPathRegexIndex = (int)tarFileType; string reqPath = Regex.Replace(tarFileName, C_RegexFilePathToMatch[reqPathRegexIndex], C_RegexReqMapPathToReplace[reqPathRegexIndex]); //controller路径中可能还是会有“\\"的格式,将之替换为"/" reqPath = reqPath.Replace("\\", "/"); return(reqPath); }
/// <summary> /// 从源代码中匹配出package信息 /// </summary> /// <param name="srcCode"></param> /// <returns></returns> private static string CreatePackageInfoFrom(string srcCode, SBFileType srcFileType, SBFileType tarFileType) { int srcIndex = (int)srcFileType; var match = Regex.Match(srcCode, C_RegexPackagePathToMatch[srcIndex]); if (match.Success) { return(string.Format(C_FormatPackagePathToReplace[(int)tarFileType], match.Groups[1], match.Groups[2])); } else { throw new Exception("没有在文件中找到对应" + srcFileType + "的package信息,可能文件内容有点不对?"); } }
/// <summary> /// 分析文件列表中的类型,需要统一为Controller或Service或Mapper的情况下,会返回对应类型;否则返回Null和对应错误信息 /// </summary> /// <param name="filenameList"></param> /// <param name="errorMsg"></param> /// <returns></returns> public static SBFileType AnalyzeFiles(List <string> filenameList, out string errorMsg) { errorMsg = string.Empty; if (CommonUtil.IsNullOrEmpty(filenameList)) { errorMsg = "文件列表为空"; return(SBFileType.NULL); } StringBuilder sbError = new StringBuilder(); bool isSuc = true; SBFileType fileType = SBFileType.NULL; foreach (var file in filenameList) { //识别文件类型: var currFileType = RecognizeFileType(file); if (currFileType == SBFileType.NULL) { isSuc = false; sbError.AppendLine(@"传入文件未被识别为对应的Controller/Service/Mapper,请确认文件是否规范: 1、是否正确放置在了Controller或Service或Mapper文件夹的子目录下。 2、文件是否以“Controller或Service或Mapper”结尾。"); currFileType = SBFileType.NULL; break; } if (fileType == SBFileType.NULL) { //之前未有识别,则接受: fileType = currFileType; } else if (fileType != currFileType) { //一直是此状态,接受,但是如果出现新状态,则暂不接受: isSuc = false; sbError.AppendLine("传入的文件类型被识别为:" + currFileType + ",其前面类型为" + fileType + ",暂时不接受不同类型的文件同时处理。"); } } if (!isSuc) { errorMsg = sbError.ToString(); } return(fileType); }
/// <summary> /// 创建文件名对应的Java文件 /// </summary> /// <param name="srcFileName">原文件,用于生成对应的目标类型</param> /// <param name="tarFileType">目标文件类型</param> /// <returns></returns> public static bool CreateFileFor(string srcFileName, SBFileType srcFileType, SBFileType tarFileType) { //获得目标文件名,并确保目录可用: string tarFileName = GetFileNameFor(srcFileName, srcFileType, tarFileType); TryCreateDirectoryFor(tarFileName); //文件内容填充: StringBuilder sbFileContent = FillFileContentFor(srcFileName, srcFileType, tarFileName, tarFileType); //指定要utf-8 with no BOM编码 File.WriteAllText(tarFileName, sbFileContent.ToString(), new UTF8Encoding(false)); return(true); }
/// <summary> /// 从源文件中,推断出目标文件内容: /// </summary> /// <param name="srcFileName"></param> /// <param name="tarFileType"></param> /// <param name="tarFileName"></param> /// <returns></returns> private static StringBuilder FillFileContentFor(string srcFileName, SBFileType srcFileType, string tarFileName, SBFileType tarFileType) { StringBuilder sb = new StringBuilder(); string srcCode = File.ReadAllText(srcFileName); Dictionary <string, string> dictCodeIndexs = new Dictionary <string, string>(); //Java文件名: string javaFileName = Path.GetFileNameWithoutExtension(tarFileName); dictCodeIndexs["{JAVA_FILE_NAME}"] = javaFileName; //生成package信息: string packageInfo = CreatePackageInfoFrom(srcCode, srcFileType, tarFileType); dictCodeIndexs["{PACKAGE_INFO}"] = packageInfo; //作者信息: string authorName = g_javaAuthorName; dictCodeIndexs["{AUTHOR_NAME}"] = authorName; //生成日期: string createDate = DateTime.Now.ToString("yyyy年MM月dd日"); dictCodeIndexs["{CREATE_DATE}"] = createDate; //文件注释 string classComment = "TODO 文件注释"; dictCodeIndexs["{CLASS_COMMENT}"] = classComment; //映射路径:对controller时: string reqMapPath = GetRequestPathFor(tarFileType, tarFileName); dictCodeIndexs["{REQ_MAP_PATH}"] = reqMapPath; //映射成员类型和成员名称: string memType = GetMemberTypeFor(tarFileType, tarFileName); dictCodeIndexs["{MEM_TYPE}"] = memType; //--名称 string memName = GetMemberName(memType); dictCodeIndexs["{MEM_NAME}"] = memName; //成员匹配完后,生成需要导入的import语句: string importMemPath = GetImportMemPath(tarFileType, packageInfo, memType); dictCodeIndexs["{IMPORT_MEM_PATH}"] = importMemPath; //TODO:方法区通过TokenSwallow的方法吞噬类原有内容,然后再进行捕获 //TODO:需要注意:构造方法不需要纳入捕获范围 //同样需要状态机: //1、捕获注释 //2、捕获方法 //方法是构造方法,跳过;否则捕获完一个方法,纳入。 //函数区: string funcArea = "//TODO: 方法区域"; dictCodeIndexs["{FUNC_AREA}"] = funcArea; //读入代码模板内容 string templateCodeFileName = GetCodeTemplateFileNameFor(tarFileType); string templateCodes = File.ReadAllText(templateCodeFileName); //按照模板,塞入内容: foreach (var dictCode in dictCodeIndexs) { //不断替换成员变量: templateCodes = templateCodes.Replace(dictCode.Key, dictCode.Value); } sb.Append(templateCodes); return(sb); }