This repository was archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdev-precommit
More file actions
executable file
·374 lines (312 loc) · 9.29 KB
/
dev-precommit
File metadata and controls
executable file
·374 lines (312 loc) · 9.29 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/perl
use local::lib;
use v5.10;
use strict;
use warnings;
use FindBin qw( $RealBin );
use lib "$RealBin/lib";
use Cwd qw( abs_path );
use File::Basename qw( basename );
use File::Find qw( find );
use List::Util qw( any );
use LogBot::Util qw( file_time run );
use Mojo::File ();
use Mojo::Util qw( trim );
use Perl::Critic ();
use Perl::Tidy ();
$| = 1;
chdir($RealBin);
our @ARGV = ();
# tabs
find(
sub {
return unless -f $File::Find::name && -s $File::Find::name;
my $file = substr($File::Find::name, 2);
return if $file =~ m{/} && $file !~ m{^(?:lib|web)/};
return if basename($file) =~ /^\./ || $file =~ /\.(bak|swp|png|ttf)$/;
return if $file eq 'makefile' or $file eq 'perltidy.ERR';
my $content = Mojo::File->new("$RealBin/$file")->slurp();
return unless $content =~ /\t/;
say "\e[31m$file contains tabs\e[0m";
},
'.'
);
# tidy sass (sort selectors)
foreach my $file (glob('web/*.sass')) {
next if basename($file) =~ /^_/;
print $file;
if (!is_modified($file)) {
say ' unchanged';
next;
}
my @original = split(/\n/, Mojo::File->new($file)->slurp());
my @output;
my @variables;
my $chunk = {};
reset_chunk($chunk);
foreach my $line (@original) {
my $indent = $line =~ /^(\s+)/ ? length($1) : 0;
if ($line eq '') {
push @output, process_chunk($chunk);
push @output, '';
$chunk->{indent} = $indent;
next;
}
if ($line =~ /^(\$[^:]+):.+$/) {
push @variables, $1;
}
if ($indent != $chunk->{indent}) {
push @output, process_chunk($chunk);
$chunk->{indent} = $indent;
push @{ $chunk->{lines} }, $line;
next;
}
push @{ $chunk->{lines} }, $line;
}
push @output, process_chunk($chunk);
sub reset_chunk {
my ($ch) = @_;
$ch->{indent} = -1;
$ch->{lines} = [];
}
sub process_chunk {
my ($ch) = @_;
my @lines =
sort {
return -1 if $a =~ m{^\s*//};
return 1 if $b =~ m{^\s*//};
return $a cmp $b;
} @{ $ch->{lines} };
reset_chunk($ch);
return @lines;
}
my $original = join("\n", @original) . "\n";
my $output = join("\n", @output) . "\n";
if ($original ne $output) {
say " \e[34mupdated\e[0m";
Mojo::File->new($file)->spurt($output);
} else {
say ' unchanged';
}
set_modified($file);
foreach my $var (@variables) {
my $count = 0;
$count++ while $output =~ /\Q$var\E/g;
say "\e[33munused sass variable: ", $var, "\e[0m" if $count == 1;
}
}
# tidy all the perl as per .perltidy
# critic all the perl as per .perlcriticrc
my $critic = Perl::Critic->new(-profile => $RealBin . '/.perlcriticrc');
find(
sub {
# look for files in . and under lib/
my $file = $File::Find::name;
return unless -f $file && -s $file;
my $rel_file = substr($file, length($RealBin) + 1);
return if $rel_file =~ m{/} && $rel_file !~ m{^lib/};
return if basename($rel_file) =~ /^\./ || $rel_file =~ /\.(bak|swp)$/;
# detect perl by file extension or #!
my $is_perl = 0;
if ($file =~ /\.p[ml]$/) {
$is_perl = 1;
} else {
open(my $fh, '<', $file) or die "open $file $!\n";
my ($first_line) = <$fh>;
close($fh) or die $!;
$is_perl = $first_line =~ m{\#!/usr/bin/perl};
}
return unless $is_perl;
print $rel_file;
if (!is_modified($file)) {
say ' unchanged';
return;
}
# init
my $original = Mojo::File->new($file)->slurp();
my $input = $original;
my $output = '';
# sort use lines, as well as their imports
$input = sort_use($input);
# apply perltidy
Perl::Tidy::perltidy(
source => \$input,
destination => \$output,
);
# apply changes
if ($original ne $output) {
Mojo::File->new($file)->spurt($output);
say " \e[34mupdated\e[0m";
} else {
say ' unchanged';
}
# critic
my @issues = $critic->critique($file);
my $has_issues = !!scalar(@issues);
foreach my $issue (@issues) {
(my $policy = $issue->policy) =~ s/^Perl::Critic::Policy:://;
say "\e[33m", $rel_file, ':', $issue->line_number, ' ', $issue->description, ' (', $policy, ")\e[0m";
}
# warn for unused imports
@issues = find_unused_imports($input);
$has_issues ||= !!scalar(@issues);
foreach my $issue (@issues) {
say "\e[33m", $rel_file, ': ', $issue, "\e[0m";
}
if ($has_issues) {
clear_modified($file);
} else {
set_modified($file);
}
},
$RealBin
);
# javascript
foreach my $js_file (qw( web/logbot.js web/redirect.js )) {
run('js-beautify', '-r', '-n', $js_file);
run('jshint', $js_file);
}
# make static assets
run("$RealBin/dev-make", '-B');
sub sort_use {
my ($input) = @_;
return foreach_use_block(
$input,
sub {
my @uses = @_;
# sort imports
foreach my $line (@uses) {
next unless $line =~ /^(use\s+\S+)\s+qw\(([^\)]+)\);$/;
my ($module, $imports) = ($1, $2);
my @imports = split(/\s+/, trim($imports));
$imports = join(' ', sort @imports);
$line = "$module qw( $imports );";
}
# sort modules
return [sort { lc($a) cmp lc($b) } @uses];
}
);
}
sub find_unused_imports {
my ($input) = @_;
my (%modules, %imports);
foreach_use_block(
$input,
sub {
my @uses = @_;
foreach my $line (@uses) {
if ($line =~ /^use\s+(\S+)\s+\(\);$/) {
$modules{$1} = 1;
} elsif ($line =~ /^(use\s+\S+)\s+qw\(([^\)]+)\);$/) {
my ($module, $imports) = ($1, $2);
my @imports = split(/\s+/, trim($imports));
foreach my $import (@imports) {
next if $import =~ m{^[:/]};
$imports{$import} = 1;
}
}
}
return undef;
}
);
$input = join("\n", grep { !/^use\s/ } split(/\n/, $input));
my @issues;
foreach my $module (sort keys %modules) {
next if $input =~ /\b$module(?:->|::)/ || $input =~ /'$module'/;
push @issues, "package $module is unused";
}
foreach my $import (sort keys %imports) {
if ($import =~ /^\$/) {
next if $input =~ /\Q$import\E\b/;
} else {
next if $input =~ /\b\Q$import\E\b/;
}
push @issues, "import $import is unused";
}
return @issues;
}
sub foreach_use_block {
my ($input, $callback) = @_;
# use-block is replaced by callback's return, unless callback returns undef
# find use blocks, assumes they are always followed by a blank line
my @output;
my $in_use_block = 0;
my @uses;
foreach my $line (split(/\n/, $input)) {
if ($in_use_block) {
if ($line eq '') {
$in_use_block = 0;
process_uses(\@uses, \@output, $callback);
@uses = ();
next;
}
}
if ($line =~ /^use /) {
$in_use_block = 1;
push @uses, $line;
next;
}
push @output, $line;
}
process_uses(\@uses, \@output, $callback);
return join("\n", @output) . "\n";
}
sub process_uses {
my ($uses, $output, $callback) = @_;
return unless @{$uses};
# skip setup / lib blocks
if (any { /^use local::lib/ || /^use lib / || /^use base / } @{$uses}) {
push @{$output}, @{$uses}, '';
} else {
my $replace = $callback->(@{$uses});
if (defined $replace) {
push @{$output}, @{$replace}, '';
} else {
push @{$output}, @{$uses}, '';
}
}
}
# lastmod tracking
my $lastmod;
sub _init_modified {
return if defined $lastmod;
$lastmod = {};
if (open(my $fh, '<', "$RealBin/.dev-precommit")) {
while (<$fh>) {
next unless /^(.+)\t(\d+)$/;
$lastmod->{$1} = $2;
}
close($fh) || die $!;
}
if (is_modified("$RealBin/dev-precommit")) {
$lastmod = {};
_write_modified();
}
}
sub _write_modified {
open(my $fh, '>', "$RealBin/.dev-precommit") or die $!;
foreach my $fn (sort keys %{$lastmod}) {
say {$fh} "$fn\t" . $lastmod->{$fn};
}
close($fh) or die $!;
}
sub is_modified {
my ($file) = @_;
_init_modified();
my $mtime = file_time($file);
my $abs_file = abs_path($file);
$lastmod->{$abs_file} //= 0;
return $lastmod->{$abs_file} != $mtime;
}
sub set_modified {
my ($file) = @_;
_init_modified();
$lastmod->{ abs_path($file) } = file_time($file);
_write_modified();
}
sub clear_modified {
my ($file) = @_;
_init_modified();
delete $lastmod->{ abs_path($file) };
_write_modified();
}