File Coverage

blib/lib/Dist/Zilla/Plugin/Web/FileHeader.pm
Criterion Covered Total %
statement 23 35 65.7
branch 1 4 25.0
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 30 48 62.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Web::FileHeader;
2             $Dist::Zilla::Plugin::Web::FileHeader::VERSION = '0.0.10';
3             # ABSTRACT: Prepend header to files
4              
5 1     1   647626 use Moose;
  1         1  
  1         11  
6 1     1   4107 use Path::Class;
  1         1  
  1         64  
7 1     1   764 use String::BOM qw(strip_bom_from_string);
  1         14  
  1         6  
8              
9             with 'Dist::Zilla::Role::FileMunger';
10             with 'Dist::Zilla::Plugin::Web::Role::FileMatcher';
11              
12              
13             has 'header_filename' => (
14             isa => 'Str',
15             is => 'rw'
16             );
17              
18              
19             #================================================================================================================================================================================================================================================
20             sub munge_files {
21 1     1 0 72502 my $self = shift;
22            
23 1 50       31 return unless -e $self->header_filename;
24            
25 1         29 my $header_content = strip_bom_from_string(file($self->header_filename)->slurp() . "");
26            
27 1         381 my $version = $self->zilla->version;
28            
29 1         70 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
30            
31             # perl is such an oldboy :)
32 1         2 $year += 1900;
33            
34 1         5 $header_content =~ s/%v/$version/;
35 1         4 $header_content =~ s/%y/$year/;
36              
37              
38             $self->for_each_matched_file(sub {
39 2     2   3 my ($file) = @_;
40            
41 2         6 $file->content($header_content . $file->content);
42 1         13 });
43             }
44              
45              
46             #================================================================================================================================================================================================================================================
47             sub prepend_header_to_file {
48 0     0 0   my ($self, $file) = @_;
49            
50 0 0         return unless -e $self->header_filename;
51            
52 0           my $header_content = file($self->header_filename)->slurp();
53            
54 0           my $version = $self->zilla->version;
55            
56 0           my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
57            
58             # perl is such an oldboy :)
59 0           $year += 1900;
60            
61 0           $header_content =~ s/%v/$version/;
62 0           $header_content =~ s/%y/$year/;
63              
64 0           my $file_content = file($file)->slurp;
65            
66 0           my $fh = file($file)->openw;
67            
68 0           print $fh $header_content . $file_content;
69            
70 0           $fh->close();
71             }
72              
73              
74              
75              
76 1     1   255 no Moose;
  1         1  
  1         5  
77             __PACKAGE__->meta->make_immutable(inline_constructor => 0);
78              
79             1;
80              
81             __END__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             Dist::Zilla::Plugin::Web::FileHeader - Prepend header to files
90              
91             =head1 VERSION
92              
93             version 0.0.10
94              
95             =head1 SYNOPSIS
96              
97             In your F<dist.ini>:
98              
99             [Web::FileHeader]
100             header_filename = build/header.txt
101            
102             file_match = ^lib/.*\.js$ ; default, regex for file names to process
103             file_match = ^lib/.*\.css$ ; allow several values
104             excelude_match = ^lib/special.css$ ; default, regex for file names to exclude
105             ; from processing
106             excelude_match = ^lib/donotinclude.css$ ; allow several values
107              
108             In your "build/header.txt":
109              
110             /*
111            
112             My Project %v
113             Copyright(c) 2009-%y My Company
114            
115             */
116              
117             =head1 DESCRIPTION
118              
119             This plugin will prepend the content of the file specified with the "header_filename" option to all files in your
120             distribution, matching any of the "file_match" regular expressions. Files matching any of the "excelude_match"
121             regular expression will not be processed.
122              
123             When prepending the header, the "%v" placeholder will be replaced with the version of distribution. The "%y" placeholder
124             will be replaced with the current year.
125              
126             =head1 AUTHOR
127              
128             Nickolay Platonov <nplatonov@cpan.org>
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             This software is copyright (c) 2015 by Nickolay Platonov.
133              
134             This is free software; you can redistribute it and/or modify it under
135             the same terms as the Perl 5 programming language system itself.
136              
137             =cut