-
Notifications
You must be signed in to change notification settings - Fork 9
/
tidy.pl
62 lines (52 loc) · 1.27 KB
/
tidy.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use Modern::Perl;
use Config::Simple;
use File::Find;
use Perl::Tidy;
use Config::Simple;
my $config;
if ( -e 'tidy.ini' ) {
say "Load configfile";
$config = new Config::Simple('tidy.ini');
} else {
die "tidy.ini not found";
}
my $filter = $config->param('filter') || '.pl$';
my @files = find_files($filter);
foreach my $file (@files) {
say "Working on $file";
my @output;
open( my $fh, '<', $file ) or die "Could not open $file: $!";
my $content = do { local $/; <$fh> };
close($fh);
perltidy(
source => \$content,
destination => $file,
postfilter => \&add_modeline
);
}
sub add_modeline {
my $content = shift;
return unless defined $config->param('modeline');
if ( $content !~ /^\s*#\s*vim: .*$/is ) {
say "\tadd vim modeline";
$content .= sprintf( "\n# vim: %s\n", $config->param('modeline') );
}
return $content;
}
sub find_files {
my $filter = shift || '.pl$';
say "Search for file matching '$filter'";
my @files;
find(
sub {
return unless -f;
push @files, $File::Find::name if $_ =~ /$filter/;
},
'.'
);
return @files;
}
# vim: syntax=perl sw=4 ts=4 noet shiftround