File Coverage

blib/lib/App/Perl/Module/CopyrightYears.pm
Criterion Covered Total %
statement 44 84 52.3
branch 1 16 6.2
condition 1 6 16.6
subroutine 12 14 85.7
pod 2 2 100.0
total 60 122 49.1


line stmt bran cond sub pod time code
1             package App::Perl::Module::CopyrightYears;
2              
3 4     4   79642 use strict;
  4         26  
  4         121  
4 4     4   28 use warnings;
  4         10  
  4         133  
5              
6 4     4   2042 use Class::Utils qw(set_params);
  4         57899  
  4         83  
7 4     4   2496 use File::Find::Rule;
  4         40699  
  4         32  
8 4     4   2125 use File::Spec::Functions qw(catfile);
  4         3529  
  4         240  
9 4     4   8336 use Getopt::Std;
  4         204  
  4         248  
10 4     4   1894 use IO::Barf qw(barf);
  4         2690  
  4         58  
11 4     4   2114 use Pod::CopyrightYears;
  4         123056  
  4         159  
12 4     4   2073 use Perl6::Slurp qw(slurp);
  4         6588  
  4         26  
13 4     4   219 use String::UpdateYears qw(update_years);
  4         17  
  4         3291  
14              
15             our $VERSION = 0.03;
16              
17             # Constructor.
18             sub new {
19 3     3 1 2405 my ($class, @params) = @_;
20              
21             # Create object.
22 3         9 my $self = bless {}, $class;
23              
24             # Process parameters.
25 3         15 set_params($self, @params);
26              
27             # Object.
28 2         21 return $self;
29             }
30              
31             # Run.
32             sub run {
33 1     1 1 12 my $self = shift;
34              
35             # Process arguments.
36 1         14 $self->{'_opts'} = {
37             'd' => 0,
38             'h' => 0,
39             's' => 'LICENSE AND COPYRIGHT',
40             'y' => undef,
41             };
42 1 50 33     7 if (! getopts('dhy:', $self->{'_opts'})
43             || $self->{'_opts'}->{'h'}) {
44              
45 1         118 print STDERR "Usage: $0 [-d] [-h] [-s section(s)] [-y last_year] [--version]\n";
46 1         16 print STDERR "\t-d\t\tDebug mode.\n";
47 1         12 print STDERR "\t-h\t\tPrint help.\n";
48 1         12 print STDERR "\t-s section(s)\tSection(s) to look (default is LICENSE AND COPYRIGHT)\n";
49 1         11 print STDERR "\t-y last_year\tLast year (default value is actual year)\n";
50 1         12 print STDERR "\t--version\tPrint version.\n";
51 1         6 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           require Dumpvalue;
65 0           my $dump = Dumpvalue->new;
66 0           $dump->dumpValues(\@pm);
67             }
68              
69             # Update years.
70 0           foreach my $perl_module_file (@pm) {
71 0           $self->_update_pod($perl_module_file);
72             }
73              
74             # Change copyright years in LICENSE file.
75 0           my ($license) = $self->_files('.', 'LICENSE');
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             # Look for scripts with copyright years.
96 0 0         if (-d 'bin') {
97 0           my @bin = $self->_files('bin', '*');
98              
99             # Dump tools in debug mode.
100 0 0         if ($self->{'_opts'}->{'d'}) {
101 0           require Dumpvalue;
102 0           my $dump = Dumpvalue->new;
103 0           $dump->dumpValues(\@bin);
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 0           my $cy = Pod::CopyrightYears->new(
134             'pod_file' => $file,
135             'section_names' => \@sections,
136             );
137 0           $cy->change_years($self->{'_opts'}->{'y'});
138 0           barf($file, $cy->pod);
139              
140 0           return;
141             }
142              
143             1;
144              
145             __END__