|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +void main() { |
| 4 | + printMessage('Start filtering the lcov.info file'); |
| 5 | + final file = File('coverage/lcov.info'); |
| 6 | + if (!file.existsSync()) { |
| 7 | + printMessage('"lcov.info" does not exist'); |
| 8 | + return; |
| 9 | + } |
| 10 | + const endOfRecord = 'end_of_record'; |
| 11 | + final sections = <LcovSection>[]; |
| 12 | + final lines = file.readAsLinesSync(); |
| 13 | + LcovSection? currentSection; |
| 14 | + for (final line in lines) { |
| 15 | + if (line.endsWith('.dart')) { |
| 16 | + final filePath = line.replaceAll('SF:', ''); |
| 17 | + currentSection = LcovSection() |
| 18 | + ..header = line |
| 19 | + ..filePath = filePath; |
| 20 | + } else if (line == endOfRecord) { |
| 21 | + final currentSectionTmp = currentSection; |
| 22 | + if (currentSectionTmp != null) { |
| 23 | + currentSectionTmp.footer = line; |
| 24 | + sections.add(currentSectionTmp); |
| 25 | + } |
| 26 | + } else { |
| 27 | + currentSection?.body.add(line); |
| 28 | + } |
| 29 | + } |
| 30 | + final filteredSections = getFilteredSections(sections); |
| 31 | + final sb = StringBuffer(); |
| 32 | + for (final section in filteredSections) { |
| 33 | + sb.write(section.toString()); |
| 34 | + } |
| 35 | + file.writeAsStringSync(sb.toString()); |
| 36 | + printMessage('Filtered the lcov.info file'); |
| 37 | +} |
| 38 | + |
| 39 | +class LcovSection { |
| 40 | + String? filePath; |
| 41 | + String? header; |
| 42 | + final body = <String>[]; |
| 43 | + String? footer; |
| 44 | + |
| 45 | + String? getBodyString() { |
| 46 | + final filePathTmp = filePath; |
| 47 | + if (filePathTmp == null) return null; |
| 48 | + final file = File(filePathTmp); |
| 49 | + final content = file.readAsLinesSync(); |
| 50 | + final sb = StringBuffer(); |
| 51 | + getFilteredBody(body, content).forEach((item) => sb..write(item)..write('\n')); |
| 52 | + return sb.toString(); |
| 53 | + } |
| 54 | + |
| 55 | + @override |
| 56 | + String toString() { |
| 57 | + return '$header\n${getBodyString()}$footer\n'; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +List<LcovSection> getFilteredSections(List<LcovSection> sections) { |
| 62 | + return sections.where((section) { |
| 63 | + final header = section.header; |
| 64 | + if (header == null) return false; |
| 65 | + if (header.endsWith('.g.dart')) { |
| 66 | + return false; |
| 67 | + } else if (header.endsWith('dummy_service.dart')) { |
| 68 | + return false; |
| 69 | + } else if (header.startsWith('SF:lib/vendor/')) { |
| 70 | + return false; |
| 71 | + } else if (header.startsWith('SF:lib/util/locale')) { |
| 72 | + return false; |
| 73 | + } else if (header.contains('widgetbook/')) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + return true; |
| 77 | + }).toList(); |
| 78 | +} |
| 79 | + |
| 80 | +List<String> getFilteredBody(List<String> body, List<String> lines) { |
| 81 | + return body.where((line) { |
| 82 | + if (line.startsWith('DA:')) { |
| 83 | + final sections = line.split(','); |
| 84 | + final lineNr = int.parse(sections[0].replaceAll('DA:', '')); |
| 85 | + final callCount = int.parse(sections[1]); |
| 86 | + if (callCount == 0) { |
| 87 | + final fileLine = lines[lineNr - 1].trim(); |
| 88 | + if (excludedLines.contains(fileLine)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + for (final line in excludedStartsWithLines) { |
| 92 | + if (fileLine.trim().startsWith(line)) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return true; |
| 99 | + }).toList(); |
| 100 | +} |
| 101 | + |
| 102 | +const excludedLines = [ |
| 103 | + 'AppConstants._();', |
| 104 | + 'EnvUtils._();', |
| 105 | + 'FlutterTemplateLogger._();', |
| 106 | + 'FlutterTemplateThemeData._();', |
| 107 | + 'Keys._();', |
| 108 | + 'LicenseUtil._();', |
| 109 | + 'ThemeAssets._();', |
| 110 | + 'ThemeColors._();', |
| 111 | + 'ThemeDimens._();', |
| 112 | + 'ThemeDurations._();', |
| 113 | + 'ThemeFonts._();', |
| 114 | + 'ThemeTextStyles._();', |
| 115 | +]; |
| 116 | + |
| 117 | +const excludedStartsWithLines = [ |
| 118 | + 'IntColumn get ', |
| 119 | + 'TextColumn get ', |
| 120 | + 'BoolColumn get ', |
| 121 | + 'DateTimeColumn get ', |
| 122 | +]; |
| 123 | + |
| 124 | +void printMessage(String message) { |
| 125 | + // ignore: avoid_print |
| 126 | + print(message); |
| 127 | +} |
0 commit comments