File Coverage

blib/lib/Rails/Assets/Processor.pm
Criterion Covered Total %
statement 68 68 100.0
branch 16 16 100.0
condition 6 6 100.0
subroutine 9 9 100.0
pod 4 4 100.0
total 103 103 100.0


line stmt bran cond sub pod time code
1             package Rails::Assets::Processor {
2              
3 4     4   645   use 5.006;
  4         11  
4 4     4   19   use strict;
  4         7  
  4         80  
5 4     4   18   use warnings;
  4         16  
  4         162  
6              
7               our $VERSION = '0.02';
8              
9 4     4   19   use Exporter qw(import);
  4         8  
  4         201  
10               our @EXPORT = qw(
11             process_asset_file
12             process_template_file
13             process_scss_file
14             process_map_file
15             );
16              
17 4         3438   use Rails::Assets::Formatter qw(
18             format_asset_elem
19             format_referral_elem
20             format_template_elem
21 4     4   1029 );
  4         9  
22              
23               sub process_asset_file {
24 30     30 1 2438     my ($filename, $reversed_ext, $assets_hash, $assets_paths) = @_;
25 30         152     my ($ext) = $filename =~ /(\.[a-zA-Z0-9]+)$/;
26 30   100     108     my $type = $reversed_ext->{$ext} || 'unknown';
27              
28 30 100       77     if ($type ne 'unknown'){
29 26         76       my $elem = format_asset_elem($filename, $ext, $assets_paths);
30 26         52       push @{$assets_hash->{$type}}, $elem;
  26         124  
31                 } else {
32 4 100       47       print "Found unknown type: $ext ($filename)" . "\n" if $ENV{VERBOSE};
33                 }
34               }
35              
36               sub process_template_file {
37 12     12 1 2012     my ($filename, $template_hash, $template_extensions) = @_;
38 12         76     my ($ext) = $filename =~ /(\.[a-zA-Z0-9]+)$/;
39 12 100       113     if (grep /$ext/, @$template_extensions){
40              
41 9         234       open FILE, $_;
42 9         136       while (my $line=<FILE>){
43 66         237         my @stylesheet_tags = $line =~ /stylesheet_link_tag\s*\(*\s*['"](.+?)['"]\s*\)*/g;
44 66         172         my @javascript_tags = $line =~ /javascript_include_tag\s*\(*\s*['"](.+)['"]\s*\)*/g;
45 66         158         my @image_tags = $line =~ /asset_path\s*\(*\s*['"](.+?)['"]\s*\)*/g;
46              
47 66         146         push @{$template_hash->{stylesheets}}, $_ foreach (map {format_template_elem($filename, $_)} @stylesheet_tags);
  9         29  
  9         33  
48 66         134         push @{$template_hash->{javascripts}}, $_ foreach (map {format_template_elem($filename, $_)} @javascript_tags);
  9         81  
  9         35  
49 66         372         push @{$template_hash->{images}}, $_ foreach (map {format_template_elem($filename, $_)} @image_tags);
  3         16  
  3         23  
50                   }
51                 } else {
52 3 100       73       print "Found unknown type: $ext ($filename)" . "\n" if $ENV{VERBOSE};
53                 }
54               }
55              
56               sub process_scss_file {
57 3     3 1 2120     my ($filename, $reversed_ext, $scss_hash) = @_;
58 3         76     open FILE, $filename;
59 3         41     while (my $line=<FILE>){
60 24         166       my @assets_tags = $line =~ /asset\-url\s*\(*\s*['"](.+?)['"]\s*\)*/g;
61 24         111       foreach my $asset (@assets_tags){
62 21         41         my $clean_name = $asset;
63 21         82         $clean_name =~ s/([\?#].*)//;
64 21         77         my ($ext) = $clean_name =~ /(\.[a-zA-Z0-9]+)$/;
65 21   100     72         my $type = $reversed_ext->{$ext} || 'unknown';
66 21 100       49         if ($type ne 'unknown'){
67 18         48           my $elem = format_referral_elem($clean_name, $ext, $filename);
68 18         29           push @{$scss_hash->{$type}}, $elem;
  18         77  
69                     } else {
70 3 100       42           print "Found unknown type: $ext ($filename)" . "\n" if $ENV{VERBOSE};
71                     }
72                   };
73                 }
74               }
75              
76               sub process_map_file {
77 8     8 1 2123     my ($filename, $reversed_ext, $map_hash) = @_;
78 8         157     open FILE, $_;
79 8         103     while (my $line=<FILE>){
80 4         32       my @assets_tags = $line =~ /sourceMappingURL=(.+\.map?)/;
81 4         11       foreach my $asset (@assets_tags){
82 4         10         my $clean_name = $asset;
83 4         12         $clean_name =~ s/([\?#].*)//;
84 4         16         my ($ext) = $clean_name =~ /(\.[a-zA-Z0-9]+)$/;
85 4   100     22         my $type = $reversed_ext->{$ext} || 'unknown';
86 4 100       13         if ($type ne 'unknown'){
87 2         9           my $elem = format_referral_elem($clean_name, $ext, $filename);
88 2         6           push @{$map_hash->{$type}}, $elem;
  2         27  
89                     } else {
90 2 100       38           print "Found unknown type: $ext ($filename)" . "\n" if $ENV{VERBOSE};
91                     }
92                   };
93                 }
94               }
95             }
96              
97             =head1 NAME
98            
99             Rails::Assets::Processor - Processing Functions for Rails::Assets
100            
101             =head1 VERSION
102            
103             Version 0.02
104            
105             =head1 SYNOPSIS
106            
107             This module contains some functions for processing data references.
108            
109             use Rails::Assets::Processor;
110            
111             process_template_file($_, $template_hash, $template_extensions) foreach @{find_files($template_directories)};
112             process_asset_file($_, $reversed_ext, $assets_hash, $assets_paths) foreach @{find_files($assets_directories)};
113            
114             my $scss_files = [grep { $_->{ext} eq '.scss' } @{$assets_hash->{stylesheets}}];
115             my $js_files = [grep { $_->{ext} eq '.js' } @{$assets_hash->{javascripts}}];
116            
117             process_scss_file($_, $reversed_ext, $scss_hash) foreach map {$_->{full_path}} @{$scss_files};
118             process_map_file($_, , $reversed_ext, $map_hash) foreach map {$_->{full_path}} @{$js_files};
119             ...
120            
121             =head1 EXPORT
122            
123             =head2 process_asset_file
124            
125             This function find the file extension, assign a file type based on C<$reversed_ext> and pushed a formatted element into C<<< @{$assets_hash->{$type}} >>>
126            
127             =head2 process_template_file
128            
129             This function parse template files, extract the references to assets, find the file extension, assign a file type based on C<$reversed_ext> and pushed a formatted element into C<<< @{$template_hash->{$type}} >>>
130            
131             =head2 process_scss_file
132            
133             This function parse scss files, extract the references to assets, find the file extension, assign a file type based on C<$reversed_ext> and pushed a formatted element into C<<< @{$scss_hash->{$type}} >>>
134            
135             =head2 process_map_file
136            
137             This function parse javascript files, extract the references to .js.map files, assign a file type based on C<$reversed_ext> and pushed a formatted element into C<<< @{$map_hash->{$type}} >>>
138            
139            
140             =head1 SUBROUTINES/METHODS
141            
142             =head1 AUTHOR
143            
144             Mauro Berlanda, C<< <kupta at cpan.org> >>
145            
146             =head1 BUGS
147            
148             Please report any bugs or feature requests to C<bug-rails-assets at rt.cpan.org>, or through
149             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rails-Assets>. I will be notified, and then you'll
150             automatically be notified of progress on your bug as I make changes.
151            
152             Pull Requests, Issues, Stars and Forks on the project L<github repository|https://github.com/mberlanda/rails-assets-coverage> are welcome!
153            
154            
155             =head1 SUPPORT
156            
157             You can find documentation for this module with the perldoc command.
158            
159             perldoc Rails::Assets::Processor
160            
161             You can also look for information at:
162            
163             =over 4
164            
165             =item * RT: CPAN's request tracker (report bugs here)
166            
167             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=.>
168            
169             =item * AnnoCPAN: Annotated CPAN documentation
170            
171             L<http://annocpan.org/dist/.>
172            
173             =item * CPAN Ratings
174            
175             L<http://cpanratings.perl.org/d/.>
176            
177             =item * Search CPAN
178            
179             L<http://search.cpan.org/dist/./>
180            
181             =back
182            
183             =head1 ACKNOWLEDGEMENTS
184            
185             =head1 LICENSE AND COPYRIGHT
186            
187             Copyright 2017 Mauro Berlanda.
188            
189             This program is free software; you can redistribute it and/or modify it
190             under the terms of the the Artistic License (2.0). You may obtain a
191             copy of the full license at:
192            
193             L<http://www.perlfoundation.org/artistic_license_2_0>
194            
195             Any use, modification, and distribution of the Standard or Modified
196             Versions is governed by this Artistic License. By using, modifying or
197             distributing the Package, you accept this license. Do not use, modify,
198             or distribute the Package, if you do not accept this license.
199            
200             If your Modified Version has been derived from a Modified Version made
201             by someone other than you, you are nevertheless required to ensure that
202             your Modified Version complies with the requirements of this license.
203            
204             This license does not grant you the right to use any trademark, service
205             mark, tradename, or logo of the Copyright Holder.
206            
207             This license includes the non-exclusive, worldwide, free-of-charge
208             patent license to make, have made, use, offer to sell, sell, import and
209             otherwise transfer the Package with respect to any patent claims
210             licensable by the Copyright Holder that are necessarily infringed by the
211             Package. If you institute patent litigation (including a cross-claim or
212             counterclaim) against any party alleging that the Package constitutes
213             direct or contributory patent infringement, then this Artistic License
214             to you shall terminate on the date that such litigation is filed.
215            
216             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
217             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
218             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
219             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
220             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
221             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
222             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
223             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
224            
225             =cut
226              
227             1; # End of Rails::Assets::Processor
228