File Coverage

blib/lib/Assets/Versionize.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Assets::Versionize;
2             {
3             $Assets::Versionize::VERSION = '0.1';
4             }
5 1     1   64501 use Moose;
  0            
  0            
6             use namespace::autoclean;
7             use Assets::Fileinfo qw/regexp_list/;
8              
9             use File::Find qw/find/;
10             use File::Copy qw/move/;
11              
12             has dry_run => (
13             is => 'rw',
14             isa => 'Bool',
15             default => 0
16             );
17              
18             has verbose => (
19             is => 'rw',
20             isa => 'Bool',
21             default => 0
22             );
23              
24             has strict => (
25             is => 'rw',
26             isa => 'Bool',
27             default => 0
28             );
29              
30             has prefix => (
31             is => 'rw',
32             isa => 'Str',
33             default => '',
34             );
35              
36             has assets => (
37             is => 'rw',
38             isa => 'Assets::Fileinfo',
39             required => 1
40             );
41              
42             has extensions => (
43             is => 'rw',
44             isa => 'ArrayRef',
45             default => sub { [qw/php/] }
46             );
47              
48             sub process {
49             my $self = shift;
50             for my $file_dir ( @_ ) {
51             if ( -f $file_dir ) {
52             $self->processFile($file_dir);
53             } elsif ( -d $file_dir ) {
54             $self->processDirectory($file_dir);
55             }
56             }
57             }
58              
59             sub processFileToString {
60             my ($self, $file) = @_;
61             my $changed = 0;
62             my $buffer = '';
63             my $verbose = $self->verbose || $self->dry_run;
64             my $fileinfo = $self->assets->fileinfo;
65             my $prefix_re = quotemeta($self->prefix);
66             my $filepath_re = qr{(?:[-\w\.]+/)*[-\w\.]+?};
67             my $version_re = '(?:\.v[0-9a-f]{6})' . ($self->strict ? '' : '?');
68             my $extension_re = regexp_list(@{$self->assets->extensions});
69              
70             if ( $verbose ) {
71             print STDERR "[INFO] Do with $file...\n";
72             }
73             open(my $fh, "<", $file) or die "Can't open file $file: $!";
74             while ( <$fh> ) {
75             my $linenum = $.;
76             my $orig = $_;
77             my $line_changed = 0;
78             s/($prefix_re)($filepath_re)$version_re\.($extension_re)/
79             my $file_name = $2 . '.' . $3;
80             if ( exists $fileinfo->{$file_name} ) {
81             $1. $2 . '.v'.substr($fileinfo->{$file_name}{md5}, 0, 6) . '.' . $3
82             } else {
83             $&
84             }
85             /gex;
86             if ( $orig ne $_ ) {
87             $line_changed = 1;
88             $changed = 1;
89             }
90             if ( $line_changed && $verbose ) {
91             print STDERR $file, ':', $linenum, ": \n",
92             " ", $orig,
93             " => ", $_, "\n";
94             }
95             $buffer .= $_;
96             }
97             return ($buffer, $changed);
98             }
99              
100             sub processFile {
101             my ($self, $file) = @_;
102             my ($output, $changed) = $self->processFileToString($file);
103             if ( $changed && !$self->dry_run ) {
104             open(my $fh, ">", $file) or die "Can't create file $file: $!";
105             print {$fh} $output;
106             }
107             }
108              
109             sub processDirectory {
110             my ($self, $dir) = @_;
111             my $extension_re = regexp_list(@{$self->extensions});
112             my $wanted = sub {
113             my $file = $File::Find::name;
114             if ( -f $file && $file =~ /\.$extension_re$/ ) {
115             $self->processFile($file);
116             }
117             };
118             find($wanted, $dir);
119             }
120              
121             __PACKAGE__->meta->make_immutable;
122              
123             1;
124              
125             __END__
126              
127             =head1 NAME
128              
129             Assets::Versionize - Utility to add version to assets file in web page
130              
131             =head1 VERSION
132              
133             version 0.1
134              
135             =head1 SYNOPSIS
136              
137             use Assets::Versionize;
138             use Assets::Fileinfo;
139             my $assets = Assets::Fileinfo->new(directory => $assets_dir);
140             my $versionizer = Assets::Versionize->new(fileinfo => $assets);
141             $versionizer->process($file_or_directory);
142              
143             =head1 DESCRIPTION
144              
145              
146              
147             =cut