-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcounter.dart
More file actions
73 lines (57 loc) · 1.49 KB
/
Copy pathcounter.dart
File metadata and controls
73 lines (57 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import 'dart:io';
class Statistics {
int files = 0;
int totalLines = 0;
int codeLines = 0;
int commentLines = 0;
int blankLines = 0;
}
void main(List<String> args) {
final directory = Directory(args.isEmpty ? '.' : args.first);
if (!directory.existsSync()) {
print('目录不存在');
return;
}
final stat = Statistics();
for (final entity in directory.listSync(recursive: true)) {
if (entity is! File || !entity.path.endsWith('.dart')) {
continue;
}
stat.files++;
bool inBlockComment = false;
for (final line in entity.readAsLinesSync()) {
stat.totalLines++;
final text = line.trim();
if (text.isEmpty) {
stat.blankLines++;
continue;
}
if (inBlockComment) {
stat.commentLines++;
if (text.contains('*/')) {
inBlockComment = false;
}
continue;
}
if (text.startsWith('//')) {
stat.commentLines++;
continue;
}
if (text.startsWith('/*')) {
stat.commentLines++;
if (!text.contains('*/')) {
inBlockComment = true;
}
continue;
}
stat.codeLines++;
}
}
print('========== 总计 ==========');
print('Dart 文件数 : ${stat.files}');
print('总代码行数 : ${stat.totalLines}');
print('有效代码行 : ${stat.codeLines}');
print('注释行数 : ${stat.commentLines}');
print('空行数 : ${stat.blankLines}');
}
// dart compile exe counter.dart -o counter.exe