File Coverage

blib/lib/App/Perl/Module/CopyrightYears.pm
Criterion Covered Total %
statement 44 85 51.7
branch 1 18 5.5
condition 2 6 33.3
subroutine 12 14 85.7
pod 2 2 100.0
total 61 125 48.8


line stmt bran cond sub pod time code
1             package App::Perl::Module::CopyrightYears;
2              
3 4     4   138623 use strict;
  4         8  
  4         208  
4 4     4   23 use warnings;
  4         9  
  4         222  
5              
6 4     4   5525 use Class::Utils qw(set_params);
  4         86370  
  4         110  
7 4     4   3278 use File::Find::Rule;
  4         56876  
  4         40  
8 4     4   3581 use File::Spec::Functions qw(catfile);
  4         7192  
  4         424  
9 4     4   2485 use Getopt::Std;
  4         13053  
  4         377  
10 4     4   2330 use IO::Barf qw(barf);
  4         3807  
  4         87  
11 4     4   2689 use Pod::CopyrightYears;
  4         184679  
  4         271  
12 4     4   4514 use Perl6::Slurp qw(slurp);
  4         9938  
  4         30  
13 4     4   287 use String::UpdateYears qw(update_years);
  4         10  
  4         4232  
14              
15             our $VERSION = 0.05;
16              
17             # Constructor.
18             sub new {
19 4     4 1 920411 my ($class, @params) = @_;
20              
21             # Create object.
22 4         14 my $self = bless {}, $class;
23              
24             # Process parameters.
25 4         28 set_params($self, @params);
26              
27             # Object.
28 3         63 return $self;
29             }
30              
31             # Run.
32             sub run {
33 2     2 1 5 my $self = shift;
34              
35             # Process arguments.
36 2         24 $self->{'_opts'} = {
37             'd' => 0,
38             'h' => 0,
39             's' => 'LICENSE AND COPYRIGHT',
40             'y' => undef,
41             };
42 2 50 66     14 if (! getopts('dhs:y:', $self->{'_opts'})
43             || $self->{'_opts'}->{'h'}) {
44              
45 2         364 print STDERR "Usage: $0 [-d] [-h] [-s section(s)] [-y last_year] [--version]\n";
46 2         64 print STDERR "\t-d\t\tDebug mode.\n";
47 2         44 print STDERR "\t-h\t\tPrint help.\n";
48 2         28 print STDERR "\t-s section(s)\tSection(s) to look (default is LICENSE AND COPYRIGHT)\n";
49 2         24 print STDERR "\t-y last_year\tLast year (default value is actual year)\n";
50 2         34 print STDERR "\t--version\tPrint version.\n";
51 2         15 return 1;
52             }
53              
54             # Default year.
55 0 0         if (! defined $self->{'_opts'}->{'y'}) {
56 0           $self->{'_opts'}->{'y'} = (localtime(time))[5] + 1900;
57             }
58              
59             # Find all perl module files in actual directory.
60 0           my @pm = $self->_files('.', '*.pm', '*.pod');
61              
62             # Dump perl modules in debug mode.
63 0 0         if ($self->{'_opts'}->{'d'}) {
64 0           print "Found files:\n";
65 0           print map { '- '.$_."\n"; } @pm;
  0            
66             }
67              
68             # Update years.
69 0           foreach my $perl_module_file (@pm) {
70 0           $self->_update_pod($perl_module_file);
71             }
72              
73             # Change copyright years in LICENSE file.
74 0           my @licenses = $self->_files('.', 'LICENSE*');
75 0           foreach my $license (@licenses) {
76 0 0 0       if (defined $license && -r $license) {
77 0           my @license = slurp($license);
78 0           my $opts_hr = {
79             'prefix_glob' => '.*\(c\)\s+',
80             };
81 0           my $update_file = 0;
82 0           foreach (my $i = 0; $i < @license; $i++) {
83             my $updated = update_years($license[$i], $opts_hr,
84 0           $self->{'_opts'}->{'y'});
85 0 0         if ($updated) {
86 0           $license[$i] = $updated;
87 0           $update_file = 1;
88             }
89             }
90 0 0         if ($update_file) {
91 0           barf($license, (join '', @license));
92             }
93             }
94             }
95              
96             # Look for scripts with copyright years.
97 0 0         if (-d 'bin') {
98 0           my @bin = $self->_files('bin', '*');
99              
100             # Dump tools in debug mode.
101 0 0         if ($self->{'_opts'}->{'d'}) {
102 0           print "Found scripts:\n";
103 0           print map { '- '.$_."\n"; } @bin;
  0            
104             }
105              
106             # Update years.
107 0           foreach my $bin (@bin) {
108 0           $self->_update_pod($bin);
109             }
110             }
111            
112 0           return 0;
113             }
114              
115             sub _files {
116 0     0     my ($self, $dir, @file_globs) = @_;
117              
118 0           my $rule = File::Find::Rule->new;
119 0           my @pm = $rule->or(
120             $rule->new->directory->name('t')->prune->discard,
121             $rule->new->directory->name('inc')->prune->discard,
122             $rule->new->directory->name('blib')->prune->discard,
123             $rule->new,
124             )->name(@file_globs)->in($dir);
125              
126 0           return @pm;
127             }
128              
129             sub _update_pod {
130 0     0     my ($self, $file) = @_;
131              
132 0           my @sections = split m/,/, $self->{'_opts'}->{'s'};
133             my $cy = Pod::CopyrightYears->new(
134 0 0         $self->{'_opts'}->{'d'} ? ('debug' => 1) : (),
135             'pod_file' => $file,
136             'section_names' => \@sections,
137             );
138 0           $cy->change_years($self->{'_opts'}->{'y'});
139 0           barf($file, $cy->pod);
140              
141 0           return;
142             }
143              
144             1;
145              
146             __END__