private static long GetChars(string path, Languages filterLang, out int fileCount) { string ext = string.Join(";", ProjectAspects.GetExtensions(filterLang)); var files = Directory.GetFiles(path, ext, System.IO.SearchOption.AllDirectories).ToArray(); fileCount = files.Length; return(files.Select(file => new FileInfo(file).Length).Sum()); }
public static ProjectData GetData(string path, string clocDump, Languages filterLang = default) { // TODO: Parse dump // TODO: Pass language filter in a param var projectData = new ProjectData(); const string langNeedle = "Language"; const string LangGroup = "Language"; const string FileGroup = "File"; const string BlankGroup = "Blank"; const string CommentGroup = "Comment"; const string CodeGroup = "Code"; string RegexPattern = $@"^(?<{LangGroup}>(.+?)) {{1,}}(?<{FileGroup}>\d+) {{1,}}(?<{BlankGroup}>\d+) {{1,}}(?<{CommentGroup}>\d+) {{1,}}(?<{CodeGroup}>\d+)"; var lines = Regex.Split(clocDump, @"\r?\n|\r"); bool flagLang = false; foreach (var line in lines) { if (!line.StartsWith(langNeedle) && !flagLang) { continue; } if (!flagLang) { flagLang = true; } if (ProjectAspects.ContainsLine(line)) { var match = Regex.Match(line, RegexPattern); string langName = match.Groups[LangGroup]?.Value; string fileValue = match.Groups[FileGroup]?.Value; string blankValue = match.Groups[BlankGroup]?.Value; string commentValue = match.Groups[CommentGroup]?.Value; string codeValue = match.Groups[CodeGroup]?.Value; if (langName?.ToLowerInvariant().Contains("sum") == true) { continue; } var lang = ProjectAspects.GetLang(langName); if (!lang.HasValue) { Console.WriteLine($"Lang '{langName}' not recognized!", Color.Red); continue; } var data = new LanguageData(lang.Value, fileValue, blankValue, commentValue, codeValue); projectData.LangData.Add(data); } } int?totalLines = GetLines(projectData.LangData, filterLang); //if (!totalLines.HasValue) // throw new Exception(); projectData.Path = path; projectData.Lines = totalLines.HasValue ? totalLines.Value : -1; projectData.Chars = GetChars(path, filterLang, out int fileCount); projectData.Files = fileCount; return(projectData); }