private ResClassData GetClass(List <string> lines, int startIndex) { if (startIndex < 0 || startIndex > 0 && startIndex > lines.Count) { return(null); } ResClassData resClassData = new ResClassData(); int end = lines.Count; for (int i = startIndex; i < end; i++) { string line = lines[i]; if (line.Contains(CLASS_LIT)) { var removeSpacesStr = Regex.Replace(line, @"\s{2,}|:", " "); var nameClassMatch = Regex.Match(removeSpacesStr, $"(?<={CLASS_LIT} )\\w+"); if (nameClassMatch.Success) { resClassData.Name = nameClassMatch.Value; resClassData.StartIdx = i; resClassData.EndIdx = GetEndBraces(GetStartBraces(i, lines), lines); for (int k = resClassData.StartIdx; k <= resClassData.EndIdx; k++) { string codeLine = lines[k]; if (IsIncludeEmptyLine || !string.IsNullOrEmpty(codeLine)) { if ((IsIncludeComment || IsStartSubstring(codeLine, CommentExclude) || !IsStartSubstring(codeLine, COMMENT_LIT)) && (IsIncludeRegion || !IsStartSubstring(codeLine, REGION_LIT)) && (IsIncludeRegion || !IsStartSubstring(codeLine, END_REGION_LIT))) { resClassData.Code.Add(codeLine); } } } return(resClassData); } } } return(null); }
private StringBuilder GetCode(string dir, string fileExt, string[] exclude) { List <string> allfiles = Directory.GetFiles(dir, fileExt, SearchOption.AllDirectories).ToList(); Exclude(allfiles, exclude); Dictionary <string, List <string> > filesLines = new Dictionary <string, List <string> >(); foreach (string patoToFile in allfiles) { string[] lines = File.ReadAllLines(patoToFile); filesLines.Add(patoToFile, lines.ToList()); } Dictionary <string, ResClassData> clases = new Dictionary <string, ResClassData>(); ResClassData programClassData = null; foreach (KeyValuePair <string, List <string> > pair in filesLines) { if (GetLineNum(pair.Value, LabelExclude) >= 0) { continue; } ResClassData res = null; int idx = 0; do { res = GetClass(pair.Value, idx); if (res != null) { if (res.Name == PROGRAM_LIT) { programClassData = res; } if (!clases.ContainsKey(res.Name)) { clases.Add(res.Name, res); } else { throw new Exception($"There are two classes with the same name - \"{res.Name}\"\nline: {res.StartIdx}\n{pair.Key}"); } idx = res.EndIdx; } } while (res != null); } if (programClassData == null) { return(null); } StringBuilder resCode = new StringBuilder(); foreach (KeyValuePair <string, ResClassData> pair in clases) { if (pair.Key != PROGRAM_LIT) { var classData = pair.Value; int idx = GetLineNum(programClassData.Code, classData.Name); if (idx > 0) { int lenTab = GetLenTab(classData.Code[0]); foreach (var line in classData.Code) { resCode.AppendLine(line.Length > lenTab ? line.Remove(0, lenTab) : line); } resCode.AppendLine(); } } } int startBraces = GetStartBraces(0, programClassData.Code); for (int i = startBraces; i < programClassData.Code.Count; i++) { if (programClassData.Code[i].Length > 8) { startBraces = i; break; } } int lenTabPc = GetLenTab(programClassData.Code[startBraces]); for (int i = 2; i < programClassData.Code.Count - 1; i++) { resCode.AppendLine(programClassData.Code[i].Length > lenTabPc ? programClassData.Code[i].Remove(0, lenTabPc) : programClassData.Code[i]); } return(resCode); }