-
Notifications
You must be signed in to change notification settings - Fork 0
/
countloc
executable file
·155 lines (129 loc) · 3.72 KB
/
countloc
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env php
<?php
function findFiles( $path = '.' ) {
$files = array();
if (is_file($path)) {
$files[] = "$path";
return $files;
}
if (!is_dir($path)) {
return $files;
}
$dh = opendir($path);
if (!$dh) {
return $files;
}
$exclude = array_flip( array('.', '..', '.DS_Store') );
while( false !== ($file=readdir($dh))) {
if (isset($exclude[$file]))
continue;
if (is_dir("$path/$file")) {
$files = array_merge($files, findFiles("$path/$file"));
} else {
$files[] = "$path/$file";
}
}
closedir($dh);
return $files;
}
function fileContains($path, $string) {
$handle = @fopen($path, 'r');
$found = false;
if ($handle) {
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, $string) !== false) {
$found = true;
break;
}
}
fclose($handle);
}
return $found;
}
// Options
$printPaths = false;
$paths = array();
// Ensure existence
empty($argv) && ($argv = array());
// Remove filename from arguments
if (! empty($argv[0]) && basename($argv[0]) === basename(__FILE__)) {
array_shift($argv);
}
// Parse options
foreach ($argv as $arg) {
if ($arg === '-v') {
$printPaths = true;
} elseif (! file_exists($arg)) {
error_log("ERROR: Could not find $arg");
} else {
$paths[] = $arg;
}
}
if (! $paths) {
$paths[] = '.';
}
$totalLoc = 0;
$includeTotal = 1 < count($paths);
foreach ($paths as $path) {
echo "Searching '$path' ... ";
// Perform search
$files = findFiles($path);
$filesCount = count($files);
echo "\nFound $filesCount files\n";
$loc = 0;
$lastStrLen = 0;
$filesSkipped = 0;
$filesDone = 0;
$filesPHP = 0;
$statements = 0;
foreach ($files as $f) {
// Format printed string
if ($printPaths) {
echo "\nSearching $f";
} else {
$s = number_format(($filesSkipped + $filesDone) / $filesCount * 100, 1) . "% : $f";
// Ensure we always cover our tracks with whitespace when clearing the terminal line
$curStrLen = strlen($s);
echo "\r$s" . str_repeat(' ', max(0, $lastStrLen - $curStrLen));
$lastStrLen = $curStrLen;
}
// Skip binary files
if (preg_match('#\.(png|jpe?g|gif|swf)$#', $f)) {
$filesSkipped++;
if ($printPaths) {
echo ' ... SKIPPED';
}
continue;
}
// Do work
$filesDone++;
if (fileContains($f, '<?php')) {
$filesPHP++;
$content = trim(file_get_contents($f));
$statements += substr_count($content, ';') + substr_count($content, '?>');
$loc += 1 + preg_match_all('#[\n\r]+#', $content); // ignores trailing line-breaks
} else {
// This will still count lines that only contain whitespace, but as a codebase's trailing whitespace
// is removed, this becomes moot. Code better! :-p
$loc += count( file($f, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) );
}
}
$totalLoc += $loc;
// Clear the line
if (! $printPaths) {
echo "\r" . str_repeat(' ', $lastStrLen);
}
// Print stats for task
echo "\rSearched ".$filesDone." files and found $loc lines of code. (skipped $filesSkipped)";
if ($filesPHP) {
echo "\n $filesPHP files were PHP with $statements statements.";
}
echo "\n";
// Create space if large task
if ($includeTotal) {
echo "\n";
}
}
if ($includeTotal) {
echo "Found $totalLoc total in " . count($paths) . " paths.\n";
}